Advertisement
Guest User

ToonVerticalDissolve

a guest
Mar 13th, 2020
1,917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Toon/VerticalDissolve" {
  3.     Properties{
  4.         [Header(Base)]
  5.         _Color("Color", Color) = (1,1,1,1)
  6.         _MainTex("Albedo (RGB)", 2D) = "white" {}
  7.         _OffsetT("ToonRamp Offset", Range(0,1)) = 0.5
  8.        
  9.  
  10.         [Space]
  11.         [Header(Noise)]
  12.         _Noise("Noise Texture (RGB)", 2D) = "white" {}
  13.         _Scale("Noise Texture Scale", Range(0,5)) = 1
  14.         [Toggle(LOCAL)] _LOCAL("Local Texture?", Float) = 0
  15.  
  16.         [Space]
  17.         [Header(Dissolve)]
  18.         _DisAmount("Dissolve Amount", Range(-2,2)) = 0.0
  19.         [Toggle(INVERT)] _INVERT("Inverse Direction?", Float) = 1
  20.         _DisColor("Dissolve Color", Color) = (1,1,0,1)
  21.         _DissolveColorWidth("Dissolve Color Width", Range(0,0.1)) = 0.01
  22.         _Brightness("Dissolve Color Brightness", Range(0,20)) = 10
  23.         _Cutoff("Noise Cutoff", Range(0,1)) = 0.5
  24.         _Smoothness("Cutoff Smoothness", Range(0,2)) = 0.05
  25.  
  26.         [Space]
  27.         [Header(Vertex Displacement)]
  28.         _ScaleV("Displacement Scale", Range(0,1)) = 0.1
  29.         _Offset("Displacement Y Offset", Range(-1,1)) = 0.7
  30.         _DisplacementWidth("Displacement Segment Width", Range(0,1)) = 0.3
  31.         _HeightScale("Height Displacement Amount", Range(0,0.2)) = 0.0
  32.     }
  33.  
  34.         SubShader{
  35.             Tags{ "RenderType" = "Opaque" }
  36.         LOD 200
  37.         Cull Off
  38.  
  39.         CGPROGRAM
  40.  
  41.         #pragma shader_feature INVERT
  42.         #pragma shader_feature LOCAL
  43.         #pragma surface surf ToonRamp vertex:vert fullforwardshadows addshadow keepalpha// alphatest:_Cutoff
  44. #pragma target 3.5
  45.             float _OffsetT;
  46.             // custom lighting function that uses a texture ramp based
  47.             // on angle between light direction and normal
  48.         #pragma lighting ToonRamp //exclude_path:prepass
  49.             inline half4 LightingToonRamp(SurfaceOutput s, half3 lightDir, half atten)
  50.             {
  51.         #ifndef USING_DIRECTIONAL_LIGHT
  52.                 lightDir = normalize(lightDir);
  53.         #endif
  54.                   float d = dot(s.Normal, lightDir);
  55.                 float3 lightIntensity = smoothstep(0 , fwidth(d) + _OffsetT, d);
  56.  
  57.                 half4 c;
  58.                 c.rgb = s.Albedo * _LightColor0.rgb * lightIntensity * (atten * 2);
  59.                 c.a = s.Alpha;
  60.                 return c;
  61.             }
  62.  
  63.             float _DisAmount, _Scale, _ScaleV;
  64.             float _HeightScale;
  65.             float _DisplacementWidth;
  66.             float _Offset;
  67.  
  68.             struct Input
  69.             {
  70.                 float2 uv_MainTex;
  71.                 float4 pos : POSITION;
  72.                 float3 worldNormal;
  73.                 float3 worldPos;
  74.                 float3 local;
  75.             };
  76.  
  77.             void vert(inout appdata_full v, out Input o)
  78.             {
  79.                 UNITY_INITIALIZE_OUTPUT(Input, o);
  80.                 // vertex position
  81.                 o.pos = mul(unity_ObjectToWorld, v.vertex.xyz);
  82.  
  83.                 // local position that also takes rotation into account
  84.                 float3 rotatedLocal = mul((float3x3)unity_WorldToObject, o.pos);
  85.                 o.local = rotatedLocal;
  86.  
  87.                 // position on model
  88.                 float dispPos = (o.pos.y + _DisAmount + _Offset);
  89.                 // clamped segment of model
  90.                 float dispPosClamped = smoothstep(0, 0.15, dispPos) * smoothstep(dispPos, dispPos + 0.15, _DisplacementWidth);
  91. #if INVERT
  92.                 // position on model
  93.                 dispPos = 1 - (o.pos.y + _DisAmount + _Offset);
  94.                 // clamped segment of model
  95.                 dispPosClamped = smoothstep(0, 0.15, dispPos) * smoothstep(dispPos, dispPos + 0.15, _DisplacementWidth);
  96.  
  97.                 //distort the mesh up
  98.                 v.vertex.y += (dispPosClamped *_HeightScale);
  99. #else
  100.                 //or down
  101.                 v.vertex.y -= (dispPosClamped *_HeightScale);
  102. #endif
  103.  
  104.                 //distort the mesh sideways
  105.                 v.vertex.xz += dispPosClamped * (_ScaleV* (v.normal.xz));
  106.  
  107.                 // do this again to account for displacement
  108.                 o.pos = mul(unity_ObjectToWorld, v.vertex.xyz);
  109.  
  110.             }
  111.  
  112.             sampler2D _MainTex, _Noise;
  113.             half _Glossiness;
  114.             half _Metallic;
  115.             float _DissolveColorWidth, _Brightness, _Cutoff, _Smoothness;
  116.             fixed4 _Color, _DisColor;
  117.  
  118.             // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
  119.             // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
  120.             // #pragma instancing_options assumeuniformscaling
  121.             UNITY_INSTANCING_BUFFER_START(Props)
  122.                 //UNITY_DEFINE_INSTANCED_PROP(float, _DisAmount) // uncomment this to use it per-instance
  123.                 // put more per-instance properties here
  124.                 UNITY_INSTANCING_BUFFER_END(Props)
  125.  
  126.                 void surf(Input IN, inout SurfaceOutput o)
  127.             {
  128.                 // Main texture tinted by color
  129.                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  130.  
  131.                 float3 blendNormal = saturate(pow(IN.worldNormal * 1.4, 4));
  132.  
  133.                 float3 adjustedworldpos = IN.worldPos;
  134. #if LOCAL
  135.                 adjustedworldpos = IN.local;
  136. #endif
  137.  
  138. #if INVERT
  139.                 adjustedworldpos.y -= _Time.x;
  140. #else
  141.                 adjustedworldpos.y += _Time.x;
  142. #endif
  143.                 // normal noise triplanar for x, y, z sides
  144.                 float3 xn = tex2D(_Noise, adjustedworldpos.zy * _Scale);
  145.                 float3 yn = tex2D(_Noise, adjustedworldpos.zx * _Scale);
  146.                 float3 zn = tex2D(_Noise, adjustedworldpos.xy * _Scale);
  147.  
  148.                 // lerped together all sides for noise texture
  149.                 float3 noisetexture = zn;
  150.                 noisetexture = lerp(noisetexture, xn, blendNormal.x);
  151.                 noisetexture = lerp(noisetexture, yn, blendNormal.y);
  152.  
  153.                 float noise = noisetexture.r;
  154.  
  155.                 // position on model
  156.                 float MovingPosOnModel = _DisAmount + IN.pos.y;
  157.                 // add noise
  158.                 MovingPosOnModel *= noise;
  159.  
  160.                 // glowing bit that's a bit longer
  161.                 float maintexturePart = smoothstep(0, _Smoothness, MovingPosOnModel - _DissolveColorWidth);
  162.                 maintexturePart = step(_Cutoff, maintexturePart);
  163.  
  164.                 // normal texture
  165.                 float glowingPart = smoothstep(0, _Smoothness, MovingPosOnModel);
  166.                 glowingPart = step(_Cutoff, glowingPart);
  167.                 // take out the normal texture part
  168.                 glowingPart *= (1 - maintexturePart);
  169.  
  170. #if INVERT
  171.  
  172.                 // glowing bit that's a bit longer
  173.                 maintexturePart = 1 - smoothstep(0, _Smoothness, MovingPosOnModel + _DissolveColorWidth);
  174.                 maintexturePart = step(_Cutoff, maintexturePart);
  175.  
  176.                 // normal texture
  177.                 glowingPart = 1 - smoothstep(0, _Smoothness, MovingPosOnModel);
  178.                 glowingPart = step(_Cutoff, glowingPart);
  179.                 // take out the normal texture part
  180.                 glowingPart *= (1 - maintexturePart);
  181. #endif
  182.  
  183.                 // Colorized Dissolve
  184.                 float4 glowingColored = glowingPart * _DisColor;
  185.  
  186.                 // discard pixels beyond dissolving
  187.                 clip((maintexturePart + glowingPart) - 0.01);
  188.  
  189.                 // main texture cutoff by dissolve
  190.                 float3 mainTexture = maintexturePart * c.rgb;
  191.  
  192.                 // set main texture
  193.                 o.Albedo = mainTexture;
  194.  
  195.                 // glowing dissolve
  196.                 o.Emission = (glowingColored * noisetexture) * _Brightness;
  197.  
  198.                 // base settings
  199.                 o.Alpha = c.a;
  200.             }
  201.             ENDCG
  202.  
  203.         }
  204.  
  205.             Fallback "Diffuse"
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement