post.wgsl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Post-Process Composite Pass:
  2. // 1. Depth-Aware Bilateral Upsample of Half-Res Volumetric Clouds onto Full-Res HDR Scene
  3. // 2. ACES Filmic Tone Mapping + Cold Chechen Winter Grading + Subtle Vignette
  4. // 3. Optional F4 Render Pass PIP (Dual Paraboloid Front/Back, Sun Shadow Map, Cloud Shadow Map)
  5. @group(0) @binding(0) var<uniform> u: GlobalUniforms;
  6. @group(0) @binding(6) var shadow_tex: texture_depth_2d;
  7. @group(0) @binding(8) var cloud_shadow_tex: texture_2d<f32>;
  8. @group(0) @binding(9) var linear_clamp_sampler: sampler;
  9. @group(0) @binding(10) var dp_tex: texture_2d_array<f32>;
  10. @group(1) @binding(0) var hdr_scene_tex: texture_2d<f32>;
  11. @group(1) @binding(1) var half_cloud_tex: texture_2d<f32>;
  12. @group(1) @binding(2) var scene_depth_tex: texture_depth_2d;
  13. struct VsOut {
  14. @builtin(position) position: vec4<f32>,
  15. @location(0) uv: vec2<f32>,
  16. };
  17. @vertex
  18. fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
  19. var out: VsOut;
  20. let x = f32(i32(vid & 1u) * 4 - 1);
  21. let y = f32(i32(vid >> 1u) * 4 - 1);
  22. out.position = vec4<f32>(x, y, 0.0, 1.0);
  23. out.uv = vec2<f32>(x * 0.5 + 0.5, -y * 0.5 + 0.5);
  24. return out;
  25. }
  26. fn aces_filmic(x: vec3<f32>) -> vec3<f32> {
  27. let a = 2.51;
  28. let b = 0.03;
  29. let c = 2.43;
  30. let d = 0.59;
  31. let e = 0.14;
  32. return clamp((x * (a * x + b)) / (x * (c * x + d) + e), vec3<f32>(0.0), vec3<f32>(1.0));
  33. }
  34. fn sample_cloud_bilateral(uv: vec2<f32>, full_depth: f32) -> vec4<f32> {
  35. if (full_depth < 0.999999) {
  36. return vec4<f32>(0.0, 0.0, 0.0, 1.0);
  37. }
  38. let half_dims = vec2<f32>(textureDimensions(half_cloud_tex));
  39. let depth_dims = vec2<f32>(textureDimensions(scene_depth_tex));
  40. let base_uv = (floor(uv * half_dims - 0.5) + 0.5) / half_dims;
  41. let step_uv = 1.0 / half_dims;
  42. var sum = vec4<f32>(0.0);
  43. var wsum = 0.0;
  44. for (var y = 0; y <= 1; y = y + 1) {
  45. for (var x = 0; x <= 1; x = x + 1) {
  46. let suv = clamp(base_uv + vec2<f32>(f32(x), f32(y)) * step_uv, vec2<f32>(0.0), vec2<f32>(1.0));
  47. let d_px = vec2<i32>(clamp(suv * depth_dims, vec2<f32>(0.0), depth_dims - 1.0));
  48. let sd = textureLoad(scene_depth_tex, d_px, 0);
  49. let w = select(0.001, 1.0, sd >= 0.999999);
  50. sum += textureSampleLevel(half_cloud_tex, linear_clamp_sampler, suv, 0.0) * w;
  51. wsum += w;
  52. }
  53. }
  54. if (wsum > 0.001) {
  55. return sum / wsum;
  56. }
  57. return vec4<f32>(0.0, 0.0, 0.0, 1.0);
  58. }
  59. @fragment
  60. fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
  61. let supp = clamp(f32(u.flags.z) * 0.001, 0.0, 1.0);
  62. let dmg = clamp(f32(u.flags.w) * 0.001, 0.0, 1.0);
  63. let dims = vec2<f32>(textureDimensions(hdr_scene_tex));
  64. let aspect = dims.x / max(dims.y, 1.0);
  65. let centered = (in.uv - vec2<f32>(0.5, 0.5)) * vec2<f32>(aspect, 1.0);
  66. let rad = length(centered);
  67. // Dynamic Suppression Pixelated Vision (edges pixelate earlier; full frame pixelates at peak pinning)
  68. var sample_uv = in.uv;
  69. if (supp > 0.04) {
  70. let pix_amt = smoothstep(0.04, 1.0, supp);
  71. let edge_factor = mix(0.45, 1.35, smoothstep(0.18, 0.82, rad));
  72. let block_px = max(1.0, floor(1.0 + pix_amt * pix_amt * 11.0 * edge_factor));
  73. sample_uv = clamp(
  74. (floor(in.uv * dims / block_px) + 0.5) * block_px / dims,
  75. vec2<f32>(0.0005),
  76. vec2<f32>(0.9995),
  77. );
  78. }
  79. let depth_dims = vec2<f32>(textureDimensions(scene_depth_tex));
  80. let px = vec2<i32>(clamp(sample_uv * depth_dims, vec2<f32>(0.0), depth_dims - 1.0));
  81. let raw_depth = textureLoad(scene_depth_tex, px, 0);
  82. var hdr = textureSampleLevel(hdr_scene_tex, linear_clamp_sampler, sample_uv, 0.0).rgb;
  83. // Subtle peripheral chromatic split under heavy suppression
  84. if (supp > 0.30) {
  85. let ca_amt = 0.008 * smoothstep(0.30, 1.0, supp) * smoothstep(0.20, 0.85, rad);
  86. let ca_dir = (in.uv - vec2<f32>(0.5, 0.5)) * ca_amt;
  87. let r_ch = textureSampleLevel(hdr_scene_tex, linear_clamp_sampler, clamp(sample_uv + ca_dir, vec2<f32>(0.001), vec2<f32>(0.999)), 0.0).r;
  88. let b_ch = textureSampleLevel(hdr_scene_tex, linear_clamp_sampler, clamp(sample_uv - ca_dir, vec2<f32>(0.001), vec2<f32>(0.999)), 0.0).b;
  89. hdr.r = mix(hdr.r, r_ch, 0.65);
  90. hdr.b = mix(hdr.b, b_ch, 0.65);
  91. }
  92. // Composite volumetric clouds ONLY onto sky background with bilateral depth edge awareness
  93. if (raw_depth >= 0.999999) {
  94. let cloud = sample_cloud_bilateral(sample_uv, raw_depth);
  95. hdr = hdr * cloud.a + cloud.rgb;
  96. }
  97. // ACES Filmic Tone Mapping + cold winter & adrenaline suppression desaturation
  98. var ldr = aces_filmic(hdr * 1.08);
  99. let luma = dot(ldr, vec3<f32>(0.299, 0.587, 0.114));
  100. ldr = mix(vec3<f32>(luma), ldr, 0.86 - supp * 0.44);
  101. ldr = pow(ldr, vec3<f32>(1.0 / 2.2));
  102. // Baseline subtle vignette + pulsed radial suppression tunnel-vision vignette
  103. let vig_uv = in.uv * (1.0 - in.uv.yx);
  104. let vig_base = clamp(pow(vig_uv.x * vig_uv.y * 16.0, 0.16), 0.72, 1.0);
  105. let pulse = 0.5 + 0.5 * sin(u.camera_pos.w * 7.5);
  106. let supp_curve = smoothstep(0.02, 1.0, supp);
  107. let inner_r = mix(0.68, 0.16, supp_curve) - pulse * 0.03 * supp_curve;
  108. let outer_r = mix(1.22, 0.66, supp_curve);
  109. let tunnel = 1.0 - smoothstep(inner_r, outer_r, rad);
  110. let supp_vig = mix(1.0, mix(0.03, 1.0, tunnel), supp_curve * 0.96);
  111. ldr *= vig_base * supp_vig;
  112. // Smooth radial crimson edge flash on direct hit
  113. if (dmg > 0.01) {
  114. let dmg_edge = smoothstep(0.25, 0.92, rad) * dmg;
  115. ldr = mix(ldr, vec3<f32>(0.62, 0.05, 0.04), clamp(dmg_edge * 0.65, 0.0, 0.65));
  116. }
  117. // Optional F4 Render Pass PIP Inspection Overlay (Bottom-Right 4 tiles)
  118. if (u.flags.y != 0u) {
  119. let pip_w = 0.165;
  120. let pip_h = 0.22;
  121. let pad = 0.012;
  122. let y0 = 1.0 - pad - pip_h;
  123. let y1 = 1.0 - pad;
  124. if (in.uv.y >= y0 && in.uv.y <= y1) {
  125. let py = (in.uv.y - y0) / pip_h;
  126. // Tile 3 (rightmost): 2D Cloud Shadow Map
  127. let x3_0 = 1.0 - pad - pip_w;
  128. // Tile 2: Sun Shadow Map
  129. let x2_0 = x3_0 - pad - pip_w;
  130. // Tile 1: Dual Paraboloid Back Hemisphere
  131. let x1_0 = x2_0 - pad - pip_w;
  132. // Tile 0: Dual Paraboloid Front Hemisphere
  133. let x0_0 = x1_0 - pad - pip_w;
  134. if (in.uv.x >= x0_0 && in.uv.x <= x0_0 + pip_w) {
  135. let puv = vec2<f32>((in.uv.x - x0_0) / pip_w, py);
  136. let dp = textureSampleLevel(dp_tex, linear_clamp_sampler, puv, 0, 0.0).rgb;
  137. ldr = pow(aces_filmic(dp), vec3<f32>(1.0 / 2.2));
  138. } else if (in.uv.x >= x1_0 && in.uv.x <= x1_0 + pip_w) {
  139. let puv = vec2<f32>((in.uv.x - x1_0) / pip_w, py);
  140. let dp = textureSampleLevel(dp_tex, linear_clamp_sampler, puv, 1, 0.0).rgb;
  141. ldr = pow(aces_filmic(dp), vec3<f32>(1.0 / 2.2));
  142. } else if (in.uv.x >= x2_0 && in.uv.x <= x2_0 + pip_w) {
  143. let puv = vec2<f32>((in.uv.x - x2_0) / pip_w, py);
  144. let s_dims = vec2<f32>(textureDimensions(shadow_tex));
  145. let spx = vec2<i32>(clamp(puv * s_dims, vec2<f32>(0.0), s_dims - 1.0));
  146. let d = textureLoad(shadow_tex, spx, 0);
  147. ldr = vec3<f32>(d);
  148. } else if (in.uv.x >= x3_0 && in.uv.x <= x3_0 + pip_w) {
  149. let puv = vec2<f32>((in.uv.x - x3_0) / pip_w, py);
  150. let cs = textureSampleLevel(cloud_shadow_tex, linear_clamp_sampler, puv, 0.0).r;
  151. ldr = vec3<f32>(cs);
  152. }
  153. }
  154. }
  155. return vec4<f32>(ldr, 1.0);
  156. }