[Unity Shaders] Transparency-uses a rendering queue for deep sorting and unity Rendering

Source: Internet
Author: User

[Unity Shaders] Transparency-uses a rendering queue for deep sorting and unity Rendering

This series mainly references the book "Unity Shaders and Effects Cookbook" (thanks to the author of the original book) and adds a bit of personal understanding or expansion.

Here are all the illustrations in this book. Here is the code and resources required for this book (you can also download them from the official website ).

========================================================== ===================================================== =====



Preface
To make us really understand transparency, we need to understand the deep sorting, or the order in which objects are drawn. Unity allows us to control the order in which a specific object is drawn to the screen, so we can better control which objects should overwrite other objects. You can understand the order of painting as the concept of layers in Photoshop. The painting sequence is particularly important when processing transparency or elements similar to interface objects.
This article will explain how to use the built-in tags of Unity to render your objects using this layered method. This is very important because you will have better control over how your objects are drawn to the game interface.


Preparations




Implementation

The Shader part of the code is actually very simple; it only requires two lines of new code.

The complete code is as follows:
Shader "Custom/Depth001" {Properties {_MainTex ("Base (RGB)", 2D) = "white" {}}SubShader {Tags { "Queue"="Geometry-20" }ZWrite OffLOD 200CGPROGRAM#pragma surface surf Lambertsampler2D _MainTex;struct Input {float2 uv_MainTex;};void surf (Input IN, inout SurfaceOutput o) {half4 c = tex2D (_MainTex, IN.uv_MainTex);o.Albedo = c.rgb;o.Alpha = c.a;}ENDCG} FallBack "Diffuse"}



Explanation
By default, Unity sorts your objects based on their distance from the camera. Therefore, when an object is closer to the camera, it is preferentially drawn on other farther objects. This is effective and appropriate in most cases, but in some special cases, you may want to control the order of object painting by yourself. With the Tags {} block, we can get this control.
Unity provides us with some default rendering queues, each corresponding to a unique value, to guide Unity to draw objects to the screen. These built-in rendering queues are called Background, Geometry, AlphaTest, Transparent, Qverlay. These queues are not created at will. They are designed to make it easier for us to write Shader and process real-time rendering. The following table describes the usage of these rendering Queues:
Rendering queue Rendering queue description Rendering queue Value
Background This queue is first rendered. It is used for skyboxes and so on. 1000
Geometry This is the default rendering queue. It is used for most objects. The queue is used by the opaque ry. 2000
AlphaTest The body of the channel check uses this queue. It is different from the Geometry queue and is more effective for objects that are rendered after all stereo objects are drawn. 2450
Transparent The rendering queue is rendered after the Geometry and AlphaTest queues. Any channel-mixed Shaders objects (that is, those that do not write into the deep cache) use this queue, such as glass and particle effects. 3000
Overlay The rendering queue serves the covering effect. Any final rendered object uses this queue, such as lens halo. 4000

Therefore, once you know which rendering queue your object belongs to, you can specify its built-in rendering queue tag. Our Shader uses the Geometry Queue, So we write: Tags {"Queue" = "Geometry "}. However, we want to tell us that the object is drawn after all the objects in our Geometry queue and before the Background queue object, therefore, we changed it to Tags {"Queue" = "Geometry-20 "}. This tells Unity that we want to treat this object as a three-dimensional object, but please render it after all other opaque objects.
Note: The queue value corresponding to Geometry is 2000. Therefore, "Geometry-20" means that a queue with a queue value of 1980 is used. The smaller the value, the higher the rendering, it will be blocked by the rendered objects.
Finally, we need to declare the ZWrite tag in the SubShader block. This tells Unity that we want to rewrite the object's deep sorting and we will specify a new rendering queue for it. Therefore, we simply set the ZWrite value to Off. (No effect if not set)




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.