Advertisement
Guest User

Untitled

a guest
Jan 31st, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. Shader "Custom/Water_Lit"
  2. {
  3. Properties
  4. {
  5. _DeepColor ("Deep Color", Color) = (0, 0.5, 1, 1)
  6. _ShallowColor ("Shallow Color", Color) = (0, 0.2, 0.5, 1)
  7.  
  8. _NormalMap1 ("Normal map 1", 2D) = "bump" {}
  9. _NormalMap2 ("Normal map 2", 2D) = "bump" {}
  10. _Glossiness ("Smoothness", Range(0, 1)) = 0.5
  11. _Metallic ("Metallic", Range(0, 1)) = 0.0
  12.  
  13. _SpeedMap1 ("Wave Speed 1", Float) = 0.1
  14. _SpeedMap2 ("Wave Speed 2", Float) = 0.2
  15. _Scale ("Wave Scale", Range(0, 1)) = 0.1
  16. _Amplitude ("Wave Amplitude", Range(0, 1)) = 0.1
  17.  
  18. [HDR]_FoamColor ("Foam Color", Color) = (1, 1, 1, 1)
  19. _FoamIntensity ("Foam Intensity", Float) = 0.5
  20. _FoamScale ("Foam Scale", Float) = 1.0
  21. _FoamCutoff ("Foam Cutoff", Float) = 1.0
  22.  
  23. _DepthFactor ("Depth Factor", Float) = 1.0
  24. _CenterPoint ("Center Point", Vector) = (0,0,0,0)
  25.  
  26.  
  27. _RefractoringNormal("Refractoring Normal", 2D) = "bump" {}
  28. _RefractionStrength ("Refraction Strengt", Float) = 1.0
  29. _RefractionSpeed ("Refraction Speed", Float) = 1.0
  30. _RefractionDepth ("Refract depth", Float) = 1.0
  31. }
  32. SubShader
  33. {
  34. GrabPass
  35. {
  36. "_GrabTexture"
  37. }
  38. Tags
  39. {
  40. "Queue" = "Transparent"
  41. "RenderType"="Transparent"
  42. }
  43.  
  44.  
  45.  
  46. CGPROGRAM
  47. #pragma surface surf Standard alpha vertex:vert
  48. #pragma target 3.0
  49.  
  50. #include "UnityCG.cginc"
  51.  
  52. sampler2D _NormalMap1;
  53. sampler2D _NormalMap2;
  54. sampler2D _GrabTexture;
  55. sampler2D _CameraDepthTexture;
  56. sampler2D _RefractoringNormal;
  57.  
  58. struct Input
  59. {
  60. float2 uv_NormalMap1;
  61. float2 uv_NormalMap2;
  62. float2 uv_GrabTexture;
  63. float2 uv_RefractoringNormal;
  64.  
  65. float3 worldNormal;
  66. float3 worldPos;
  67.  
  68. float4 screenPos;
  69. };
  70.  
  71.  
  72. half _Glossiness;
  73. half _Metallic;
  74. float4 _DeepColor;
  75. float4 _ShallowColor;
  76.  
  77.  
  78. float _SpeedMap1, _SpeedMap2, _Scale, _Amplitude;
  79.  
  80. float4 _FoamColor;
  81.  
  82. float _FoamIntensity, _FoamScale, _FoamCutoff;
  83.  
  84. float _DepthFactor;
  85.  
  86. float4 _CenterPoint;
  87.  
  88. float _RefractionStrength;
  89. float _RefractionSpeed;
  90. float _RefractionDepth;
  91.  
  92. void vert(inout appdata_full v, out Input i)
  93. {
  94. UNITY_INITIALIZE_OUTPUT(Input, i);
  95. i.screenPos = ComputeScreenPos(v.vertex);
  96. }
  97.  
  98. ///////////////////////////////////////////HELPERS////////////////////////////////////////////
  99. float4 ComputeScreenCoords(Input i)
  100. {
  101. float4 screenUV = i.screenPos;
  102. screenUV.xy /= screenUV.w;
  103. return screenUV;
  104. }
  105.  
  106. float CalculateDepth(Input IN, float scaleFactor)
  107. {
  108. float4 screenUV = ComputeScreenCoords(IN);
  109.  
  110. float cameraToUnderwaterDist = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screenUV.xy));
  111. float cameraToSurfaceDist = UNITY_Z_0_FAR_FROM_CLIPSPACE(screenUV.z);
  112.  
  113. float depthDiff = saturate((cameraToUnderwaterDist - cameraToSurfaceDist) / scaleFactor);
  114.  
  115. return depthDiff;
  116. }
  117.  
  118. float3 blendedNormals(Input IN)
  119. {
  120. float t1 = _Time * _SpeedMap1;
  121. float t2 = _Time * _SpeedMap2;
  122.  
  123. float2 offset1 = float2(t1 * _Scale, 0);
  124. float2 offset2 = float2(0, t2 * _Scale);
  125.  
  126. float2 uv1 = IN.uv_NormalMap1 + offset1;
  127. float2 uv2 = IN.uv_NormalMap2 + offset2;
  128.  
  129. float3 normal1 = UnpackNormal(tex2D(_NormalMap1, uv1));
  130. float3 normal2 = UnpackNormal(tex2D(_NormalMap2, uv2));
  131.  
  132. float3 blendedNormal = normalize(normal1 + normal2);
  133.  
  134. return blendedNormal;
  135. }
  136.  
  137. /////////////////////////////////////////////////////////////////////////////////////////////////////
  138.  
  139.  
  140. ///////////////////////////////////////////GRADIENT NOISE ////////////////////////////////////////////
  141. float2 unity_gradientNoise_dir(float2 p)
  142. {
  143. p = p % 289;
  144. float x = (34 * p.x + 1) * p.x % 289 + p.y;
  145. x = (34 * x + 1) * x % 289;
  146. x = frac(x / 41) * 2 - 1;
  147. return normalize(float2(x - floor(x + 0.5), abs(x) - 0.5));
  148. }
  149.  
  150. float unity_gradientNoise(float2 p)
  151. {
  152. float2 ip = floor(p);
  153. float2 fp = frac(p);
  154. float d00 = dot(unity_gradientNoise_dir(ip), fp);
  155. float d01 = dot(unity_gradientNoise_dir(ip + float2(0, 1)), fp - float2(0, 1));
  156. float d10 = dot(unity_gradientNoise_dir(ip + float2(1, 0)), fp - float2(1, 0));
  157. float d11 = dot(unity_gradientNoise_dir(ip + float2(1, 1)), fp - float2(1, 1));
  158. fp = fp * fp * fp * (fp * (fp * 6 - 15) + 10);
  159. return lerp(lerp(d00, d01, fp.y), lerp(d10, d11, fp.y), fp.x);
  160. }
  161.  
  162. void Unity_GradientNoise_float(float2 UV, float Scale, out float Out)
  163. {
  164. Out = unity_gradientNoise(UV * Scale) + 0.5;
  165. }
  166.  
  167. float GradientNoiseGeneration(float foamScale, Input IN)
  168. {
  169. float3 blendUVs = blendedNormals(IN);
  170. float gradientNoise;
  171. Unity_GradientNoise_float(blendUVs.xy, foamScale, gradientNoise);
  172.  
  173. return (gradientNoise);
  174. }
  175.  
  176. /////////////////////////////////////////////////////////////////////////////////////////////////////
  177.  
  178.  
  179. ///////////////////////////////////////////FOAM//////////////////////////////////////////////////////
  180. float foam(Input IN, float foamAmount, float foamCutoff)
  181. {
  182. float depth = CalculateDepth(IN, foamAmount);
  183.  
  184. float gradNoise = GradientNoiseGeneration(_FoamScale, IN);
  185. float foamThreshold = depth * foamCutoff;
  186. float foamPlacement = step(foamThreshold, gradNoise);
  187. float res = foamPlacement * _FoamColor.a;
  188. return res;
  189. }
  190.  
  191. /////////////////////////////////////////////////////////////////////////////////////////////////////
  192.  
  193.  
  194. ///////////////////////////////////////////REFRACTION////////////////////////////////////////////
  195. //Snell's Law
  196. float3 Refraction(Input IN)
  197. {
  198. float n1 = 1.0; // air
  199. float n2 = 1.33; // water
  200.  
  201. float t1 = _Time * _SpeedMap1;
  202. float t2 = _Time * _SpeedMap2;
  203.  
  204. float2 offset1 = float2(t1 * _Scale, 0);
  205. float2 offset2 = float2(0, t2 * _Scale);
  206.  
  207. float2 uv1 = IN.uv_RefractoringNormal + offset1 + offset2;
  208. float3 lightPos = _WorldSpaceLightPos0;
  209. float3 normal = normalize(UnpackNormal(tex2D(_RefractoringNormal, uv1)));
  210.  
  211. float3 incidentRay = normalize(lightPos - IN.worldPos);
  212.  
  213. float3 refractionRay = normalize(refract(incidentRay, normal, n1 / n2));
  214.  
  215. return refractionRay;
  216. }
  217.  
  218.  
  219. /////////////////////////////////////////////////////////////////////////////////////////////////////
  220.  
  221. void surf(Input IN, inout SurfaceOutputStandard o)
  222. {
  223. float depth = CalculateDepth(IN, _DepthFactor);
  224.  
  225. float3 refractionRay = Refraction(IN);
  226. float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
  227.  
  228. float2 distortedUV = screenUV + refractionRay.xy * _RefractionStrength * 0.02;
  229.  
  230. float3 refractedColor = tex2D(_GrabTexture, distortedUV);
  231.  
  232. float4 waterColor = lerp(_ShallowColor, _DeepColor, depth);
  233.  
  234. float foamAmount = foam(IN, _FoamIntensity, _FoamCutoff);
  235. float4 waterFoamColor = lerp(waterColor, _FoamColor, foamAmount);
  236.  
  237. float3 finalColor = lerp(refractedColor, waterFoamColor, 0.4);
  238.  
  239. o.Albedo = finalColor;
  240.  
  241. o.Alpha = _DeepColor.a;
  242. o.Metallic = _Metallic;
  243. o.Smoothness = _Glossiness;
  244.  
  245. o.Normal = blendedNormals(IN);
  246. }
  247. ENDCG
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement