Ayfel / PrefabLightmapping

Script for saving lightmapping data to prefabs. Used through the Assets tab in Unity. Place your prefbas in the scene with this script at the root. Set up your lighting and in the editor go to Assets->Bake Prefab Lightmaps. After is processed you can now spawn your prefabs in different scenes and they will use the lightmapping from the original scene.
618 stars 88 forks source link

Bake lighmap with consistent UV Layout #42

Open Ardoos opened 2 years ago

Ardoos commented 2 years ago

Is there a way to generate lighmaps with the same UV layout? Right now when I bake lightmap again, without any changes, it looks slightly different. I'm trying to swap lightmaps on runtime and I need to generate few lighmaps with the same UV layout.

Ayfel commented 2 years ago

Hi, so this script allows you to set lightmaps regardless of the UVs so the code inside takes care of assigning lightmaps with the right uvs and all, so you technically can record different lightmaps in different lightmap scripts and load them independently. It is not something working out of the box but you can do it. Let me know if that makes sense. The script takes the lightmap it has reference to and loads it. So you could make different prefabs with different lighting and just use the serialized data for each to load each different lighting

Ardoos commented 2 years ago

Hi, thanks for the reply! I think I solved my problem, but I'm not sure why it works that way. So my idea was to:

  1. Bake first lightmap, it generates _Lightmap-0_compdir.png and _Lightmap-0_complight.exr file.
  2. Duplicate and rename this files, change lights and generate new lightmap. That way I have 2 different lightmaps.
  3. Write simple code that is able to change _mLightmaps and _mLightmapsDir and cal Init() again. I don't see any perfomance issues doing it that way.

I did it before opening this thread, because everything worked fine except 1 thing. Even if I didn't change anything _Lightmap-0_complight.exr file was different than previously baked copy. Just now I found a way to solve this problem, but it's a little annoying. To resolve this issue I have to bake lighmaps before I create prefab of an object. So basically I have to unpack my prefab, perform steps 1 and 2, then create new prefab. Somehow this way generated files have exactly same layout and everything works fine. It would be nice to find out how to do this without unpacking prefab.