water.wgsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Cold Mountain River Water Shader
  2. // Uses Dual Paraboloid 360 Reflections + Animated Flow Ripple Normals + Schlick Fresnel
  3. // + Vogel Disk PCF Sun Shadows + 2D Projected Cloud Shadows + Shoreline Foam.
  4. @group(0) @binding(0) var<uniform> u: GlobalUniforms;
  5. @group(0) @binding(1) var<storage, read> lights: array<GpuPointLight>;
  6. @group(0) @binding(6) var shadow_tex: texture_depth_2d;
  7. @group(0) @binding(7) var shadow_sampler: sampler_comparison;
  8. @group(0) @binding(8) var cloud_shadow_tex: texture_2d<f32>;
  9. @group(0) @binding(9) var linear_clamp_sampler: sampler;
  10. @group(0) @binding(10) var dp_tex: texture_2d_array<f32>;
  11. struct WaterIn {
  12. @location(0) position: vec3<f32>,
  13. @location(1) flow_foam_depth: vec4<f32>,
  14. };
  15. struct WaterOut {
  16. @builtin(position) position: vec4<f32>,
  17. @location(0) world_pos: vec3<f32>,
  18. @location(1) flow_foam_depth: vec4<f32>,
  19. };
  20. @vertex
  21. fn vs_main(v: WaterIn) -> WaterOut {
  22. var out: WaterOut;
  23. let t = u.camera_pos.w;
  24. let wave = 0.04 * sin(v.position.x * 0.65 + t * 2.2) * cos(v.position.z * 0.55 - t * 1.8)
  25. * clamp(v.flow_foam_depth.w, 0.0, 1.0);
  26. let wp = vec3<f32>(v.position.x, v.position.y + wave, v.position.z);
  27. out.position = u.view_proj * vec4<f32>(wp, 1.0);
  28. out.world_pos = wp;
  29. out.flow_foam_depth = v.flow_foam_depth;
  30. return out;
  31. }
  32. @fragment
  33. fn fs_main(in: WaterOut) -> @location(0) vec4<f32> {
  34. let t = u.camera_pos.w;
  35. let flow = normalize(in.flow_foam_depth.xy + vec2<f32>(0.001, 0.001));
  36. let p = in.world_pos.xz;
  37. // Multi-directional organic river ripples (avoids regular grid striping)
  38. let q1 = p * 0.65 - flow * (t * 1.9);
  39. let q2 = p * 1.35 - flow * (t * 3.1) + vec2<f32>(7.3, -3.1);
  40. let d1 = vec2<f32>(0.73, 0.68);
  41. let d2 = vec2<f32>(-0.61, 0.79);
  42. let d3 = vec2<f32>(0.91, -0.41);
  43. let d4 = vec2<f32>(-0.34, -0.94);
  44. let ph1 = dot(q1, d1) * 2.3;
  45. let ph2 = dot(q1, d2) * 3.1 + 1.4;
  46. let ph3 = dot(q2, d3) * 2.7 - 0.8;
  47. let ph4 = dot(q2, d4) * 3.9 + 2.1;
  48. let grad = d1 * cos(ph1) * 0.018
  49. + d2 * cos(ph2) * 0.015
  50. + d3 * cos(ph3) * 0.013
  51. + d4 * cos(ph4) * 0.010;
  52. let N = normalize(vec3<f32>(-grad.x, 1.0, -grad.y));
  53. let V = normalize(u.camera_pos.xyz - in.world_pos);
  54. let sun_d = normalize(u.sun_dir.xyz);
  55. let R = reflect(-V, N);
  56. // Sharp Dual Paraboloid reflection of sky, clouds, bridge & ruined skyline!
  57. let refl = sample_dual_paraboloid(dp_tex, linear_clamp_sampler, R, 0.04);
  58. let ndv = max(dot(N, V), 0.001);
  59. let fresnel = 0.04 + 0.96 * pow(1.0 - ndv, 4.5);
  60. let sun_vis = sample_sun_shadow_vogel(
  61. shadow_tex, shadow_sampler, u.sun_view_proj,
  62. in.world_pos, Vec3_Y(), sun_d, in.position.xy, 12u
  63. );
  64. let cloud_vis = sample_cloud_shadow(cloud_shadow_tex, linear_clamp_sampler, in.world_pos, u.cloud_shadow_bounds);
  65. // Deep icy Caucasus river teal-slate base water + crisp sun glitter
  66. let deep_col = vec3<f32>(0.022, 0.052, 0.066) * (0.45 + 0.55 * cloud_vis);
  67. let H = normalize(V + sun_d);
  68. let spec = pow(max(dot(N, H), 0.0), 260.0) * 2.2 * sun_vis * cloud_vis;
  69. var color = mix(deep_col, refl, clamp(fresnel * 1.15, 0.14, 0.96)) + u.sun_color.rgb * spec;
  70. // Non-periodic multi-octave shoreline cold-water foam confined to shallow edges
  71. let depth_foam = 1.0 - smoothstep(0.0, 0.42, in.flow_foam_depth.w);
  72. let shore_mask = in.flow_foam_depth.z * depth_foam;
  73. let fp = p * 0.42 - flow * (t * 0.35);
  74. let fn1 = water_vnoise(fp);
  75. let fn2 = water_vnoise(fp * 2.15 + vec2<f32>(11.7, -5.3) - flow * (t * 0.55));
  76. let fn3 = water_vnoise(fp * 4.6 + vec2<f32>(-4.1, 8.9));
  77. let foam_fbm = fn1 * 0.52 + fn2 * 0.33 + fn3 * 0.15;
  78. let foam = clamp(shore_mask * smoothstep(0.46, 0.78, foam_fbm), 0.0, 0.55);
  79. color = mix(color, vec3<f32>(0.68, 0.74, 0.78) * (0.5 + 0.5 * cloud_vis), foam * 0.52);
  80. // Valley fog match
  81. let cam_dist = length(in.world_pos - u.camera_pos.xyz);
  82. let fog_scale = select(1.0, 0.20, u.screen_params.w > 1500.0);
  83. let fog_amount = 1.0 - exp(-cam_dist * 0.0024 * fog_scale);
  84. let fog_col = eval_winter_sky(normalize(in.world_pos - u.camera_pos.xyz), sun_d, u.sun_dir.w);
  85. color = mix(color, fog_col, clamp(fog_amount, 0.0, 0.90));
  86. return vec4<f32>(color, 1.0);
  87. }
  88. fn water_hash21(p: vec2<f32>) -> f32 {
  89. let p3 = fract(vec3<f32>(p.x, p.y, p.x) * 0.1031);
  90. let d = p3 + dot(p3, p3.yzx + 33.33);
  91. return fract((d.x + d.y) * d.z);
  92. }
  93. fn water_vnoise(p: vec2<f32>) -> f32 {
  94. let i = floor(p);
  95. let f = fract(p);
  96. let u2 = f * f * (3.0 - 2.0 * f);
  97. let a = water_hash21(i);
  98. let b = water_hash21(i + vec2<f32>(1.0, 0.0));
  99. let c = water_hash21(i + vec2<f32>(0.0, 1.0));
  100. let d = water_hash21(i + vec2<f32>(1.0, 1.0));
  101. return mix(mix(a, b, u2.x), mix(c, d, u2.x), u2.y);
  102. }
  103. fn Vec3_Y() -> vec3<f32> {
  104. return vec3<f32>(0.0, 1.0, 0.0);
  105. }