Advertisement
Guest User

WaterfallInteractiveAdv.shader

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