Page 1 of 1

Generated lightmaps are removed on unity play

Posted: Sun Jun 24, 2018 4:45 pm
by monkeybrother_1
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?

Re: Generated lightmaps are removed on unity play

Posted: Mon Jun 25, 2018 9:14 am
by ChrisHekman
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.

Re: Generated lightmaps are removed on unity play

Posted: Mon Jun 25, 2018 9:52 am
by monkeybrother_1
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.
Ok. Do you have an example that shows how this could look in code?

Re: Generated lightmaps are removed on unity play

Posted: Mon Jun 25, 2018 11:58 am
by ChrisHekman
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;	
	}

Re: Generated lightmaps are removed on unity play

Posted: Wed Jun 27, 2018 3:34 am
by OnlyVR
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.

Re: Generated lightmaps are removed on unity play

Posted: Thu Jun 28, 2018 8:12 am
by ChrisHekman
OnlyVR wrote:My bad, I forgot to set my light as static.

:) I have run into that problem so many times.

Re: Generated lightmaps are removed on unity play

Posted: Mon Jul 02, 2018 5:32 pm
by OnlyVR
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?

Re: Generated lightmaps are removed on unity play

Posted: Tue Jul 03, 2018 9:58 am
by ChrisHekman
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.
AtlasData.png
- 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 () {
...
}
And then fill those lists manually.