轉(zhuǎn)載:http://www.seven-fire.cn/archives/161
最近發(fā)現(xiàn)Unity的一個黑科技(其實很久以前就發(fā)現(xiàn)了剩彬,只是方便開頭而已抖锥,哈哈)登舞;可以自定義材質(zhì)的檢視面板乎婿,通過這個功能還可以把功能差不多的著色器合并成一個。
例如有的著色器只是融合方式(Blend)票唆、深度測試開關(guān)(ZTest)朴读、渲染隊列(RenderQueue)等等不一樣,而基本的核心著色代碼是一樣的走趋。這就是本文即將介紹的枚舉類型(其他類型以后介紹)
本文的研究來自Unity的官網(wǎng)文檔MaterialPropertyDrawer
1衅金、首先看看BlendMode上的應(yīng)用
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模式就不用我解釋了吧簿煌。
當(dāng)然氮唯,某些情況下,我們并不希望列舉出所有的混合模式姨伟,而只給少數(shù)的幾個給美術(shù)人員使用惩琉。這一也是可以實現(xiàn)的,只需要我們自己來定義枚舉的數(shù)值和顯示就可以了夺荒。
源碼如下所示:
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)]
如圖所示:
注意:枚舉中的值不能隨便定義瞒渠,一定要跟融合的模型一一對應(yīng)上,具體可以參看BlendMode中的定義:
Zero = 0,
One = 1,
DstColor = 2,
SrcColor = 3,
OneMinusDstColor = 4,
SrcAlpha = 5,
OneMinusSrcColor = 6,
DstAlpha = 7,
OneMinusDstAlpha = 8,
SrcAlphaSaturate = 9,
OneMinusSrcAlpha = 10
2技扼、在來看看在其他屬性設(shè)置上的使用
因為在其他屬性上的設(shè)置都類似于BlendMode的設(shè)置伍玖,這里就不單獨舉例某一個,就直接列出所有的吧剿吻,然后自己在里面查找自己需要的屬性窍箍,完整版源碼如下:
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"
}
暫時就先列舉這么多吧,剩下沒有列舉的可以根據(jù)規(guī)則自己擴展丽旅。