- Home /
Force elements in same batch
In our game iphone game we're drawing fake shadows under about every object. Although most are defined static (can't move only destroyed) they aren't batched all together. This is probably done to prevent drawing order issues. But as with many shadows this results in easily 10 draw calls for the shadows alone it would be useful to be able to force them in one batch (and live with possible ordering issues). Is there a way to do this?
An answer to this probably will also answer my question: http://answers.unity3d.com/questions/2372/what-exactly-in-a-shader-stops-batching-from-happening
Possibly it's more about which lights affect the objects then distance from camera, but it's black magic. Added an answer to your q with my findings.
Answer by ReJ · May 24, 2010 at 01:11 PM
First, make sure all your fake shadows use the same material. Next, 'move' all your fake shadows into the separate rendering layer (Render Queue) by specifying:
Tags {"Queue" = "Transparent-1" } in the shader
Explanation: Unity have to sort all transparent objects by distance instead of sorting them by material. If you have different materials on transparent objects in your frame, then chance of them being batched decreases. By putting them into the separate rendering layer you tell Unity to sort them independently of other transparent objects - maximizing batching.
I already did that (sorry, haven't been in Answers for quite a while) but still get more batches than I would expect when using lighting, even though the shader doesn't use lighting. We fixed it with making shadow part of of the objects using vertex colors to make them transparant.
You can avoid editing the shader by using a script to change the queue. This script will do it if it is attached to 1 of the objects using the material. http://wiki.unity3d.com/index.php/SetRenderQueue