Generated lightmaps are removed on unity play
Moderator: ChrisHekman
- monkeybrother_1
- Posts: 2
- Joined: Sun Jun 24, 2018 4:38 pm
I'm playing around with the lightmapper, but when I press play, all lightmaps are cleared. Is this correct beta behaviour or am I missing something?
- ChrisHekman
- Posts: 1061
- Joined: Wed Jan 18, 2017 3:09 pm
Unity uses an object called LightingDataAsset to store its lightmaps. Regrettably unity does not expose the data in this object.
Ref: https://docs.unity3d.com/ScriptReferenc ... Asset.html https://docs.unity3d.com/Manual/LightmapSnapshot.html
In order to make them work in play mode you will need to set them via script at the start of your scene load.
Ref: https://docs.unity3d.com/ScriptReferenc ... pData.html
We are looking into making a script to do this for the user.
Ref: https://docs.unity3d.com/ScriptReferenc ... Asset.html https://docs.unity3d.com/Manual/LightmapSnapshot.html
In order to make them work in play mode you will need to set them via script at the start of your scene load.
Ref: https://docs.unity3d.com/ScriptReferenc ... pData.html
We are looking into making a script to do this for the user.
- monkeybrother_1
- Posts: 2
- Joined: Sun Jun 24, 2018 4:38 pm
Ok. Do you have an example that shows how this could look in code?ChrisHekman wrote:Unity uses an object called LightingDataAsset to store its lightmaps. Regrettably unity does not expose the data in this object.
Ref: https://docs.unity3d.com/ScriptReferenc ... Asset.html https://docs.unity3d.com/Manual/LightmapSnapshot.html
In order to make them work in play mode you will need to set them via script at the start of your scene load.
Ref: https://docs.unity3d.com/ScriptReferenc ... pData.html
We are looking into making a script to do this for the user.
- ChrisHekman
- Posts: 1061
- Joined: Wed Jan 18, 2017 3:09 pm
Similar to this
Code: Select all
void SetLightmaps( List<Texture2D> LightMaps, List<Texture2D> DirectionMaps)
{
LightmapData[] lightmapData= new LightmapData[LightMaps.Size];
for(int i = 0; i < LightMaps.Size; i++)
{
lightmapData[i] = new LightmapData();
lightmapData[i].lightmapColor = LightMaps[i];
lightmapData[i].lightmapDir = DirectionalMaps[i];
}
LightmapSettings.lightmapsMode = LightmapsMode.CombinedDirectional;;
LightmapSettings.lightmaps = lightmapData;
}
How to bake the lightmaps? I can successfully render in PBR Viewport but "PBR Lightmapping" creates black PBRLightmap_N_LIGHT_DIRECTION.png and PBRLightmap_N_IRRADIANCE.exr files.
What am I missing ? Any special settings for camera etc?
My bad, I forgot to set my light as static.
What am I missing ? Any special settings for camera etc?
My bad, I forgot to set my light as static.
- ChrisHekman
- Posts: 1061
- Joined: Wed Jan 18, 2017 3:09 pm
OnlyVR wrote:My bad, I forgot to set my light as static.

