Unity Rendering Principle (8) Unity Rendering Order and Rendering Path

The previous blogs talked about the basic principles of game rendering and how to write a Shader in Unity. This blog first inserts two concepts:

  • What is the rendering order and overlay relationship between different objects in Unity, that is, the rendering order.
  • What is Unity’s render path and what does it have to do with the render order?

Rendering order

In Unity we will have a lot of objects, are these objects rendered at the same time, what is the order of rendering, and what is the overlay relationship? Let’s take a look at some of the factors that will affect.

Camera

This property indicates the depth of the camera, if there are multiple cameras in the scene, the greater the depth of the more backward, that is to say, the depth of the content of the camera will be directly overlaid in the depth of the small camera content.

The dividing line between transparent and opaque objects (Render

The RenderQueue 2500 is the watershed between transparent and opaque.

Under the same camera

Objects with a Renderqueue less than 2500 are always drawn before the Renderqueue is greater than 2500.

Sorting

Visible in Tags & Layers settings

If the Camera is the same, then look at Sorting Layers, the lower the earlier the drawing.

Sorting Layers are set via the sortingLayerName property of the Renderer.

Note that this Layer is not the Layer set in the upper right corner of the Inspector of the GameObject, that Layer is for the camera to do Culling

Order

Compared to the Sorting Layer suborder, this value is only valid when all are in the same layer.

SoringOrder is also an attribute of Render, this order is to set a number, the larger the number, the more displayed on it.

Render

“Queue” set for Tags in Shader.

If the above items are the same, it depends on the renderQueue, renderQueure is a property of Material, in fact, is the renderQueue in Shader, this is also an int property, the smaller the value, the more front rendering.

By default, Unity sorts your objects based on how close they are to the camera. Therefore, when an object is closer to the camera, it will be drawn on top of other objects farther away. This is valid and appropriate for most cases, but in some special cases you may want to control the order in which objects are drawn yourself. With the Tags {} block we can get such control.

Unity provides us with some default render queues, each corresponding to a unique value, to guide Unity to draw objects to the screen. These built-in render queues are called Background, Geometry, AlphaTest, GeometryLast, Transparent, Overlay. These queues are not created casually, they are designed to make it easier for us to write Shaders and handle real-time rendering.

At the same time, we need to display the declaration ZWrite Off in SubShader to inform Unity that we will rewrite the rendering depth sorting of the object.

This does not conflict with the transparent and opaque dividing line mentioned above.

Depth sorting. Sort by bounding box depth

When the above elements are the same, for example, there are several buildings that are blocked from the camera, which one is rendered last depends on the distance between the object and the camera.

This property is enabled by ZTest in the Shader.

Opaque objects prioritize from near to far

Transparent objects prioritize from far to near

Sort by the depth of the center point of the bounding box.

Summary

Sort the rendering processing in order of condition. Sort by major conditions first, then by minor conditions

  1. Camera Depth: The smaller the priority
  2. RenderQueue 2500 or less
    1. Sorting Layer/Order in Layer
           1. According to the value set in Sorting Layer/Order in Layer, the smaller the priority.
           2. Without this property, it is equivalent to Sorting Layer = default, Order in Layer = 0 participating in sorting
    2. The smaller the RenderQueue, the higher the priority
    3. RenderQueue is equal, sorting from near to far takes precedence
  3. RenderQueue 2500 or more
    1. Sorting Layer/Order in Layer
           1. According to the value set in Sorting Layer/Order in Layer, the smaller the priority.
           2. Without this property, it is equivalent to Sorting Layer = default, Order in Layer = 0 participating in sorting
    2. The smaller the RenderQueue, the higher the priority
    3. RenderQueue is equal,

Note: 2500 is the key value, which is the boundary between transparent and opaque, so we should pay attention to the point when considering hierarchy: renderqueue

Rendering path

After talking about rendering order, let’s talk about another very similar term in Unity, called rendering path

Unity’s built-in rendering pipeline supports different rendering paths. Rendering paths are a series of actions related to lighting and shadows. Different rendering paths have different functional and performance characteristics. Depending on the project type and target hardware, it should be determined which rendering path is best for your project.

You can select the render path that your project uses in the Graphics window and override that path for each camera.

If the GPU on the device running the project does not support the selected rendering path, Unity will automatically use a lower-fidelity rendering path. For example, on GPUs that cannot handle delayed shading, Unity uses forward rendering.

Forward rendering

Forward rendering is the default render path in the built-in render pipeline. This is the general render path.

Rendering real-time light sources with forward rendering is very resource-intensive. To offset this cost, you can choose the number of light sources Unity should render for each pixel at any one time. Unity will render the remaining light sources in the scene with lower fidelity: each vertex or each object.

If the project does not use a lot of real-time light sources, or if light fidelity is not important to the project, this rendering path may be a good choice for this project.

Delayed coloring

Delayed shading is the render path with maximum light and shadow fidelity in the built-in render pipeline.

Deferred shading requires GPU support and has some limitations. This shading method does not support semi-transparent objects (Unity uses forward rendering to render these objects), orthogonal projection (Unity uses forward rendering for these cameras), or hardware anti-aliasing (but post-processing effects can be used to achieve similar results). Deferred shading has limited support for culling masks and will always treat the Renderer.receiveShadows flag as true.

If the project has a large number of real-time light sources and requires a high level of lighting fidelity, and the target hardware supports delayed shading, then this rendering path may be a good choice for the project.

LightMode

The Rendering Path determines how lighting is applied to UnityShader. In order to obtain light source data in the scene, its rendering path needs to be specified for each Pass. In versions after 5.0, Unity supports Forward Renddering Path and Deferred Rendering Path. The system selects the forward rendering path by default, which can be changed. If you want to use multiple rendering paths, you can adjust the Rendering Path option in different cameras.
Specify the render path used by Pass by using tags in each Pass, that is, implemented with LightMode tags. Different types of render paths may contain multiple tag settings.

1
2
3
Pass{
Tags{"LightMode"="ForwardBase"}
}

Reference article:
Unity中渲染顺序总结
Unity渲染顺序(Queue,ZWrite,ZTest)