Considerations for lightmaps
An object's lightmap coordinates are stored in a combination of:
- the renderer's lightmapIndex
- the renderer's lightmapTilingOffset and scale
- the uv2 coordinates
When lightmaps are baked the uv2 coordinates are read and the first time you bake, the renderer gets a lightmapIndex and lightmapTilingOffsets assigned.
When you use lightmaps and also want to merge meshes together, you have to be aware of a few nasty things, that are hard, if not impossible, to evade.
Order is important
To illustrate this I have made a simple scene in which I stacked 3 cubes within a room with 2 lights and 3 walls in different prime colors.
The "art work" (ahem) hierarchy looks like:
Each of these cubes has uv2 coordinates that run from (0,0) to (1,1)
When I merge the artwork with all 3 cubes together, the uv2 coordinates or each of the cubes that are now part of the same mesh still run from (0,0) to (1,1) 3 times.
When I bake a lightmap, the artwork renderer gets a lightmap index and lightmap coordinates assigned. But the result is not what you would expect
The reason is that there is only 1 meshrenderer and that mesh renderer has only 1 value for the lightmap offset and the 3 cubes all have uv2 coordinates that use up the entire region that was assigned for this renderer in the lightmap.
The proper way is to create the lightmaps first and then merge the cubes together.
Here the cubes are still separate objects and a lightmap is baked. Each of the 3 renderers gets its own lightmap offset and its own area in the lightmap.
Next we merge the artwork into 1 mesh and the uv2 coordinates are changed according to the lightmap tiling offsets that were found in each of the 3 renderers. And as you can see the lightmaps remain unchanged
Dont bake again after merging
Let's say each of these cubes occupies about 10% of the lightmap and the positions of the 3 cubes on the lightmaps are:
left bottom corner
right bottom corner
right top corner
So the cubes have the following atlas coordinates:
Cube 1: Tiling (0.1, 0.1) Offset (0, 0)
Cube 2: Tiling (0.1, 0.1) Offset (0.9, 0)
Cube 3: Tiling (0.1, 0.1) Offset (0.9, 0.9)
There will probably be lightmap sections of other objects between those regions.
When you merge the artwork into 1 mesh, the new meshrenderer will have the following atlas coordinates: Tiling (1, 1) Offset (0, 0). In other words: It will consume the entire lightmap. This is no big deal because the objects in between don't know that.
Until you bake again. The entire space in between will remain empty and either you end up with an extra lightmap or a lightmap with a very inefficient resolution.
You can't merge objects that use different lightmaps
Let's say we have a different gameobject that again contains 3 child objects that each have a mesh. Those meshes have a regular uv map and also an second uv map that is used for lightmapping. This second uv map will ideally have uv coordinates between (0, 0) and (1, 1).
So when we combine the meshes into 1 new mesh, those uv2 coordinates of the 3 meshes will overlap. Baking the lightmap will create a mess.
To prevent this, SimpleLOD looks up the lightmap tiling coordinates of the 2 mesh renderers and scales and shifts the uv2 coordinates accordingly. This prevents the uv2 coordinates from overlapping and you can still use the existing lightmap.
To deal with these rather annoying limitations as good as humanly possible, you could work as follows:
- Leave your entire scene as it is. Don't merge any meshes, don't create any lod levels, nothing.
- When your scene is finished, you bake the lightmap and make sure that for each gameobject you want to merge later on, all children have the same lightmap index
- Ideally, you even make sure the children have lightmap tiling offsets that position them next to eachother, because the space in between will only result in blank useless waste when the lightmap is rebaked.
- When everything is ready, you merge the meshes and create your lod levels.
It may be advisable to keep a working copy of the original scene, you work in that scene and only after you baked the lightmap, the meshes are merged again. Because if you rebake a lightmap after the meshes have been merged, the lightmap will get messed up and/or the lightmap space is used very inefficient (see part 1 of this webpage).