unity shader之LOD以及渲染队列

LOD设置

LOD全称Level of Detail
作用:unity引擎会根据不同的LOD值在不同的平台上使用不同的SubShader
注意:在上几篇博客中已经说过在一个着色器中会有一到多个SubShader,但是系统每次只会执行一个子着色器,选择子着色器的标准就是根据子着色器所设置的LOD的值来进行选择,每一次使用着色器,都会选择第一个小于等于LOD值的子着色器。

如何设置Shader的LOD的值
:通过Shader maximumlOOD来设置最大的LOD值即Shader.globalMaximumLOD;

unity内置着色器分LOD等级
unity shader之LOD以及渲染队列_第1张图片
注意在设置最大LOD值的时候不能小于 Ver texLit kind of shaders 的值(100),否则unity将不会显示使用此着色器的物体
Demo:
场景
unity shader之LOD以及渲染队列_第2张图片
shader代码

Shader "Custom/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
    }
//每一次只会使用一个SubShader(根据LOD的值来使用)
//找到第一个<=Shader.maximumLOD这个subShader执行;如果所有的均不符合,则最后使用fallback
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 600

        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)
        {
           o.Albedo = fixed4(1.0,0.0,0.0,1.0);
            
        }
        ENDCG
    }
			SubShader
		{
			Tags { "RenderType" = "Opaque" }
			LOD 500

			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)
			{
			   o.Albedo = fixed4(0.0,1.0,0.0,1.0);

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

				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)
				{
				   o.Albedo = fixed4(0.0,0.0,1.0,1.0);

				}
				ENDCG
			}
    FallBack "Diffuse"
}

c#控制LOD值的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// 
/// 根据不同的平台选择不同的SubShader 
/// 
public class My_Scripts : MonoBehaviour
{
    public Shader shader;
    public int LOD_Value = 600;
    //这个值最小为100,如果小于100将什么都不显示(系统内置的shaderLOD值最小为100,如果小于100将什么都不显示)
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(this.shader.maximumLOD);
        //this.shader.maximumLOD = this.LOD_Value;
    }

    // Update is called once per frame
    void Update()
    {
        //当前这个shader最大的LOD值
        this.shader.maximumLOD = this.LOD_Value;
    }
}

显示效果
unity shader之LOD以及渲染队列_第3张图片
unity shader之LOD以及渲染队列_第4张图片
unity shader之LOD以及渲染队列_第5张图片

渲染队列

渲染队列
Unity引擎会将需要渲染的物体分成几个渲染的队列
Unity默认有几个渲染队列
背景1000、Geometry(几何队列)2000、Alpha Test Alpha测试(2450)、Transparent(透明)对应值为3000、Overlay(覆盖)对应值为4000这个队列是最后的渲染队列
从数值小的开始一直到数值最大(需要注意是否透明)
Unity渲染模式:普通物体从前向后渲染Alpha从后向前绘制
Demo:
场景:
将红球放置在篮球的后面
unity shader之LOD以及渲染队列_第6张图片
摄像机观测场景如下
红球shader代码

Shader "Custom/Render_text"
{
    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" }
			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)
        {
            o.Albedo = _Color.rgb;        
        }
        ENDCG
    }
    FallBack "Diffuse"
}

注意如果要修改某着色器的渲染队列并且令其起作用的话,需要将深度测试关闭
修改后的场景
unity shader之LOD以及渲染队列_第7张图片

你可能感兴趣的:(unity,shader学习)