Advertisement
antonkudin

Untitled

Oct 14th, 2017
4,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. Shader "Unlit/maskedSprite"
  2. {
  3.      Properties
  4.      {
  5.         [Toggle]_ShowMask ("Preview mask", Float) = 0
  6.         [Space]
  7.         [Toggle]_UseAlpha ("Vertex Alpha > Threshold", Float) = 0
  8.         [Space]
  9.         _MaskThrs ("Mask Threshold", Range(0,1)) = .5
  10.         _MaskSoft ("Mask Width", Range (0.001, 3)) = 1
  11.         _MaskRot ("Mask rotation", Range (0, 1)) = 0
  12.         [Space]
  13.         [Toggle]_AutoThresh ("AutoCycle Threshold", Float) = 0
  14.         _AutoSpeed ("Auto Speed", Range(0,120)) = 5
  15.         [Space(20)]
  16.         [Header(Calibration)]
  17.         _MaskOffs ("Offset", Float) = 0
  18.         _MaskScal ("Scale", Float) = 1
  19.         [Space(20)]
  20.         [HDR]_Color ("Color", Color) = (1,1,1,1)  
  21.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white"
  22.         _MaskTxtr ("Mask Gradient", 2D) = "white"
  23.     }
  24.  
  25.     Category
  26.     {
  27.          Lighting Off
  28.          ZWrite Off
  29.          //ZWrite On  // uncomment if you have problems like the sprite disappear in some rotations.
  30.          Cull off
  31.          Blend SrcAlpha OneMinusSrcAlpha
  32.         //  AlphaTest Greater 0.001  // uncomment if you have problems like the sprites or 3d text have white quads instead of alpha pixels.
  33.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"
  34.         "DisableBatching"="True" // this disables batching (fixes issue when mask can be applied to multiple objects)
  35.         }
  36.          SubShader
  37.          {
  38.  
  39.               Pass
  40.               {
  41.                     CGPROGRAM
  42.                     #pragma vertex vert
  43.                     #pragma fragment frag
  44.                     #include "UnityCG.cginc"
  45.  
  46.                     half4 _Color;
  47.                    
  48.                     sampler2D _MainTex;
  49.                     float4 _MainTex_ST;
  50.                     sampler2D _MaskTxtr;
  51.                     float4 _MaskTxtr_ST;
  52.                     half _ShowMask, _AutoThresh, _UseAlpha;
  53.                     float _MaskThrs, _MaskAngl, _MaskSoft, _MaskRot, _MaskOffs, _MaskScal, _AutoSpeed;
  54.  
  55.                     struct appdata_t {
  56.                         float4 vertex : POSITION;
  57.                         fixed4 color : COLOR;
  58.                         float2 uv : TEXCOORD0;
  59.                            
  60.                     };
  61.  
  62.                     struct v2f {
  63.                         float4 vertex : SV_POSITION;
  64.                         fixed4 color : COLOR;
  65.                         float2 uv : TEXCOORD0;
  66.                         float2 mask : TEXCOORD1;
  67.                     };
  68.                    
  69.                     v2f vert (appdata_t v)
  70.                     {
  71.                         v2f o;
  72.                         o.vertex = UnityObjectToClipPos(v.vertex);
  73.  
  74.                         o.color = lerp(v.color, half4(v.color.rgb, 1), _UseAlpha);
  75.                         o.uv = v.uv;
  76.                         o.mask.x = (lerp(v.vertex.x,v.vertex.y,_MaskRot) + _MaskOffs) * _MaskScal;
  77.                         float thsh = lerp( lerp(_MaskThrs, v.color.a, _UseAlpha), frac(_Time.x*_AutoSpeed), _AutoThresh);
  78.                         o.mask.y = (thsh-.5) * (1 + _MaskSoft*2) + (thsh-.5);
  79.                         return o;
  80.                     }
  81.  
  82.                     fixed4 frag (v2f i) : SV_Target
  83.                     {
  84.                        
  85.                         float maskX = smoothstep( i.mask.y-_MaskSoft, i.mask.y+_MaskSoft, i.mask.x);
  86.                         float mask = tex2D(_MaskTxtr, half2(maskX, _MaskTxtr_ST.z));
  87.                         fixed4 smpl = tex2D(_MainTex, i.uv);
  88.                         smpl.a *= mask;
  89.                        
  90.                         smpl = lerp(smpl, fixed4(mask,mask,mask,1), _ShowMask);
  91.                         clip(smpl.a-.001);
  92.                        
  93.                         fixed4 color = i.color * _Color;
  94.                         return smpl * color;
  95.                     }
  96.                     ENDCG
  97.                  }
  98.              }
  99.          }
  100.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement