(13) Shader LOD and rendering queue of computer graphics

Set the LOD value

1: LOD (Level of Detail), use different versions of Shader according to LOD;
2: Give SubShader an LOD value in the shader,
LOD value, the program sets the LOD value of this shader, only the first one is less than or equal to LOD value subShader will be executed;
3: Each shader will only have at most one SubShader used;
4: Set the maximum LOD value through Shader maximumLoD;
5: Set the global LOD value, Shader.globalMaximumLOD;
6: Unity built-in coloring LOD level of device points:
(1)VertexLit kind of shaders 100
(2) Decal, Reflective VertexLit 150
(3)Diffuse 200
(4)Difuse Detail 250
(5) Bumped, Specular 300
(6) BumpedSpecular 400
(7) Parallax 500
( 8) Parallax Specular 600 

shader case:

The LOD values ​​are (600, 500, 400) respectively 

Shader "GFStudy/LodShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }

    // 一个shader程序,即使有多个SubShader ,也会根据实际情况,选择一个执行,其他的就不执行了。
    // 找到第一个 小于等于 Shader.maximumLODSubShaderSubShader 的值
    // 所以一般 lod排序 大的在前面,小的在后面, 如果小的在前面的话,在改变lod值时,会被这个小的满足而停止往后查找SubShader
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 600  // LOD

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = fixed3(1.0f,0.0f,0.0f);   // 红色
            // Metallic and smoothness come from slider variables
            /*o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;*/
        }
        ENDCG
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 500  // LOD

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = fixed3(0.0f,1.0f,0.0f);  // 绿色
            // Metallic and smoothness come from slider variables
            /*o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;*/
        }
        ENDCG
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 400  // LOD

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = fixed3(0.0f,0.0f,1.0f);  // 蓝色
            // Metallic and smoothness come from slider variables
            /*o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;*/
        }
        ENDCG
    }

    FallBack "Diffuse"
}

C# control code:

using UnityEngine;

public class LodShaderCtrl : MonoBehaviour
{
    public Shader m_Shader;
    [SerializeField]
    private int m_lodValue;

    // Update is called once per frame
    void Update()
    {
        // 当前shader 最大的lod value
        this.m_Shader.maximumLOD = this.m_lodValue;
    }
}

LOD200: Use Diffuse 

LOD400: 



LOD500: 

LOD600:

render queue

1: Rendering queue label optional values:
(1) Background background, the corresponding value is 1000;
(2) Geometry (default) The corresponding value of geometry is 2000, this queue is the default rendering queue, most opaque
objects;
( 3) AlphaTest Alpha test, the corresponding value is 2450, the geometry of alpha test uses this queue, it is
a queue of independent hand Geometry, it can render the object using Alpha test more effectively after all solid objects are drawn;
(4)Transparent : Transparent, the corresponding value is 3000, this rendering queue is rendered in Geometry, and
any object with alpha blending is rendered in this queue from the back to front ;
(5) Overlay grants a corresponding value of 4000, this rendering queue It is the last rendered object;
2: Unity rendering mode: ordinary objects are rendered from front to back, and Apha is rendered from back to front;
2: The value of the rendering queue determines the order in which Unity renders scene objects, and when the depth test is turned off ;

Rendering queue modification code:

Shader "GFStudy/Renderqueue"
{
    // 渲染队列 
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry+100" } // "Geometry+100" 这里时在集合体渲染队列 在 加 100
        LOD 200
        ZTest off //关闭深度测试

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = _Color.rgb;
            // Metallic and smoothness come from slider variables
            /*o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;*/
        }
        ENDCG
    }
    FallBack "Diffuse"
}

By modifying the rendering queue, the following objects can be drawn first

The effect is as follows:

Before the rendering queue is not modified: (the green ball is behind the white square, and the actual placement is like this)

After modifying the rendering queue: (the green ball will be rendered in front of the white square, and the depth test should be turned off in the shader)

Guess you like

Origin blog.csdn.net/u013774978/article/details/130233574