@woosh/meep-engine 3.11.0 → 3.11.2

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 (86) hide show
  1. package/package.json +1 -1
  2. package/src/engine/ecs/storage/populateEngineSerializationRegistry.d.ts.map +1 -1
  3. package/src/engine/ecs/storage/populateEngineSerializationRegistry.js +294 -290
  4. package/src/engine/graphics3/ParticipatingMedia.d.ts +111 -0
  5. package/src/engine/graphics3/ParticipatingMedia.d.ts.map +1 -0
  6. package/src/engine/graphics3/ParticipatingMedia.js +203 -0
  7. package/src/engine/graphics3/ParticipatingMediaSerializationAdapter.d.ts +34 -0
  8. package/src/engine/graphics3/ParticipatingMediaSerializationAdapter.d.ts.map +1 -0
  9. package/src/engine/graphics3/ParticipatingMediaSerializationAdapter.js +73 -0
  10. package/src/engine/graphics3/ParticipatingMediaSystem3.d.ts +87 -0
  11. package/src/engine/graphics3/ParticipatingMediaSystem3.d.ts.map +1 -0
  12. package/src/engine/graphics3/ParticipatingMediaSystem3.js +184 -0
  13. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  14. package/src/shade/playground/volumetrics_froxel/README.md +191 -0
  15. package/src/shade/playground/volumetrics_froxel/index.html +270 -0
  16. package/src/shade/playground/volumetrics_froxel/main.d.ts +2 -0
  17. package/src/shade/playground/volumetrics_froxel/main.d.ts.map +1 -0
  18. package/src/shade/playground/volumetrics_froxel/main.js +917 -0
  19. package/src/shade/playground/volumetrics_froxel/measure.d.ts +119 -0
  20. package/src/shade/playground/volumetrics_froxel/measure.d.ts.map +1 -0
  21. package/src/shade/playground/volumetrics_froxel/measure.js +559 -0
  22. package/src/shade/renderer/Renderer.d.ts +13 -0
  23. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  24. package/src/shade/renderer/Renderer.js +45 -18
  25. package/src/shade/renderer/postprocess/nss/NSS.d.ts +1 -1
  26. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.d.ts +14 -13
  27. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.d.ts.map +1 -1
  28. package/src/shade/renderer/shadow/map/shader/chunk_shadowmap_sample_point2.js +133 -129
  29. package/src/shade/renderer/volumetrics/GPUViewVolumetrics.d.ts.map +1 -1
  30. package/src/shade/renderer/volumetrics/GPUViewVolumetrics.js +480 -315
  31. package/src/shade/renderer/volumetrics/NOTES.md +691 -9
  32. package/src/shade/renderer/volumetrics/ParticipatingMediaVolume.d.ts +5 -0
  33. package/src/shade/renderer/volumetrics/ParticipatingMediaVolume.d.ts.map +1 -1
  34. package/src/shade/renderer/volumetrics/ParticipatingMediaVolume.js +21 -2
  35. package/src/shade/renderer/volumetrics/SceneVolumetrics.d.ts +26 -0
  36. package/src/shade/renderer/volumetrics/SceneVolumetrics.d.ts.map +1 -1
  37. package/src/shade/renderer/volumetrics/SceneVolumetrics.js +64 -21
  38. package/src/shade/renderer/volumetrics/VOLUMETRICS_METADATA_STRUCT.d.ts.map +1 -1
  39. package/src/shade/renderer/volumetrics/VOLUMETRICS_METADATA_STRUCT.js +84 -28
  40. package/src/shade/renderer/volumetrics/VOLUMETRICS_SUN_SHADOW_RESOLUTION.d.ts +98 -0
  41. package/src/shade/renderer/volumetrics/VOLUMETRICS_SUN_SHADOW_RESOLUTION.d.ts.map +1 -0
  42. package/src/shade/renderer/volumetrics/VOLUMETRICS_SUN_SHADOW_RESOLUTION.js +111 -0
  43. package/src/shade/renderer/volumetrics/build_volumetrics_sun_shadow_transform.d.ts +84 -0
  44. package/src/shade/renderer/volumetrics/build_volumetrics_sun_shadow_transform.d.ts.map +1 -0
  45. package/src/shade/renderer/volumetrics/build_volumetrics_sun_shadow_transform.js +252 -0
  46. package/src/shade/renderer/volumetrics/chunk_camera_ray_planar_depth_scale.d.ts +21 -0
  47. package/src/shade/renderer/volumetrics/chunk_camera_ray_planar_depth_scale.d.ts.map +1 -0
  48. package/src/shade/renderer/volumetrics/chunk_camera_ray_planar_depth_scale.js +47 -0
  49. package/src/shade/renderer/volumetrics/chunk_integrate_optical_depth.d.ts.map +1 -1
  50. package/src/shade/renderer/volumetrics/chunk_integrate_optical_depth.js +75 -64
  51. package/src/shade/renderer/volumetrics/chunk_volumetrics_froxel_content_offset.d.ts +37 -0
  52. package/src/shade/renderer/volumetrics/chunk_volumetrics_froxel_content_offset.d.ts.map +1 -0
  53. package/src/shade/renderer/volumetrics/chunk_volumetrics_froxel_content_offset.js +56 -0
  54. package/src/shade/renderer/volumetrics/chunk_volumetrics_local_light_shadow.d.ts +43 -0
  55. package/src/shade/renderer/volumetrics/chunk_volumetrics_local_light_shadow.d.ts.map +1 -0
  56. package/src/shade/renderer/volumetrics/chunk_volumetrics_local_light_shadow.js +145 -0
  57. package/src/shade/renderer/volumetrics/chunk_volumetrics_sample_froxel_extinction.d.ts +20 -0
  58. package/src/shade/renderer/volumetrics/chunk_volumetrics_sample_froxel_extinction.d.ts.map +1 -0
  59. package/src/shade/renderer/volumetrics/chunk_volumetrics_sample_froxel_extinction.js +48 -0
  60. package/src/shade/renderer/volumetrics/chunk_volumetrics_sun_optical_depth.d.ts +38 -0
  61. package/src/shade/renderer/volumetrics/chunk_volumetrics_sun_optical_depth.d.ts.map +1 -0
  62. package/src/shade/renderer/volumetrics/chunk_volumetrics_sun_optical_depth.js +107 -0
  63. package/src/shade/renderer/volumetrics/graph_build_volumetrics.d.ts +4 -1
  64. package/src/shade/renderer/volumetrics/graph_build_volumetrics.d.ts.map +1 -1
  65. package/src/shade/renderer/volumetrics/graph_build_volumetrics.js +577 -465
  66. package/src/shade/renderer/volumetrics/shader_volumetrics_build_lighting.d.ts.map +1 -1
  67. package/src/shade/renderer/volumetrics/shader_volumetrics_build_lighting.js +418 -324
  68. package/src/shade/renderer/volumetrics/shader_volumetrics_build_scattering_lut.d.ts.map +1 -1
  69. package/src/shade/renderer/volumetrics/shader_volumetrics_build_scattering_lut.js +14 -3
  70. package/src/shade/renderer/volumetrics/shader_volumetrics_build_sun_shadow_volume.d.ts +8 -0
  71. package/src/shade/renderer/volumetrics/shader_volumetrics_build_sun_shadow_volume.d.ts.map +1 -0
  72. package/src/shade/renderer/volumetrics/shader_volumetrics_build_sun_shadow_volume.js +174 -0
  73. package/src/shade/renderer/volumetrics/volumetrics_position_world_to_froxel_uvw.d.ts.map +1 -1
  74. package/src/shade/renderer/volumetrics/volumetrics_position_world_to_froxel_uvw.js +48 -36
  75. package/src/shade/wgsl/emulator/CPUBitmapData.d.ts +25 -0
  76. package/src/shade/wgsl/emulator/CPUBitmapData.d.ts.map +1 -1
  77. package/src/shade/wgsl/emulator/CPUBitmapData.js +33 -0
  78. package/src/shade/wgsl/emulator/WGSLJavaScriptCompiler.js +2221 -2215
  79. package/src/shade/wgsl/emulator/WGSLLanguageCore.d.ts.map +1 -1
  80. package/src/shade/wgsl/emulator/WGSLLanguageCore.js +1297 -1117
  81. package/src/shade/wgsl/emulator/cpu_texture_gather.d.ts +69 -0
  82. package/src/shade/wgsl/emulator/cpu_texture_gather.d.ts.map +1 -0
  83. package/src/shade/wgsl/emulator/cpu_texture_gather.js +148 -0
  84. package/src/shade/wgsl/emulator/cpu_texture_sample.d.ts +16 -0
  85. package/src/shade/wgsl/emulator/cpu_texture_sample.d.ts.map +1 -1
  86. package/src/shade/wgsl/emulator/cpu_texture_sample.js +176 -172
@@ -0,0 +1,252 @@
1
+ import { assert } from "../../../core/assert.js";
2
+
3
+ /**
4
+ * How many voxels of empty margin the box keeps around the frustum's bounding sphere, per side.
5
+ *
6
+ * One is load-bearing, the rest is comfort. The sun-side face is where the optical-depth scan
7
+ * starts, so the slab it begins on must be guaranteed empty for the snap below to be free: the
8
+ * medium only exists inside the camera frustum, the sphere contains the frustum, so any slab
9
+ * outside the sphere integrates to zero and shifting the box by whole voxels cannot change the
10
+ * answer. Snapping moves the box by up to one voxel, which spends one of these.
11
+ *
12
+ * @type {number}
13
+ */
14
+ const MARGIN_VOXELS = 2;
15
+
16
+ /**
17
+ * Far distance of cascade `index`, i.e. the view depth its box has to cover.
18
+ *
19
+ * A logarithmic split, `far / ratio^(count - 1 - index)`. The choice is forced by what the cascades
20
+ * are for: every box is sized from the bounding sphere of the sub-frustum it covers, so its voxel is
21
+ * proportional to its far distance, and a constant ratio between far distances is a constant ratio
22
+ * between voxel sizes. Any other split leaves one cascade carrying a resolution jump the others do
23
+ * not.
24
+ *
25
+ * The innermost cascade still starts at the camera rather than at the previous cascade's far plane —
26
+ * cascades here nest rather than tile, because the box is sized from a *frustum's* bounding sphere
27
+ * and a shell would need a sphere around the shell, which is the outer sphere again.
28
+ *
29
+ * @param {number} far the full range the structure must cover
30
+ * @param {number} index
31
+ * @param {number} count
32
+ * @param {number} ratio
33
+ * @returns {number}
34
+ */
35
+ export function volumetrics_sun_shadow_cascade_far(far, index, count, ratio) {
36
+ assert.isNumber(far, 'far');
37
+ assert.isNumber(index, 'index');
38
+
39
+ return far / Math.pow(ratio, count - 1 - index);
40
+ }
41
+
42
+ /**
43
+ * Radius of the smallest sphere containing a symmetric perspective frustum, and where its centre
44
+ * sits along the view axis.
45
+ *
46
+ * This is the quantity that makes the structure temporally stable, and the reason it is a sphere
47
+ * rather than a tight fit: it depends only on `fov`, `aspect`, `near` and `far`, so it does not
48
+ * change when the camera turns. A box sized from it therefore has a fixed extent, hence a fixed
49
+ * voxel size, hence a world-space voxel lattice that only ever translates.
50
+ *
51
+ * Both frustum corner rings are equidistant from the centre when
52
+ * `z = (far + near)(k^2 + 1) / 2` with `k^2 = tan^2(theta_x) + tan^2(theta_y)`. Past `z = far` that
53
+ * solution leaves the sphere behind the far plane, and the far ring alone bounds it.
54
+ *
55
+ * @param {number[]} out `[center_view_z, radius]`
56
+ * @param {number} fov_y vertical field of view, radians
57
+ * @param {number} aspect width / height
58
+ * @param {number} near
59
+ * @param {number} far
60
+ * @returns {number[]} out
61
+ */
62
+ export function frustum_bounding_sphere(out, fov_y, aspect, near, far) {
63
+ const tan_y = Math.tan(fov_y * 0.5);
64
+ const tan_x = tan_y * aspect;
65
+
66
+ const k2 = tan_x * tan_x + tan_y * tan_y;
67
+
68
+ const center_z = (far + near) * (k2 + 1) * 0.5;
69
+
70
+ if (center_z >= far) {
71
+ out[0] = far;
72
+ out[1] = far * Math.sqrt(k2);
73
+ } else {
74
+ const d = center_z - far;
75
+ out[0] = center_z;
76
+ out[1] = Math.sqrt(k2 * far * far + d * d);
77
+ }
78
+
79
+ return out;
80
+ }
81
+
82
+ /**
83
+ * Orthonormal basis whose third axis is `direction`.
84
+ *
85
+ * A function of the direction alone, so a stationary sun produces a bit-identical basis every
86
+ * frame. The reference axis is the world axis least aligned with the direction, which keeps the
87
+ * cross product well conditioned; the frame necessarily has a seam somewhere (no continuous
88
+ * tangent frame exists on the sphere), and it sits where two components of `direction` tie.
89
+ *
90
+ * @param {Float32Array} out 9 floats, `[x_axis, y_axis, direction]`
91
+ * @param {ArrayLike<number>} direction unit length
92
+ */
93
+ function orthonormal_basis(out, direction) {
94
+ const dx = direction[0], dy = direction[1], dz = direction[2];
95
+
96
+ const ax = Math.abs(dx), ay = Math.abs(dy), az = Math.abs(dz);
97
+
98
+ let rx = 0, ry = 0, rz = 0;
99
+
100
+ if (ax <= ay && ax <= az) {
101
+ rx = 1;
102
+ } else if (ay <= az) {
103
+ ry = 1;
104
+ } else {
105
+ rz = 1;
106
+ }
107
+
108
+ // x = normalize(cross(reference, direction))
109
+ let xx = ry * dz - rz * dy;
110
+ let xy = rz * dx - rx * dz;
111
+ let xz = rx * dy - ry * dx;
112
+
113
+ const x_length = Math.hypot(xx, xy, xz);
114
+ xx /= x_length;
115
+ xy /= x_length;
116
+ xz /= x_length;
117
+
118
+ // y = cross(direction, x)
119
+ const yx = dy * xz - dz * xy;
120
+ const yy = dz * xx - dx * xz;
121
+ const yz = dx * xy - dy * xx;
122
+
123
+ out[0] = xx;
124
+ out[1] = xy;
125
+ out[2] = xz;
126
+ out[3] = yx;
127
+ out[4] = yy;
128
+ out[5] = yz;
129
+ out[6] = dx;
130
+ out[7] = dy;
131
+ out[8] = dz;
132
+ }
133
+
134
+ const _sphere = [0, 0];
135
+ const _basis = new Float32Array(9);
136
+
137
+ /**
138
+ * Place the sun's optical-depth volume for this frame.
139
+ *
140
+ * The volume is a box aligned to the sun: its third axis is the direction light travels, so a
141
+ * column of voxels along that axis is a light ray, and the optical depth of every voxel in the
142
+ * column is one running sum away from its neighbour's. That is what
143
+ * `shader_volumetrics_build_sun_shadow_volume` computes and what
144
+ * `chunk_volumetrics_sun_optical_depth` reads back in a single fetch.
145
+ *
146
+ * Two properties matter and both are about time rather than space, because the froxel lighting this
147
+ * feeds goes through a 0.95-blend temporal filter that turns any instability into either ghosting
148
+ * or flicker:
149
+ *
150
+ * - **The extent never changes with camera orientation.** It comes from the frustum's bounding
151
+ * sphere, which is a function of the projection only. A tight fit of the frustum corners in sun
152
+ * space — the obvious thing, and what this engine's CSM deliberately does for its cascades —
153
+ * would breathe as the camera turned, moving every voxel centre continuously and making the
154
+ * whole structure swim. The CSM can afford the tight fit because it is two orders of magnitude
155
+ * denser and its consumer is a filtered depth comparison; this is neither.
156
+ *
157
+ * - **The origin is snapped to a whole voxel.** With a fixed extent that makes the world-space
158
+ * voxel lattice identical from frame to frame no matter how the camera moves: voxel centres land
159
+ * on the same world positions, so the sampled medium is the same and the reconstruction is the
160
+ * same function of world position. Only the indexing changes. The box shifts by whole voxels,
161
+ * and the slab it gains or loses lies outside the frustum where the medium is zero, so even the
162
+ * scan's starting value is unaffected.
163
+ *
164
+ * @param {Float32Array} ws_to_uvw 16 floats, column-major. World position -> [0,1]^3 volume coords.
165
+ * @param {Float32Array} uvw_to_ws 16 floats, column-major. Its inverse, for the build pass.
166
+ * @param {ArrayLike<number>} light_direction unit vector along which light TRAVELS (the
167
+ * `light_directional` table's `direction`, i.e. NOT the direction toward the light).
168
+ * @param {ArrayLike<number>} camera_position world space
169
+ * @param {ArrayLike<number>} camera_forward unit, world space
170
+ * @param {number} fov_y vertical field of view, radians
171
+ * @param {number} aspect width / height
172
+ * @param {number} near
173
+ * @param {number} far the depth the medium actually extends to — `min(froxel_far, camera.far)`,
174
+ * since the froxel grid is where the extinction is read from and it ends there.
175
+ * @param {ArrayLike<number>} resolution `[x, y, z]` voxel counts
176
+ * @returns {number} world-space length of one voxel along the sun axis, i.e. the scan's `dz`
177
+ */
178
+ export function build_volumetrics_sun_shadow_transform({
179
+ ws_to_uvw,
180
+ uvw_to_ws,
181
+ light_direction,
182
+ camera_position,
183
+ camera_forward,
184
+ fov_y,
185
+ aspect,
186
+ near,
187
+ far,
188
+ resolution,
189
+ }) {
190
+
191
+ assert.greaterThanOrEqual(ws_to_uvw.length, 16, 'ws_to_uvw must hold 16 floats');
192
+ assert.greaterThanOrEqual(uvw_to_ws.length, 16, 'uvw_to_ws must hold 16 floats');
193
+
194
+ frustum_bounding_sphere(_sphere, fov_y, aspect, near, far);
195
+
196
+ const center_distance = _sphere[0];
197
+ const radius = _sphere[1];
198
+
199
+ orthonormal_basis(_basis, light_direction);
200
+
201
+ const center_x = camera_position[0] + camera_forward[0] * center_distance;
202
+ const center_y = camera_position[1] + camera_forward[1] * center_distance;
203
+ const center_z = camera_position[2] + camera_forward[2] * center_distance;
204
+
205
+ uvw_to_ws[12] = 0;
206
+ uvw_to_ws[13] = 0;
207
+ uvw_to_ws[14] = 0;
208
+
209
+ for (let axis = 0; axis < 3; axis++) {
210
+ const ax = _basis[axis * 3];
211
+ const ay = _basis[axis * 3 + 1];
212
+ const az = _basis[axis * 3 + 2];
213
+
214
+ const count = resolution[axis];
215
+
216
+ // The sphere spans 2r; the box is MARGIN_VOXELS wider on each side, so solving
217
+ // `step * (count - 2 * MARGIN_VOXELS) == 2r` gives a step that leaves that margin exactly.
218
+ const step = (2 * radius) / (count - 2 * MARGIN_VOXELS);
219
+ const extent = step * count;
220
+
221
+ // where the box's centre sits on this axis, in sun space
222
+ const center = center_x * ax + center_y * ay + center_z * az;
223
+
224
+ const origin = Math.floor((center - extent * 0.5) / step) * step;
225
+
226
+ // ws_to_uvw row `axis`: dot(position, axis) / extent - origin / extent
227
+ ws_to_uvw[axis] = ax / extent;
228
+ ws_to_uvw[4 + axis] = ay / extent;
229
+ ws_to_uvw[8 + axis] = az / extent;
230
+ ws_to_uvw[12 + axis] = -origin / extent;
231
+
232
+ // uvw_to_ws column `axis`: the axis scaled to the full extent
233
+ uvw_to_ws[axis * 4] = ax * extent;
234
+ uvw_to_ws[axis * 4 + 1] = ay * extent;
235
+ uvw_to_ws[axis * 4 + 2] = az * extent;
236
+ uvw_to_ws[axis * 4 + 3] = 0;
237
+
238
+ // and accumulate the box origin into its translation column
239
+ uvw_to_ws[12] += ax * origin;
240
+ uvw_to_ws[13] += ay * origin;
241
+ uvw_to_ws[14] += az * origin;
242
+ }
243
+
244
+ ws_to_uvw[3] = 0;
245
+ ws_to_uvw[7] = 0;
246
+ ws_to_uvw[11] = 0;
247
+ ws_to_uvw[15] = 1;
248
+
249
+ uvw_to_ws[15] = 1;
250
+
251
+ return (2 * radius) / (resolution[2] - 2 * MARGIN_VOXELS);
252
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Converts a planar view-space depth into a distance along a unit-length view ray.
3
+ *
4
+ * The froxel depth curve is planar. `cluster_depth_from_z_slice` and its inverse are built in
5
+ * `-position_vs.z` — that is what `build_light_cluster_parameters` is derived from, what
6
+ * `volumetrics_position_world_to_froxel_uvw` reads back, and what the deferred composite feeds in
7
+ * (`get_view_space_depth` is a function of the depth buffer alone, with no screen-position term, so
8
+ * it cannot be anything else). `camera_ray_from_uv` returns a *unit* direction. A ray therefore
9
+ * reaches the depth plane at `depth` only after travelling `depth / cos(theta)`, where theta is the
10
+ * angle off the camera forward axis.
11
+ *
12
+ * Multiplying a planar depth by a unit direction stops on the arc of constant distance instead —
13
+ * exact on the centre axis, and short by cos(theta) everywhere else, which reads as a radial error
14
+ * rather than an offset and is why it survives casual inspection.
15
+ *
16
+ * @see shader_volumetrics_build_lighting, shader_volumetrics_build_scattering_lut
17
+ * @type {CodeChunk}
18
+ */
19
+ export const chunk_camera_ray_planar_depth_scale: CodeChunk;
20
+ import { CodeChunk } from "../shader/compiler/CodeChunk.js";
21
+ //# sourceMappingURL=chunk_camera_ray_planar_depth_scale.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk_camera_ray_planar_depth_scale.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/volumetrics/chunk_camera_ray_planar_depth_scale.js"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;GAiBG;AACH,kDAFU,SAAS,CA2BjB;0BA7CwB,iCAAiC"}
@@ -0,0 +1,47 @@
1
+ import { CAMERA_UNIFORM_STRUCT } from "../camera/CAMERA_UNIFORM_STRUCT.js";
2
+ import { CodeChunk } from "../shader/compiler/CodeChunk.js";
3
+
4
+ /**
5
+ * Converts a planar view-space depth into a distance along a unit-length view ray.
6
+ *
7
+ * The froxel depth curve is planar. `cluster_depth_from_z_slice` and its inverse are built in
8
+ * `-position_vs.z` — that is what `build_light_cluster_parameters` is derived from, what
9
+ * `volumetrics_position_world_to_froxel_uvw` reads back, and what the deferred composite feeds in
10
+ * (`get_view_space_depth` is a function of the depth buffer alone, with no screen-position term, so
11
+ * it cannot be anything else). `camera_ray_from_uv` returns a *unit* direction. A ray therefore
12
+ * reaches the depth plane at `depth` only after travelling `depth / cos(theta)`, where theta is the
13
+ * angle off the camera forward axis.
14
+ *
15
+ * Multiplying a planar depth by a unit direction stops on the arc of constant distance instead —
16
+ * exact on the centre axis, and short by cos(theta) everywhere else, which reads as a radial error
17
+ * rather than an offset and is why it survives casual inspection.
18
+ *
19
+ * @see shader_volumetrics_build_lighting, shader_volumetrics_build_scattering_lut
20
+ * @type {CodeChunk}
21
+ */
22
+ export const chunk_camera_ray_planar_depth_scale = CodeChunk.from(
23
+ //language=WGSL
24
+ `
25
+ fn camera_ray_planar_depth_scale(
26
+ direction_ws: vec3<f32>,
27
+ camera: ${CAMERA_UNIFORM_STRUCT.wgsl_ref}
28
+ ) -> f32 {
29
+
30
+ // The z row of the view matrix dotted with the direction — i.e. (V * vec4(dir, 0)).z without
31
+ // the rest of the multiply. WGSL indexes a matrix by column, so m[c][2] walks that row.
32
+ // View space looks down -Z, so negate to get a positive cosine for a forward-going ray.
33
+ let cos_theta = -(
34
+ camera.view_matrix[0][2] * direction_ws.x
35
+ + camera.view_matrix[1][2] * direction_ws.y
36
+ + camera.view_matrix[2][2] * direction_ws.z
37
+ );
38
+
39
+ // A ray at or past 90 degrees off-axis never reaches the plane at all. Clamping rather than
40
+ // dividing by ~0 keeps the caller finite; no froxel ray is anywhere near this, since every one
41
+ // of them lies inside the frustum.
42
+ return 1.0 / max(cos_theta, 1e-4);
43
+ }
44
+ `, [
45
+ CAMERA_UNIFORM_STRUCT.declaration_chunk,
46
+ ]
47
+ );
@@ -1 +1 @@
1
- {"version":3,"file":"chunk_integrate_optical_depth.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/volumetrics/chunk_integrate_optical_depth.js"],"names":[],"mappings":"AAKA,sDA2DC;0BA9DyB,iCAAiC"}
1
+ {"version":3,"file":"chunk_integrate_optical_depth.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/volumetrics/chunk_integrate_optical_depth.js"],"names":[],"mappings":"AAKA,sDAsEC;0BAzEyB,iCAAiC"}
@@ -1,65 +1,76 @@
1
- import { chunk_frustum_clip_ray } from "../shader/chunk/geometry/frustum/chunk_frustum_clip_ray.js";
2
- import { RAY_STRUCT } from "../shader/chunk/geometry/ray/RAY_STRUCT.js";
3
- import { CodeChunk } from "../shader/compiler/CodeChunk.js";
4
- import { volumetrics_position_world_to_froxel_uvw } from "./volumetrics_position_world_to_froxel_uvw.js";
5
-
6
- export const chunk_integrate_optical_depth = CodeChunk.from(
7
- //language=WGSL
8
- `
9
- fn integrate_optical_depth(ray_ws: ${RAY_STRUCT.wgsl_ref}) -> vec3<f32>{
10
-
11
- var frustum_tmax = min(ray_ws.tmax, frustum_clip_ray(
12
- ray_ws.origin,
13
- ray_ws.direction,
14
- camera.frustum
15
- ));
16
-
17
- if(frustum_tmax <= 0.0){
18
- return vec3<f32>(0.0);
19
- }
20
-
21
- // go as far as we can in frustum space
22
- const STEP_COUNT = 16;
23
-
24
- let step_size = frustum_tmax / STEP_COUNT;
25
-
26
- var optical_depth = vec3<f32>(0.0);
27
-
28
- // offset ray forward a tiny mount to avoid aliasing
29
- var t_previous = step_size * 0.1;
30
- var t_current = step_size;
31
-
32
- for(var i = 0; i < STEP_COUNT; i++){
33
-
34
- let position_ws = ray_ws.origin + ray_ws.direction * t_current;
35
-
36
- let uvw = volumetrics_position_world_to_froxel_uvw(position_ws);
37
-
38
- if(any(abs(uvw.xyz - 0.5) >= vec3(0.5))){
39
- // out of bounds, stop integration
40
- break;
41
- }
42
-
43
- let t_delta = t_current - t_previous;
44
-
45
- let sample_extinction = textureSampleLevel(tParticipatingMedia1, sLinear, uvw, 0.0).rgb;
46
-
47
- optical_depth += sample_extinction * t_delta;
48
-
49
- // update
50
- t_previous = t_current;
51
- t_current += step_size;
52
- }
53
-
54
- // if ray goes outside of the frustum, integrate over fog volumes
55
- // TODO implement
56
-
57
- return optical_depth;
58
- }
59
- `,
60
- [
61
- RAY_STRUCT.declaration_chunk,
62
- volumetrics_position_world_to_froxel_uvw,
63
- chunk_frustum_clip_ray,
64
- ]
1
+ import { chunk_frustum_clip_ray } from "../shader/chunk/geometry/frustum/chunk_frustum_clip_ray.js";
2
+ import { RAY_STRUCT } from "../shader/chunk/geometry/ray/RAY_STRUCT.js";
3
+ import { CodeChunk } from "../shader/compiler/CodeChunk.js";
4
+ import { volumetrics_position_world_to_froxel_uvw } from "./volumetrics_position_world_to_froxel_uvw.js";
5
+
6
+ export const chunk_integrate_optical_depth = CodeChunk.from(
7
+ //language=WGSL
8
+ `
9
+ fn integrate_optical_depth(ray_ws: ${RAY_STRUCT.wgsl_ref}) -> vec3<f32>{
10
+
11
+ // volumetrics_metadata.frustum, NOT camera.frustum. The camera's frustum has no usable far
12
+ // plane: an infinite reverse-Z projection puts (0,0,0,near) in row 2 of the VP, a normal that
13
+ // cannot be normalized, so PerspectiveCamera.update_frustum zeroes the entry outright.
14
+ // frustum_clip_ray then discards it — correctly — which leaves the frustum OPEN at the far end,
15
+ // and a ray whose direction lies inside the view cone crosses no plane outward at all. t_exit
16
+ // stays at its 1e20 sentinel; capped by a directional light's tmax of 1.496e11 that gives a
17
+ // step of ~9.35e9, so the first sample lands far outside the grid, the loop below breaks on
18
+ // iteration one, and this returns zero — no media self-shadowing at all whenever the sun is in
19
+ // front of the camera, which is exactly when shafts are wanted.
20
+ //
21
+ // The metadata frustum is the same one rebuilt with a FINITE far plane
22
+ // (update_frustum_far_plane), which the participating-media pass already slices against.
23
+ var frustum_tmax = min(
24
+ ray_ws.tmax,
25
+ frustum_clip_ray(ray_ws.origin, ray_ws.direction, volumetrics_metadata.frustum)
26
+ );
27
+
28
+ if(frustum_tmax <= 0.0){
29
+ return vec3<f32>(0.0);
30
+ }
31
+
32
+ // go as far as we can in frustum space
33
+ const STEP_COUNT = 16;
34
+
35
+ let step_size = frustum_tmax / STEP_COUNT;
36
+
37
+ var optical_depth = vec3<f32>(0.0);
38
+
39
+ // offset ray forward a tiny mount to avoid aliasing
40
+ var t_previous = step_size * 0.1;
41
+ var t_current = step_size;
42
+
43
+ for(var i = 0; i < STEP_COUNT; i++){
44
+
45
+ let position_ws = ray_ws.origin + ray_ws.direction * t_current;
46
+
47
+ let uvw = volumetrics_position_world_to_froxel_uvw(position_ws);
48
+
49
+ if(any(abs(uvw.xyz - 0.5) >= vec3(0.5))){
50
+ // out of bounds, stop integration
51
+ break;
52
+ }
53
+
54
+ let t_delta = t_current - t_previous;
55
+
56
+ let sample_extinction = textureSampleLevel(tParticipatingMedia1, sLinear, uvw, 0.0).rgb;
57
+
58
+ optical_depth += sample_extinction * t_delta;
59
+
60
+ // update
61
+ t_previous = t_current;
62
+ t_current += step_size;
63
+ }
64
+
65
+ // if ray goes outside of the frustum, integrate over fog volumes
66
+ // TODO implement
67
+
68
+ return optical_depth;
69
+ }
70
+ `,
71
+ [
72
+ RAY_STRUCT.declaration_chunk,
73
+ volumetrics_position_world_to_froxel_uvw,
74
+ chunk_frustum_clip_ray,
75
+ ]
65
76
  )
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Where a froxel's stored value actually sits inside its slab, in slices from the slab's near face.
3
+ *
4
+ * The froxel grid is *cell-averaged*: texel `k` holds an average over the slab spanning slice
5
+ * coordinates `[k + j, k + 1 + j]`, where `j` is the shared column jitter. Reading it back by depth
6
+ * therefore needs the inverse of that, and the centroid is not the midpoint — the lighting pass
7
+ * importance-samples uniformly in `z^3` (a froxel's volume grows as `z^2`), and the depth curve is
8
+ * exponential, so the mean lands slightly past halfway.
9
+ *
10
+ * With `r` the per-slice depth ratio — constant on a log curve, which is what makes the answer the
11
+ * same for every slice and every jitter:
12
+ *
13
+ * z(u) = z0 * (1 + u (r^3 - 1))^(1/3), u ~ U(0,1)
14
+ * offset(u) = ln(1 + u (r^3 - 1)) / (3 ln r)
15
+ * E[offset] = r^3 / (r^3 - 1) - 1 / (3 ln r)
16
+ *
17
+ * That runs ~0.525 to ~0.543 across the depth ranges this engine uses, and tends to exactly 0.5 as
18
+ * the curve flattens toward linear. Treating it as 0.5 — or, as the shipping code did, addressing
19
+ * texel `k` as if it held slice `k` — leaves a systematic bias of about half a froxel, which on a
20
+ * 64-slice log grid is a ~7% depth error at every distance.
21
+ *
22
+ * The jitter `j` is deliberately NOT corrected for here. It is zero-mean, so the temporal filter
23
+ * integrates it away, and a sample lands between two columns carrying *different* jitters anyway
24
+ * (see C-05) — so there is no single `j` to subtract. What is left after this is the part that does
25
+ * not average out.
26
+ *
27
+ * Contrast the aerial LUT, which is *boundary-sampled*: it stores at texel `k` the integral
28
+ * accumulated up to `from_z_slice(k)`, so texel `k` really is slice `k` and the plain `+0.5`
29
+ * texel-centre convention is correct there. Two grids, two conventions; the bug was applying the
30
+ * boundary one to the cell-averaged textures.
31
+ *
32
+ * @see volumetrics_position_world_to_froxel_uvw, shader_volumetrics_build_lighting
33
+ * @type {CodeChunk}
34
+ */
35
+ export const chunk_volumetrics_froxel_content_offset: CodeChunk;
36
+ import { CodeChunk } from "../shader/compiler/CodeChunk.js";
37
+ //# sourceMappingURL=chunk_volumetrics_froxel_content_offset.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk_volumetrics_froxel_content_offset.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/volumetrics/chunk_volumetrics_froxel_content_offset.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,sDAFU,SAAS,CAqBjB;0BAvDwB,iCAAiC"}
@@ -0,0 +1,56 @@
1
+ import { CodeChunk } from "../shader/compiler/CodeChunk.js";
2
+
3
+ /**
4
+ * Where a froxel's stored value actually sits inside its slab, in slices from the slab's near face.
5
+ *
6
+ * The froxel grid is *cell-averaged*: texel `k` holds an average over the slab spanning slice
7
+ * coordinates `[k + j, k + 1 + j]`, where `j` is the shared column jitter. Reading it back by depth
8
+ * therefore needs the inverse of that, and the centroid is not the midpoint — the lighting pass
9
+ * importance-samples uniformly in `z^3` (a froxel's volume grows as `z^2`), and the depth curve is
10
+ * exponential, so the mean lands slightly past halfway.
11
+ *
12
+ * With `r` the per-slice depth ratio — constant on a log curve, which is what makes the answer the
13
+ * same for every slice and every jitter:
14
+ *
15
+ * z(u) = z0 * (1 + u (r^3 - 1))^(1/3), u ~ U(0,1)
16
+ * offset(u) = ln(1 + u (r^3 - 1)) / (3 ln r)
17
+ * E[offset] = r^3 / (r^3 - 1) - 1 / (3 ln r)
18
+ *
19
+ * That runs ~0.525 to ~0.543 across the depth ranges this engine uses, and tends to exactly 0.5 as
20
+ * the curve flattens toward linear. Treating it as 0.5 — or, as the shipping code did, addressing
21
+ * texel `k` as if it held slice `k` — leaves a systematic bias of about half a froxel, which on a
22
+ * 64-slice log grid is a ~7% depth error at every distance.
23
+ *
24
+ * The jitter `j` is deliberately NOT corrected for here. It is zero-mean, so the temporal filter
25
+ * integrates it away, and a sample lands between two columns carrying *different* jitters anyway
26
+ * (see C-05) — so there is no single `j` to subtract. What is left after this is the part that does
27
+ * not average out.
28
+ *
29
+ * Contrast the aerial LUT, which is *boundary-sampled*: it stores at texel `k` the integral
30
+ * accumulated up to `from_z_slice(k)`, so texel `k` really is slice `k` and the plain `+0.5`
31
+ * texel-centre convention is correct there. Two grids, two conventions; the bug was applying the
32
+ * boundary one to the cell-averaged textures.
33
+ *
34
+ * @see volumetrics_position_world_to_froxel_uvw, shader_volumetrics_build_lighting
35
+ * @type {CodeChunk}
36
+ */
37
+ export const chunk_volumetrics_froxel_content_offset = CodeChunk.from(
38
+ //language=WGSL
39
+ `
40
+ fn volumetrics_froxel_content_offset(cluster_scale: f32) -> f32 {
41
+
42
+ // r = 2^(1/scale) is the depth ratio across one slice, so ln(r) = ln(2) / scale
43
+ let ln_r = 0.69314718 / max(cluster_scale, 1e-6);
44
+
45
+ // As the curve flattens both terms below diverge while their difference tends to 1/2. Bail out
46
+ // before the subtraction loses its significant digits rather than after.
47
+ if (ln_r < 1e-3) {
48
+ return 0.5;
49
+ }
50
+
51
+ let r3 = exp(3.0 * ln_r);
52
+
53
+ return r3 / (r3 - 1.0) - 1.0 / (3.0 * ln_r);
54
+ }
55
+ `
56
+ );
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Shadow visibility for clustered point and spot lights, sampled the way a *volume* wants rather
3
+ * than the way a surface does: one nearest texel out of the shadow atlas, no filter kernel, no
4
+ * receiver bias.
5
+ *
6
+ * This exists alongside {@link chunk_shadowmap_get_light_visibility} rather than replacing it. The
7
+ * deferred path needs its filtering — a hard shadow edge on a surface is a visible artefact — and
8
+ * its cost is paid once per pixel. Here the cost is paid once per light per froxel, with up to 255
9
+ * point and 255 spot lights in a cluster, and the playground scene alone carries 130 shadow-casting
10
+ * point lights. The two callers want genuinely different things out of the same atlas, so they get
11
+ * two functions over it; the atlas, its tile rects and its projections are untouched.
12
+ *
13
+ * Three reasons the cheap tap is not a compromise here:
14
+ *
15
+ * - The value being sampled is already an average over a froxel slab several pixels across, it is
16
+ * jittered in xy and in depth every frame, and it then goes through a 3x3x3 Blackman-Harris
17
+ * temporal resample at a 0.95 blend. A PCF kernel would be filtering something about to be
18
+ * filtered three more times. Frostbite takes exactly this route for its froxel pass and accepts
19
+ * the aliasing; Wronski's alternative (a separate pre-filtered low-resolution ESM) needs its own
20
+ * target and build pass, and cannot be a downsample of THIS atlas because blurring moments
21
+ * bleeds across tile borders.
22
+ *
23
+ * - There is no receiver bias because there is no receiver. Normal-offset and slope-scaled depth
24
+ * bias exist to stop a surface shadowing itself; a medium cannot. Note that the deferred helpers
25
+ * reach the same answer when a volumetric caller passes the light direction in place of a normal:
26
+ * `get_shadow_offsets(N, L)` with `N == L` gives `cos_alpha = 1`, hence `sqrt(1 - 1) = 0` for the
27
+ * normal scale and `0` for the depth scale, so both offsets vanish. Dropping them here therefore
28
+ * does not change the sampled position relative to those helpers — only the tap count does.
29
+ *
30
+ * - `textureLoad` rather than `textureGatherCompare`: the comparison is one instruction either way,
31
+ * and the raw load carries no uniformity requirement, so it is legal in the middle of a
32
+ * non-uniform light loop without the gather dance `shadowmap_sample_filtered` needs.
33
+ *
34
+ * Both functions early-out to 1.0 on a light with no `CASTS_SHADOW_BIT`, so a non-shadowing light in
35
+ * the cluster stays free.
36
+ *
37
+ * Requires `shadowmapAtlas` in the calling shader's scope.
38
+ *
39
+ * @type {CodeChunk}
40
+ */
41
+ export const chunk_volumetrics_local_light_shadow: CodeChunk;
42
+ import { CodeChunk } from "../shader/compiler/CodeChunk.js";
43
+ //# sourceMappingURL=chunk_volumetrics_local_light_shadow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunk_volumetrics_local_light_shadow.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/volumetrics/chunk_volumetrics_local_light_shadow.js"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,mDAFU,SAAS,CA6FjB;0BA1IwB,iCAAiC"}