Problems with instantiating baked prefabs

We’ve been having an issue with our lighting information being lost when instantiating baked prefabs. I created a topic in Unity Answers here:

and I’ve noticed three other similar cases but nothing has been resolved:

Anyone have any ideas??

1 Like

We were encountering lighting issues with baked prefabs that turned out to be a result of design changes within Unity 5. For background see: [RESOLVED] lost lighting information on baked prefabs when instantiated - Questions & Answers - Unity Discussions

The workaround provided by Unity is an editor script

struct LightmapInfo
{
Renderer target;
Vector4 offsetScale;
int lightmapIndex;
}

However after implementing this script we still are encountering issues. Attached is a photo of the original scene, the problem before the script, and the problem after using the script (visible in the camera preview)
Any ideas on a solution?

Can you post the full script of how you are handling storing the prefabs. Best would be a small sample project that has the script you use to create a full workflow of generating the prefabs from the scene with lightmap information

Sure! Thanks for your help.
The uploader said that the .7z file was too large to upload although it is only reading at just above 2mb so I’ve uploaded it to google drive.
Once uncompressed, open the scene ‘test’ and go to the main camera window. From there just go through the buttons in order as seen in the attached image (apply and save → Instantiate prefab → Show original → show instantiated, etc…) to see the problem we are encountering.
https://goo.gl/esLQMq

2171533--143703--gi_main.png

We’re having the same issue. We’ve been happily thinking that our dynamically loaded objects and lightmaps were working great, until we tried to export a build.
So @Joachim_Ante_1 , any progress on this? Can we expect it to be patched? It’s a pretty major issue for a whole range of games.

First of all reasoning behind the changes in Unity 5.

Why we changed behaviour?
Renderer.lightmapIndex and Renderer.lightmapScaleOffset in unity 4.6 was stored on each renderer. This made it possible to store this data in prefabs.

In Unity 5 we have moved this data into the lightmap snapshot. The lightmap snapshot is an asset that can be stored next to the scene file. The reason we did this is because when we stored LightmapIndex, lightmapScale and lightmapOffset in the scene file, an automated process (clicking the bake button) resulted in lots of changes that are highly likely to cause merge conflicts in scene files.

By storing all the generated data of the bake process in a separate file, merge conflicts on the scene are avoided and it becomes possible to work in a team on the same scene. As a side-effect of not storing the data on the renderer, it no longer gets stored in prefabs either.

How you can handle lightmapped prefabs in 5.0
Overall we very much think this is the right tradeoff. For the cases where you do need it to store the data with prefabs.

I made a sample project to show how to do this.

You can make a scene with all prefabs you want to bake. Each prefab one has to have the PrefabLightmapData in its root game object.

Then you can just click on the Assets → Assets → Bake Prefab Lightmaps menu item.

This will first bake all scene objects. Then extract all lightmap indices + scale and offset. Then it will store it in each prefab instance. Then it will replace all prefabs with the new information.

At load time the script patches the lightmap settings, thus it applies the lightmaps, lightmap index & offset/scale to all renderers which need to be patched at load time.

2177524–146782–LightmapInPrefab.zip (196 KB)

10 Likes

Does it still work if you bundle the prefab and lightmaps into an asset bundle, then try to download the bundle and instantiate from it? Because that’s where my solution falls apart. Works fine if I load the assets locally, but doesn’t seem to work with bundles.

Thanks, I checked it out, but it has the same issue. It works fine in the editor, but not in builds (I tried PC and web).

The shader deployment pipeline automatically removes shader combinations relating to lightmaps if no lightmaps are setup in deployed scenes.

You can override this behaviour in “Shader Stripping” in Edit → Project Settings → Graphics Settings.

And ensure the specific lightmap modes that you are using in your project are supported.

I have updated the demo project above so it has said changes in Graphics Settings setup. Please let me know if the sample now works for you out of the box.

Joachim_Ante said:
Everything works as expected.

I have attached a small sample project which shows how to use the API to store lightmap index / offset&scale in a prefab.

Thanks to the script you provided this is no longer an issue, however the persistent issue that we need addressed is that the rendered scene in the camera preview is producing something completely different from our original design (as you can see in the attached photos)

We are curious, @RogueCode as to what you have been doing since this issue affects us within the editor, well before we are able to get to the point of being able to export the project. Maybe you have some insight to help us resolve the issue.

@GSdev : have you looked at the sample project i have provided and tried to apply that to your project?

Awesome, thank you so much (yes, works now)! I hadn’t even thought about ShaderStripping.

@GSdev , fortunately because of our workflow that issue you’re seeing doesn’t matter for us. Sorry I can’t be more help.

@Joachim_Ante_1 Yes we have, in fact it seems like we get the bright lighting result even when using your original project. Your project was much darker so the change is not as conspicuous, but because ours is already quite bright the end result is completely blown out. Attached is our project with yours applied to it as well as a photo of the issue.

2182243–144586–GITest (1).7z (10.4 KB)

Are you maybe double-lighting your baked scene? Because when I bake light into a renderer I set that object to a new layer called “Lightmapped Objects”, then set the Culling Mask on all my light sources in game to exclude this layer.

The problem with this demo is that it only works with one prefab. If you create 2 different prefabs, bake their light separately, then try to load them both into the LightmapInstances scene, it doesn’t work. The prefab serializes the GUIDs for the lightmaps, but not the data itself. As soon as you “ExtractLightmapData” on a second prefab, the data required for the first prefab gets overwritten.

Unless I’m missing something, you have to copy the lightmap .exr textures somewhere after you bake them, then load them explicitly in your Awake function.

And that will work fine until you try to put both the lightmaps and prefab into an asset bundle.

Could anyone tell me How to get Terrain’s lightmap offset&scale infomation ?

I’ve checked the API of terrain class, there’s only lightmap index, nothing about offset&scale.

Copying of the textures is only necessary if at a later point you use the same scene to bake new textures.

The setup i would suggest for a real production workflow is to make a single scene with all the prefabs instantiated (placed in seperate locations so lighting doesnt bounce between prefabs)

So you run baking once. Then you extract lightmap index / scale offset for all prefabs.

Effectively updating all prefabs at once.

Alternatively one seperately named scene per prefab could work too, but that leads to more textures and smaller atlasses. If you are always using all the prefabs together at runtime, then packing it together in a single scene is definately the way to go.

I have updated the sample project to demonstrate that it works with two objects. The approach of a single scene to bake all prefabs, works very well and the workflow is now so you just open the scene and click the “Assets → Bake Prefab Lightmaps” menu item. This finds all scene objects that have a LightmapPrefabData script on it and uploads the baked lightmap information to the prefabs.

@Joachim_Ante_1 The script works fine in play mode but it is still missing lightmap uv aka lightmapScaleOffset on my iOS device (iPhone 5)
Could you please have a look at it?
p.s The general gi directional mode is “non directional”.

Update :
If these objects was set inactive in the editor and then set active in runtime by some other script, all the lightmaps look normal. Are there some differences about awake mechanism on iOS device? Confused :confused: