Transparency sorting within meshes

I just stumbled across a problem I thought I caused with my shader, but then realized, that unitys standard shader is also not able to handle this simple setup: A cube with normals pointing outward, and in there a cube with normals pointing inwards:

As you can see in the screenshot, the problem can be fixed by separating the cubes, because then the triangles get sorted correctly (ZSorting problem with Unity`s Transparent Diffuse shader - Unity Engine - Unity Discussions).

Is there any other possibility to solve that sorting issue? It irritates me, that this rather simple setup causes problems.

I attached the fbx for all interested :slight_smile:

3199591–244562–TransparentCubes.zip (5.18 KB)

You need to change the order of the triangles / materials in the mesh so the interior triangles are rendered first. Unity won’t do this for you, and most tools don’t usually expose this. Unreal used to have some tools that would reorder polygons on import based on some heuristic (like closest to pivot). In 3ds max I do this by separating the components and then attaching them back together in the order I want them to render, so break apart, select the interior box, attach exterior.

You could also have the interior box use an opaque material, or use two materials and the materials’ render queue to force the render order.

Also, this isn’t a Unity problem. This is a still unsolved “welcome to real time rendering” problem.

See this for a more thorough explanation of the problem:

And for an alternative solution see:

3 Likes

Thanks a lot for your response!
I did take a look at sorting the cubes, but somehow Cinema is a bit bitchy when you just change the order of vertices. Your tip with separating them and then unifing them in the order I want them to be rendered did the trick:


1: Separate Meshes, 2: Unified mesh, 3: Sorted unified mesh

Interestingly solution does not always work. I could of course change the render order of the materials, but for my real world purpose that is not possible. I hope we will get the sorting right for the more complex mesh :slight_smile:

Thanks again for the indepth answer, really appreciated!