- Home /
Lightmaps Loaded via Script are Broken After Upgrade to Unity 5.4
In the project, I configure multiple scenes and save the data: objects on the scene, lightmaps and other. When the scene starts, I load saved lightmaps. Also, because I load objects dynamically, I define the lightmap(s) for each object that has a rendered:
// Load lightmaps
LightmapData[] lightmaps = new LightmapData[missionLightData.lightmapsNumber];
for (int i = 0; i < missionLightData.lightmapsNumber; i++) {
lightmaps[i] = new LightmapData
{
lightmapFar = Resources.Load<Texture2D>(GetLightmapPath(missionName, i))
};
}
LightmapSettings.lightmaps = lightmaps;
// Define lightmaps for the objects
foreach (MissionLightmapData.LightmapAssotiation lightAssotiation in missionLightData.lightmapAssotiations) {
GameObject go = GameObject.Find(lightAssotiation.objectName);
if (go)
{
Renderer renderer = go.GetComponent<Renderer>();
if (renderer)
{
renderer.lightmapIndex = lightAssotiation.lightmapIndex;
renderer.lightmapScaleOffset = lightAssotiation.lighmapScaleOffset;
}
}
}
It worked fine in Unity 5.3. But after upgrade to 5.4, the mission looks black&white.
I also noticed that if I don't touch the scene, everything loads fine in the editor. But as soon as I modify the scene (some other settings are updated by Unity) or if I run the scene, I get same "black&white" effect.
Any ideas what can cause such effect? Maybe Unity stores lightmap associations to the objects somewhere else now?
Answer by StaggartCreations · Sep 13, 2016 at 08:18 AM
I use the same system and I'm running into the same issue with 5.4. Before, this occurred if the scene where the objects/lightmaps had it's Directional Mode set to "Directional", and since we bake our scenes using Non-Directional lighting (no normal maps) it conflicted. Changing the scene to Non-Directional mode fixes this however.
Now in 5.4 the same issue occurred, and the solution was to set the Directional Mode through script upon loading. This was a relief, however it didn't work in the build, black-and-white like you're seeing.
So I'm assuming this has something to do with the fact that Unity expects Directional Lightmaps, where none are supplied. Perhaps loading a single blank directional lightmap next to all your "Far" lightmaps would do the trick.
Hey! Changing the Directional $$anonymous$$ode in script using LightmapSettings.lightmaps$$anonymous$$ode = Lightmaps$$anonymous$$ode.NonDirectional;
Actually worked! Also it worked in build for me! THAN$$anonymous$$ YOU!
Hm. Interesting. I didn't try this approach. I found out that it helps if the scene has at least some lightmap baked and assigned to it. Then, on the scene, it can be replaced with another lightmap and all works fine. Also, I used light.alreadyLightmapped = true
in older versions and it has to be removed now, as it causes the issue described above even with the workaround I described. Thanks for the answer, though. I may try this approach as well.
I've actually managed to resolve my issue yesterday. Under Project Settings->Graphics I had the lightmap modes set to '$$anonymous$$anual' and "Baked Non-Directional" wasn't ticked, that's why it didn't work in the build for me.
in 5.4, the new way to flag lights as baked is through the use of light.bakedIndex = <unique id>
https://docs.unity3d.com/ScriptReference/Light-bakedIndex.html
light.bakedIndex = <unique id>
- I tried this and it doesn't help. I checked the value in the bakedIndex
and it is different depending on whether you have some lightmap attached to the scene or not. If I have no lightmap, setting the index doesn't help.
Your answer
Follow this Question
Related Questions
Custom lighting model used in light bake 0 Answers
Static objects are dark when lighting is baked 0 Answers
Как управлять UVW атлассингом юнити террейна? How to manage UVW atlas unit terrain? 0 Answers
Failed baking lighting 0 Answers
Indirect baked light not respecting cast shadows flag on renderer or light object. 0 Answers