Unity Shader LOD使用

LOD:Level of Detail
作用:unity引擎会根据不同的LOD值在使用不同的SubShader
Unity选择对应的Subshader会从上往下寻找第一个小于等于LOD值的子着色器。一个着色器中会有一到多个SubShader,但是系统每次只会执行一个子着色器,选择子着色器的标准就是根据子着色器所设置的LOD的值来进行选择。

如何设置Shader的LOD的值:通过Shader的下面属性进行设置:shader.maximumLOD=500;

(1)新建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
	// 找到第一个<= Shader.maximumLOD 这个subShader执行;
	
	SubShader{
				Tags { "RenderType" = "Opaque" }
				LOD 600 // 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;

				void surf(Input IN, inout SurfaceOutputStandard o) {
					o.Albedo = fixed3(1.0, 0.0, 0.0);
				}
				ENDCG
			}
	SubShader{
					Tags { "RenderType" = "Opaque" }
					LOD 500 // 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;

					void surf(Input IN, inout SurfaceOutputStandard o) {
						o.Albedo = fixed3(0.0, 1.0, 0.0);
					}
					ENDCG
				}
	SubShader{
				Tags { "RenderType" = "Opaque" }
				LOD 400 // 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;
						void surf(Input IN, inout SurfaceOutputStandard o) {
							o.Albedo = fixed3(0.0, 0.0, 1.0);
						}
						ENDCG
					}
	FallBack "Diffuse"
}

描述如下:该Shader中存在三个SubShader,LOD分别设置为600,500,400.。其对应的颜色分别设置为红色、绿色、蓝色

(2)创建一个立方体,使用该Shader对应的材质

(3)创建脚本LODCtrl,通过键盘的ABC控制Shader内部的LOD,该脚本挂载给相机。代码如下所示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LODCtrl : MonoBehaviour
{
    public Shader shader;//将Shader拖进来即可
    void Start()
    {
        Debug.Log(this.shader.maximumLOD);
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            // 当前这个shader最大的LOD_value;
            this.shader.maximumLOD = 600;
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            this.shader.maximumLOD = 500;
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            this.shader.maximumLOD = 300;
        }

    }
}

(4)运行,查看效果,通过按ABC按键,修改maximumLOD的值。查看Cube颜色的变化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值