最近发现Unity的一个黑科技(其实很久以前就发现了,只是方便开头而已,哈哈);可以自定义材质的检视面板,通过这个功能还可以把功能差不多的着色器合并成一个。
例如有的着色器只是融合方式(Blend)、深度测试开关(ZTest)、渲染队列(RenderQueue)等等不一样,而基本的核心着色代码是一样的。这就是本文即将介绍的枚举类型(其他类型以后介绍)
本文的研究来自Unity的官网文档MaterialPropertyDrawer
1、首先看看BlendMode上的应用
Shader源码如下:
Shader "MaterialPropertyDrawer/MaterialPropertyDrawerBlendMode" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[Enum(UnityEngine.Rendering.BlendMode)] _SourceBlend ("Source Blend Mode", Float) = 2
[Enum(UnityEngine.Rendering.BlendMode)] _DestBlend ("Dest Blend Mode", Float) = 2
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Blend [_SourceBlend] [_DestBlend]
CGPROGRAM
#pragma surface surf Lambert
fixed4 _Color;
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
通过在属性列表中的Blend模式前添加
[Enum(UnityEngine.Rendering.BlendMode)]
就可以在检视面板中列举出所有的Blend模式,如图所示:
列表中的具体Blend模式就不用我解释了吧。
当然,某些情况下,我们并不希望列举出所有的混合模式,而只给少数的几个给美术人员使用。这一也是可以实现的,只需要我们自己来定义枚举的数值和显示就可以了。
源码如下所示:
Shader "MaterialPropertyDrawer/MaterialPropertyDrawerBlendModeCustom" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[Enum(One,1,SrcAlpha,5)] _SourceBlend ("Source Blend Mode", Float) = 1
[Enum(One,1,SrcAlpha,5)] _DestBlend ("Dest Blend Mode", Float) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Blend [_SourceBlend] [_DestBlend]
CGPROGRAM
#pragma surface surf Lambert
fixed4 _Color;
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
通过在需要枚举的类型前面添加如下代码就可以了:
[Enum(One,1,SrcAlpha,5)]
如图所示:
注意:枚举中的值不能随便定义,一定要跟融合的模型一一对应上,具体可以参看BlendMode中的定义:
Zero = 0,
One = 1,
DstColor = 2,
SrcColor = 3,
OneMinusDstColor = 4,
SrcAlpha = 5,
OneMinusSrcColor = 6,
DstAlpha = 7,
OneMinusDstAlpha = 8,
SrcAlphaSaturate = 9,
OneMinusSrcAlpha = 10
#2、在来看看在其他属性设置上的使用
因为在其他属性上的设置都类似于BlendMode的设置,这里就不单独举例某一个,就直接列出所有的吧,然后自己在里面查找自己需要的属性,完整版源码如下:
Shader "MaterialPropertyDrawer/MaterialPropertyDrawerEnum" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull Mode", Float) = 1
[Enum(Off,0,On,1)] _ZWrite ("ZWrite", Float) = 1
[Enum(Less,0, Greater,1, LEqual,2, GEqual,3, Equal,4, NotEqual,5, Always,6)] _ZTest ("ZTest", Float) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _SourceBlend ("Source Blend Mode", Float) = 2
[Enum(UnityEngine.Rendering.BlendMode)] _DestBlend ("Dest Blend Mode", Float) = 2
_CutOff ("Cut Off", float) = 0.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Cull [_Cull]
ZWrite [_ZWrite]
ZTest [_ZTest]
Fog{
Mode Global
}
AlphaTest Less [_CutOff]
Blend [_SourceBlend] [_DestBlend]
CGPROGRAM
#pragma surface surf Lambert
fixed4 _Color;
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
暂时就先列举这么多吧,剩下没有列举的可以根据规则自己扩展。