Static batching and lightmapping issues

Hi!

I’m having some trouble with static batching and lightmapping. I’m working on a tile-based game and as an optimization for each map I have a script to create an “exported” scene that will contain everything statically batched, and slightly modified. Basically I create one large static mesh per used material. Here’s the code I use:

It is important to note that I also modify the static batched mesh after the batching to do some custom changes and optimizations.

Anyway, after all this, I try to create a lightmap for the scene, but it ends up looking like shit. If I do it the other way around (lightmap first, batch later) it works, but obviously some of the shadows will not be in the correct place.

What is causing this? How can I fix it?
It would also be nice to have some documentation on what static batching does exactly. I assume it’s a sort of mix between batching and instancing, where an active index buffer is maintained after frustum culling. And I have no idea what the “static batching” option on the player settings tab does, and how it affects my manual batching.

I think the problem is if you batch first then all the renderers will have the same lightmapTilingOffset. This property is used to adjust lightmap UVs to fit in a subrect of a lightmap, if they’re all the same then you’ll get overdraw when rendering the lightmap which would look wrong. I think you need to get the renderers atlased before batching them so their UVs end up in different parts of the lightmap. I’m not sure it’s possible to do this separately from lightmapping so you may have to write your own code to assign lightmapTilingOffset values. Arranging your instances in a grid would probably work fine if they’ve got roughly the same surface area.

1 Like

Thank you for the suggestions, but I’m not convinced that it’s a tiling issue. I’ve attached a few screenshots to show my problem.

This is the map in the editor, without lightmapping:

This is what happens if I run lightmapping before batching - mostly OK, but obviously the modified parts of the mesh have bad lightmap colors

And this is what happens if I run batching before lightmapping (also note that it generates 9 lightmaps as opposed to the usual 2, and “exporting meshes to beast” takes a really long time:

Thanks.

Ok, got one other suggestion. Try running the lightmapping without batching. Select low quality in the bake options because you won’t need the maps. Then run the batching script. Then run lightmapping again in high quality but this time toggle ‘Lock Atlas’ in the bake options.

Thanks again, but this didn’t help. I’ll look into your previous reply as well in detail, but I don’t have a lot of hope I can solve this:(