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

设置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中要关闭深度测试)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GarFe-Liu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值