UDK Lighting
Documentation for lighting in UDK:
Remember to read the official documentation in addition to this document. The official documentation goes into a lot of detail on the specifics.
http://udn.epicgames.com/Three/TextureProperties.html
http://udn.epicgames.com/Three/LightmassTools.html
http://udn.epicgames.com/Three/LightMapUnwrapping.html
http://udn.epicgames.com/Three/LightingReference.html
(The last one is the basics of lighting in engine,
although much of it is out of date.)
http://udn.epicgames.com/Three/DistanceFieldShadows.html
Understanding UDK's Lightmass System
An Overview Of Lightmass
The system that builds lighting in UDK is called "lightmass". UDK, using lightmass, actually needs to handle at least two distint types of lighting:
- Static (unchanging) lighting, which can be heavily optimized by baking the lights to textures called lightmaps or to vertex colors
- Dynamic (changing) lighting, which can not be optimized nearly as much. UDK can use something called light environments to optimize dynamic lighting, but a lot more calculation has to be done.
During gameplay, both static and dynamic lighting are combined. Note that a single object may use some combination of static lighting and dynamic lighting at the same time. (Indirect lighting may be baked in a lightmap, but direct lighting may be dynamic to account for players and objects moving in front of the light source.)
For best performance, artists should try to use static lighting on as many objects as possible.
It's worth nothing that the UDK lightmass system is a full global illumination type system, visually similar to baking final gather lighting in Maya. The lightmass system also support creating ambient occlusion in the lightbaking process. Also, the post process system does some minor screen space ambient occlusion, glows, and color correction.
For the reason's above, lightmap baking in Maya is not always required, though sometimes in can help get a better look. What is required is for most objects to have *really good* lightmap channel uvs. Alternatives to lightmaps include vertex colors and dynamic lighting. (See the section about Dynamic objects below.)
Most parts of most scenes will just be static models with static lightmaps.
Any lightmapping on objects done outside of UDK is essentially just "extra" lightmapping. Normally, the build process in lighting computes all the lightmaps for the player. UDK just needs good UVs on the models to support the lightmaps that are created by UDK. Often it is useful to bake some additional lightmapping into the object (AO etc) however, UDK is actually able to do most of it itself.
In particular, any lights baked prior to coming into UDK should be as generic as possible, since the objects will likely be moved around and duplicated in UDK. For example, do not bake a spotlight shining a bright spot onto a part of an object using your 3d authoring package. In general you should not bake directional shadows in your 3d authoring package either.
If color variation isn't required in the baked lighting, then it's good to put the baked lighting info into a single channel of an image. (As opposed to wasting 24bits per pixel.) Either the alpha channel of one of the regular textures can be used, or one of the RGB channels on a texture can get used, with the other channels being used for other mapped properties.
Lightmass on Static Objects
Static objects will generally use static lighting, stored as either lightmaps or vertex colors. However, moving lights will light static objects dynamically. Light types with the name "Movable" in them, such as "PointLightMovable", will add dynamic light to objects that otherwise use static lighting.
Lightmass on Dynamic Objects
Dynamic lighting is generally lower quality than static lighting, because high quality dynamic lighting is extroardinarily hard to compute. (Think of how long mental ray takes to render a frame, compared to UDK rendering 60 frames every second.)
Generally, for dynamic objects, UDK light environments are used. During light building UDK bakes the global illumination lighting at various points in space, and the light environment component is able to sample that information. The light environment takes all the direct and indirect lighting into effect and turn it into "spherical harmonics" (complex math based storage of lighting) that can be applied to moving objects. In additionl to the spherical harmonics, a single direction light is created to represent the average direction of the brightest light. (Thus only one shadow will be seen, even when several lights are shining on a light environment using actor.) The end result is that the light environment creates
Lightmap Based Lighting
Lightmass generates lightmaps for most static lighting in your scene. Lightmaps are texture maps that the computer generates based on the lights that you place in your scene. When you push "build lighting" lightmass regenerates the lightmaps, as well as any vertex color based lighting. (Explained in the next section.)
Lightmaps are explained more in the section below titled "Understanding Lightmaps".
Vertex Color Based Lighting
Objects that have a lightmap resolution of zero will use vertex lighting instead of lightmaps, however, vertex lighting only works well when the objects has a lot of verts, and they are spread evenly.
To use vertex color based lighting, simply set the lightmap resolution to zero on any object, either in the static mesh editor, or on on the properties of the individual instance in your scene.
To Use Lightmass In Your Scene
A lightmass importance volume is crucial to have when using the lightmass system. The lightmass importance volume tells UDK where it needs to focus on getting good lighting quality. All the important parts of your scene, other than background elements (such as your skybox and distant scenery) should be within the lightmass importance volume.
Each scene should have exactly one lightmass importance volume. It should just be a box. UDK doesn't currently (as of March 2012) support other shapes for lightmass important volumes, nor does it support multiples. If you use another shape it will use the bounding box anyway.
The Light Source Radius Option
Most lights have an option for the light source radius. It only affects lightmass static lighting.
The light source radius controls how big the area emitting light is. (The size of the lightbulb.) It is represented by a yellow circle. It has a strong effect on the shadow sharpness for static lighting. Large numbers cause very soft shadows. Keep in mind that small numbers will cause the shadows to look more like the sharper shadows created by dynamic lights.
Be careful not to have the light source so large that it intersections with other geometry that casts shadows. That would mean that half the light sources was behind a shadow caster, and the lighting would look very strange.
In the viewport menu, you can use: show > advanced > light radius to toggle display of the light radius.
Understanding And Working With Lightmaps
Lightmap UVs
By default, lightmaps use the second UV channel/set, which has the index "1". Remember that the first UV channel/set has index "0". Most of the time, UV[0] is used for regular texture mapping, and UV[1] is used for lightmapping.
UVs for lightmapping should all fall within the 0 to 1 range in both U and V. (eg. On the main grid/texture in the Maya UV Editor) UVs should be non overlapping and unique. Some stretching is ok for lightmapping, but seams should be avoided as much as possible.
Lightmap Resolution
The lightmap resolution controls the detail of the shadow. It is the size (in pixels) of the texture map that will be created as used as a lightmap
Larger lightmap resolutions will cause build time to go up a lot. (Whenever you use "build all" or "build lighting" the computer will spend a long time calculating the lighting.
Here are a couple examples of lightmap resolutions:
- A small prop could use a lightmap resolution of 32 in the static mesh editor, and an override of 128 in the scene.
- Most fairly large objects can use a resolution of 64 in the static mesh editor and 128 or 256 in the scene.
- A very important central object could use a lightmap resolution of 128 in the static mesh editor, and an override of 512 in the scene.
In general most objects will look good enough with lightmaps of 128 or 256. Resolutions of 512 should be used quite rarely.
What resolutions you use are also largely defined by the platform you are running on. Numbers given in the above examples are for a typical modern PC.
Lightmap Resolution can be controlled for both Static mesh actors and CSG/BSP geometry (geometry built in UDK). The methods for doing so are described below.
Controlling Lightmaps Quality On CSG/BSP Geometry
The default lightmap size on bsp geometry is very low quality. You will likely want to turn it up significantly.
Select BSP surfaces, press F5 for the surface properties. Change the lightmap resolution.
Smaller numbers, give higher quality lightmap resolution. Note that this is the opposite of how the resolution is controlled on other objects (where higher resolution means better quality). Thus, for CGS/BSP geometry, turn the resolution down here for better quality. Turn it up to improve lighting speed, and use less RAM for textures.
A setting of "4" looks great for important surfaces. Farther away surfaces and less important ones can use the default settings.
Controlling Lightmap Quality For Most Objects
Every static mesh has settings for the lightmap resolution. The default lightmap size is extremely low. It will cause artifacts on most meshes.
A resolution of 128 works well on most objects, if you can afford the time when building/lighting your scene. Values of 512 and beyond start causing extreme slowdown in the build system. Very few objects should require lightmaps of such high sizes. If your lightmap needs to be that big, consider breaking your model up into smaller pieces.
For static mesh lighting, there are two main methods of controlling the resolution.
- Edit the static mesh itself from the content browser, in the static mesh editor change the light map resoluton setting.
- The default of 32 is very low. It will look very poor on most objects.
- Edit the instance of the static mesh in your scene/level.
- Double click the object to open it in the property editor. Open the Static mesh section, (or InterpObject or whatever you used.) Then goto Static Mesh Component > Override Light Map Res (Check box) and change number for the lightmass resolution to whatever you wish to use.
A useful strategy is to use fairly low resolutions in the static mesh editor, and then when nearing completion of the lighting in your scene, override those values with larger numbers.
Things To Watch Out For When Using Lightmaps
If you have one giant high poly static mesh (such as an entire house), it will all need to use one lightmap, of a very high resolution. This can be very problematic.
Attempt to split you objects up into modular components that can fit together. A house that the player can walk up to should be made of several wall pieces, window pieces, and other small pieces. A house far away in the background can of course be very low poly and use a small lightmap.
For any objects that are very small and simple or far away from the camera, consider using vertex lighting (a lightmap resolution of zero). In many cases it will look good enough and save texture map memory.
Lightmass Settings
Emmisive Materials with Lightmass
You can cause emmisive materials to actually illuminate the scene using lightmass. You need to turn the option on for each object in the scene that should cast light via it's emmisive texture. Only static objects can be emmisive. It won't work for animated/moving objects.
Static Mesh Actor
Static Mesh Component
Lightmass
Lightmass Settings
Use Emissive For Static Lighting
Note that emmisive lighting can affect dynamic object in a way, since the light environment spherical harmonics do sample the global illumination system. It's not entirely perfect, but it actually works reasonably well.
Controlling The Strength of Lightmass Global Illumination Lighting - Lightmass Boost Values
There are several places where you can change the strength of the lightmass.
The boost settings all multiply with each other. (So if they are all set to "1", the end result is "1", meaning regular intensity of lightmass lighting.)
Places to find boost settings:
World Settings
Actor
Static Mesh Itself
Material
Diffuse Boost is set to 5 in many scenes (and in many default scenes). It is recommended that you start by setting it to a value of 1 and adjust from there. In the World Properties Lightmass settings, check the "Diffuse Boost" and make sure it's not set to 5. Such a high value would cause ultrabright lighting that would be too colorful. (Only useful for really stylized unrealistic lighting.)
Useful Properties Related To Lighting
- Static Mesh Actor > Static Mesh Component > Lightmass > Use emmisive for static lighting
- Light > Light Component > Lightmass > Light source radius (size of the light bulb)
- Light > Light Component > Point Light Component > Radius (how far the light reaches)
- Light > Light Component > Light Component > Brightness (like intensite in Maya)
- Light > Light Component > Light Component > Light Color
- Light > Light Component > Light Component > Light Shadow Mode > LightShadow_Normal
- Light > Light Component > Light Component > Shadow Filter Quality > SFQ_Low
Select Some Surfaces and use: RMB > Surfaces > Lighting > Lightmap resolution
Indirect normal influence boost, found in World Properties
When this setting is too high (often anywhere above 0) it can cause artifacts in lightmass, such as dark patches on object lighting. If you are having such a problem, try setting it to zero and using build > build all.
A Strategy For Lighting Your Scenes
Here are some examples of workflows you can use to work fast before lighting your scene, and speed up the process when you are finally ready to actually do the final lighting.
Please note: This is not an artistic guide to lighting. Just some workflow tips.
In the notes below, "Dynamic objects" mean any objects that can ever move during gameplay. Doors, elevators, destructables, characters, physics rigidbodies, deforming objects, etc.
All the various light types available can be found in the content browser in the Actor Classes tab. Find lights in the tree and open up that section to find them all.
Before lighting, when first building your level
- At the start of your level construction go to world properties, lightmass, and turn off Global Illumination. Later on you can turn it back on, but turning it off at first will keep things running fast.Also, while you are editing lightmass settings change the lightmass settings to reasonable defaults. Enable ambient occlusion, but change the amount. Good starting points are 0.8 for indirect and 0.2 for direct. Ensure that Diffuse boost is set to 1. (Sometimes it defaults to 5, which results in ultrabright lighting that is too colorful.)
- Add a SkylightToggleable to your scene, and turn it's brightness way down. (perhaps 0.1) The skylight can be deleted later, but having a skylight will provide some ambient lighting during the early stages of the process.
- When building, and movable point lights so you can see what you are doing, but keep them on a separate layer, and delete them when you go to do your final lighting.
While Lighting
- Once you have a general idea of how your lighting will work, enable Global illumination, and delete your skylight. However, I recommend getting a solid start on your lighting before using global illumination. It will slow the process down a lot.
- Remember to build lighting frequently, to see how your changes will really look.
- The most important light settings are brightness, color, and radius.For any lights that you wish to animate, use the moveable type. Movable lights won't be taken into account by lightmass, and will be entirely dynamic.
- When creating new lights remember to adjust the "light source radius" on your lights to control their softness. Larger values make the shadows softer and smaller values keep the shadows sharper. Remember that smaller values will match your dynamic objects lights better.
- If you are on a PC, and can afford the rendering time, keep all lights shadow types as "normal". (Instead of modulated or modulated better.) This will keep your lighting much more accurate. On any modern PC it should run fine. The modulated shadows are more useful for consoles.
- Remember, most dynamic objects will cast a "composite" shadow. This means that their shadow will be cast by.In most situations it's good to keep all lights that contribute to the composite shadow in the same direction. Usually this means having those lights come from above. (Not directly above, but all coming from that general direction.)This will prevent your shadows from fading in and out too much as the shadow transitions from one light source to the next.
- On any dynamic objects, go into their properties (somewhere under skeletal or static mesh and then rendering) and set the properties to use a light environment. Enable it's "generate spherical harmonincs" feature. This will cause dynamic objects to light quite nicely.
- If you have a large outdoor scene, use a dominant directional light. By default its shadows will fade out whenever you get far away from them. To fix this, enable the light's cascaded shadow map settings, and edit the settings to that the shadows can be seen at a large distance.
All of these are just suggestions and starting points. Since every scene is different, you'll need to use different settings for every scene. However, in most scenes, the settings that look the best are similar to what is described above.