TL;DR What is the LightmapSnapshot asset actually for, and how is it intended to be used?
Okay, now for a longer explanation. We build our world dynamically at runtime by stitching randomly-selected prefabs together as we go. Each prefab is a 50m chunk of the world – a ground mesh and some arrangement of props – and they’re designed to stitch together seamlessly at the the edges.
In Unity 4, we wanted to lightmap our world, but that doesn’t work out-of-the-box because lightmaps are generated and stored at the scene level, not the prefab level. So I devised a custom bake solution that wraps the existing lightmapping API. The custom bake sequence is:
Trigger a lightmap bake
When the bake finishes, move the generated lightmap textures out of their auto-generated sceneName folder into a destination folder of the user’s choosing (typically this sits alongside the prefab to which these lightmaps belong)
Save a bunch of data into an array on the prefab itself which maps each child renderer to the lightmap texture asset it was assigned during the bake
And then at runtime startup:
Build a list of all the chunk prefabs that will be used in this scene
For each, walk its data array and add each unique lightmap texture to the scene’s lightmap textures list (which was empty to begin with)
For each child of each chunk, set its lightmapIndex by looking up its stored lightmap texture reference in the scene’s newly-updated lightmap textures list. This binds each child back to the lightmap it was baked with, even though that lightmap probably occupies a different index in the scene now.
This solution worked well! And it mostly seems to be working in Unity 5, too, but I’m seeing a few weird things that I don’t quite understand:
When I bake a chunk, as soon as the bake completes I see the new lightmap on the chunk as I’d expect. If at this time I change the scene view render mode from Shaded to Baked, I see the baked lighting in the scene view, again just like I’d expect.
When I start an actual level (thus running that startup script that builds a new lightmaps list and reassigns all the lightmap indices), I see the correct lightmaps in the game view, but in the scene view in Baked mode I see no lighting whatsoever, just a semi-transparent gray grid on everything. I don’t know if this is indicative of a problem, or…?
Currently I’m throwing away the LightmapSnapshot asset after the custom bake. I suspect this might explain why I don’t see any lighting in the scene view after starting a level, but that doesn’t really explain why I do still see the correct lighting in the game view? If we’re actually dependent on the LightmapSnapshot then wouldn’t I expect lightmaps to break in both places?
Also (and sort of unrelated) I tried dragging a LightmapSnapshot into the snapshot field in the Lighting panel (under the Lightmaps tab) and that had no effect on anything at all. I guess I was expecting that to update the bake settings in the Scene tab to what had been used when that LightmapSnapshot was generated. That would seem intuitive, anyway. Am I just completely misunderstanding the purpose of this asset?
The documentation for the new lighting system is woefully lacking, unfortunately. I’d really love to see much more comprehensive documentation on this. There’s not even a simple reference for what the different bake settings actually do.
Hi! We are aware of the lacking state of the lightmapping documentation and we are working on improving it.
I will try and shed some light on how lightmap snapshots work: The snapshot contains the input data and all the intermediate files needed when creating the lightmaps for a scene. This means the renderers, the realtime lightmaps, the baked lightmaps and some additional data that describes how they fit together. The snapshot is an Editor only construct so far, so you can’t use it in the player. When you change the scene, for instance by breaking a prefab connection, the snapshot data is out of date and has to be rebuilt.
So does that mean I only need to keep the snapshot around if I want to make it easy to re-bake lighting with the same settings?
Like I described above, we’re using a very customized lightmapping workflow so we can effectively lightmap individual prefabs and then stitch them together at runtime. Currently I’m just throwing away the snapshot after baking a prefab. (It had occurred to me to save the snapshot alongside the prefab, but since I can’t seem to use it to restore the original bake settings, it doesn’t seem useful to keep around.)
But now I’m trying to figure out why the lightmaps aren’t getting assigned in standalone builds, and from what you said it sounds like the absent snapshot should NOT be contributing to that problem. Is that right?
I reproduced this in an extremely simple test project and submitted bug 685322 for it.
In the test project, I created two identical scenes, each consisting of a cube sitting on a plane. The cube and plane are both prefabs. I lightmapped scene A in the normal way, then used a script to capture the lightmap texture, index, and scaleOffset and save those to member variables on each prefab. In scene B – which has never been lightmapped – I apply those saved values to each object’s renderer in Start(). The expected result is that both scenes look identical, but what actually happens is scene B just doesn’t display the lightmaps. This type of configuration worked in Unity 4, but does not work in Unity 5, which is why I consider it a bug.
We cache the intermediate lighting files in the GI Cache folder, so as long as we can locate the files in the cache, we can quickly regenerate the snapshot.
Hi,
I have similar problem. I baked one model in the scene. I got a snapshot file and couple of exr files (one far and one near lightmap texture). I see those three files assigned automatically in the Lighting window immediately after baking process is finished. In our game, I cloned the model which is already baked and in the same scene, I dont see lightmaps applying on it (but I can still see snapshot and lightmap textures assigned under Lightmaps section in Lighiting window).