- Home /
Mixing 2d sprites with 3d environment - sorting issues
I want to achieve rendering similiar to "Don't Starve" - a mixture of 2d sprites and 3d projection on the same scene. Also my scene will use real 3d objects.
Camera is set to use orthographic projection.
My characters are gameobject hierarchies, some nodes of which contain sprite renderers. Those have default sorting layer and different layer order values (to layout various body parts). Then these characters are positioned in 3d-space.
The issue is that sorting order takes precedence over z-index. Is it possible to use sorting layers and z-ordering together? Where am I going wrong? How to implement that sorting of mine?
ps: the offending helmet of dark knight
Hi, default sprite shader doesn't write to the Z buffer and sprites are considered transparent (= with different alpha values, not only 0 or 1 alpha cut like your sprites have -as far as I can see-). Then, the sorting order of the transform is what will decide the rendering order.
So, you could use the transform sorting, changing hierarchy and sibling order to manage sorting.
You could also duplicate the default sprite shader and make some changes: removing the "ZWrite Off" line should be enough. If you have only alpha cut sprites, you could also change the Queue from "Transparent" to "Geometry" and the renderType to "TransparentCutout". Doing so might help sorting everything if you have other transparent objects like smoke/magic particle...
EDIT: don't forget to rename the (EDIT2: internal shader) name too (first line of the shader)
I'm afraid that custom shader didn't fix the issue - it brought z-fighting and a need to modify all character animations (they animate z value back to 0 even if I do add z offsets).
Ho, yes, didn't think about animation messing with local Z, sorry.
If you know what sprite is in front of what other sprite in a character, you could add some LateUpdate script (=executed after animation is done) that restore the local Z to a predeter$$anonymous$$ed value, like a layer system local to each character. Then you wouldn't have to modify animations. This "z override" value could even be exposed to be animated if you need to change layer order during some animations.
Also, I can see that the shadows of the characters are cut by the ground. What I did in a previous project is to modify the Z offset in the sprite shader so the sprites are drawn a little "in front" of 3D geometry. This is a little tricky to tune and may not be possible in your game. It's quite easy to try: add a new line with "Offset 0, -3" next to the "ZWrite On" line. This -3 value is the one to tune, it will depend on your camera settings (I needed greater values for an orthographic camera)
Your answer
![](https://koobas.hobune.stream/wayback/20220613201149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sorting Layer Conflict 1 Answer
Special sprites ordering 1 Answer
Sprites rendering slightly off world coordinates 1 Answer
Unity 2020.3 URP shader color won't change properly 0 Answers