Moveable lightmaps and light probes

I’d like to have a moveable object with baked lightmapping AND light proibe information.

Basically, you make a thing, parent light probes to it, stuff it all into a prefab and then I want to be able to spawn that prefab at runtime and move it around, while keeping light probe information and lightmaps intact.

Of course the lighting won’t update itself while the object is being moved.

Now, is there a way to do it?

Currently it is possible to move lightmapped object in the scene, but it isn’t possible to place lightmaps into prefab and any attempt to move a light probe destroys its lighting information.
It is also possible to roll some sort of custom lightmapping solution and bunch of custom shaders… but isn’t the engine supposed to provide something like that?

Now, is there a way to do it?

You can set the lightmap via script:

MeshRenderer mr = GetComponent<MeshRenderer>();
mr.lightmapScaleOffset = lightmapScaleOffset;
mr.lightmapIndex = lightmapIndex;

You need an object in the scene when baking, bake it, take the index and scale offset from that object in the editor, store it somewhere, and then at runtime assign it to the object. Likely the index and scale offset will be different for every scene the object is in. I’m using this technique to do lightmaps on procedural geometry.

1 Like

Interesting. Also useful.

Is lightmap data tied to the scene though, or am I overlooking something?
https://docs.unity3d.com/ScriptReference/LightmapData.html
https://docs.unity3d.com/ScriptReference/LightmapSettings.html

Given that you’re using procedural geometry, have you tried tinkering with light probes? Having a local moveable light probe array would’ve been incredibly useful.

Yes, it’s tied to the scene. So you would need to add your prefab to every scene, set it as static, bake, take the index/offset, delete the prefab, and then when you instantiate it, restore those values based on what scene it’s in. You could script a bunch of this work so it would be done automatically for you. But might not be worth it.

It’s procedurally generated, but actually just a “cutout” of static geometry based on camera angle and character locations. So in this case, a single light map works fine (for some definition of “fine”; my dev art leaves something to be desired):

7539002--931184--upload_2021-10-1_10-49-10.png

But yeah light probes would be awesome to have.

7539002--931184--upload_2021-10-1_10-49-10.png

1 Like