I’ve been beating my head against this for a week or so. I’m generating 4 different light maps for different regions of my level, and then I run a script which makes a final light map which contains portions from each of the 4 different light maps. This final resulting Texture2D is stored in LightmapSettings.lightmaps. It works great! That is until I hit play. The light map vanishes. If somebody has some ideas of why this happens, please help.
An alternative I tried is to have the script store the final Texture2D as an exr or png. But for some reason, it stores it close but wrong (I’ve tried using EncodeAsPNG and MiniEXR but get the same results). Let is light map i constructed, and right is what it looks like when I try to save it:
Anybody seen anything like this? I’d just like to save down the light map I’ve constructed and restore it on run.
Did you set the import settings on your new png correctly?
I am not sure about overwriting the generated light so file but maybe you need to try and mark it as dirty.
Similar things can happen with other unity prefabs if you don’t mark them as dirty and run.
I tried different import settings, but this matches the way the png looks when loaded in photoshop.
I’ll try the mark as dirty thing again. I tried marking everything I could think of dirty and saving assets for extra grins, but the light map still clears when I start:
EditorUtility.SetDirty(Lightmapping.lightingDataAsset);
foreach(LightmapData ld in LightmapSettings.lightmaps) {
EditorUtility.SetDirty(ld.lightmapLight);
}
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
Well if you copy the data exactly into your own png. Assuming you can get the raw values.
Then you may just have to create your own shader code to read it. I think all the shader source is there to see how unity uses the light maps.
An issue would be if unity does something to it before sending to the gpu. Which is possible since they have different texture import settings for light maps.
Unity uses that rgbm format or some other hdr like format. So looking at that file in Photoshop isn’t the same.
You need to know what you are reading is decoded properly.
It looks like left is in unity and right is not? If so then they are probably applying the unpacking correctly in the preview.
I think rgbm is rgb plus an exponent in the alpha. So if you just open this in Photoshop it won’t look the same.
But I would think if you set the texture in unity import to say it uses the rgbm or similar encoding it should look the same if you have saved the correct data in the preview.
Just some ideas.
Thanks for the leads! I’ll look into the shader thing too. Both the left and right are unity. The left is what is shown in the lighting > light maps window and the right is what the png or exr looks like in the inspector.
Thanks for the suggestion on importing as RGBM. I did some reading and it turns out if you set for Lightmap mode it will try to import as RGBM. Sadly it made no difference leading me to wonder if the exr tool I’m using is dropping some useful info.
As much as I don’t want to, I may have to upgrade to Unity 5.6 which has a native Texture2D.EncodeToEXR utility.