Control renderQueue to solve the problem of rendering order of NGUI and Unity3D objects

Everyone who has done UI should have encountered the problem of rendering order of NGUI and Unity3D objects. It mainly refers to the layering problem when the UI is displayed together with the special effects and 3D characters produced by Unity.

Since the UI and special effects are all rendered in a transparent manner, Unity and NGUI actually have no awareness of each other when managing the render queue of the same transparent object, which leads to a sorting problem. Now introduce the render queue value to achieve the purpose of rendering the UI and special effects in the order we want.

Here we first explain the details about setting up the draw call of the UI, which is also the most tedious part. We know that NGUI will merge widgets using the same material into the same draw call for rendering. However, in our requirement, this feature leads to the problem of not being able to insert other rendering queues between widgets, which is the original sandwich problem. If it only involves interspersing UI controls, NGUI can solve it through depth settings:

If multiple controls using the same material are set to different depth values, NGUI will still merge these controls into the same draw call.
If multiple controls with different depths are set and different materials are used, NGUI will merge them into one draw call. Break up into different draw calls, and the order is specified according to the depth.

In Unity, material.renderQueue is a property used to set the material's rendering queue (Render Queue). The render queue is a mechanism used to control the rendering order. It determines the drawing order of objects in the rendering pipeline, thus affecting their rendering order in the scene.

Each material has an associated render queue, which determines at which stage of the rendering pipeline the material will be rendered. A smaller render queue value means the object will be rendered early in the rendering pipeline, while a larger render queue value means the object will be rendered later in the rendering pipeline.

The material's render queue can be set using the material.renderQueue property. Typically this can be used to control the rendering order of materials to resolve rendering issues with transparent objects, ensuring they render correctly in different depth orders.

using UnityEngine;

public class RenderQueueExample : MonoBehaviour
{
    
    
    public Material customMaterial;
    public int customRenderQueue = 3000; // 设置自定义渲染队列值

    private void Start()
    {
    
    
        if (customMaterial != null)
        {
    
    
            customMaterial.renderQueue = customRenderQueue; // 设置材质的渲染队列值
        }
    }
}

customMaterial is a shader that can be assigned to a public field of this script in the Unity Editor. You can then set the material's render queue value using the customMaterial.renderQueue property.

Advantages and Disadvantages

Using material.renderQueue to control a material's render queue has some benefits and potential drawbacks, depending on your project's needs and usage.

benefit:

Addresses depth ordering issues with transparent objects: When there are multiple transparent objects overlapping in a scene, their rendering order is critical to display correctly. By adjusting the render queue for different materials, you can ensure that transparent objects are rendered in the correct order, avoiding depth sorting issues.

Implement a specific rendering order: Sometimes you may want specific objects to be rendered at a certain stage in the rendering pipeline, for example after or before the skybox is rendered. By setting up a render queue, you can control the order in which objects are rendered to suit your needs.

Custom rendering effects: Some rendering effects may require objects to be rendered in a specific order. By setting up the render queue, you can achieve customized rendering effects such as strokes, glows, etc.

harm:

Increased complexity: Using a render queue can increase the complexity of your project, especially when fine-tuning the rendering order is required. Too many render queue adjustments can make a project difficult to maintain. It is even possible that multiple UIs or objects share a material, but the displayed positions are inconsistent in different scenarios.

Potential rendering issues: Improperly setting up the render queue may cause rendering issues such as object flickering (z_fightong), depth conflicts, etc. Care needs to be taken when adjusting the render queue to ensure there are no problems in the actual rendering.

May not apply to all situations: Not all projects require explicit control of the render queue. The default rendering order in some projects may be sufficient and does not require additional adjustments. Adjusting the Z axis may be a better solution

To sum up, using material.renderQueue can solve the problem of rendering order in some cases, but you need to be careful when using it. When making adjustments to the render queue, it's a good idea to test and carefully observe the rendering results to ensure you're achieving the desired results.

I will add picture descriptions later

Guess you like

Origin blog.csdn.net/qq_45498613/article/details/132245399