paraboloid.wgsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Dual Paraboloid Reflection Probe Pass (512x512x2 Rgba16Float, 1 hemisphere/frame)
  2. // Renders winter sky dome + lit low-poly terrain, buildings, wrecks, and muzzle flashes!
  3. @group(0) @binding(0) var<uniform> u: GlobalUniforms;
  4. @group(0) @binding(1) var<storage, read> lights: array<GpuPointLight>;
  5. @group(0) @binding(2) var mat_albedo: texture_2d_array<f32>;
  6. @group(0) @binding(3) var mat_sampler: sampler;
  7. @group(0) @binding(4) var shadow_tex: texture_depth_2d;
  8. @group(0) @binding(5) var shadow_sampler: sampler_comparison;
  9. @group(0) @binding(6) var cloud_shadow_tex: texture_2d<f32>;
  10. @group(0) @binding(7) var linear_clamp_sampler: sampler;
  11. struct SkyOut {
  12. @builtin(position) position: vec4<f32>,
  13. @location(0) uv: vec2<f32>,
  14. };
  15. @vertex
  16. fn vs_sky(@builtin(vertex_index) vid: u32) -> SkyOut {
  17. var out: SkyOut;
  18. let x = f32(i32(vid & 1u) * 4 - 1);
  19. let y = f32(i32(vid >> 1u) * 4 - 1);
  20. out.position = vec4<f32>(x, y, 0.9999, 1.0);
  21. out.uv = vec2<f32>(x, y);
  22. return out;
  23. }
  24. @fragment
  25. fn fs_sky(in: SkyOut) -> @location(0) vec4<f32> {
  26. // Reconstruct parabolic hemisphere ray direction from NDC xy
  27. let p2 = dot(in.uv, in.uv);
  28. if (p2 > 1.25) {
  29. return vec4<f32>(0.25, 0.30, 0.36, 1.0);
  30. }
  31. let denom = 1.0 + p2;
  32. let hemi = u.dp_origin.w;
  33. let dir = normalize(vec3<f32>(2.0 * in.uv.x / denom, 2.0 * in.uv.y / denom, hemi * (1.0 - p2) / denom));
  34. var col = eval_winter_sky(dir, normalize(u.sun_dir.xyz), u.sun_dir.w);
  35. // Subtle cloud bands in DPR sky dome
  36. if (dir.y > 0.04) {
  37. let sky_uv = dir.xz / (dir.y + 0.15) * 0.25 + u.cloud_wind.xy * 0.0005;
  38. let band = 0.5 + 0.5 * sin(sky_uv.x * 7.0 + sin(sky_uv.y * 5.0));
  39. let cloud_tint = mix(vec3<f32>(0.26, 0.29, 0.34), vec3<f32>(0.78, 0.80, 0.84), band);
  40. col = mix(col, cloud_tint, smoothstep(0.3, 0.85, u.sun_dir.w) * 0.55 * smoothstep(0.04, 0.25, dir.y));
  41. }
  42. return vec4<f32>(col, 1.0);
  43. }
  44. struct StaticIn {
  45. @location(0) position: vec3<f32>,
  46. @location(1) normal: vec3<f32>,
  47. @location(2) uv: vec2<f32>,
  48. @location(3) params: vec4<f32>,
  49. };
  50. struct DpOut {
  51. @builtin(position) position: vec4<f32>,
  52. @location(0) world_pos: vec3<f32>,
  53. @location(1) normal: vec3<f32>,
  54. @location(2) uv: vec2<f32>,
  55. @location(3) params: vec4<f32>,
  56. @location(4) clip_z: f32,
  57. };
  58. @vertex
  59. fn vs_static(v: StaticIn) -> DpOut {
  60. var out: DpOut;
  61. let rel = v.position - u.dp_origin.xyz;
  62. let hemi = u.dp_origin.w;
  63. let p = vec3<f32>(rel.x, rel.y, rel.z * hemi);
  64. let dist = length(p);
  65. let dir = p / max(dist, 0.0001);
  66. let denom = max(dir.z + 1.0, 0.06);
  67. let depth = clamp((dist - 0.4) / 480.0, 0.0, 0.999);
  68. out.clip_z = dir.z;
  69. out.position = vec4<f32>(dir.x / denom, dir.y / denom, depth, 1.0);
  70. out.world_pos = v.position;
  71. out.normal = v.normal;
  72. out.uv = v.uv;
  73. out.params = v.params;
  74. return out;
  75. }
  76. @fragment
  77. fn fs_static(in: DpOut) -> @location(0) vec4<f32> {
  78. // 5% seam overlap eliminates equator cracks between Front (+Z) & Back (-Z) hemispheres
  79. if (in.clip_z < -0.05) {
  80. discard;
  81. }
  82. let layer = i32(clamp(in.params.x + 0.5, 0.0, 20.0));
  83. let albedo = textureSample(mat_albedo, mat_sampler, in.uv, layer).rgb * in.params.w;
  84. let N = normalize(in.normal);
  85. let sun_d = normalize(u.sun_dir.xyz);
  86. let ndl = max(dot(N, sun_d), 0.0);
  87. let sun_vis = sample_sun_shadow_vogel(
  88. shadow_tex, shadow_sampler, u.sun_view_proj,
  89. in.world_pos, N, sun_d, in.position.xy, 4u
  90. );
  91. let cloud_vis = sample_cloud_shadow(cloud_shadow_tex, linear_clamp_sampler, in.world_pos, u.cloud_shadow_bounds);
  92. let hemi_amb = mix(vec3<f32>(0.12, 0.14, 0.17), vec3<f32>(0.34, 0.40, 0.48), N.y * 0.5 + 0.5);
  93. var color = albedo * (hemi_amb * in.params.z + u.sun_color.rgb * ndl * sun_vis * cloud_vis);
  94. // Dynamic muzzle flashes & explosions reflected in Dual Paraboloid!
  95. let lcount = min(u.flags.x, 64u);
  96. for (var i = 0u; i < lcount; i = i + 1u) {
  97. let lp = lights[i].pos_radius;
  98. let to_l = lp.xyz - in.world_pos;
  99. let d = length(to_l);
  100. if (d < lp.w) {
  101. let atten = pow(1.0 - d / lp.w, 2.0);
  102. let l_ndl = max(dot(N, to_l / max(d, 0.01)), 0.0);
  103. color += albedo * lights[i].color_intensity.rgb * lights[i].color_intensity.w * atten * l_ndl;
  104. }
  105. }
  106. let dist = length(in.world_pos - u.dp_origin.xyz);
  107. let fog = 1.0 - exp(-dist * 0.0022);
  108. let fog_col = eval_winter_sky(normalize(in.world_pos - u.dp_origin.xyz), sun_d, u.sun_dir.w);
  109. color = mix(color, fog_col, clamp(fog, 0.0, 0.85));
  110. return vec4<f32>(color, 1.0);
  111. }