- Home /
Alpha blending, depth sorting and batching
Hello! Making 2D game, where I have very many transparent objects, I noticed a strange thing with dynamic batching. If I have alpha blended objects (in my example, there are about 500 grass planes), which I z-sort when starting a game, everysthing is OK. Grass is batched perfectly. But the thing is going wrong when I'm trying to group different objects with different materials. For instance I'm making a tree, so there is a parent empty object, and 4-5 objects for leaves, crone and so on. Every different part has its own material. So, I put the tree in my scene and duplicate it few times, with X offset. Batching is all right. Because they have the same Z-depth. But if I change Y coordinate (depending on Y coordinate, script makes calculations for Z coord), batching is broken. I know that depth buffer has problems with alpha blending. But with the single objects everything is ok. Even if I have a group of objects with one shared material, z-sorting wotks. The bad thing happens only when I have a group of objects with different materials.
Thanks in advance :)
Answer by Jessy · Jul 26, 2011 at 03:38 PM
Use a different queue on every material. http://answers.unity3d.com/questions/9942/force-elements-in-same-batch.html
Answer by Xroft666 · Jul 26, 2011 at 04:18 PM
Thank you, it helped ) But damn.. if I have 10+ types of trees and other different vegetations, for each subobject I have to create its own shader with different queue value?
This should be a comment, not a question. But yes, you either need to do that, or put them in an earlier queue than 2500, and not have them sorted automatically by depth. You can also script renderQueue, but it isn't serialized. http://forum.unity3d.com/threads/58794-renderqueue-sorting?p=376039&viewfull=1#post376039
One last question... i hope :) If alpha blending has problems with depth sorting, why it is put in 3000+ query by default, so it has to be sorted by depth?
I'm not sure what you mean. Those queues are helpful, so farther-away objects are rendered first, providing a better chance that blending will look right. With flat objects far enough apart, it's a perfect solution.
I have only now implemented my objects into whole project, and have only now understood that they are not sorting. What I wanted is: use blended objects with sorting and batching. But, if I use static render queue, there is no chance to sort objects in the scene? Or as you said I can script render queue, but I think it is very not optimized? Thank you