Shader "Custom/Luminance" { Properties { _MainTex ("Texture", 2D) = "white" {} //材质上加个复选框 [Toggle(UNITY_UI_LUMINANCE)] _Luminance ("Luminance", Float) = 0 //支持 UGUI ScrollRect 遮罩 _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 _ColorMask ("Color Mask", Float) = 15 //END } SubShader { Tags { "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True" "PreviewType" = "Plane" } LOD 100 //支持 UGUI ScrollRect 遮罩 Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } ColorMask [_ColorMask] //END //Alpha混合 Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma target 3.0 //DX9 着色器模型 3.0 #pragma vertex vert #pragma fragment frag //生成多版本shader #pragma multi_compile __ UNITY_UI_LUMINANCE #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; float _Luminance; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); #ifdef UNITY_UI_LUMINANCE col.rgb = Luminance(col.rgb); #endif return col; } ENDCG } } }