Unity3d Shaderlab Modify render queue for deep sorting
For a deeper understanding of transparency, we also need to learn about depth ordering, which is simply the order in which objects are rendered.
Unity allows us to control the order in which a particular object renders to the screen through code. This approach is similar to the concept of layers in Photoshop.
Before you begin, prepare for the test scenario, or create a new shader Material. In order to compare it is necessary to use 2 material balls.
Open our shader and you can see the effect with a little editing. The process is simple as follows:
Shader"91ygame/depthsort"{Properties {_maintex ("Base (RGB)", 2D) =" White"{}} subshader {//tags {"rendertype" = "Opaque"} default tags.Tags {"Queue"="Geometry-20"}//of ourZwrite OFF//notifies unity to rewrite the object's render depth order. Lod $Cgprogram#pragmaSurface Surf Lambertsampler2d _maintex; structInput {float2 Uv_maintex; }; voidSurf (Input in, InOut surfaceoutput o) {half4 c=tex2d (_maintex, In.uv_maintex); O.albedo=C.rgb; O.alpha=C.A; } ENDCG} FallBack"Diffuse"}
As you can see, the content of the changes is less than the ultimate .....
Return to unity and see the effect:
Yes, you've read it right. The shadow was in front, exposing his true coordinate position. But in the final image rendering, the green sphere is rendered to the back.
By default, unity determines the rendering order based on the distance between the object and the camera, which is consistent with most scenario requirements. If one day you need to change.
Then you can control the order of rendering autonomously using the tags{} module.
Unity's built-in render queue is as follows:
Render Queue |
Render Queue Description |
Render Queue values |
Background Background |
This queue is first rendered. It is used for skyboxes and so on. |
1000 |
Geometry Geometry |
This is the default render queue. It is used for most objects. This queue is used by opaque geometry. |
2000 |
Alphatest
"Transparency Test" |
The geometry used for alpha testing, unlike the geometry queue, is more efficient when objects that are rendered after all geometry objects are drawn. |
2450 |
Transparent Transparent |
The render queue is rendered after the geometry and alphatest queues, taking the order from the back to the front. Any shaders objects that are mixed through the alpha channel (that is, those that do not write to the depth cache) use the queue, such as glass and particle effects. |
3000 |
Overlay Coverage |
The render queue is used to implement the overlay effect. Any object that is finally rendered uses the queue, such as a lens flare. |
4000 |
Once you are sure which render queue to use, you can give him a built-in render queue tag. Our shader uses Geometry, so we're writing tags { "Queue" ="Geometry-20"} .
We also tell the object to render after the geometry queue, so we have modified it to "Geometry-20". The purpose of this is to tell unity that we have changed it to an opaque or entity object.
But it will be rendered after all other opaque objects.
Finally, we need to declare the zwrite tag in Subshader. This tells unity that we want to rewrite the object's rendering depth order.
We assign a value to the render queue that it identifies, and the value of Zwrite is set to OFF. Does not set, the wood has the effect Oh!
Zwrite: Whether the pixel depth of this object will be recorded (default record)
Zwrite on: Record depth
Zwrite off: Does not record depth, usually used for translucent objects.
Unity3d Shaderlab Modify render queue for deep sorting