I am making my first game in unity. In that game the player can place “ecomponents” such as logic gates and other stuff.
I am using the SRP batcher to draw large numbers of components, however I am experiecning an issue with the sorting layers of the renderers. I have those 2 types of components:
“Logic Gate” : uses the "“ECompDefault” material that uses the “ECompDefault” Shader
“Power Source” : uses the “PowerSource” material and the shader “Emission” Shader
So I have 2 materials from 2 different shaders. What I expected from the srp batcher, is that it would batch all the logic gates together and all the power sources and draw one batch first adn then the other.
This is also the behaviour that I get, when I put the logic gates on the “default” sorting layer, and the power srouces on the “emission” sorting layer.
When the objects are on the same layer though, for some reason the srp bacther draws them in alternation. Each batch contains of component and it draws a logic gate, then a power source, then a logic gate again, then a power srouce, etc… instead of grouping the bacthes together.
Here is an image of the level. You can see the yellow power sources and the logic gates
Instead of 6 batches I now have 128.
Ignore the first 5 batches. You can see that the batches are now much smaller. Most of them only draw a single gameObject. The batch cause is “Node have different shaders” because for some reason it draws the different components in alternation.
I have tried to find any explanation for this but I couldn’t find anything. I have also tried using dynamic batching and disabling srp but the performance was worse. GPU instancing is also not an option because I can’t find any reasonable 2d application.
Just wondering, which are the sorting orders for the objects when you place them all in the same sorting layer?
You probably already know this, but the drawing order will follow the sorting order in this case, so if each gate object is sorted immediately after each source object, you’ll never have multiple gates/sources drawn in a row, which is required for them to batch.
If you aren’t assigning different sorting orders to the objects, then their sorting will be based on Y position I think.
Hi,
I suppose with “sorting order” you mean the “order in layer” property of the renderer. They both have it set to 0.
I just tried changing the order in layer instead of the sorting layer.
When I set the gates to 0 but the power sources to 1 there was no difference but when I set the power sources to 1000 it worked just like changing the sorting layer.
I have tried the same with changing the position’s z value and this also worked.
This helps me for setting the drawing order dynamically, because I don’t want to change the z value and now I can just change the “order in layer” property when moving a component.
However I still need to assign a specific value for each shader (in this case 0 for the gates and 1000 for the power sources, and then when moving a component I can set it to 2000) to get the batching to work. Is that intended?
The only requirement for the srp batcher to apply is that multiple objects with the same shader are rendered subsequently, the specific value of the Renderer.sortingOrder shouldn’t make a difference as long as it results in that rendering order.
When you tested setting all gates to 0 and all sources to 1, have you checked in the frame debugger that all the gates were actually rendered before all the sources?
Because they should’ve been batched in that case, unless there’s something else being rendered in-between them.
I just checked again. For 0 and 1 they are drawn in alternation but everything above 1 works and they get batched successfully and in the correct order
That’s odd, I wonder if maybe the frame debugger didn’t update when you tried those values, because If they were really drawn in alternation that would mean the sorting system wasn’t working as it should.
You could try overlapping the objects and verifying that all sources appear on top of all gates.
I found the issue regarding this. As you can see in my first post the logic gates have a symbol which is actually a child of the gate but uses the same material, however their order is set to 1