Symptoms
- My Emissive material/shader does not appear in my Lightmap.
- My Emissive material/shader sometimes appears and sometimes does not.
Cause
Unity needs the flag Emission GI set to Baked always, to include the Emissive materials into lightmaps, and a custom shader may not have this flag.
Resolution
To get the Emissive material into the lightmap, please follow these steps:
- Make sure lightmap UVs are included in the mesh. Use the option “Mode > Import Settings > Generate Lightmap UV” in the mesh importer to create the second UV channel utilized by the Lightmapping.
- Checking the option Important GI from the “Lighting Window > Object > Important GI”. Sometimes Enlighten will cull small objects away because they are small relative to the indirect resolution. To avoid this behavior use this option on each object that you want to include into the lightmap (even Emissive Objects).
- Set the “Emission > Global Illumination” to Baked. It makes the Emissive light from this material to be baked into the static lightmaps for the scene. You can find this option seeing the material into the Inspector panel.
If you custom shaders, you cannot make the third point from the shader code and you cannot get Emissive light into the Lightmaps because of that. In this case, we have to use a script to solves this problem:
using UnityEngine;
using UnityEditor;
public class LightMapMenu : MonoBehaviour
{
// The menu item.
[MenuItem ("MyCustomBake/Bake")]
static void Bake ()
{
// Find all objects with the tag <Emissive_to_baked>
// We have to set the tag “Emissive_to_baked” on each GO to be baked.
GameObject[] _emissiveObjs = GameObject.FindGameObjectsWithTag("Emissive_to_baked");
// Then, by each object, set the globalIllumiationFlags to BakedEmissive.
foreach (GameObject tmpObj in _emissiveObjs)
{
Material tmpMaterial = tmpObj.GetComponent<Renderer> ().sharedMaterial;
tmpMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;
}
// Bake the lightmap.
Lightmapping.Bake ();
}
}
This script will set the Global Illumination option to Baked in each object tagged as Emissive_to_Lightmap. It will then execute the Bake function to create the Lightmap. Now, everything should work as expected and the Emissive Light should appear in the Lightmap even with custom shaders.
More Information
For scripting:
- https://docs.unity3d.com/ScriptReference/Lightmapping.Bake.html
- https://docs.unity3d.com/ScriptReference/Material-globalIlluminationFlags.html
- https://docs.unity3d.com/ScriptReference/MaterialGlobalIlluminationFlags.html
For Tags System:
For Emissive Standard Shader Settings:
For Generate Lightmap UV:
For Important GI:
For Lighting and rendering (Search by “Emissive Materials”):
Comments
0 comments
Article is closed for comments.