Advertisement
Guest User

StandardVerticalDissolve

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