Advertisement
lindseyreid

Dissolve Shader - Unity CG

Dec 16th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/CelEffectsDissolve"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex("Texture", 2D) = "white" {}
  6.         _RampTex("Ramp", 2D) = "white" {}
  7.         _Color("Color", Color) = (1, 1, 1, 1)
  8.         _NoiseTex("Noise Texture", 2D) = "white" {}
  9.         _DissolveSpeed("Dissolve Speed", float) = 1.0
  10.         _DissolveColor1("Dissolve Color 1", Color) = (1, 1, 1, 1)
  11.         _DissolveColor2("Dissolve Color 2", Color) = (1, 1, 1, 1)
  12.         _ColorThreshold1("Color Threshold 1", float) = 1.0
  13.         _ColorThreshold2("Color Threshold 2", float) = 1.0
  14.         _StartTime("Start Time", float) = 1.0
  15.         _FadeSpeed("Fade Speed", float) = 1.0
  16.     }
  17.  
  18.     SubShader
  19.     {
  20.         Tags
  21.         {
  22.             "Queue" = "Transparent"
  23.         }
  24.  
  25.         // Regular color & lighting pass
  26.         Pass
  27.         {
  28.             Blend SrcAlpha OneMinusSrcAlpha
  29.  
  30.             CGPROGRAM
  31.             #pragma vertex vert
  32.             #pragma fragment frag
  33.            
  34.             // Properties
  35.             sampler2D _MainTex;
  36.             sampler2D _RampTex;
  37.             float4 _Color;
  38.             float4 _LightColor0; // provided by Unity
  39.             float4 _DissolveColor1;
  40.             float4 _DissolveColor2;
  41.             sampler2D _NoiseTex;
  42.             float _DissolveSpeed;
  43.             float _ColorThreshold1;
  44.             float _ColorThreshold2;
  45.             float _StartTime;
  46.             float _FadeSpeed;
  47.  
  48.             struct vertexInput
  49.             {
  50.                 float4 vertex : POSITION;
  51.                 float3 normal : NORMAL;
  52.                 float3 texCoord : TEXCOORD0;
  53.             };
  54.  
  55.             struct vertexOutput
  56.             {
  57.                 float4 pos : SV_POSITION;
  58.                 float3 normal : NORMAL;
  59.                 float3 texCoord : TEXCOORD0;
  60.             };
  61.  
  62.             vertexOutput vert(vertexInput input)
  63.             {
  64.                 vertexOutput output;
  65.  
  66.                 // convert input to world space
  67.                 output.pos = UnityObjectToClipPos(input.vertex);
  68.                 float4 normal4 = float4(input.normal, 0.0); // need float4 to mult with 4x4 matrix
  69.                 output.normal = normalize(mul(normal4, unity_WorldToObject).xyz);
  70.  
  71.                 // texture coordinates
  72.                 output.texCoord = input.texCoord;
  73.  
  74.                 return output;
  75.             }
  76.  
  77.             float4 frag(vertexOutput input) : COLOR
  78.             {
  79.                 // convert light direction to world space & normalize
  80.                 // _WorldSpaceLightPos0 provided by Unity
  81.                 float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
  82.  
  83.                 // finds location on ramp texture that we should sample
  84.                 // based on angle between surface normal and light direction
  85.                 float ramp = clamp(dot(input.normal, lightDir), 0.001, 1.0);
  86.                 float3 lighting = tex2D(_RampTex, float2(ramp, 0.5)).rgb;
  87.  
  88.                 // sample texture for color
  89.                 float4 albedo = tex2D(_MainTex, input.texCoord.xy);
  90.  
  91.                 // base color
  92.                 float4 color = float4(albedo.rgb * _LightColor0.rgb * lighting * _Color.rgb, 1.0);
  93.  
  94.                 // sample noise texture
  95.                 float noiseSample = tex2Dlod(_NoiseTex, float4(input.texCoord.xy, 0, 0));
  96.  
  97.                 // dissolve colors
  98.                 float thresh2 = _Time * _ColorThreshold2 - _StartTime;
  99.                 float useDissolve2 = noiseSample - thresh2 < 0;
  100.                 color = (1-useDissolve2)*color + useDissolve2*_DissolveColor2;
  101.  
  102.                 float thresh1 = _Time * _ColorThreshold1 - _StartTime;
  103.                 float useDissolve1 = noiseSample - thresh1 < 0;
  104.                 color = (1-useDissolve1)*color + useDissolve1*_DissolveColor1;
  105.  
  106.                 float threshold = _Time * _DissolveSpeed - _StartTime;
  107.                 clip(noiseSample - threshold);
  108.  
  109.                 color.a -= saturate(_Time * _FadeSpeed);
  110.  
  111.                 return color;
  112.             }
  113.  
  114.             ENDCG
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement