Advertisement
Guest User

LitWaterfall.shader

a guest
May 9th, 2018
9,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. Shader "FX/Waterfall" {
  2. Properties {
  3. [Space]
  4. [Header(Water)]
  5. _TColor ("Top Water Tint", Color) = (0,1,1,1)
  6. _WaterColor ("Side Water Tint", Color) = (0,0.6,1,1)
  7. _BrightNess ("Water Brightness", Range(0.5,2)) = 1.2
  8. [Space]
  9. [Header(Surface Noise and Movement)]
  10. _SideNoiseTex ("Side Water Texture", 2D) = "white" {}
  11. _TopNoiseTex ("Top Water Texture", 2D) = "white" {}
  12. _HorSpeed ("Horizontal Flow Speed", Range(-4,4)) = 0.14
  13. _VertSpeed("Vertical Flow Speed", Range(0,10)) = 6.8
  14. _TopScale ("Top Noise Scale", Range(0,1)) = 0.4
  15. _NoiseScale ("Side Noise Scale", Range(0,1)) = 0.04
  16. [Toggle(VERTEX)] _VERTEX("Use Vertex Colors", Float) = 0
  17.  
  18. [Space]
  19. [Header(Foam)]
  20. _FoamColor ("Foam Tint", Color) = (1,1,1,1)
  21. _Foam ("Edgefoam Width", Range(1,10)) = 2.35
  22. _TopSpread("Foam Position", Range(0,6)) = 0.05
  23. _Softness ("Foam Softness", Range(0,0.5)) = 0.1
  24. _EdgeWidth("Foam Width", Range(0,2)) = 0.4
  25.  
  26. [Space]
  27. [Header(Rim Light)]
  28. [Toggle(RIM)] _RIM("Hard Rim", Float) = 0
  29. _RimPower("Rim Power", Range(1,20)) = 18
  30. _RimColor("Rim Color", Color) = (0,0.5,0.25,1)
  31.  
  32. }
  33. SubShader {
  34. Tags{ "Queue" = "Transparent"}
  35. LOD 200
  36. Blend SrcAlpha OneMinusSrcAlpha
  37. CGPROGRAM
  38. // Physically based Standard lighting model, and enable shadows on all light types
  39. #pragma surface surf Standard vertex:vert fullforwardshadows keepalpha
  40.  
  41. // Use shader model 3.0 target, to get nicer looking lighting
  42. #pragma target 3.0
  43. #pragma shader_feature VERTEX
  44. #pragma shader_feature RIM
  45.  
  46. sampler2D _SideNoiseTex, _TopNoiseTex;
  47.  
  48. uniform sampler2D _CameraDepthTexture; //Depth Texture
  49.  
  50. struct Input {
  51. float3 worldNormal; INTERNAL_DATA// world normal built-in value
  52. float3 worldPos; // world position built-in value
  53. float3 viewDir;// view direction for rim
  54. float4 color : COLOR; // vertex colors
  55. float4 screenPos; // screen position for edgefoam
  56. float eyeDepth;// depth for edgefoam
  57. };
  58.  
  59. void vert (inout appdata_full v, out Input o)
  60. {
  61. UNITY_INITIALIZE_OUTPUT(Input, o);
  62. COMPUTE_EYEDEPTH(o.eyeDepth);
  63. }
  64.  
  65. fixed4 _FoamColor, _WaterColor, _RimColor, _TColor;
  66. fixed _HorSpeed, _TopScale, _TopSpread, _EdgeWidth, _RimPower,_NoiseScale , _VertSpeed;
  67. float _BrightNess, _Foam, _Softness;
  68.  
  69. void surf(Input IN, inout SurfaceOutputStandard o) {
  70.  
  71. // get the world normal
  72. float3 worldNormal = WorldNormalVector(IN, o.Normal);
  73. // grab the vertex colors from the model
  74. float3 vertexColors = IN.color.rgb;
  75. // normal for triplanar mapping
  76. float3 blendNormal = saturate(pow(worldNormal * 1.4,4));
  77.  
  78.  
  79. #if VERTEX // use vertex colors for flow
  80. float3 flowDir= (vertexColors * 2.0f) - 1.0f;
  81. #else // or world normal
  82. float3 flowDir= -(worldNormal * 2.0f) - 1.0f;
  83. #endif
  84. // horizontal flow speed
  85. flowDir *= _HorSpeed;
  86.  
  87. // flowmap blend timings
  88. float timing = frac(_Time.y * 0.5f + 0.5f);
  89. float timing2 = frac(_Time.y* 0.5f);
  90. float timingLerp = abs((0.5f - timing) / 0.5f);
  91.  
  92. // move 2 textures at slight different speeds fased on the flowdirection
  93. half3 topTex1 = tex2D(_TopNoiseTex, IN.worldPos.xz * _TopScale + (flowDir.xz * timing));
  94. half3 topTex2 = tex2D(_TopNoiseTex, IN.worldPos.xz * _TopScale + (flowDir.xz * timing2));
  95.  
  96. // vertical flow speed
  97. float vertFlow = _Time.y * _VertSpeed;
  98.  
  99. // noise sides
  100. float3 TopFoamNoise = lerp(topTex1, topTex2, timingLerp);
  101. float3 SideFoamNoiseZ = tex2D(_SideNoiseTex, float2(IN.worldPos.z* 10, IN.worldPos.y + vertFlow) * _NoiseScale );
  102. float3 SideFoamNoiseX = tex2D(_SideNoiseTex, float2(IN.worldPos.x* 10, IN.worldPos.y + vertFlow) * _NoiseScale);
  103.  
  104. // lerped together all sides for noise texture
  105. float3 noisetexture = SideFoamNoiseX;
  106. noisetexture = lerp(noisetexture, SideFoamNoiseZ, blendNormal.x);
  107. noisetexture = lerp(noisetexture, TopFoamNoise, blendNormal.y);
  108.  
  109. // add noise to normal
  110. o.Normal *= noisetexture;
  111.  
  112. // edge foam calculation
  113. half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture , UNITY_PROJ_COORD(IN.screenPos))); // depth
  114. half4 foamLine =1 - saturate(_Foam * float4(noisetexture,1) * (depth - IN.screenPos.w));// foam line by comparing depth and screenposition
  115.  
  116. // rimline
  117. #if RIM
  118. int rim = 1.0 - saturate(dot(normalize(IN.viewDir) , o.Normal));
  119. #else
  120. half rim = 1.0 - saturate(dot(normalize(IN.viewDir) , o.Normal));
  121. #endif
  122. float3 colorRim = _RimColor.rgb * pow (rim, _RimPower);
  123.  
  124. // Normalbased Foam
  125. float worldNormalDotNoise = dot(o.Normal , worldNormal.y);
  126. float3 foam = (smoothstep(_TopSpread, _TopSpread + _Softness, worldNormalDotNoise) * smoothstep(worldNormalDotNoise,worldNormalDotNoise + _Softness, _TopSpread + _EdgeWidth));
  127.  
  128. // combine depth foam and foam + add color
  129. float3 combinedFoam = (foam + foamLine.rgb) * _FoamColor;
  130.  
  131. // colors lerped over blendnormal
  132. float4 color = lerp(_WaterColor, _TColor, blendNormal.y) * _BrightNess;
  133. o.Albedo = color;
  134.  
  135. // glowing combined foam and colored rim
  136. o.Emission = combinedFoam + colorRim ;
  137.  
  138. // clamped alpha
  139. o.Alpha = clamp(color.a + combinedFoam + foamLine.a, 0, 1);
  140.  
  141.  
  142. }
  143. ENDCG
  144. }
  145. FallBack "Diffuse"
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement