top of page
Writer's picturesantosh nalla

Rendering Order And Sorting( Z Sorting- Depth Sorting)

Optical illusions in games(Unity) can be achieved using shaders. There is so much a shader can do to your game. Rendering order in shaders is one of those topics i found confusing to understand to start with. But when we try to break this down, it gets simple.


Here i wanna start with a question "What is Rendering Order?"

It is nothing but an order in which objects are rendered in unity. Lets suppose there are two objects O1 and O2. When O1 is rendered first and O2 later, O1 gets behind O2. O1 hides behind O2, because O2 is the latest rendered object.

So the distance between object as a whole from camera(Z axis distance), decides the rendering order which is nothing but PAINTERS ALGORITHM.

This order in which the objects are rendered can also depends up on a number called "Queue" number. Lowest Queue number object is rendered first in Painters Algorithm. But a point to note here is distance between camera and object as a whole decides the rendering order in painters algorithm only when the queue number is same for all. If this Queue number varies, distance is no more the factor which decides the rendering order.

By "Object as a whole", i mean something. In the above example even when parts of object O1 are before O2, it doesn't matter, because distance of O2 object as a whole is considered and not the individual pixels of the objects. This can be a problem/drawback of painters algorithm.


Z-Sorting is kind of algorithm that corrects Painters Algorithm. It says Z Distance from camera to the object pixel, decides the Rendering order and not the Z distance of object as a whole. In this algorithm the object pixels which are at farthest distance from camera are rendered first. So as i mentioned in the above example, object O1 and object O2 is divided into pixels and each pixel follows the "Z-distance from the camera" rule. Hence, now the parts of the object O1 which are in front of O2 gets depth.


Let me take an example and explain Painter Algorithm and Z-Sorting algorithms.

Before we proceed remember " Z- SORTING " by default is true for what ever shader we write in Unity.


So let me take 3 Quads, with 3 different shaders applied.


Shader "ShaderLearning/RenderingQueueAndZ-Sorting/Quard1"

{

Properties

{

_Color("Main Color", Color) = (1,1,1,1)

}



Subshader

{

Pass

{

ZWrite off

CGPROGRAM

#pragma vertex vert

#pragma fragment frag


uniform half4 _Color;

struct vertexInput

{

float4 vertex: POSITION;

};


struct vertexOutput

{

float4 pos : SV_POSITION;

};


vertexOutput vert(vertexInput v)

{

vertexOutput o;

o.pos = UnityObjectToClipPos(v.vertex);

return o;

}



half4 frag(vertexOutput i) : COLOR

{

return _Color;

}


ENDCG

}


}

}


Copy paste the above line of code to 3 different shaders and apply 3 different colors to them.

CASE 1: Lets test Painters Algorithm. To test this as mentioned, switch off ZWrite(Z-Sorting).

" ZWrite off". Apply same Render Queue to all the 3 in the material inspector. Now as Z-write is off and Render Queue number is same in all, distance between camera and object is considered for rendering order(Far object is rendered first).

CASE 2: Now, Lets test painters algorithm based on Render Queue number(object having lowest number is rendered first). To do this lets not forget to switch off Zwrite in the code as we did before.

For blue quad- Render Queue = 100

For Yellow quad - Render Queue = 50

For Red quad - Render Queue = 0

Set these values in the inspector tab. So Blue is rendered last and appears to be in front of every quad.




So this is it. Thanks for reading. Do drop me a mail/Comment below if you are having any doubt.

Thanks once again. :) Happy coding.



29 views0 comments

Recent Posts

See All

Comments


bottom of page