I've tried to use the script to assign the lightmaps in runtime mode but it doesn't work form. Actually my script is a little bit more complicated:
void Start () {
string m_Scene = SceneManager.GetActiveScene().name;
string m_pathLigtmap = "Assets/" + m_Scene + "/Octane/";
DirectoryInfo m_dir = new DirectoryInfo(m_pathLigtmap);
FileInfo[] fileInfoLightMaps = m_dir.GetFiles("*_BEAUTY_DENOISER_OUTPUT.exr");
FileInfo[] fileInfoDirectionalMaps = m_dir.GetFiles("*_LIGHT_DIRECTION.png");
LightmapData[] lightmapData = new LightmapData[fileInfoLightMaps.Length];
for (int i = 0; i < fileInfoLightMaps.Length; i++)
{
lightmapData = new LightmapData();
lightmapData.lightmapColor = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(m_pathLigtmap + fileInfoLightMaps.Name, typeof(Texture2D));
lightmapData.lightmapDir = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(m_pathLigtmap + fileInfoDirectionalMaps.Name, typeof(Texture2D));
}
LightmapSettings.lightmapsMode = LightmapsMode.CombinedDirectional; ;
LightmapSettings.lightmaps = lightmapData;
}
I can see the script correctly assigned the lightmapper, but in the game window there is no GI light.
Any idea?
void Start () {
string m_Scene = SceneManager.GetActiveScene().name;
string m_pathLigtmap = "Assets/" + m_Scene + "/Octane/";
DirectoryInfo m_dir = new DirectoryInfo(m_pathLigtmap);
FileInfo[] fileInfoLightMaps = m_dir.GetFiles("*_BEAUTY_DENOISER_OUTPUT.exr");
FileInfo[] fileInfoDirectionalMaps = m_dir.GetFiles("*_LIGHT_DIRECTION.png");
LightmapData[] lightmapData = new LightmapData[fileInfoLightMaps.Length];
for (int i = 0; i < fileInfoLightMaps.Length; i++)
{
lightmapData = new LightmapData();
lightmapData.lightmapColor = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(m_pathLigtmap + fileInfoLightMaps.Name, typeof(Texture2D));
lightmapData.lightmapDir = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(m_pathLigtmap + fileInfoDirectionalMaps.Name, typeof(Texture2D));
}
LightmapSettings.lightmapsMode = LightmapsMode.CombinedDirectional; ;
LightmapSettings.lightmaps = lightmapData;
}
I can see the script correctly assigned the lightmapper, but in the game window there is no GI light.
Any idea?
- ChrisHekman
- Posts: 1061
- Joined: Wed Jan 18, 2017 3:09 pm
OnlyVR wrote:I've tried to use the script to assign the lightmaps in runtime mode but it doesn't work form. Actually my script is a little bit more complicated:
void Start () {
string m_Scene = SceneManager.GetActiveScene().name;
string m_pathLigtmap = "Assets/" + m_Scene + "/Octane/";
DirectoryInfo m_dir = new DirectoryInfo(m_pathLigtmap);
FileInfo[] fileInfoLightMaps = m_dir.GetFiles("*_BEAUTY_DENOISER_OUTPUT.exr");
FileInfo[] fileInfoDirectionalMaps = m_dir.GetFiles("*_LIGHT_DIRECTION.png");
LightmapData[] lightmapData = new LightmapData[fileInfoLightMaps.Length];
for (int i = 0; i < fileInfoLightMaps.Length; i++)
{
lightmapData = new LightmapData();
lightmapData.lightmapColor = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(m_pathLigtmap + fileInfoLightMaps.Name, typeof(Texture2D));
lightmapData.lightmapDir = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath(m_pathLigtmap + fileInfoDirectionalMaps.Name, typeof(Texture2D));
}
LightmapSettings.lightmapsMode = LightmapsMode.CombinedDirectional; ;
LightmapSettings.lightmaps = lightmapData;
}
I can see the script correctly assigned the lightmapper, but in the game window there is no GI light.
Any idea?
This looks correct.
Couple of possible issues
- "Baked Lightmap" atlas info was not saved to scene.
You can check this by selecting a static MeshRenderer. In the inspector check Lightmap Settings -> Baked Lightmap
If the atlas is 65535 that means it did not save its atlas info. - Textures are not found by the assetdatabase and the lightmapsettings are filled by null textures
- Script start function is not called when going into play mode.
Additional comment
- "UnityEditor.AssetDatabase" is not useable from a standalone build of your game.
My advice would be to do the following:
Code: Select all
public Texture2D[] LightMaps;
public Texture2D[] DirectionalMaps;
void Start () {
...
}