Advertisement
Guest User

WaterfallInteractive.shader

a guest
Aug 19th, 2020
6,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "FX/Waterfall Interactive" {
  2.     Properties {
  3.         [Space]
  4.         [Header(Water)]
  5.         _TColor ("Deep Tint", Color) = (0,1,1,1)
  6.         _WaterColor ("Edge Tint", Color) = (0,0.6,1,1)
  7.         _DepthOffset("Depth Offset", Range(-10,10)) = 0
  8.         _Stretch("Depth Stretch", Range(0,5)) = 2
  9.         _Brightness ("Water Brightness", Range(0.5,2)) = 1.2
  10.         [Space]
  11.         [Header(Surface Noise and Movement)]
  12.         _SideNoiseTex ("Side Water Texture", 2D) = "white" {}
  13.         _TopNoiseTex ("Top Water Texture", 2D) = "white" {}
  14.         _HorSpeed ("Horizontal Flow Speed", Range(0,4)) = 0.14
  15.         _VertSpeed("Vertical Flow Speed", Range(0,60)) = 6.8
  16.         _TopScale ("Top Noise Scale", Range(0,1)) = 0.4
  17.         _NoiseScale ("Side Noise Scale", Range(0,1)) = 0.04
  18.         [Toggle(VERTEX)] _VERTEX("Use Vertex Colors", Float) = 0
  19.      
  20.         [Space]
  21.         [Header(Foam)]
  22.         _FoamColor ("Foam Tint", Color) = (1,1,1,1)
  23.         _Foam ("Edgefoam Width", Range(1,50)) = 2.35
  24.         _TopSpread("Foam Position", Range(-1,6)) = 0.05
  25.         _Softness ("Foam Softness", Range(0,0.5)) = 0.1
  26.         _EdgeWidth("Foam Width", Range(0,2)) = 0.4
  27.  
  28.         [Space]
  29.         [Header(Rim Light)]
  30.         _RimPower("Rim Power", Range(1,20)) = 18
  31.         _RimColor("Rim Color", Color) = (0,0.5,0.25,1)
  32.  
  33.         [Space]
  34.         [Header(Vertex Movement)]
  35.         _Amount("Wave Amount", Range(0,10)) = 0.6
  36.         _SpeedV("Speed", Range(0,10)) = 0.5
  37.         _Height("Wave Height", Range(0,1)) = 0.1
  38.  
  39.          [Space]
  40.         [Header(Reflections)]
  41.         _ReflectionTex("Refl Texture", 2D) = "black" {}
  42.          _Reflectivity("Reflectivity", Range(0,1)) = 0.6
  43.        
  44.     }
  45.     SubShader {
  46.         Tags{ "Queue" = "Transparent"}
  47.         LOD 200
  48.         Blend SrcAlpha OneMinusSrcAlpha
  49.         CGPROGRAM
  50.         // Physically based Standard lighting model, and enable shadows on all light types
  51.         #pragma surface surf Standard vertex:vert fullforwardshadows keepalpha
  52.  
  53.         // Use shader model 3.0 target, to get nicer looking lighting
  54.         #pragma target 3.0    
  55.         #pragma shader_feature VERTEX
  56.        
  57.    
  58.         sampler2D _SideNoiseTex, _TopNoiseTex;
  59.    
  60.         uniform sampler2D _CameraDepthTexture; //Depth Texture
  61.  
  62.         struct Input {
  63.             float3 worldNormal; INTERNAL_DATA// world normal built-in value
  64.             float3 worldPos; // world position built-in value
  65.             float3 viewDir;// view direction for rim
  66.             float4 color : COLOR; // vertex colors
  67.             float4 screenPos; // screen position for edgefoam
  68.              float eyeDepth;// depth for edgefoam
  69.         };
  70.  
  71.         float _SpeedV, _Amount, _Height;
  72.         fixed4 _FoamColor, _WaterColor, _RimColor, _TColor;
  73.         fixed _HorSpeed, _TopScale, _TopSpread, _EdgeWidth, _RimPower, _NoiseScale, _VertSpeed;
  74.         float _Brightness, _Foam, _Softness;
  75.         float _DepthOffset, _Stretch;
  76.         sampler2D _ReflectionTex;
  77.         float _Reflectivity;
  78.  
  79.          void vert (inout appdata_full v, out Input o)
  80.         {
  81.             UNITY_INITIALIZE_OUTPUT(Input, o);
  82.             COMPUTE_EYEDEPTH(o.eyeDepth);
  83.             float3 worldNormal = mul(unity_ObjectToWorld, v.normal);
  84.             float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  85.             half3 tex = tex2Dlod(_SideNoiseTex, float4(worldPos.xz * _TopScale * 1, 1,1));
  86.             float3 movement = sin(_Time.z * _SpeedV + (v.vertex.x * v.vertex.z * _Amount * tex)) * _Height * (1 - worldNormal.y);
  87.        
  88.             v.vertex.xyz += movement;
  89.            
  90.         }
  91.  
  92.  
  93.         uniform float3 _Position;
  94.         uniform sampler2D _GlobalEffectRT;
  95.         uniform float _OrthographicCamSize;
  96.  
  97.         void surf(Input IN, inout SurfaceOutputStandard  o) {
  98.  
  99.          // get the world normal
  100.         float3 worldNormal = WorldNormalVector(IN, o.Normal);
  101.          // grab the vertex colors from the model
  102.         float3 vertexColors = IN.color.rgb;
  103.         // normal for triplanar mapping
  104.         float3 blendNormal = saturate(pow(worldNormal * 1.4,4));
  105.            
  106.        
  107. #if VERTEX // use vertex colors for flow
  108.         float3 flowDir= (vertexColors * 2.0f) - 1.0f;
  109. #else // or world normal
  110.         float3 flowDir= -(worldNormal * 2.0f) - 1.0f;
  111. #endif
  112.         // horizontal flow speed
  113.         flowDir *= _HorSpeed;
  114.  
  115.         // flowmap blend timings
  116.         float timing = frac(_Time[1] * 0.5 + 0.5);
  117.         float timing2 = frac(_Time[1] * 0.5);
  118.         float timingLerp = abs((0.5 - timing) / 0.5);
  119.  
  120.         // move 2 textures at slight different speeds fased on the flowdirection
  121.         half3 topTex1 = tex2D(_TopNoiseTex, (IN.worldPos.xz * _TopScale) + flowDir.xz * timing);
  122.         half3 topTex2 = tex2D(_TopNoiseTex, (IN.worldPos.xz * _TopScale) + flowDir.xz * timing2);
  123.    
  124.         // vertical flow speed
  125.         float vertFlow = _Time.y * _VertSpeed;
  126.  
  127.  
  128.         // rendertexture UV
  129.         float2 uv = IN.worldPos.xz - _Position.xz;
  130.         uv = uv / (_OrthographicCamSize * 2);
  131.         uv += 0.5;
  132.         // Ripples
  133.         float ripples = tex2D(_GlobalEffectRT, uv).b;
  134.  
  135.         // noise sides
  136.         float3 TopFoamNoise = lerp(topTex1, topTex2, timingLerp) + ripples;
  137.  
  138.    
  139.  
  140.         float3 SideFoamNoiseZ = tex2D(_SideNoiseTex, float2(IN.worldPos.z* 10, IN.worldPos.y + vertFlow) * _NoiseScale );
  141.         float3 SideFoamNoiseX = tex2D(_SideNoiseTex, float2(IN.worldPos.x* 10, IN.worldPos.y + vertFlow)  * _NoiseScale);
  142.  
  143.         float3 SideFoamNoiseZE = tex2D(_TopNoiseTex, float2(IN.worldPos.z * 10, IN.worldPos.y + vertFlow) * _NoiseScale/3);
  144.         float3 SideFoamNoiseXE = tex2D(_TopNoiseTex, float2(IN.worldPos.x * 10, IN.worldPos.y + vertFlow)  * _NoiseScale/3);
  145.  
  146.         // lerped together all sides for noise texture
  147.         float3 noisetexture = (SideFoamNoiseX + SideFoamNoiseXE) /2;
  148.         noisetexture = lerp(noisetexture, (SideFoamNoiseZ +SideFoamNoiseZE) / 2, blendNormal.x);
  149.         noisetexture = lerp(noisetexture, TopFoamNoise, blendNormal.y);
  150.  
  151.         // Normalbased Foam
  152.         float worldNormalDotNoise = dot(o.Normal, worldNormal.y + 0.3);
  153.         worldNormalDotNoise *= noisetexture;
  154.         // add noise to normal
  155.         o.Normal = BlendNormals(o.Normal, noisetexture * 2);
  156.         o.Normal = BlendNormals(o.Normal, ripples * 2);
  157.  
  158.  
  159.         // edge foam calculation
  160.         half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture ,IN.screenPos)  ); // depth
  161.         float foamLineS =1 - saturate(_Foam * float4(noisetexture,1) * (depth - IN.screenPos.w));// foam line by comparing depth and screenposition
  162.         float4 foamLine = smoothstep(0.5, 0.8, foamLineS);
  163.         // rimline
  164.  
  165.         int rim = 1.0 - saturate(dot(normalize(IN.viewDir) , o.Normal));
  166.  
  167.         float3 colorRim = _RimColor.rgb * pow (rim, _RimPower);
  168.        
  169.         float foamS = smoothstep(worldNormalDotNoise, worldNormalDotNoise + _Softness, _TopSpread + _EdgeWidth) * saturate(1-worldNormal.y );
  170.  
  171.         foamS *= 4;
  172.  
  173.         // combine depth foam and foam + add color
  174.         float3 combinedFoam =  (foamS + foamLine.rgb + ripples) * _FoamColor;
  175.        
  176.         // colors lerped over blendnormal
  177.         float4 color = lerp(_WaterColor, _TColor, saturate((depth - IN.screenPos.w + _DepthOffset * noisetexture.r) * _Stretch) ) * _Brightness;
  178.         o.Albedo = color;
  179.  
  180.         o.Smoothness =  smoothstep(0, 0.5, o.Normal);
  181.         o.Metallic = 0.5;
  182.         // glowing combined foam and colored rim
  183.  
  184.         half4 rtReflections = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(IN.screenPos + worldNormalDotNoise));
  185.         rtReflections *= dot(o.Normal, worldNormal.y);
  186.         o.Albedo += combinedFoam + colorRim + rtReflections;
  187.            
  188.         // clamped alpha
  189.         o.Alpha = saturate(color.a + (rtReflections * _Reflectivity) + combinedFoam + foamLine.a + ripples);
  190.        
  191.         }
  192.         ENDCG
  193.     }
  194.     FallBack "Diffuse"
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement