@woosh/meep-engine 3.54.0 → 3.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/package.json +1 -1
  2. package/src/shade/RENDERER_CONTRACT.md +5 -2
  3. package/src/shade/device/SHADE_DEVICE_FLOOR.d.ts.map +1 -1
  4. package/src/shade/device/SHADE_DEVICE_FLOOR.js +2 -0
  5. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  6. package/src/shade/renderer/Renderer.js +1 -1
  7. package/src/shade/renderer/hiz/generate_downsampler_code.js +1 -1
  8. package/src/shade/renderer/hiz/shader_hzb_reduce_mip0_clip.js +1 -1
  9. package/src/shade/renderer/postprocess/denoise/fragment_shader_denoise_ao.d.ts.map +1 -1
  10. package/src/shade/renderer/postprocess/denoise/fragment_shader_denoise_ao.js +154 -147
  11. package/src/shade/renderer/postprocess/gtao/GTAO.d.ts +2 -1
  12. package/src/shade/renderer/postprocess/gtao/GTAO.d.ts.map +1 -1
  13. package/src/shade/renderer/postprocess/gtao/GTAO.js +5 -1
  14. package/src/shade/renderer/postprocess/gtao/GTAO_CONSTANTS.d.ts +8 -0
  15. package/src/shade/renderer/postprocess/gtao/GTAO_CONSTANTS.d.ts.map +1 -1
  16. package/src/shade/renderer/postprocess/gtao/GTAO_CONSTANTS.js +10 -1
  17. package/src/shade/renderer/postprocess/gtao/ao_reproject_shader.d.ts.map +1 -1
  18. package/src/shade/renderer/postprocess/gtao/ao_reproject_shader.js +5 -0
  19. package/src/shade/renderer/postprocess/gtao/chunk_gtao_compute_mip_level.d.ts +22 -0
  20. package/src/shade/renderer/postprocess/gtao/chunk_gtao_compute_mip_level.d.ts.map +1 -0
  21. package/src/shade/renderer/postprocess/gtao/chunk_gtao_compute_mip_level.js +33 -0
  22. package/src/shade/renderer/postprocess/gtao/chunk_gtao_resolve_hzb_depth.d.ts +22 -0
  23. package/src/shade/renderer/postprocess/gtao/chunk_gtao_resolve_hzb_depth.d.ts.map +1 -0
  24. package/src/shade/renderer/postprocess/gtao/chunk_gtao_resolve_hzb_depth.js +56 -0
  25. package/src/shade/renderer/postprocess/gtao/chunk_gtao_sample_depth.d.ts +13 -0
  26. package/src/shade/renderer/postprocess/gtao/chunk_gtao_sample_depth.d.ts.map +1 -0
  27. package/src/shade/renderer/postprocess/gtao/chunk_gtao_sample_depth.js +42 -0
  28. package/src/shade/renderer/postprocess/gtao/fragment_shader_gtao.d.ts +2 -1
  29. package/src/shade/renderer/postprocess/gtao/fragment_shader_gtao.d.ts.map +1 -1
  30. package/src/shade/renderer/postprocess/gtao/fragment_shader_gtao.js +382 -357
  31. package/src/shade/renderer/postprocess/gtao/fragment_shader_gtao_with_irradiance.d.ts.map +1 -1
  32. package/src/shade/renderer/postprocess/gtao/fragment_shader_gtao_with_irradiance.js +414 -410
  33. package/src/shade/renderer/postprocess/ssr/prefilter/NOTES.md +0 -7
  34. package/src/shade/renderer/volumetrics/irradiance_clipmap/chunk_irradiance_clipmap_local_light.d.ts +10 -5
  35. package/src/shade/renderer/volumetrics/irradiance_clipmap/chunk_irradiance_clipmap_local_light.d.ts.map +1 -1
  36. package/src/shade/renderer/volumetrics/irradiance_clipmap/chunk_irradiance_clipmap_local_light.js +30 -14
  37. package/src/shade/renderer/volumetrics/irradiance_clipmap/chunk_irradiance_clipmap_sample.d.ts.map +1 -1
  38. package/src/shade/renderer/volumetrics/irradiance_clipmap/chunk_irradiance_clipmap_sample.js +17 -3
  39. package/src/engine/graphics3/decal/DecalAtlas.d.ts +0 -43
  40. package/src/engine/graphics3/decal/DecalAtlas.d.ts.map +0 -1
  41. package/src/engine/graphics3/decal/DecalAtlasFill.d.ts +0 -12
  42. package/src/engine/graphics3/decal/DecalAtlasFill.d.ts.map +0 -1
  43. package/src/engine/graphics3/decal/decal_atlas_border.d.ts +0 -52
  44. package/src/engine/graphics3/decal/decal_atlas_border.d.ts.map +0 -1
  45. package/src/shade/renderer/postprocess/gtao/graph_build_depth_mipmap.d.ts +0 -26
  46. package/src/shade/renderer/postprocess/gtao/graph_build_depth_mipmap.d.ts.map +0 -1
  47. package/src/shade/renderer/postprocess/gtao/graph_build_depth_mipmap.js +0 -268
  48. package/src/shade/renderer/postprocess/ssr/copy_shader_f32.d.ts +0 -3
  49. package/src/shade/renderer/postprocess/ssr/copy_shader_f32.d.ts.map +0 -1
  50. package/src/shade/renderer/postprocess/ssr/copy_shader_f32.js +0 -32
  51. package/src/shade/renderer/postprocess/ssr/depth_max_shader.d.ts +0 -17
  52. package/src/shade/renderer/postprocess/ssr/depth_max_shader.d.ts.map +0 -1
  53. package/src/shade/renderer/postprocess/ssr/depth_max_shader.js +0 -98
  54. package/src/shade/renderer/postprocess/ssr/graph_build_depth_max_pyramid.d.ts +0 -23
  55. package/src/shade/renderer/postprocess/ssr/graph_build_depth_max_pyramid.d.ts.map +0 -1
  56. package/src/shade/renderer/postprocess/ssr/graph_build_depth_max_pyramid.js +0 -118
  57. package/src/shade/renderer/volumetrics/chunk_volumetrics_froxel_sample.d.ts +0 -24
  58. package/src/shade/renderer/volumetrics/chunk_volumetrics_froxel_sample.d.ts.map +0 -1
  59. package/src/shade/renderer/volumetrics/gi/chunk_volumetrics_froxel_centre_ws.d.ts +0 -15
  60. package/src/shade/renderer/volumetrics/gi/chunk_volumetrics_froxel_centre_ws.d.ts.map +0 -1
  61. package/src/shade/renderer/volumetrics/gi/chunk_volumetrics_gi.d.ts +0 -17
  62. package/src/shade/renderer/volumetrics/gi/chunk_volumetrics_gi.d.ts.map +0 -1
  63. package/src/shade/renderer/volumetrics/gi/chunk_volumetrics_gi_sample.d.ts +0 -35
  64. package/src/shade/renderer/volumetrics/gi/chunk_volumetrics_gi_sample.d.ts.map +0 -1
  65. package/src/shade/renderer/volumetrics/gi/graph_build_froxel_gi.d.ts +0 -35
  66. package/src/shade/renderer/volumetrics/gi/graph_build_froxel_gi.d.ts.map +0 -1
  67. package/src/shade/renderer/volumetrics/gi/shader_volumetrics_gi.d.ts +0 -10
  68. package/src/shade/renderer/volumetrics/gi/shader_volumetrics_gi.d.ts.map +0 -1
  69. package/src/shade/renderer/volumetrics/gi/volumetrics_gi_resources.d.ts +0 -28
  70. package/src/shade/renderer/volumetrics/gi/volumetrics_gi_resources.d.ts.map +0 -1
  71. package/src/shade/renderer/volumetrics/graph_build_froxel_irradiance.d.ts +0 -48
  72. package/src/shade/renderer/volumetrics/graph_build_froxel_irradiance.d.ts.map +0 -1
  73. package/src/shade/renderer/volumetrics/shader_volumetrics_build_irradiance.d.ts +0 -28
  74. package/src/shade/renderer/volumetrics/shader_volumetrics_build_irradiance.d.ts.map +0 -1
  75. package/src/shade/renderer/volumetrics/shader_volumetrics_classify_froxels.d.ts +0 -16
  76. package/src/shade/renderer/volumetrics/shader_volumetrics_classify_froxels.d.ts.map +0 -1
  77. package/src/shade/renderer/volumetrics/shader_volumetrics_lighting_regimes.d.ts +0 -23
  78. package/src/shade/renderer/volumetrics/shader_volumetrics_lighting_regimes.d.ts.map +0 -1
@@ -1,410 +1,414 @@
1
- import { BlendState } from "../../../descriptor/blend/BlendState.js";
2
- import { CAMERA_UNIFORM_STRUCT } from "../../camera/CAMERA_UNIFORM_STRUCT.js";
3
- import { DEPTH_STENCIL_FOREGROUND_ONLY } from "../../depth/DEPTH_STENCIL_FOREGROUND_ONLY.js";
4
- import { chunk_direction_view_to_world } from "../../shader/chunk/camera/chunk_direction_view_to_world.js";
5
- import { chunk_direction_world_to_view } from "../../shader/chunk/camera/chunk_direction_world_to_view.js";
6
- import { chunk_project_position_from_depth } from "../../shader/chunk/camera/chunk_project_position_from_depth.js";
7
- import { chunk_rgbe9995_encode } from "../../shader/chunk/color/rgbe9995/chunk_rgbe9995_encode.js";
8
- import { chunk_get_view_space_depth } from "../../shader/chunk/depth/chunk_get_view_space_depth.js";
9
- import { chunk_mat4_extract_position } from "../../shader/chunk/geometry/mat4/chunk_mat4_extract_position.js";
10
- import { chunk_decode_g_buffer_normal } from "../../shader/chunk/geometry/normal/chunk_decode_g_buffer_normal.js";
11
- import { chunk_encode_g_buffer_normal } from "../../shader/chunk/geometry/normal/chunk_encode_g_buffer_normal.js";
12
- import { chunk_v3_matrix4_rotate } from "../../shader/chunk/geometry/vec3/chunk_v3_matrix4_rotate.js";
13
- import { chunk_fast_acos } from "../../shader/chunk/math/fast/chunk_fast_acos.js";
14
- import { PI_DECLARATION } from "../../shader/chunk/math/PI_DECLARATION.js";
15
- import { PI_HALF_DECLARATION } from "../../shader/chunk/math/PI_HALF_DECLARATION.js";
16
- import { chunk_spatio_temporal_noise_r2_64 } from "../../shader/chunk/noise/chunk_spatio_temporal_noise_r2_64.js";
17
- import { CodeChunk } from "../../shader/compiler/CodeChunk.js";
18
- import { ImageShader } from "../../shader/ImageShader.js";
19
- import { ShaderResourceSetDescriptor } from "../../shader/resource/ShaderResourceSetDescriptor.js";
20
- import { ShaderDescriptor } from "../../shader/ShaderDescriptor.js";
21
- import { HILBERT_R2_INDEX_TEXTURE } from "../../texture/noise/HILBERT_R2_INDEX_TEXTURE.js";
22
- import { BLACK_PIXEL_TEXTURE } from "../../texture/util/BLACK_PIXEL_TEXTURE.js";
23
- import { SAMPLER_NEAREST } from "../../texture/SAMPLER_LINEAR_UPSCALE.js";
24
- import { chunk_is_background } from "../../shader/chunk/depth/chunk_is_background.js";
25
- import { chunk_integrate_bent_normal_fast } from "./chunk_integrate_bent_normal_fast.js";
26
- import { chunk_horizon_integrate_lighting_sample } from "./chunk_integrate_lighting_sample.js";
27
- import {
28
- GTAO_EFFECT_FALLOFF_RANGE,
29
- GTAO_EFFECT_RADIUS,
30
- GTAO_QUALITY_PROFILES,
31
- GTAO_QUALITY_TYPE
32
- } from "./GTAO_CONSTANTS.js";
33
- import { GTAO_RENDER_SETTINGS } from "./GTAO_RENDER_SETTINGS.js";
34
-
35
- const resources = new ShaderResourceSetDescriptor();
36
-
37
- // see fragment_shader_gtao — the same per-pass settings, carried as immediate data
38
- resources.setImmediate("settings", GTAO_RENDER_SETTINGS);
39
- resources.createGroup()
40
- .addTexture("tDepth")
41
- .addSampler("sPoint", "non-filtering", SAMPLER_NEAREST)
42
- .addTexture("tNormal", "uint")
43
- .addTexture("tColor", "unfilterable-float", "2d", false, BLACK_PIXEL_TEXTURE)
44
- .addTexture("tNoise", "uint", "2d", false, HILBERT_R2_INDEX_TEXTURE)
45
- .addUniform("camera", CAMERA_UNIFORM_STRUCT)
46
- ;
47
-
48
- /**
49
- * One step below the AO-only shader's Medium, because every sample here also integrates incoming
50
- * radiance. A deliberate difference, unlike the ones this fork used to carry by accident.
51
- * @type {{slices:number,steps:number}}
52
- */
53
- const QUALITY_SETTINGS = GTAO_QUALITY_PROFILES[GTAO_QUALITY_TYPE.Low];
54
-
55
- // this is our own thickness heuristic that relies on sooner discarding samples behind the center
56
- const GTAO_DEFAULT_THIN_OCCLUDER_COMPENSATION = 0.0;
57
-
58
- /*
59
- based on https://github.com/GameTechDev/XeGTAO/tree/e7698f874e90f2516fca26c696ec3cd2c70e505a
60
- */
61
- const body = CodeChunk.from(
62
- //language=WGSL
63
- `
64
- const scale = 1.0;
65
-
66
- const thinOccluderCompensation = ${GTAO_DEFAULT_THIN_OCCLUDER_COMPENSATION};
67
-
68
- // small crevices more important than big surfaces
69
- const sampleDistributionPower = 2.0;
70
-
71
- const SLICE_COUNT = ${QUALITY_SETTINGS.slices};
72
-
73
- const STEPS = ${QUALITY_SETTINGS.steps};
74
-
75
- struct Output{
76
- @location(0) visibility: vec2<f32>, // first 2 moment
77
- @location(1) bent_normal: vec2<u32>, // octahedral encoding
78
- @location(2) irradiance: u32, // rgbe9995
79
- }
80
-
81
- @fragment
82
- fn main(
83
- @builtin(position) coord: vec4<f32>,
84
- @location(0) uv: vec2<f32>,
85
- ) -> Output {
86
-
87
- // fadeout precompute optimisation
88
- const falloffRange = ${GTAO_EFFECT_FALLOFF_RANGE} * ${GTAO_EFFECT_RADIUS};
89
-
90
- const falloffFrom = ${GTAO_EFFECT_RADIUS} * (1.0 - ${GTAO_EFFECT_FALLOFF_RANGE});
91
- const falloffMul = -1.0 / ( falloffRange );
92
- const falloffAdd = falloffFrom / ( falloffRange ) + 1.0;
93
-
94
- let texel_position_u32 = vec2<u32>(coord.xy);
95
-
96
- let ViewportSize = textureDimensions(tDepth);
97
- let ViewportPixelSize = 1. / vec2<f32>(ViewportSize);
98
-
99
- var device_depth = textureLoad(tDepth, texel_position_u32, 0).r;
100
-
101
- // NOTE: no need to reject based on depth, already handled via depthStencil
102
-
103
- var viewspaceZ = get_view_space_depth(device_depth, camera);
104
-
105
- // Move center pixel slightly towards camera to avoid imprecision artifacts due to depth buffer imprecision; offset depends on depth texture format used
106
- viewspaceZ *= 0.99999; // this is good for FP32 depth buffer
107
-
108
- let position_ws = project_position_from_depth(uv, device_depth, camera.view_projection_matrix_inverse );
109
- let view_position_ws = mat4_extract_position(camera.view_matrix_inverse);
110
- let view_direction_ws = normalize(view_position_ws - position_ws);
111
-
112
- let view_normal_ws = decode_g_buffer_normal(textureLoad(tNormal, texel_position_u32, 0).xy); // shading normal
113
- let view_normal_vs = direction_world_to_view(view_normal_ws, camera);
114
-
115
- let noiseTexel = spatio_temporal_noise_r2_64(texel_position_u32, settings.frame_index, tNoise);
116
-
117
- let noiseSample = noiseTexel.x;
118
- let noiseSlice = noiseTexel.y;
119
-
120
- // quality settings / tweaks / hacks
121
- const pixelTooCloseThreshold = 1.3; // if the offset is under approx pixel size (pixelTooCloseThreshold), push it out to the minimum distance
122
-
123
- // approx viewspace pixel size at pixCoord; approximation of NDCToViewspace( normalizedScreenPos.xy + consts.ViewportPixelSize.xy, pixCenterPos.z ).xy - pixCenterPos.xy;
124
- // device_depth_to_view_space.z = 1/a = aspect * tan(fovY/2) = tan(fovX/2), pairs with horizontal pixel size.
125
- // factor of 2 converts a single uv-step (= 1 pixel) into the matching ndc-step (range 2 wide).
126
- // This read .w — tan(fovY/2) — with no factor of 2, i.e. 2*aspect too small, 3.56x at 16:9, which
127
- // put every AO sample that much further out than the effect radius asked for.
128
- let NDCToViewMul_x_PixelSize = 2.0 * camera.device_depth_to_view_space.z * ViewportPixelSize.x;
129
- let pixelDirRBViewspaceSizeAtCenterZ = viewspaceZ * NDCToViewMul_x_PixelSize;
130
-
131
- let screenspaceRadius = abs( ${GTAO_EFFECT_RADIUS} / pixelDirRBViewspaceSizeAtCenterZ);
132
-
133
- // fade out for small screen radii
134
- var visibility = saturate((10 - screenspaceRadius)/100)*0.5;
135
- var bent_normal:vec3<f32>;
136
- var lighting: vec3<f32>;
137
- var lighting_weight_total = 0.0;
138
-
139
- // this is the min distance to start sampling from to avoid sampling from the center pixel (no useful data obtained from sampling center pixel)
140
- let minS = pixelTooCloseThreshold / screenspaceRadius;
141
-
142
- const INV_SLICE_COUNT = 1.0 / f32(SLICE_COUNT);
143
-
144
- for (var slice = 0; slice < SLICE_COUNT; slice++) {
145
-
146
- let sliceK = ( f32(slice) + noiseSlice ) * INV_SLICE_COUNT;
147
-
148
- // lines 5,6 from the paper
149
- let phi = sliceK * PI; // slice angle
150
-
151
- let cosPhi = cos(phi);
152
- let sinPhi = sin(phi);
153
-
154
- let omega = vec2(cosPhi, -sinPhi) * screenspaceRadius;
155
-
156
- // line 8 from the paper
157
- let sliceViewDir = vec3(cosPhi, sinPhi, 0.0);
158
- let sliceWorldDir = v3_matrix4_rotate(sliceViewDir, camera.view_matrix_inverse);
159
-
160
- // line 9 from the paper
161
- // NOTE: optimized with FMA, readability suffers
162
- let orthoWorldDir = fma( vec3( -dot(sliceWorldDir, view_direction_ws) ), view_direction_ws, sliceWorldDir );
163
-
164
- // line 10 from the paper
165
- // axisVec is orthogonal to sliceViewDir and viewVec, used to define projectedNormal
166
- let axisVec = normalize( cross(orthoWorldDir, view_direction_ws) );
167
-
168
- // line 11 from paper
169
- let projectedWorldNormal = fma( -axisVec, vec3( dot(view_normal_ws, axisVec) ), view_normal_ws );
170
-
171
- // line 13 from the paper
172
- let signNorm = sign( dot( orthoWorldDir, projectedWorldNormal ) );
173
-
174
- // line 14 from the paper
175
- var projectedNormalVecLength = length(projectedWorldNormal);
176
- let norm_cos = saturate( dot( projectedWorldNormal, view_direction_ws ) / projectedNormalVecLength );
177
-
178
- // line 15 from the paper
179
- let n = signNorm * fast_acos(norm_cos);
180
-
181
- // this is a lower weight target; not using -1 as in the original paper because it is under horizon, so a 'weight' has different meaning based on the normal
182
- let lowHorizonCos0 = cos(n + PI_HALF);
183
- let lowHorizonCos1 = -lowHorizonCos0; // cos(n - PI_HALF);
184
-
185
- // lines 17, 18 from the paper, manually unrolled the 'side' loop
186
- var horizonCos0 = lowHorizonCos0; // -1
187
- var horizonCos1 = lowHorizonCos1; // -1
188
-
189
- const INV_STEPS = 1.0 / f32(STEPS);
190
-
191
- // Calculate lighting integrant factors correctly in view space.
192
- let light_integrant_front = 0.5 * vec2(dot(view_normal_vs.xy, sliceViewDir.xy), view_normal_vs.z);
193
- // The back-side integrant has a flipped X component because the slice direction is flipped.
194
- let light_integrant_back = vec2(-light_integrant_front.x, light_integrant_front.y);
195
-
196
- var sliceLighting: vec3<f32>;
197
-
198
- for (var step_index = 0; step_index < STEPS; step_index++) {
199
-
200
- // R1 sequence (http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
201
- // Standard multiplier is 0.6180339887498948482 = ( 1 / GOLDEN_RATION )
202
- let stepBaseNoise = f32(slice + step_index * STEPS) * 0.6180339887498948482;
203
-
204
- let stepNoise = fract(noiseSample + stepBaseNoise);
205
-
206
- // approx line 20 from the paper, with added noise
207
- var s = ( f32(step_index) + stepNoise ) * INV_STEPS;
208
-
209
- // additional distribution modifier
210
- s = pow( s, sampleDistributionPower );
211
-
212
- // avoid sampling center pixel
213
- s += minS;
214
-
215
- // approx lines 21-22 from the paper, unrolled
216
- var sampleOffset = s * omega;
217
-
218
- let sampleOffsetLength = length( sampleOffset );
219
-
220
- // note: when sampling, using point_point_point or point_point_linear sampler works, but linear_linear_linear will cause unwanted interpolation between neighbouring depth values on the same MIP level!
221
- const mipLevel = 0; // Disabled currently
222
-
223
- // Snap to pixel center (more correct direction math, avoids artifacts due to sampling pos not matching depth texel center - messes up slope - but adds other
224
- // artifacts due to them being pushed off the slice). Also use full precision for high res cases.
225
- sampleOffset = round( sampleOffset ) * ViewportPixelSize;
226
-
227
- let sampleScreenPos0 = uv + sampleOffset;
228
-
229
- let SZ0 = textureSampleLevel(tDepth, sPoint, sampleScreenPos0, mipLevel).x;
230
- let samplePos0 = project_position_from_depth(sampleScreenPos0, SZ0, camera.view_projection_matrix_inverse);
231
-
232
- let sampleScreenPos1 = uv - sampleOffset;
233
- let SZ1 = textureSampleLevel(tDepth, sPoint, sampleScreenPos1, mipLevel).x;
234
- let samplePos1 = project_position_from_depth(sampleScreenPos1, SZ1, camera.view_projection_matrix_inverse);
235
-
236
- let sampleDelta0 = (samplePos0 - position_ws); // using lpfloat for sampleDelta causes precision issues
237
- let sampleDelta1 = (samplePos1 - position_ws); // using lpfloat for sampleDelta causes precision issues
238
-
239
- let sampleDist0 = length( sampleDelta0 );
240
- let sampleDist1 = length( sampleDelta1 );
241
-
242
- // approx lines 23, 24 from the paper, unrolled
243
- let sampleHorizonVec0 = sampleDelta0 / sampleDist0;
244
- let sampleHorizonVec1 = sampleDelta1 / sampleDist1;
245
-
246
- let weight0 = saturate( fma(sampleDist0, falloffMul, falloffAdd) );
247
- let weight1 = saturate( fma(sampleDist1, falloffMul, falloffAdd) );
248
-
249
- // sample horizon cos
250
- var shc0 = dot( sampleHorizonVec0, view_direction_ws );
251
- var shc1 = dot( sampleHorizonVec1, view_direction_ws );
252
-
253
- // discard unwanted samples
254
- shc0 = mix( lowHorizonCos0, shc0, weight0 ); // this would be more correct but too expensive: cos(lerp( acos(lowHorizonCos0), acos(shc0), weight0 ));
255
- shc1 = mix( lowHorizonCos1, shc1, weight1 ); // this would be more correct but too expensive: cos(lerp( acos(lowHorizonCos1), acos(shc1), weight1 ));
256
-
257
- if(shc0 > horizonCos0 && !is_background(SZ0) ){
258
-
259
- let s0 = horizon_integrate_lighting_sample(
260
- sampleScreenPos0,
261
- view_normal_ws, // ref_normal
262
- sampleHorizonVec0, // ref_direction
263
- horizonCos0, // h0 (previous horizon)
264
- shc0, // h1 (new horizon)
265
- light_integrant_back
266
- );
267
-
268
- sliceLighting += s0;
269
- }
270
-
271
- if(shc1 > horizonCos1 && !is_background(SZ1) ){
272
-
273
- let s1 = horizon_integrate_lighting_sample(
274
- sampleScreenPos1,
275
- view_normal_ws, // ref_normal
276
- sampleHorizonVec1, // ref_direction
277
- horizonCos1, // h0 (previous horizon)
278
- shc1, // h1 (new horizon)
279
- light_integrant_front
280
- );
281
-
282
- sliceLighting += s1;
283
- }
284
-
285
- // this is a version where thicknessHeuristic is completely disabled
286
- horizonCos0 = max( horizonCos0, shc0 );
287
- horizonCos1 = max( horizonCos1, shc1 );
288
-
289
- }
290
-
291
- // line ~27, unrolled
292
- let h0 = -fast_acos(horizonCos1);
293
- let h1 = fast_acos(horizonCos0);
294
-
295
- let norm_sin = sin(n);
296
- let norm_sin2 = 2.0 * norm_sin;
297
-
298
- let iarc0 = ( fma( h0 , norm_sin2, norm_cos ) - cos( fma( 2.0, h0, - n)) ) * 0.25;
299
- let iarc1 = ( fma( h1 , norm_sin2, norm_cos ) - cos( fma( 2.0, h1, - n)) ) * 0.25;
300
-
301
- // Slight overdarkening on high slopes is compensated by tilting projected length toward 1.
302
- // In the training set, 0.05 is close (PSNR 21.34) to disabled (PSNR 21.45).
303
- // Scoped to the visibility branch so bent normals keep their unfudged slice weighting — this
304
- // used to assign back into projectedNormalVecLength, so the bent-normal accumulation below got
305
- // the fudged weight too.
306
- let visibilityProjLength = mix( projectedNormalVecLength, 1, 0.05 );
307
-
308
- let localVisibility = visibilityProjLength * ( iarc0 + iarc1 );
309
-
310
- visibility += localVisibility;
311
-
312
- // unfudged, like the bent normal below: the fudge corrects the visibility integral only
313
- sliceLighting *= projectedNormalVecLength;
314
-
315
- lighting += sliceLighting;
316
-
317
- // see "Algorithm 2 Extension that computes bent normals b."
318
- let slice_tangent_ws = normalize(orthoWorldDir);
319
- let local_bent_ts = integrate_bent_normal(
320
- horizonCos1,
321
- horizonCos0,
322
- norm_cos,
323
- norm_sin,
324
- );
325
-
326
- let t0 = local_bent_ts.x;
327
- let t1 = local_bent_ts.y;
328
-
329
- var slice_bent_normal_ws = slice_tangent_ws * t0 + view_direction_ws * t1;
330
-
331
- bent_normal = fma(slice_bent_normal_ws, vec3(projectedNormalVecLength), bent_normal);
332
- }
333
-
334
- visibility *= INV_SLICE_COUNT;
335
-
336
- visibility = pow(visibility, scale);
337
-
338
- // disallow total occlusion (which wouldn't make any sense anyhow since pixel is visible but also helps with packing bent normals)
339
- visibility = max(0.03, visibility);
340
-
341
- bent_normal = normalize(bent_normal);
342
-
343
- lighting *= PI * INV_SLICE_COUNT;
344
-
345
- var out: Output;
346
- out.visibility = vec2(visibility, visibility * visibility);
347
- out.bent_normal = encode_g_buffer_normal( bent_normal );
348
- out.irradiance = rgbe9995_encode( lighting );
349
-
350
- return out;
351
- }
352
- `, [
353
- chunk_get_view_space_depth,
354
- PI_DECLARATION,
355
- PI_HALF_DECLARATION,
356
-
357
- chunk_decode_g_buffer_normal,
358
- chunk_encode_g_buffer_normal,
359
- chunk_spatio_temporal_noise_r2_64,
360
-
361
- chunk_fast_acos,
362
-
363
- chunk_direction_world_to_view,
364
- chunk_direction_view_to_world,
365
- chunk_is_background,
366
- chunk_mat4_extract_position,
367
- chunk_project_position_from_depth,
368
- chunk_v3_matrix4_rotate,
369
- chunk_integrate_bent_normal_fast,
370
-
371
- //
372
- chunk_rgbe9995_encode,
373
- chunk_horizon_integrate_lighting_sample,
374
- ]);
375
-
376
- export const fragment_shader_gtao_with_irradiance = new ImageShader({
377
- descriptor: ShaderDescriptor.from(
378
- {
379
- label: "GTAO",
380
- resources,
381
- body
382
- },
383
- ),
384
- depthStencil: DEPTH_STENCIL_FOREGROUND_ONLY,
385
- targets: [
386
- {
387
- label: "Occlusion",
388
- // first 2 moments of visibility
389
- format: "rg16float",
390
- clearValue: [1, 1], // Stores visibility, so 1 == not occluded
391
- blend: BlendState.from({
392
- color: {
393
- operation: "add",
394
- srcFactor: "one",
395
- dstFactor: "zero",
396
- }
397
- })
398
- },
399
- {
400
- label: "Bent Normals",
401
- // Direction is encoded with octahedral
402
- format: "rg16uint",
403
- },
404
- {
405
- label: "Irradiance",
406
- // encoded as RGBE9995
407
- format:"r32uint"
408
- }
409
- ]
410
- });
1
+ import { BlendState } from "../../../descriptor/blend/BlendState.js";
2
+ import { CAMERA_UNIFORM_STRUCT } from "../../camera/CAMERA_UNIFORM_STRUCT.js";
3
+ import { DEPTH_STENCIL_FOREGROUND_ONLY } from "../../depth/DEPTH_STENCIL_FOREGROUND_ONLY.js";
4
+ import { chunk_direction_view_to_world } from "../../shader/chunk/camera/chunk_direction_view_to_world.js";
5
+ import { chunk_direction_world_to_view } from "../../shader/chunk/camera/chunk_direction_world_to_view.js";
6
+ import { chunk_project_position_from_depth } from "../../shader/chunk/camera/chunk_project_position_from_depth.js";
7
+ import { chunk_rgbe9995_encode } from "../../shader/chunk/color/rgbe9995/chunk_rgbe9995_encode.js";
8
+ import { chunk_get_view_space_depth } from "../../shader/chunk/depth/chunk_get_view_space_depth.js";
9
+ import { chunk_mat4_extract_position } from "../../shader/chunk/geometry/mat4/chunk_mat4_extract_position.js";
10
+ import { chunk_decode_g_buffer_normal } from "../../shader/chunk/geometry/normal/chunk_decode_g_buffer_normal.js";
11
+ import { chunk_encode_g_buffer_normal } from "../../shader/chunk/geometry/normal/chunk_encode_g_buffer_normal.js";
12
+ import { chunk_v3_matrix4_rotate } from "../../shader/chunk/geometry/vec3/chunk_v3_matrix4_rotate.js";
13
+ import { chunk_fast_acos } from "../../shader/chunk/math/fast/chunk_fast_acos.js";
14
+ import { PI_DECLARATION } from "../../shader/chunk/math/PI_DECLARATION.js";
15
+ import { PI_HALF_DECLARATION } from "../../shader/chunk/math/PI_HALF_DECLARATION.js";
16
+ import { chunk_spatio_temporal_noise_r2_64 } from "../../shader/chunk/noise/chunk_spatio_temporal_noise_r2_64.js";
17
+ import { CodeChunk } from "../../shader/compiler/CodeChunk.js";
18
+ import { ImageShader } from "../../shader/ImageShader.js";
19
+ import { ShaderResourceSetDescriptor } from "../../shader/resource/ShaderResourceSetDescriptor.js";
20
+ import { ShaderDescriptor } from "../../shader/ShaderDescriptor.js";
21
+ import { HILBERT_R2_INDEX_TEXTURE } from "../../texture/noise/HILBERT_R2_INDEX_TEXTURE.js";
22
+ import { BLACK_PIXEL_TEXTURE } from "../../texture/util/BLACK_PIXEL_TEXTURE.js";
23
+ import { SAMPLER_NEAREST } from "../../texture/SAMPLER_LINEAR_UPSCALE.js";
24
+ import { chunk_is_background } from "../../shader/chunk/depth/chunk_is_background.js";
25
+ import { chunk_gtao_compute_mip_level } from "./chunk_gtao_compute_mip_level.js";
26
+ import { chunk_gtao_sample_depth } from "./chunk_gtao_sample_depth.js";
27
+ import { chunk_integrate_bent_normal_fast } from "./chunk_integrate_bent_normal_fast.js";
28
+ import { chunk_horizon_integrate_lighting_sample } from "./chunk_integrate_lighting_sample.js";
29
+ import {
30
+ GTAO_EFFECT_FALLOFF_RANGE,
31
+ GTAO_EFFECT_RADIUS,
32
+ GTAO_QUALITY_PROFILES,
33
+ GTAO_QUALITY_TYPE
34
+ } from "./GTAO_CONSTANTS.js";
35
+ import { GTAO_RENDER_SETTINGS } from "./GTAO_RENDER_SETTINGS.js";
36
+
37
+ const resources = new ShaderResourceSetDescriptor();
38
+
39
+ // see fragment_shader_gtao — the same per-pass settings, carried as immediate data
40
+ resources.setImmediate("settings", GTAO_RENDER_SETTINGS);
41
+ resources.createGroup()
42
+ .addTexture("tDepth")
43
+ .addTexture("tHZB")
44
+ .addSampler("sPoint", "non-filtering", SAMPLER_NEAREST)
45
+ .addTexture("tNormal", "uint")
46
+ .addTexture("tColor", "unfilterable-float", "2d", false, BLACK_PIXEL_TEXTURE)
47
+ .addTexture("tNoise", "uint", "2d", false, HILBERT_R2_INDEX_TEXTURE)
48
+ .addUniform("camera", CAMERA_UNIFORM_STRUCT)
49
+ ;
50
+
51
+ /**
52
+ * One step below the AO-only shader's Medium, because every sample here also integrates incoming
53
+ * radiance. A deliberate difference, unlike the ones this fork used to carry by accident.
54
+ * @type {{slices:number,steps:number}}
55
+ */
56
+ const QUALITY_SETTINGS = GTAO_QUALITY_PROFILES[GTAO_QUALITY_TYPE.Low];
57
+
58
+ // this is our own thickness heuristic that relies on sooner discarding samples behind the center
59
+ const GTAO_DEFAULT_THIN_OCCLUDER_COMPENSATION = 0.0;
60
+
61
+ /*
62
+ based on https://github.com/GameTechDev/XeGTAO/tree/e7698f874e90f2516fca26c696ec3cd2c70e505a
63
+ */
64
+ const body = CodeChunk.from(
65
+ //language=WGSL
66
+ `
67
+ const scale = 1.0;
68
+
69
+ const thinOccluderCompensation = ${GTAO_DEFAULT_THIN_OCCLUDER_COMPENSATION};
70
+
71
+ // small crevices more important than big surfaces
72
+ const sampleDistributionPower = 2.0;
73
+
74
+ const SLICE_COUNT = ${QUALITY_SETTINGS.slices};
75
+
76
+ const STEPS = ${QUALITY_SETTINGS.steps};
77
+
78
+ struct Output{
79
+ @location(0) visibility: vec2<f32>, // first 2 moment
80
+ @location(1) bent_normal: vec2<u32>, // octahedral encoding
81
+ @location(2) irradiance: u32, // rgbe9995
82
+ }
83
+
84
+ @fragment
85
+ fn main(
86
+ @builtin(position) coord: vec4<f32>,
87
+ @location(0) uv: vec2<f32>,
88
+ ) -> Output {
89
+
90
+ // fadeout precompute optimisation
91
+ const falloffRange = ${GTAO_EFFECT_FALLOFF_RANGE} * ${GTAO_EFFECT_RADIUS};
92
+
93
+ const falloffFrom = ${GTAO_EFFECT_RADIUS} * (1.0 - ${GTAO_EFFECT_FALLOFF_RANGE});
94
+ const falloffMul = -1.0 / ( falloffRange );
95
+ const falloffAdd = falloffFrom / ( falloffRange ) + 1.0;
96
+
97
+ let texel_position_u32 = vec2<u32>(coord.xy);
98
+
99
+ let ViewportSize = textureDimensions(tDepth);
100
+ let ViewportPixelSize = 1. / vec2<f32>(ViewportSize);
101
+
102
+ var device_depth = textureLoad(tDepth, texel_position_u32, 0).r;
103
+
104
+ // NOTE: no need to reject based on depth, already handled via depthStencil
105
+
106
+ var viewspaceZ = get_view_space_depth(device_depth, camera);
107
+
108
+ // Move center pixel slightly towards camera to avoid imprecision artifacts due to depth buffer imprecision; offset depends on depth texture format used
109
+ viewspaceZ *= 0.99999; // this is good for FP32 depth buffer
110
+
111
+ let position_ws = project_position_from_depth(uv, device_depth, camera.view_projection_matrix_inverse );
112
+ let view_position_ws = mat4_extract_position(camera.view_matrix_inverse);
113
+ let view_direction_ws = normalize(view_position_ws - position_ws);
114
+
115
+ let view_normal_ws = decode_g_buffer_normal(textureLoad(tNormal, texel_position_u32, 0).xy); // shading normal
116
+ let view_normal_vs = direction_world_to_view(view_normal_ws, camera);
117
+
118
+ let noiseTexel = spatio_temporal_noise_r2_64(texel_position_u32, settings.frame_index, tNoise);
119
+
120
+ let noiseSample = noiseTexel.x;
121
+ let noiseSlice = noiseTexel.y;
122
+
123
+ // quality settings / tweaks / hacks
124
+ const pixelTooCloseThreshold = 1.3; // if the offset is under approx pixel size (pixelTooCloseThreshold), push it out to the minimum distance
125
+
126
+ // approx viewspace pixel size at pixCoord; approximation of NDCToViewspace( normalizedScreenPos.xy + consts.ViewportPixelSize.xy, pixCenterPos.z ).xy - pixCenterPos.xy;
127
+ // device_depth_to_view_space.z = 1/a = aspect * tan(fovY/2) = tan(fovX/2), pairs with horizontal pixel size.
128
+ // factor of 2 converts a single uv-step (= 1 pixel) into the matching ndc-step (range 2 wide).
129
+ // This read .w — tan(fovY/2) — with no factor of 2, i.e. 2*aspect too small, 3.56x at 16:9, which
130
+ // put every AO sample that much further out than the effect radius asked for.
131
+ let NDCToViewMul_x_PixelSize = 2.0 * camera.device_depth_to_view_space.z * ViewportPixelSize.x;
132
+ let pixelDirRBViewspaceSizeAtCenterZ = viewspaceZ * NDCToViewMul_x_PixelSize;
133
+
134
+ let screenspaceRadius = abs( ${GTAO_EFFECT_RADIUS} / pixelDirRBViewspaceSizeAtCenterZ);
135
+
136
+ // fade out for small screen radii
137
+ var visibility = saturate((10 - screenspaceRadius)/100)*0.5;
138
+ var bent_normal:vec3<f32>;
139
+ var lighting: vec3<f32>;
140
+ var lighting_weight_total = 0.0;
141
+
142
+ // this is the min distance to start sampling from to avoid sampling from the center pixel (no useful data obtained from sampling center pixel)
143
+ let minS = pixelTooCloseThreshold / screenspaceRadius;
144
+
145
+ const INV_SLICE_COUNT = 1.0 / f32(SLICE_COUNT);
146
+
147
+ for (var slice = 0; slice < SLICE_COUNT; slice++) {
148
+
149
+ let sliceK = ( f32(slice) + noiseSlice ) * INV_SLICE_COUNT;
150
+
151
+ // lines 5,6 from the paper
152
+ let phi = sliceK * PI; // slice angle
153
+
154
+ let cosPhi = cos(phi);
155
+ let sinPhi = sin(phi);
156
+
157
+ let omega = vec2(cosPhi, -sinPhi) * screenspaceRadius;
158
+
159
+ // line 8 from the paper
160
+ let sliceViewDir = vec3(cosPhi, sinPhi, 0.0);
161
+ let sliceWorldDir = v3_matrix4_rotate(sliceViewDir, camera.view_matrix_inverse);
162
+
163
+ // line 9 from the paper
164
+ // NOTE: optimized with FMA, readability suffers
165
+ let orthoWorldDir = fma( vec3( -dot(sliceWorldDir, view_direction_ws) ), view_direction_ws, sliceWorldDir );
166
+
167
+ // line 10 from the paper
168
+ // axisVec is orthogonal to sliceViewDir and viewVec, used to define projectedNormal
169
+ let axisVec = normalize( cross(orthoWorldDir, view_direction_ws) );
170
+
171
+ // line 11 from paper
172
+ let projectedWorldNormal = fma( -axisVec, vec3( dot(view_normal_ws, axisVec) ), view_normal_ws );
173
+
174
+ // line 13 from the paper
175
+ let signNorm = sign( dot( orthoWorldDir, projectedWorldNormal ) );
176
+
177
+ // line 14 from the paper
178
+ var projectedNormalVecLength = length(projectedWorldNormal);
179
+ let norm_cos = saturate( dot( projectedWorldNormal, view_direction_ws ) / projectedNormalVecLength );
180
+
181
+ // line 15 from the paper
182
+ let n = signNorm * fast_acos(norm_cos);
183
+
184
+ // this is a lower weight target; not using -1 as in the original paper because it is under horizon, so a 'weight' has different meaning based on the normal
185
+ let lowHorizonCos0 = cos(n + PI_HALF);
186
+ let lowHorizonCos1 = -lowHorizonCos0; // cos(n - PI_HALF);
187
+
188
+ // lines 17, 18 from the paper, manually unrolled the 'side' loop
189
+ var horizonCos0 = lowHorizonCos0; // -1
190
+ var horizonCos1 = lowHorizonCos1; // -1
191
+
192
+ const INV_STEPS = 1.0 / f32(STEPS);
193
+
194
+ // Calculate lighting integrant factors correctly in view space.
195
+ let light_integrant_front = 0.5 * vec2(dot(view_normal_vs.xy, sliceViewDir.xy), view_normal_vs.z);
196
+ // The back-side integrant has a flipped X component because the slice direction is flipped.
197
+ let light_integrant_back = vec2(-light_integrant_front.x, light_integrant_front.y);
198
+
199
+ var sliceLighting: vec3<f32>;
200
+
201
+ for (var step_index = 0; step_index < STEPS; step_index++) {
202
+
203
+ // R1 sequence (http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
204
+ // Standard multiplier is 0.6180339887498948482 = ( 1 / GOLDEN_RATION )
205
+ let stepBaseNoise = f32(slice + step_index * STEPS) * 0.6180339887498948482;
206
+
207
+ let stepNoise = fract(noiseSample + stepBaseNoise);
208
+
209
+ // approx line 20 from the paper, with added noise
210
+ var s = ( f32(step_index) + stepNoise ) * INV_STEPS;
211
+
212
+ // additional distribution modifier
213
+ s = pow( s, sampleDistributionPower );
214
+
215
+ // avoid sampling center pixel
216
+ s += minS;
217
+
218
+ // approx lines 21-22 from the paper, unrolled
219
+ var sampleOffset = s * omega;
220
+
221
+ // omega = vec2(cosPhi, -sinPhi) * screenspaceRadius has length screenspaceRadius
222
+ let sampleOffsetLength = s * screenspaceRadius;
223
+ let mipLevel = gtao_compute_mip_level(sampleOffsetLength);
224
+
225
+ // Snap to pixel center (more correct direction math, avoids artifacts due to sampling pos not matching depth texel center - messes up slope - but adds other
226
+ // artifacts due to them being pushed off the slice). Also use full precision for high res cases.
227
+ sampleOffset = round( sampleOffset ) * ViewportPixelSize;
228
+
229
+ let sampleScreenPos0 = uv + sampleOffset;
230
+
231
+ let SZ0 = gtao_sample_depth(sampleScreenPos0, mipLevel, camera);
232
+ let samplePos0 = project_position_from_depth(sampleScreenPos0, SZ0, camera.view_projection_matrix_inverse);
233
+
234
+ let sampleScreenPos1 = uv - sampleOffset;
235
+ let SZ1 = gtao_sample_depth(sampleScreenPos1, mipLevel, camera);
236
+ let samplePos1 = project_position_from_depth(sampleScreenPos1, SZ1, camera.view_projection_matrix_inverse);
237
+
238
+ let sampleDelta0 = (samplePos0 - position_ws); // using lpfloat for sampleDelta causes precision issues
239
+ let sampleDelta1 = (samplePos1 - position_ws); // using lpfloat for sampleDelta causes precision issues
240
+
241
+ let sampleDist0 = length( sampleDelta0 );
242
+ let sampleDist1 = length( sampleDelta1 );
243
+
244
+ // approx lines 23, 24 from the paper, unrolled
245
+ let sampleHorizonVec0 = sampleDelta0 / sampleDist0;
246
+ let sampleHorizonVec1 = sampleDelta1 / sampleDist1;
247
+
248
+ let weight0 = saturate( fma(sampleDist0, falloffMul, falloffAdd) );
249
+ let weight1 = saturate( fma(sampleDist1, falloffMul, falloffAdd) );
250
+
251
+ // sample horizon cos
252
+ var shc0 = dot( sampleHorizonVec0, view_direction_ws );
253
+ var shc1 = dot( sampleHorizonVec1, view_direction_ws );
254
+
255
+ // discard unwanted samples
256
+ shc0 = mix( lowHorizonCos0, shc0, weight0 ); // this would be more correct but too expensive: cos(lerp( acos(lowHorizonCos0), acos(shc0), weight0 ));
257
+ shc1 = mix( lowHorizonCos1, shc1, weight1 ); // this would be more correct but too expensive: cos(lerp( acos(lowHorizonCos1), acos(shc1), weight1 ));
258
+
259
+ if(shc0 > horizonCos0 && !is_background(SZ0) ){
260
+
261
+ let s0 = horizon_integrate_lighting_sample(
262
+ sampleScreenPos0,
263
+ view_normal_ws, // ref_normal
264
+ sampleHorizonVec0, // ref_direction
265
+ horizonCos0, // h0 (previous horizon)
266
+ shc0, // h1 (new horizon)
267
+ light_integrant_back
268
+ );
269
+
270
+ sliceLighting += s0;
271
+ }
272
+
273
+ if(shc1 > horizonCos1 && !is_background(SZ1) ){
274
+
275
+ let s1 = horizon_integrate_lighting_sample(
276
+ sampleScreenPos1,
277
+ view_normal_ws, // ref_normal
278
+ sampleHorizonVec1, // ref_direction
279
+ horizonCos1, // h0 (previous horizon)
280
+ shc1, // h1 (new horizon)
281
+ light_integrant_front
282
+ );
283
+
284
+ sliceLighting += s1;
285
+ }
286
+
287
+ // this is a version where thicknessHeuristic is completely disabled
288
+ horizonCos0 = max( horizonCos0, shc0 );
289
+ horizonCos1 = max( horizonCos1, shc1 );
290
+
291
+ }
292
+
293
+ // line ~27, unrolled
294
+ let h0 = -fast_acos(horizonCos1);
295
+ let h1 = fast_acos(horizonCos0);
296
+
297
+ let norm_sin = sin(n);
298
+ let norm_sin2 = 2.0 * norm_sin;
299
+
300
+ let iarc0 = ( fma( h0 , norm_sin2, norm_cos ) - cos( fma( 2.0, h0, - n)) ) * 0.25;
301
+ let iarc1 = ( fma( h1 , norm_sin2, norm_cos ) - cos( fma( 2.0, h1, - n)) ) * 0.25;
302
+
303
+ // Slight overdarkening on high slopes is compensated by tilting projected length toward 1.
304
+ // In the training set, 0.05 is close (PSNR 21.34) to disabled (PSNR 21.45).
305
+ // Scoped to the visibility branch so bent normals keep their unfudged slice weighting — this
306
+ // used to assign back into projectedNormalVecLength, so the bent-normal accumulation below got
307
+ // the fudged weight too.
308
+ let visibilityProjLength = mix( projectedNormalVecLength, 1, 0.05 );
309
+
310
+ let localVisibility = visibilityProjLength * ( iarc0 + iarc1 );
311
+
312
+ visibility += localVisibility;
313
+
314
+ // unfudged, like the bent normal below: the fudge corrects the visibility integral only
315
+ sliceLighting *= projectedNormalVecLength;
316
+
317
+ lighting += sliceLighting;
318
+
319
+ // see "Algorithm 2 Extension that computes bent normals b."
320
+ let slice_tangent_ws = normalize(orthoWorldDir);
321
+ let local_bent_ts = integrate_bent_normal(
322
+ horizonCos1,
323
+ horizonCos0,
324
+ norm_cos,
325
+ norm_sin,
326
+ );
327
+
328
+ let t0 = local_bent_ts.x;
329
+ let t1 = local_bent_ts.y;
330
+
331
+ var slice_bent_normal_ws = slice_tangent_ws * t0 + view_direction_ws * t1;
332
+
333
+ bent_normal = fma(slice_bent_normal_ws, vec3(projectedNormalVecLength), bent_normal);
334
+ }
335
+
336
+ visibility *= INV_SLICE_COUNT;
337
+
338
+ visibility = pow(visibility, scale);
339
+
340
+ // disallow total occlusion (which wouldn't make any sense anyhow since pixel is visible but also helps with packing bent normals)
341
+ visibility = max(0.03, visibility);
342
+
343
+ bent_normal = normalize(bent_normal);
344
+
345
+ lighting *= PI * INV_SLICE_COUNT;
346
+
347
+ var out: Output;
348
+ out.visibility = vec2(visibility, visibility * visibility);
349
+ out.bent_normal = encode_g_buffer_normal( bent_normal );
350
+ out.irradiance = rgbe9995_encode( lighting );
351
+
352
+ return out;
353
+ }
354
+ `, [
355
+ chunk_get_view_space_depth,
356
+ PI_DECLARATION,
357
+ PI_HALF_DECLARATION,
358
+
359
+ chunk_decode_g_buffer_normal,
360
+ chunk_encode_g_buffer_normal,
361
+ chunk_spatio_temporal_noise_r2_64,
362
+
363
+ chunk_fast_acos,
364
+
365
+ chunk_direction_world_to_view,
366
+ chunk_direction_view_to_world,
367
+ chunk_is_background,
368
+ chunk_mat4_extract_position,
369
+ chunk_project_position_from_depth,
370
+ chunk_v3_matrix4_rotate,
371
+ chunk_integrate_bent_normal_fast,
372
+ chunk_gtao_compute_mip_level,
373
+ chunk_gtao_sample_depth,
374
+
375
+ //
376
+ chunk_rgbe9995_encode,
377
+ chunk_horizon_integrate_lighting_sample,
378
+ ]);
379
+
380
+ export const fragment_shader_gtao_with_irradiance = new ImageShader({
381
+ descriptor: ShaderDescriptor.from(
382
+ {
383
+ label: "GTAO",
384
+ resources,
385
+ body
386
+ },
387
+ ),
388
+ depthStencil: DEPTH_STENCIL_FOREGROUND_ONLY,
389
+ targets: [
390
+ {
391
+ label: "Occlusion",
392
+ // first 2 moments of visibility
393
+ format: "rg16float",
394
+ clearValue: [1, 1], // Stores visibility, so 1 == not occluded
395
+ blend: BlendState.from({
396
+ color: {
397
+ operation: "add",
398
+ srcFactor: "one",
399
+ dstFactor: "zero",
400
+ }
401
+ })
402
+ },
403
+ {
404
+ label: "Bent Normals",
405
+ // Direction is encoded with octahedral
406
+ format: "rg16uint",
407
+ },
408
+ {
409
+ label: "Irradiance",
410
+ // encoded as RGBE9995
411
+ format:"r32uint"
412
+ }
413
+ ]
414
+ });