(十三)计算机图形学 之 Shader LOD与渲染队列

文章详细介绍了Unity中LOD(细节级别)的概念和应用,如何通过设置LOD值来决定不同复杂度的Shader执行。同时,讲解了渲染队列的工作原理,通过调整渲染队列标签可以改变物体的渲染顺序,例如关闭深度测试让某些物体优先渲染。示例代码展示了如何创建和控制LOD级别的Shader以及修改渲染队列以影响渲染顺序。
摘要由CSDN通过智能技术生成

设置LOD值

1:LOD (Level of Detail),根据LOD来设置使用不同版本的Shader;
2:着色器中给SubShader一个LOD值,
个LOD值,程序来设置这 个shader的LOD值,只有第一个小于等于LOD值subShader才会被执行;
3:每个shader最多只会有一个SubShader被使用;
4:通过Shader maximumLoD来设置最大的LOD值;
5:设置全局的LOD值,Shader.globalMaximumLOD;
6: Unity内置着色器分LOD等级:
(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案例:

LOD值分别为 (600,500,400) 

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#控制代码:

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:使用  Diffuse 

LOD400: 



LOD500: 

LOD600:

渲染队列

1:渲染队列标签可选值:
(1)Background 背景,对应的值为1000;
(2) Geometry(default) 几何体对应的值为2000,这个队列是默认的渲染队列,大多数不透明
的物体;
(3)AlphaTest Alpha测试,对应值为2450, alpha测试的几何体使用这种队列,它是独立手
Geometry的队列,它可以在所有固体对象绘制后更有效的渲染采用Alpha测试的对象;
(4)Transparent:透明,对应值3000,这个渲染队列在Geometry被渲染,采用从后向前的次F
任何有alpha混合的对象都在这个队列里面渲染;
(5)Overlay 授盖对应值 为4000,这个渲染队列是最后渲染的物体;
2:Unity 渲染模式:普通物体从前向后渲染,Apha从后向前渲染;
2:渲染队列的数值决定了Unity在渲染场景物体时的先后顺序,关闭深度测试的情况下;

渲染队列修改代码:

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"
}

通过修改渲染队列,可以让后面的物体先绘制

效果如下:

未修改渲染队列前:(绿球在白方块的后面,实际摆放也是这样子的)

修改渲染队列以后:(绿球会渲染在白方块前面,在shader中要关闭深度测试)

Unity中,Tags是一种特殊的关键字,用于在Shader中指定一些渲染相关的信息,例如渲染类型、渲染队列渲染器名称等。Tags通常被用于告诉Unity如何处理Shader,以及如何在场景中使用该Shader。 下面是一些常用的Tags及其含义: - RenderType:指定渲染类型,例如Opaque(不透明)、Transparent(半透明)、TransparentCutout(半透明带透明贴图)等。 - Queue:指定渲染队列,用于控制Shader渲染顺序。具有相同队列Shader将按照其在场景中的顺序进行渲染。 - DisableBatching:禁用批处理,用于控制是否对多个物体使用相同的Shader进行批处理。如果禁用批处理,则会为每个物体单独进行渲染,可能会影响性能。 - RenderPipeline:指定渲染管线,用于控制Shader在哪个渲染管线中使用(例如Built-in或Universal Render Pipeline)。 - ShaderLOD:指定ShaderLOD级别,用于控制Shader在不同距离下的细节程度。较远的物体可以使用较低的LOD级别以提高性能。 Tags通常出现在Shader文件的SubShader块中,用于告诉Unity如何处理该SubShader。例如,在以下示例中,我们使用RenderType标签指定该SubShader渲染类型为Opaque: ```ShaderLab SubShader { Tags { "RenderType"="Opaque" } // ... } ``` 除了在Shader文件中使用Tags之外,我们还可以在Unity Editor中使用Tags进行场景配置。例如,在设置相机的渲染路径时,我们可以使用Tags来指定渲染管线: ```C# Camera.main.depthTextureMode = DepthTextureMode.Depth; Camera.main.renderingPath = RenderingPath.Forward; Camera.main.actualRenderingPath = RenderingPath.Forward; Camera.main.clearFlags = CameraClearFlags.SolidColor; Camera.main.backgroundColor = Color.black; Camera.main.allowHDR = true; Camera.main.allowMSAA = true; Camera.main.allowDynamicResolution = true; Camera.main.SetReplacementShader(Shader.Find("MyShader"), "RenderType=Opaque"); ``` 使用Tags可以为我们提供更多的灵活性和控制权,使得Shader在不同的场景中都能够得到良好的表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GarFe-Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值