Do I Have to Bake My Scene Again With Light Probes
Baked Light
Light Maps and Probes
- Broil static global illumination.
- Sample lite maps, probes, and LPPVs.
- Create a meta laissez passer.
- Support emissive surfaces.
This is the 5th part of a tutorial serial about creating a custom scriptable render pipeline. Information technology makes it possible to bake static lighting into maps and probes.
This tutorial is fabricated with Unity 2019.2.18f1.
Baking Static Lite
Up to this point nosotros've calculated all lighting while rendering, simply this isn't the only option. Lighting can also exist calculated ahead of fourth dimension and stored in light maps and probes. There are two main reasons why this is washed: to reduce the amount of realtime calculations and to add indirect lighting that cannot be calculated at runtime. The latter is part of what'southward collectively known as global illumination: light that's not coming from light sources directly, but indirectly via reflection, from the surround, or from emissive surfaces.
The downside of broiled lighting is that information technology is static so cannot change at runtime. Information technology as well needs to be stored, which increases both build size and retentiveness usage.
Scene Lighting Settings
Global Illumination is configured per scene, via the Scene tab of the Lighting window. Baked lighting is enabled via the Baked Global Illumination toggle nether Mixed Lighting. There'southward as well a Lighting Mode option, which nosotros'll set the Baked Indirect, which means that we bake all static indirect lighting.
If your project was created in Unity 2019.2 or before then you'll also see an option to enable realtime lighting, which should exist disabled. If your project was created in Unity 2019.three or afterward then that option won't be shown.
Further down is a Lightmapping Settings department that can be used to control the lightmapping process, which is done by the Unity editor. I'll employ the default settings except that LightMap Resolution is reduced to 20, Compress Lightmaps is disabled, and Directional Mode is set to Non-Directional. I also use the Progressive CPU lightmapper.
Static Objects
To demonstrate baked lighting I created a scene with a green plane equally the footing, a few boxes and spheres, and a structure in the center that only has one open side so its interior is fully shadowed.
The scene has a unmarried directional calorie-free with its Mode set to Mixed. This tells Unity that it should broil the indirect lighting for this calorie-free. Besides that the lite still works like a regular realtime light.
I likewise include the basis airplane and all cubes—including those that form the construction—in the baking process. They'll be the objects from which the light bounces off, thus becoming indirect. This is done by enabling the Contribute Global Illumination toggle of their MeshRenderer
components. Enabling this besides automatically switches their Receive Global Illumination mode to Lightmaps, which means that the indirect light that reaches their surfaces get baked into the light map. You can also enable this style by enabling Contribute GI from the object'southward Static dropdown list, or by making information technology fully static.
Once enabled, the scene's lighting will be broiled once more, bold that Auto Generate is enabled in the Lighting window, otherwise you'll have to press the Generate Lighting button. Lightmapping settings also shows up in the MeshRenderer components, including a view of the light map that contains the object.
The spheres don't bear witness up in the light map considering they don't contribute to global illumination and are thus considered dynamic. They'll take to depend on light probes, which we'll encompass later. Static objects could also be excluded from the map by switching their Receive Global Illumination style back to Light Probes. They'll still affect the baked results, but won't take up space in the lite map.
Fully-Baked Light
The baked lighting is mostly blueish because it is dominated by the sky box, which represents indirect illumination from the environment's sky. Brighter areas around the edifice at the middle are caused by indirect lighting from the light source billowy off the ground and walls.
We can also bake all lighting into the map, both straight and indirect. That'south done by setting the light's Manner to Baked. Information technology then no longer provides realtime lighting.
Finer, the directly light of a baked lite is also treated equally indirect calorie-free and thus ends up in the map, making it a lot brighter.
Sampling Baked Light
Everything currently gets rendered solid black, because there is no realtime calorie-free and our shader doesn't know well-nigh global illumination yet. We have to sample the light map to make this work.
Global Illumination
Create a new ShaderLibrary/GI.hlsl file to contain all lawmaking related to global illumination. In it, define a GI
struct and a GetGI
function to retrieve it, given some light map UV coordinates. Indirect lite comes from all directions and thus can be used for diffuse lighting only, not specular. So requite the GI
struct a diffuse color field. Initially make full information technology with the light map UV, for debugging purposes.
#ifndef CUSTOM_GI_INCLUDED #define CUSTOM_GI_INCLUDED struct GI { float3 diffuse; }; GI GetGI (float2 lightMapUV) { GI gi; gi.diffuse = float3(lightMapUV, 0.0); return gi; }