- Home /
Why the opaque is rendered not in the order of front to back?
Why the opaque is rendered not in the order of front to back? Underside is screenshot of my test scene.
There are 3 cubes in the scene(red cube is scaled). Their names are 1,3,2 from right to left. And the camera is in the far right. Two green cubes have the same material, the red cub has another one. The only difference of two materials is color. And their shader is Standard. At first, I put the red cube is near to right green cube. The render order is fully from front to back (i.e. 1-3-2). Then i drag the red cube to left to let it more close to left green cube. see the screenshot
Now the render order becomes not fully from front to back. It becomes right green cube is first, left green cube is secone, middle red cube is last (i.e. 1-2-3). I dont know why unity sort gameobjects in this way.
More strange things is if I replace the red cubes material with the green cubes material, letting the three cubes have a same material, the render order become fully from front to back. I really dont know the reason. Hoping someone can tell me.
Answer by Bunny83 · Jan 03, 2019 at 04:38 PM
Opaque geometry is generally rendered from front to back to avoid unnecessariy overdraw which consumes fillrate. Since opaque objects are opaque you can't see through them. So rendering the closest objects first you will ensure that overlapping fragments can be discarded early by the z-test.
Transparent geometry generally has to be drawn back to front since each triangle depends on what's behind the object. You can't render an object behind an already rendered object. When a triangle is rendered it can only be mixed with what is already on screen based on the used shader. When you render transparent objects in the wrong order the resulting color would be wrong.
When you set a custom render order (based on the fundamental render groups) you just force rendering of a certain material before another. However all opaque objects with the same material are always rendered front to back while transparent objects with the same material are always rendered back to front. Though keep in mind that Unity only sorts the objects based on their pivot positions. There are many transparency sorting situations that can't be handled automatically or would be extremely difficult to pull off.
Finally you should keep in mind that a single object that is rendered with a transparent shader that has self overlapping triangles can be have strange. Transparent shaders usually do not write into the depth buffer, but they perform depth testing. Since the individual triangles of an object are rendered in the order they are specified in the mesh, the order in which 2 overlapping triangles are rendered can't be sorted properly. If the object is very tranclucent this usually can barely be noticed. However if they are almost opaque a triangle further away could be rendered in front of a closer one. Transparency generally gives you the most problems. Opaque geometry usually can be rendered in any order you like. The only difference is the performance hit due to overdraw.
edit
I forgot another important thing: Batching. Batching can also mess with the rendering order. So the green cubes (since they use the same material) could be dynamically batch into a single object to reduce drawcalls / setpass calls.
Your answer
Follow this Question
Related Questions
Problem with rendering? 0 Answers
Strange Render Order Issue 0 Answers
Rendering Order 4 Answers
Sprite behind semi transparent mesh 0 Answers
What is the expected behaviour of Dynamic Batching? 2 Answers