Advertisement
Guest User

UI_Image_TextureMask.shader

a guest
Jul 7th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "UI/Image - Texture Masked"
  2. {
  3.     Properties
  4.     {
  5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6.         _MaskTex ("Mask Texture", 2D) = "white" {}
  7.         [Toggle] _UseMaskLum("Use Mask Luminance", Float) = 0
  8.         _Color ("Tint", Color) = (1,1,1,1)
  9.        
  10.         _StencilComp ("Stencil Comparison", Float) = 8
  11.         _Stencil ("Stencil ID", Float) = 0
  12.         _StencilOp ("Stencil Operation", Float) = 0
  13.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
  14.         _StencilReadMask ("Stencil Read Mask", Float) = 255
  15.  
  16.         _ColorMask ("Color Mask", Float) = 15
  17.  
  18.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  19.     }
  20.  
  21.     SubShader
  22.     {
  23.         Tags
  24.         {
  25.             "Queue"="Transparent"
  26.             "IgnoreProjector"="True"
  27.             "RenderType"="Transparent"
  28.             "PreviewType"="Plane"
  29.             "CanUseSpriteAtlas"="True"
  30.         }
  31.        
  32.         Stencil
  33.         {
  34.             Ref [_Stencil]
  35.             Comp [_StencilComp]
  36.             Pass [_StencilOp]
  37.             ReadMask [_StencilReadMask]
  38.             WriteMask [_StencilWriteMask]
  39.         }
  40.  
  41.         Cull Off
  42.         Lighting Off
  43.         ZWrite Off
  44.         ZTest [unity_GUIZTestMode]
  45.         Blend SrcAlpha OneMinusSrcAlpha
  46.         ColorMask [_ColorMask]
  47.  
  48.         Pass
  49.         {
  50.         CGPROGRAM
  51.             #pragma vertex vert
  52.             #pragma fragment frag
  53.             #pragma target 2.0
  54.  
  55.             #include "UnityCG.cginc"
  56.             #include "UnityUI.cginc"
  57.  
  58.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
  59.            
  60.             struct appdata_t
  61.             {
  62.                 float4 vertex   : POSITION;
  63.                 float4 color    : COLOR;
  64.                 float2 texcoord : TEXCOORD0;
  65.             };
  66.  
  67.             struct v2f
  68.             {
  69.                 float4 vertex   : SV_POSITION;
  70.                 fixed4 color    : COLOR;
  71.                 half2 texcoord  : TEXCOORD0;
  72.                 float4 worldPosition : TEXCOORD1;
  73.             };
  74.            
  75.             fixed4 _Color;
  76.             fixed4 _TextureSampleAdd;
  77.             float4 _ClipRect;
  78.  
  79.             v2f vert(appdata_t IN)
  80.             {
  81.                 v2f OUT;
  82.                 OUT.worldPosition = IN.vertex;
  83.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  84.  
  85.                 OUT.texcoord = IN.texcoord;
  86.                
  87.                 #ifdef UNITY_HALF_TEXEL_OFFSET
  88.                 OUT.vertex.xy += (_ScreenParams.zw-1.0) * float2(-1,1) * OUT.vertex.w;
  89.                 #endif
  90.                
  91.                 OUT.color = IN.color * _Color;
  92.                 return OUT;
  93.             }
  94.  
  95.             sampler2D _MainTex, _MaskTex;
  96.             float _UseMaskLum;
  97.  
  98.             half4 frag(v2f IN) : SV_Target
  99.             {
  100.                 half4 masktex = tex2D(_MaskTex , IN.texcoord);
  101.  
  102.                 float mask = 1.0;
  103.                 if (_UseMaskLum > 0.5 ) mask = Luminance(masktex.rgb);
  104.                 if (_UseMaskLum < 0.5) mask = masktex.a;
  105.  
  106.                 if (mask < 0.01) discard;
  107.  
  108.                 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  109.                
  110.                 color.a *= mask;
  111.                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  112.                
  113.                 #ifdef UNITY_UI_ALPHACLIP
  114.                 clip (color.a - 0.001);
  115.                 #endif
  116.  
  117.                 return color;
  118.             }
  119.         ENDCG
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement