common.wgsl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Shared WGSL structs, math, procedural filtering, Vogel Disk PCF,
  2. // Dual Paraboloid sampling, and PBR GGX helpers.
  3. struct GlobalUniforms {
  4. view_proj: mat4x4<f32>,
  5. inv_view_proj: mat4x4<f32>,
  6. sun_view_proj: mat4x4<f32>,
  7. camera_pos: vec4<f32>, // xyz = pos, w = time_secs
  8. sun_dir: vec4<f32>, // xyz = normalized toward sun, w = cloud_coverage
  9. sun_color: vec4<f32>, // rgb = sun HDR color, w = wetness
  10. cloud_wind: vec4<f32>, // xy = wind offset, z = pcf_samples, w = bicubic_enabled
  11. dp_origin: vec4<f32>, // xyz = Dual Paraboloid probe origin, w = hemi_sign (+1 or -1)
  12. screen_params: vec4<f32>, // x = width, y = height, z = near(0.3), w = far(950.0)
  13. cloud_params: vec4<f32>, // xy = cloud_seed_offset, z = cos(angle), w = sin(angle)
  14. cloud_shadow_bounds: vec4<f32>, // xy = world center XZ, z = span meters, w = 1.0 / span
  15. flags: vec4<u32>, // x = light_count, y = show_render_pip, z = suppression_x1000, w = damage_flash_x1000
  16. };
  17. fn cloud_weather_uvs(world_xz: vec2<f32>, wind_xy: vec2<f32>, params: vec4<f32>) -> vec4<f32> {
  18. let p = world_xz + wind_xy + params.xy;
  19. let c1 = params.z;
  20. let s1 = params.w;
  21. let rot1 = vec2<f32>(c1 * p.x - s1 * p.y, s1 * p.x + c1 * p.y);
  22. // Incommensurate secondary octave (~1.13 rad offset + subtle domain warp) breaks grid tiling
  23. let c2 = c1 * 0.4266 - s1 * 0.9044;
  24. let s2 = s1 * 0.4266 + c1 * 0.9044;
  25. let rot2 = vec2<f32>(c2 * p.x - s2 * p.y, s2 * p.x + c2 * p.y);
  26. let warp = vec2<f32>(sin(rot2.y * 0.0011), cos(rot1.x * 0.0009)) * 0.085;
  27. let uv1 = rot1 * 0.00042 + warp;
  28. let uv2 = rot2 * 0.000575 - warp * 0.65 + vec2<f32>(0.37, 0.71);
  29. return vec4<f32>(uv1, uv2);
  30. }
  31. struct GpuPointLight {
  32. pos_radius: vec4<f32>, // xyz = world pos, w = radius
  33. color_intensity: vec4<f32>, // rgb = color, w = intensity
  34. };
  35. const PI: f32 = 3.14159265359;
  36. const GOLDEN_ANGLE: f32 = 2.39996323;
  37. fn interleaved_gradient_noise(pixel_pos: vec2<f32>) -> f32 {
  38. let magic = vec3<f32>(0.06711056, 0.00583715, 52.9829189);
  39. return fract(magic.z * fract(dot(pixel_pos, magic.xy)));
  40. }
  41. fn vogel_disk_sample(sample_idx: u32, sample_count: u32, phi: f32) -> vec2<f32> {
  42. let r = sqrt((f32(sample_idx) + 0.5) / f32(sample_count));
  43. let theta = f32(sample_idx) * GOLDEN_ANGLE + phi;
  44. return vec2<f32>(cos(theta), sin(theta)) * r;
  45. }
  46. /// 16-tap (configurable 8/16/32) Vogel Disk PCF with IGN rotation & normal-offset slope bias.
  47. fn sample_sun_shadow_vogel(
  48. shadow_tex: texture_depth_2d,
  49. shadow_sampler: sampler_comparison,
  50. sun_vp: mat4x4<f32>,
  51. world_pos: vec3<f32>,
  52. world_normal: vec3<f32>,
  53. sun_dir: vec3<f32>,
  54. pixel_pos: vec2<f32>,
  55. sample_count: u32,
  56. ) -> f32 {
  57. let ndl = clamp(dot(world_normal, sun_dir), 0.0, 1.0);
  58. let normal_bias = world_normal * mix(0.22, 0.04, ndl);
  59. let clip = sun_vp * vec4<f32>(world_pos + normal_bias, 1.0);
  60. let ndc = clip.xyz / clip.w;
  61. let uv = vec2<f32>(ndc.x * 0.5 + 0.5, -ndc.y * 0.5 + 0.5);
  62. if (uv.x <= 0.005 || uv.x >= 0.995 || uv.y <= 0.005 || uv.y >= 0.995 || ndc.z <= 0.0 || ndc.z >= 1.0) {
  63. return 1.0;
  64. }
  65. let texel_size = 1.0 / 2048.0;
  66. let radius = 3.2 * texel_size;
  67. let phi = interleaved_gradient_noise(pixel_pos) * 6.2831853;
  68. let bias = max(0.0004, 0.0018 * (1.0 - ndl));
  69. let ref_z = ndc.z - bias;
  70. var sum = 0.0;
  71. let count = clamp(sample_count, 4u, 32u);
  72. for (var i = 0u; i < count; i = i + 1u) {
  73. let offset = vogel_disk_sample(i, count, phi) * radius;
  74. sum += textureSampleCompareLevel(shadow_tex, shadow_sampler, uv + offset, ref_z);
  75. }
  76. return sum / f32(count);
  77. }
  78. /// Samples the 2D projected volumetric cloud shadow map covering the map or a camera-snapped window.
  79. fn sample_cloud_shadow(
  80. cloud_shadow_tex: texture_2d<f32>,
  81. samp: sampler,
  82. world_pos: vec3<f32>,
  83. bounds: vec4<f32>,
  84. ) -> f32 {
  85. let uv = (world_pos.xz - bounds.xy) * bounds.w + vec2<f32>(0.5, 0.5);
  86. if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
  87. return 0.85;
  88. }
  89. return textureSampleLevel(cloud_shadow_tex, samp, uv, 0.0).r;
  90. }
  91. /// 4-tap Catmull-Rom / B-Spline bicubic texture filter for heavily filtered low-res shader textures!
  92. fn sample_bicubic_array(
  93. tex: texture_2d_array<f32>,
  94. samp: sampler,
  95. uv: vec2<f32>,
  96. layer: i32,
  97. ) -> vec4<f32> {
  98. let dims = vec2<f32>(textureDimensions(tex, 0));
  99. let inv_dims = 1.0 / dims;
  100. let tc = uv * dims - 0.5;
  101. let f = fract(tc);
  102. let tc_floor = tc - f;
  103. // B-Spline weights -> 4 bilinear hardware taps
  104. let f2 = f * f;
  105. let f3 = f2 * f;
  106. let w0 = (1.0 / 6.0) * (-f3 + 3.0 * f2 - 3.0 * f + 1.0);
  107. let w1 = (1.0 / 6.0) * (3.0 * f3 - 6.0 * f2 + 4.0);
  108. let w2 = (1.0 / 6.0) * (-3.0 * f3 + 3.0 * f2 + 3.0 * f + 1.0);
  109. let w3 = (1.0 / 6.0) * f3;
  110. let s0 = w0 + w1;
  111. let s1 = w2 + w3;
  112. let o0 = (tc_floor + w1 / max(s0, vec2<f32>(1e-4)) - 0.5) * inv_dims;
  113. let o1 = (tc_floor + 2.0 + w3 / max(s1, vec2<f32>(1e-4)) - 0.5) * inv_dims;
  114. let c00 = textureSample(tex, samp, vec2<f32>(o0.x, o0.y), layer);
  115. let c10 = textureSample(tex, samp, vec2<f32>(o1.x, o0.y), layer);
  116. let c01 = textureSample(tex, samp, vec2<f32>(o0.x, o1.y), layer);
  117. let c11 = textureSample(tex, samp, vec2<f32>(o1.x, o1.y), layer);
  118. return (c00 * s0.x + c10 * s1.x) * s0.y + (c01 * s0.x + c11 * s1.x) * s1.y;
  119. }
  120. /// Samples the 360-degree Dual Paraboloid reflection map with roughness-driven mip lookup.
  121. fn sample_dual_paraboloid(
  122. dp_tex: texture_2d_array<f32>,
  123. samp: sampler,
  124. refl_dir: vec3<f32>,
  125. roughness: f32,
  126. ) -> vec3<f32> {
  127. let r = normalize(refl_dir);
  128. let layer = select(1, 0, r.z >= 0.0);
  129. let az = abs(r.z);
  130. let uv = (r.xy / (az + 1.0)) * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5);
  131. let lod = clamp(roughness * 5.0, 0.0, 5.0);
  132. return textureSampleLevel(dp_tex, samp, clamp(uv, vec2<f32>(0.002), vec2<f32>(0.998)), layer, lod).rgb;
  133. }
  134. /// Procedural Caucasus overcast winter sky & sun halo (used in main backdrop & DPR sky dome).
  135. fn eval_winter_sky(dir: vec3<f32>, sun_dir: vec3<f32>, cloud_cov: f32) -> vec3<f32> {
  136. let d = normalize(dir);
  137. let up = clamp(d.y, 0.0, 1.0);
  138. let horizon = vec3<f32>(0.42, 0.48, 0.55);
  139. let zenith = vec3<f32>(0.14, 0.22, 0.34);
  140. var sky = mix(horizon, zenith, pow(up, 0.55));
  141. // Overcast winter chill desaturation
  142. sky = mix(sky, vec3<f32>(0.36, 0.40, 0.44), cloud_cov * 0.45);
  143. let sun_dot = max(dot(d, sun_dir), 0.0);
  144. sky += vec3<f32>(1.15, 0.95, 0.72) * pow(sun_dot, 16.0) * (0.55 - cloud_cov * 0.3);
  145. sky += vec3<f32>(3.2, 2.7, 2.0) * pow(sun_dot, 320.0);
  146. return sky;
  147. }
  148. /// GGX / Cook-Torrance PBR helper.
  149. fn eval_pbr_direct(
  150. N: vec3<f32>,
  151. V: vec3<f32>,
  152. L: vec3<f32>,
  153. albedo: vec3<f32>,
  154. roughness: f32,
  155. metallic: f32,
  156. radiance: vec3<f32>,
  157. ) -> vec3<f32> {
  158. let ndl = max(dot(N, L), 0.0);
  159. if (ndl <= 0.0) {
  160. return vec3<f32>(0.0);
  161. }
  162. let H = normalize(V + L);
  163. let ndv = max(dot(N, V), 0.001);
  164. let ndh = max(dot(N, H), 0.0);
  165. let hdv = max(dot(H, V), 0.0);
  166. let r = clamp(roughness, 0.04, 1.0);
  167. let a = r * r;
  168. let a2 = a * a;
  169. let denom = ndh * ndh * (a2 - 1.0) + 1.0;
  170. let d_ggx = a2 / (PI * denom * denom + 1e-5);
  171. let k = ((r + 1.0) * (r + 1.0)) / 8.0;
  172. let g_v = ndv / (ndv * (1.0 - k) + k);
  173. let g_l = ndl / (ndl * (1.0 - k) + k);
  174. let g_smith = g_v * g_l;
  175. let f0 = mix(vec3<f32>(0.04), albedo, metallic);
  176. let fresnel = f0 + (1.0 - f0) * pow(1.0 - hdv, 5.0);
  177. let spec = (d_ggx * g_smith * fresnel) / (4.0 * ndv * ndl + 0.001);
  178. let kd = (vec3<f32>(1.0) - fresnel) * (1.0 - metallic);
  179. let diff = kd * albedo / PI;
  180. return (diff + spec) * radiance * ndl;
  181. }