Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Local volumes: fit "mesh" fills the closed mesh in the rasterizer (#807)

Nguyen Xuan An114c2c683200
diff --git a/changes/807-raster-mesh-fit.md b/changes/807-raster-mesh-fit.md
new file mode 100644
index 00000000..e83093f5
--- /dev/null
+++ b/changes/807-raster-mesh-fit.md
@@ -0,0 +1 @@
+- `environment.localVolumes` with `shape: "mesh"` and `fit: "mesh"` now draws in the rasterizer: a mesh pass sums the length of each pixel's ray inside the closed part, exact for any number of shells, and the CPU path tracer and the rasterizer agree within 0.03 on the L-room test (#807)
diff --git a/changes/807-raster-mesh-fit.size.md b/changes/807-raster-mesh-fit.size.md
new file mode 100644
index 00000000..f0acfa2c
--- /dev/null
+++ b/changes/807-raster-mesh-fit.size.md
@@ -0,0 +1,8 @@
+- entry:fog-shader: the mesh pass (a small program, an RG32F target, the front and back face sum) and the closed-form step in the post shader, so smoke fills a modelled room in the rasterizer as it does in the path tracer instead of throwing; the code loads only with the fog addon and costs nothing in a scene without a fit "mesh" volume.
+- scene:agent-scene: the same lazy fog code plus the edge check (open edges and winding) moved into the part binding, an agent-facing capability.
+- scene:agent-scene+lazy: the same code, counted with the lazy chunks.
+- scene:lit-box: two lines in the renderer (the mesh pass call and its dispose), 26 bytes gzip; the pass itself is in the fog addon.
+- goal:lit-box: 26 bytes gzip for the renderer's call into the mesh pass; the call must sit in the frame path, and the pass is lazy.
+- scene:gltf-box: the same two renderer lines, 31 bytes gzip.
+- goal:gltf-box: the same two renderer lines, 31 bytes gzip.
+- goal:agent-scene: the scene is already over its goal; the part binding and edge check must run where the recipe is checked, and the gain is an agent-facing capability (119 bytes gzip).
diff --git a/docs/volumetric.md b/docs/volumetric.md
index 83519d6d..61e308e5 100644
--- a/docs/volumetric.md
+++ b/docs/volumetric.md
@@ -139,7 +139,9 @@ Each volume is marched on its own between its entry and its exit, cut at the fir
```
- `fit: "bounds"` (the default) uses the part's bounding box in its own frame, carried through its matrix: an oriented box, tight for a rotated or scaled part. It draws in the rasterizer and the CPU path tracer, and `falloff` fades its edges as for a box.
-- `fit: "mesh"` uses the closed mesh itself. The CPU path tracer walks the ray from crossing to crossing, so an L-shaped room or two separate shells are filled exactly, and a ray that leaves the mesh and returns crosses both pieces. The mesh has no distance field, so `falloff` is ignored (the density is the same everywhere inside). The rasterizer has no mesh pass: it throws an error that names `environment.localVolumes[n].fit` instead of drawing the bounds, so a scene never shows a box where it asked for an L.
+- `fit: "mesh"` uses the closed mesh itself. The CPU path tracer walks the ray from crossing to crossing, so an L-shaped room or two separate shells are filled exactly, and a ray that leaves the mesh and returns crosses both pieces. The mesh has no distance field, so `falloff` is ignored (the density is the same everywhere inside), and so is `noiseAmount`.
+- The rasterizer draws the closed mesh in a mesh pass before the post pass: each `fit: "mesh"` part is drawn without culling into a float buffer, adding the distance of every back face and subtracting that of every front face along each pixel's ray, each clamped to the first opaque surface. That sum is the length of the ray inside the mesh, exact for any number of shells and any shape, so there is no shell limit; the post shader then integrates the volume over that length in closed form (the light behind is dimmed by `exp(-density * absorption * length)`, the emission adds `emission * (1 - exp(-k * length)) / k`). `steps` has no effect on a mesh fit, because nothing is stepped. Volumes are still composited nearest entry first. On the L-room test (`tests/local-volume-mesh-raster.test.js`, density 1) the rasterizer's transmittance stays within 0.03 of the CPU tracer's (the test tolerance; measured differences were 0.0014 or less) and the notch of the L is exactly clear.
+- The rasterizer needs the mesh to be closed and every face to wind the same way (either outward or inward; it detects which): an open mesh throws `environment.localVolumes[n].part "x" is not a closed mesh (k open edges)`, and faces wound against each other throw `... has faces wound against each other (k edges)`. It needs `EXT_float_blend` and a buffer as wide as the canvas and as tall as the canvas times the number of `fit: "mesh"` volumes (RG32F, 8 bytes per pixel each; at 1920 x 1080 that is 16.6 MB per volume), and throws with the limit when the device cannot make it. Geometry nearer than the camera's near plane is clipped, so a ray loses the stretch between the eye and the near plane. A scene with no `fit: "mesh"` volume allocates nothing and draws nothing extra.
- A part that is `visible: false` is not drawn by the rasterizer and is left out of the traced geometry when a mesh volume names it, so a hidden part can shape smoke without becoming a wall.
- The part must be rigid (not skinned) and have volume. A mesh with open edges has no inside: `fit: "mesh"` throws an error with the part name and the number of open edges. A `part` that names nothing throws `environment.localVolumes[n].part "x" names no part in the scene` with the part names.
- The mesh walk follows at most 64 surface crossings per ray (32 segments inside), so the cost stays bounded; the `steps` of the volume are shared between its segments by length.
@@ -150,4 +152,4 @@ Each volume is marched on its own between its entry and its exit, cut at the fir
### Not covered yet
-Mesh bounds as a shape, the WebGPU and WebGL path tracers, the lightmap bake and path tracer noise are open work: mesh bounds #807, tracer noise and GPU tracers #808, the bake #809.
+The WebGPU and WebGL path tracers, the lightmap bake and path tracer noise are open work: tracer noise and GPU tracers #808, the bake #809.
diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.js
index e46e6ba4..e2e927c1 100644
--- a/src/agent-scene-capabilities.js
+++ b/src/agent-scene-capabilities.js
@@ -270,7 +270,7 @@ export const AGENT_SCENE_CAPABILITIES = Object.freeze({
maxSpotLights: MAX_SPOT_LIGHTS, maxAreaLights: MAX_AREA_LIGHTS,
localShadows: Object.freeze({ lights: Object.freeze(['pointLights', 'spotLights']), atlasTiles: 24, pointLightTiles: 6, spotLightTiles: 1, tileSize: 512 }),
lightLinking: Object.freeze({ fields: Object.freeze(['environment.keyAffects', 'pointLights[].affects', 'spotLights[].affects', 'areaLights[].affects']), unlisted: 'a light without affects lights every node', unknownNode: 'rejected', bake: 'honoured', pathTracer: 'ignored, named in report.ignored' }),
- localVolumes: Object.freeze({ field: 'environment.localVolumes', maximum: 8, shapes: Object.freeze(['box', 'sphere', 'mesh']), fields: 'shape, center, size (box) or radius (sphere) or part + fit bounds|mesh (mesh: the named part, bounds by default; fit mesh needs a closed part and backend cpu), density 0-100 (1), falloff 0-1 (0.5), absorption rgb 0-1 (1,1,1), emission rgb 0-1, emissionStrength 0-1000 (1), noiseAmount 0-1 (0), noiseScale (1), steps 4-32 (16)', note: 'Absorb and emit, no scattering; needs hdr and depth like volumeFog; noise is raster only; path tracer: backend cpu only (gpu and native throw).' }),
+ localVolumes: Object.freeze({ field: 'environment.localVolumes', maximum: 8, shapes: Object.freeze(['box', 'sphere', 'mesh']), fields: 'shape, center, size (box) or radius (sphere) or part + fit bounds|mesh (mesh: the named part, bounds by default; fit mesh fills a closed part, ignoring falloff, noise and steps), density 0-100 (1), falloff 0-1 (0.5), absorption rgb 0-1 (1,1,1), emission rgb 0-1, emissionStrength 0-1000 (1), noiseAmount 0-1 (0), noiseScale (1), steps 4-32 (16)', note: 'Absorb and emit, no scattering; needs hdr and depth like volumeFog; noise is raster only; path tracer: backend cpu only (gpu and native throw).' }),
shadowCascades: Object.freeze({ field: 'environment.shadowCascades', counts: Object.freeze([0, 1, 2, 3, 4]), zero: 'one shadow map', automatic: 'three cascades when shadow.extent is above 40 and the field is unset', maximumDistance: 150, note: 'volume fog and water read the single map' }),
lineArt: Object.freeze({ field: 'environment.lines', fields: 'width px 0.25-16 (1.5), color, boundaries, creaseAngle deg (35), depthJump (0.08), toon steps 0-8, polylines [{points [x,y,z] 2-256, color, width, depthTest, closed}] max 64; {} turns on, null clears; material toon 0-8', note: 'docs/line-art.md' }),
viewTransform: Object.freeze({
diff --git a/src/fog-shader.js b/src/fog-shader.js
index 891e66d3..739f6be1 100644
--- a/src/fog-shader.js
+++ b/src/fog-shader.js
@@ -1,6 +1,7 @@
import { registerShaderFeatureCode } from './shader-registry.js';
import { resolveLocalVolumes, createLocalVolumeBuffers, packLocalVolumes } from './local-volumes.js';
import { bindLocalVolumeParts } from './local-volume-parts.js';
+import { drawMeshVolumes, disposeMeshVolumePass } from './mesh-volume-pass.js';
import { FogFieldTexture, createFogLightBuffers, packFogLights, volumeShadowAmbient, DEFAULT_FLOW_STRENGTH, DEFAULT_FLOW_PERIOD, resolveFogDetail, fogUsesDetail } from './fog-field.js';
// The volume fog step of the post pass: its uniforms and the march that scatters light through the fog box. Only a renderer that
@@ -82,7 +83,7 @@ function prepareLocalVolumes(renderer, input) {
if (resolved.length === 0) return 0;
if (!renderer.depth || !renderer.hdr || renderer.transparent || renderer.benchmark) throw new Error('Local volumes require depth:true, active HDR and an opaque canvas');
const buffers = (renderer.localVolumeBuffers ??= createLocalVolumeBuffers());
- packLocalVolumes(bindLocalVolumeParts(resolved, renderer.sceneParts, { meshFit: false }), buffers);
+ packLocalVolumes(bindLocalVolumeParts(resolved, renderer.sceneParts), buffers);
return buffers.count;
}
@@ -99,6 +100,10 @@ function bindLocalVolumes(renderer, post, frame) {
context.uniform4fv(locate('localVolumeEmission[0]'), buffers.emission);
context.uniform4fv(locate('localVolumeSteps[0]'), buffers.steps);
context.uniform4fv(locate('localVolumeAxes[0]'), buffers.axes);
+ // The sampler always gets a texture of its own type, so the program is valid in a frame with no mesh volume.
+ context.activeTexture(context.TEXTURE7);
+ context.bindTexture(context.TEXTURE_2D, buffers.meshParts.length > 0 ? renderer.meshVolumePass.target.texture : renderer.whiteTexture);
+ context.uniform1i(locate('localMeshVolumes'), 7);
}
const LOCAL_VOLUME_DECLARATIONS = `
@@ -110,6 +115,7 @@ uniform vec4 localVolumeAbsorption[8];
uniform vec4 localVolumeEmission[8];
uniform vec4 localVolumeSteps[8];
uniform vec4 localVolumeAxes[24];
+uniform highp sampler2D localMeshVolumes;
`;
// Each volume is marched on its own between its entry and exit (cut at the first surface) and composited nearest first: the colour
@@ -173,6 +179,15 @@ vec3 localVolumes(vec3 background) {
entries[index] = 1e30;
exits[index] = 0.0;
if (index >= localVolumeCount) continue;
+ if (localVolumeCenters[index].w > 1.5) {
+ // A closed mesh: the mesh pass stored the length inside this pixel's ray and the nearest entry (see mesh-volume-pass.js).
+ vec2 inside = texelFetch(localMeshVolumes, ivec2(gl_FragCoord.xy) + ivec2(0, int(localVolumeSteps[index].y) * int(resolution.y)), 0).rg;
+ if (inside.x > 0.0) {
+ entries[index] = inside.y < 1e29 ? inside.y : 0.0;
+ exits[index] = entries[index] + inside.x;
+ }
+ continue;
+ }
vec2 span = localVolumeInterval(index, origin, direction);
float entry = max(span.x, 0.0);
float exitDistance = min(span.y, surfaceDistance);
@@ -196,6 +211,17 @@ vec3 localVolumes(vec3 background) {
float entry = entries[nearest];
float exitDistance = exits[nearest];
entries[nearest] = 1e30;
+ if (localVolumeCenters[nearest].w > 1.5) {
+ // Uniform density, so the march has a closed form: the light behind is dimmed by exp(-k L) and the emission adds
+ // (1 - exp(-k L)) / k, or L where k is 0, wherever the stretches inside lie along the ray.
+ float insideLength = exitDistance - entry;
+ vec3 absorption = localVolumeAbsorption[nearest].rgb;
+ vec3 factor = exp(-absorption * insideLength);
+ vec3 gain = mix(vec3(insideLength), (1.0 - factor) / max(absorption, vec3(1e-6)), step(vec3(1e-6), absorption));
+ radiance += transmittance * localVolumeEmission[nearest].rgb * gain;
+ transmittance *= factor;
+ continue;
+ }
int steps = int(localVolumeSteps[nearest].x);
float stepSize = (exitDistance - entry) / float(steps);
float jitter = hash(vec3(gl_FragCoord.xy, float(nearest) + 1.0));
@@ -218,7 +244,7 @@ function packLights(renderer) {
registerShaderFeatureCode('volumeFog', {
// The JavaScript half, which the renderer calls for a frame that draws a volume fog.
- support: { packLights, bindUniforms, usesDetail: fogUsesDetail, prepareLocalVolumes, bindLocalVolumes },
+ support: { packLights, bindUniforms, usesDetail: fogUsesDetail, prepareLocalVolumes, bindLocalVolumes, drawMeshVolumes, disposeMeshVolumes: disposeMeshVolumePass },
localDeclarations: LOCAL_VOLUME_DECLARATIONS,
localFunctions: LOCAL_VOLUME_FUNCTIONS,
postDeclarations: `
diff --git a/src/local-volume-parts.js b/src/local-volume-parts.js
index 746e376a..e765d425 100644
--- a/src/local-volume-parts.js
+++ b/src/local-volume-parts.js
@@ -1,7 +1,51 @@
// Binds the mesh volumes of environment.localVolumes (shape "mesh") to the parts they name. A volume that fits the bounds of its
// part becomes an oriented box: the part's local bounding box carried through its matrix, so a rotated room is still a tight box.
-// A volume that fits the closed mesh keeps the part for the CPU path tracer, which walks the mesh itself
-// (path-tracer-volume.js, prepareMeshVolume); the rasterizer has no mesh pass and refuses it with a message that names the field.
+// A volume that fits the closed mesh keeps the part: the CPU path tracer walks the mesh itself (path-tracer-volume.js,
+// prepareMeshVolume) and the rasterizer draws it into a depth buffer first (mesh-volume-pass.js).
+
+function weldedVertexIds(positions, tolerance) {
+ const ids = new Map();
+ const welded = new Uint32Array(positions.length / 3);
+ for (let vertex = 0; vertex < welded.length; vertex++) {
+ const key = `${Math.round(positions[vertex * 3] / tolerance)},${Math.round(positions[vertex * 3 + 1] / tolerance)},${Math.round(positions[vertex * 3 + 2] / tolerance)}`;
+ if (!ids.has(key)) ids.set(key, ids.size);
+ welded[vertex] = ids.get(key);
+ }
+ return welded;
+}
+
+/**
+ * Counts the edges that do not join exactly two triangles (`open`, with vertices at the same place counted as one) and, among those
+ * that do, the edges whose two triangles run the same way along it (`inconsistent`: faces wound against each other).
+ */
+export function countMeshEdgeProblems(positions, indices, tolerance) {
+ const welded = weldedVertexIds(positions, tolerance);
+ const uses = new Map();
+ for (let corner = 0; corner < indices.length; corner += 3) {
+ for (let edge = 0; edge < 3; edge++) {
+ const from = welded[indices[corner + edge]];
+ const to = welded[indices[corner + (edge + 1) % 3]];
+ if (from === to) continue;
+ const key = from < to ? `${from}-${to}` : `${to}-${from}`;
+ const use = uses.get(key) ?? { count: 0, direction: 0 };
+ use.count++;
+ use.direction += from < to ? 1 : -1;
+ uses.set(key, use);
+ }
+ }
+ let open = 0;
+ let inconsistent = 0;
+ for (const { count, direction } of uses.values()) {
+ if (count !== 2) open++;
+ else if (direction !== 0) inconsistent++;
+ }
+ return { open, inconsistent };
+}
+
+/** The number of edges that do not join exactly two triangles, with vertices at the same place counted as one. 0 for a closed mesh. */
+export function countOpenEdges(positions, indices, tolerance) {
+ return countMeshEdgeProblems(positions, indices, tolerance).open;
+}
const LISTED_PART_NAMES = 12;
const boundsByGeometry = new WeakMap();
@@ -43,6 +87,21 @@ function orientedBounds(part, label) {
return { center, size, axes };
}
+const edgeProblemsByGeometry = new WeakMap();
+
+/** Throws unless the part's mesh is closed and its faces wind the same way, which the rasterizer's front and back faces rely on. */
+export function assertClosedMesh(part, label) {
+ let problems = edgeProblemsByGeometry.get(part.geometry);
+ if (!problems) {
+ const { minimum, maximum } = localBounds(part.geometry);
+ const diagonal = Math.hypot(maximum[0] - minimum[0], maximum[1] - minimum[1], maximum[2] - minimum[2]);
+ problems = countMeshEdgeProblems(part.geometry.positions, part.geometry.indices, diagonal * 1e-6);
+ edgeProblemsByGeometry.set(part.geometry, problems);
+ }
+ if (problems.open > 0) throw new RangeError(`${label}.part "${part.name}" is not a closed mesh (${problems.open} open edges), so it has no inside; close it or use fit "bounds"`);
+ if (problems.inconsistent > 0) throw new RangeError(`${label}.part "${part.name}" has faces wound against each other (${problems.inconsistent} edges), so the rasterizer cannot tell inside from outside; flip those faces or use the path tracer`);
+}
+
function findPart(parts, name, label) {
const part = parts.find(candidate => candidate.name === name);
if (part) return part;
@@ -52,10 +111,10 @@ function findPart(parts, name, label) {
}
/**
- * Returns the resolved volumes with every mesh volume bound to its part. Pass `meshFit: false` for the rasterizer, which draws
- * the bounds only. Volumes that are not meshes are returned as they are.
+ * Returns the resolved volumes with every mesh volume bound to its part. A volume with fit "bounds" becomes a box; one with
+ * fit "mesh" keeps its part and also carries the bounds. Volumes that are not meshes are returned as they are.
*/
-export function bindLocalVolumeParts(volumes, parts, { meshFit = true, label = 'environment.localVolumes' } = {}) {
+export function bindLocalVolumeParts(volumes, parts, { label = 'environment.localVolumes' } = {}) {
if (!volumes.some(volume => volume.shape === 'mesh')) return volumes;
return volumes.map((volume, index) => {
if (volume.shape !== 'mesh') return volume;
@@ -65,7 +124,6 @@ export function bindLocalVolumeParts(volumes, parts, { meshFit = true, label = '
if (part.skin) throw new RangeError(`${field}.part "${part.name}" is skinned; a mesh volume needs a rigid part`);
const bounds = orientedBounds(part, field);
if (volume.fit === 'mesh') {
- if (!meshFit) throw new RangeError(`${field}.fit "mesh" is traced by the CPU path tracer only; the rasterizer draws the part's bounds. Use fit "bounds" or the path tracer with backend "cpu".`);
return { ...volume, ...bounds, part };
}
const rest = { ...volume };
diff --git a/src/local-volumes.js b/src/local-volumes.js
index ff09be36..c9f770e6 100644
--- a/src/local-volumes.js
+++ b/src/local-volumes.js
@@ -66,22 +66,28 @@ export function resolveLocalVolumes(input, label = 'localVolumes') {
/** The uniform arrays the post shader reads, one vec4 per volume. Pass resolved volumes. */
export function createLocalVolumeBuffers() {
const size = MAX_LOCAL_VOLUMES * 4;
- return { count: 0, centers: new Float32Array(size), extents: new Float32Array(size), absorption: new Float32Array(size), emission: new Float32Array(size), steps: new Float32Array(size), axes: new Float32Array(size * 3) };
+ return { count: 0, meshParts: [], centers: new Float32Array(size), extents: new Float32Array(size), absorption: new Float32Array(size), emission: new Float32Array(size), steps: new Float32Array(size), axes: new Float32Array(size * 3) };
}
const IDENTITY_AXES = Object.freeze([1, 0, 0, 0, 1, 0, 0, 0, 1]);
+// The w of a packed centre tells the shader how to find the volume: 0 a box, 1 a sphere, 2 a closed mesh read from the mesh pass.
+export const PACKED_SHAPE_MESH = 2;
+
export function packLocalVolumes(volumes, buffers) {
- if (volumes.some(volume => volume.shape === 'mesh')) throw new RangeError('A mesh volume names a part: bind it with bindLocalVolumeParts before packing');
+ if (volumes.some(volume => volume.shape === 'mesh' && !volume.center)) throw new RangeError('A mesh volume names a part: bind it with bindLocalVolumeParts before packing');
buffers.count = volumes.length;
+ buffers.meshParts = [];
volumes.forEach((volume, index) => {
const at = index * 4;
const half = volume.shape === 'sphere' ? [volume.radius, volume.radius, volume.radius] : volume.size.map(extent => extent / 2);
- buffers.centers.set([...volume.center, volume.shape === 'sphere' ? 1 : 0], at);
+ const packedShape = volume.shape === 'sphere' ? 1 : volume.part ? PACKED_SHAPE_MESH : 0;
+ buffers.centers.set([...volume.center, packedShape], at);
buffers.extents.set([...half, volume.falloff], at);
buffers.absorption.set([...volume.absorption.map(channel => channel * volume.density), volume.noiseAmount], at);
buffers.emission.set([...volume.emission.map(channel => channel * volume.emissionStrength), volume.noiseScale], at);
- buffers.steps.set([volume.steps, 0, 0, 0], at);
+ buffers.steps.set([volume.steps, packedShape === PACKED_SHAPE_MESH ? buffers.meshParts.length : 0, 0, 0], at);
+ if (packedShape === PACKED_SHAPE_MESH) buffers.meshParts.push({ part: volume.part, label: `environment.localVolumes[${index}]` });
const axes = volume.axes ?? IDENTITY_AXES;
for (let axis = 0; axis < 3; axis++) buffers.axes.set([axes[axis * 3], axes[axis * 3 + 1], axes[axis * 3 + 2], 0], (index * 3 + axis) * 4);
});
diff --git a/src/mesh-volume-pass.js b/src/mesh-volume-pass.js
new file mode 100644
index 00000000..53914991
--- /dev/null
+++ b/src/mesh-volume-pass.js
@@ -0,0 +1,216 @@
+import { compileProgram } from './gl-program.js';
+import { assertClosedMesh } from './local-volume-parts.js';
+
+// The rasterizer's half of a local volume with shape "mesh" and fit "mesh" (issue 807). Before the post pass, each such part is drawn
+// without culling into a layer of one float target, and the post shader reads the result per pixel. A ray crosses a closed mesh an even
+// number of times, and the length it spends inside is the sum over the back faces of (distance - the distance of each front face):
+// channel R adds +distance at a back face and -distance at a front face, each clamped to [0, distance of the first surface];
+// channel G keeps the nearest front face distance (the entry, used to order the volumes).
+// That sum is exact for any number of shells and any shape, so there is no shell limit. The post shader then integrates the volume
+// over that length in closed form, which is exact because a mesh volume has the same density everywhere inside (no falloff, no noise).
+// The cost is two draws of the part per frame, so a scene without mesh volumes pays nothing.
+// Limits: the mesh must be closed and rigid; geometry nearer than the camera's near plane is clipped, so a ray loses the stretch
+// between the eye and the near plane; the target is as wide as the canvas and as tall as the canvas times the number of mesh volumes.
+
+const MESH_VERTEX = `#version 300 es
+precision highp float;
+layout(location = 0) in vec3 position;
+uniform mat4 viewProjection;
+uniform mat4 model;
+out vec3 worldPosition;
+void main() {
+ vec4 world = model * vec4(position, 1.0);
+ worldPosition = world.xyz;
+ gl_Position = viewProjection * world;
+}`;
+
+const MESH_FRAGMENT = `#version 300 es
+precision highp float;
+precision highp sampler2D;
+in vec3 worldPosition;
+uniform sampler2D sceneDepth;
+uniform mat4 inverseViewProjection;
+uniform vec3 eye;
+uniform float orthographic;
+uniform vec2 resolution;
+uniform float layerOffset;
+uniform float entryPass;
+out vec4 result;
+void main() {
+ vec2 pixel = vec2(gl_FragCoord.x, gl_FragCoord.y - layerOffset);
+ vec2 clipXY = pixel / resolution * 2.0 - 1.0;
+ vec4 farPoint = inverseViewProjection * vec4(clipXY, 1.0, 1.0);
+ vec4 nearPoint = inverseViewProjection * vec4(clipXY, -1.0, 1.0);
+ vec3 origin = orthographic > 0.5 ? nearPoint.xyz / nearPoint.w : eye;
+ vec3 direction = normalize(farPoint.xyz - origin * farPoint.w);
+ float depth = texelFetch(sceneDepth, ivec2(pixel), 0).r;
+ vec4 surfacePoint = inverseViewProjection * vec4(clipXY, depth * 2.0 - 1.0, 1.0);
+ float surfaceDistance = 1e30;
+ if (surfacePoint.w > 0.0) surfaceDistance = max(0.0, dot(surfacePoint.xyz - origin * surfacePoint.w, direction) / surfacePoint.w);
+ float distanceAlongRay = clamp(dot(worldPosition - origin, direction), 0.0, surfaceDistance);
+ if (entryPass > 0.5) result = vec4(0.0, gl_FrontFacing ? distanceAlongRay : 1e30, 0.0, 0.0);
+ else result = vec4(gl_FrontFacing ? -distanceAlongRay : distanceAlongRay, 0.0, 0.0, 0.0);
+}`;
+
+const UNIFORM_NAMES = ['viewProjection', 'model', 'sceneDepth', 'inverseViewProjection', 'eye', 'orthographic', 'resolution', 'layerOffset', 'entryPass'];
+const NO_ENTRY = 1e30;
+
+// Positive when the triangles wind counter-clockwise seen from outside, which is what the draw's front faces rely on.
+function signedVolume(positions, indices) {
+ let total = 0;
+ for (let corner = 0; corner < indices.length; corner += 3) {
+ const a = indices[corner] * 3;
+ const b = indices[corner + 1] * 3;
+ const c = indices[corner + 2] * 3;
+ total += positions[a] * (positions[b + 1] * positions[c + 2] - positions[b + 2] * positions[c + 1])
+ + positions[a + 1] * (positions[b + 2] * positions[c] - positions[b] * positions[c + 2])
+ + positions[a + 2] * (positions[b] * positions[c + 1] - positions[b + 1] * positions[c]);
+ }
+ return total;
+}
+
+function matrixDeterminant(matrix) {
+ return matrix[0] * (matrix[5] * matrix[10] - matrix[6] * matrix[9])
+ - matrix[4] * (matrix[1] * matrix[10] - matrix[2] * matrix[9])
+ + matrix[8] * (matrix[1] * matrix[6] - matrix[2] * matrix[5]);
+}
+
+function uploadGeometry(context, geometry) {
+ const vertexArray = context.createVertexArray();
+ context.bindVertexArray(vertexArray);
+ const positionBuffer = context.createBuffer();
+ context.bindBuffer(context.ARRAY_BUFFER, positionBuffer);
+ context.bufferData(context.ARRAY_BUFFER, geometry.positions instanceof Float32Array ? geometry.positions : new Float32Array(geometry.positions), context.STATIC_DRAW);
+ context.enableVertexAttribArray(0);
+ context.vertexAttribPointer(0, 3, context.FLOAT, false, 0, 0);
+ const shortIndices = geometry.indices instanceof Uint16Array;
+ const indexBuffer = context.createBuffer();
+ context.bindBuffer(context.ELEMENT_ARRAY_BUFFER, indexBuffer);
+ context.bufferData(context.ELEMENT_ARRAY_BUFFER, shortIndices || geometry.indices instanceof Uint32Array ? geometry.indices : new Uint32Array(geometry.indices), context.STATIC_DRAW);
+ context.bindVertexArray(null);
+ return {
+ vertexArray,
+ buffers: [positionBuffer, indexBuffer],
+ count: geometry.indices.length,
+ indexType: shortIndices ? context.UNSIGNED_SHORT : context.UNSIGNED_INT,
+ insideOut: signedVolume(geometry.positions, geometry.indices) < 0,
+ };
+}
+
+function createState(context) {
+ if (!context.getExtension('EXT_float_blend')) throw new Error('Local volumes with fit "mesh" need the WebGL extension EXT_float_blend, which this browser does not offer; use fit "bounds"');
+ const program = compileProgram(context, MESH_VERTEX, MESH_FRAGMENT);
+ const uniforms = Object.fromEntries(UNIFORM_NAMES.map(name => [name, context.getUniformLocation(program, name)]));
+ return { program, uniforms, geometries: new Map(), target: null };
+}
+
+function allocateTarget(renderer, state, layers) {
+ const context = renderer.context;
+ const width = renderer.canvas.width;
+ const height = renderer.canvas.height * layers;
+ if (state.target?.width === width && state.target.height === height) return;
+ if (height > context.getParameter(context.MAX_TEXTURE_SIZE)) {
+ throw new RangeError(`${layers} mesh volumes at ${width}x${renderer.canvas.height} need a ${height} pixel tall buffer, past this device's limit of ${context.getParameter(context.MAX_TEXTURE_SIZE)}; use fewer volumes with fit "mesh", a smaller canvas or fit "bounds"`);
+ }
+ releaseTarget(renderer, state);
+ const texture = context.createTexture();
+ context.bindTexture(context.TEXTURE_2D, texture);
+ context.texImage2D(context.TEXTURE_2D, 0, context.RG32F, width, height, 0, context.RG, context.FLOAT, null);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.NEAREST);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.NEAREST);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
+ const framebuffer = context.createFramebuffer();
+ context.bindFramebuffer(context.FRAMEBUFFER, framebuffer);
+ context.framebufferTexture2D(context.FRAMEBUFFER, context.COLOR_ATTACHMENT0, context.TEXTURE_2D, texture, 0);
+ if (context.checkFramebufferStatus(context.FRAMEBUFFER) !== context.FRAMEBUFFER_COMPLETE) throw new Error('The mesh volume buffer could not be allocated; use fit "bounds" or a smaller canvas');
+ state.target = { texture, framebuffer, width, height };
+ renderer.renderTargets.record('meshVolumes', { width, height, format: 'RG32F' });
+}
+
+function releaseTarget(renderer, state) {
+ if (!state.target) return;
+ renderer.context.deleteTexture(state.target.texture);
+ renderer.context.deleteFramebuffer(state.target.framebuffer);
+ state.target = null;
+ renderer.renderTargets.release('meshVolumes');
+}
+
+function drawPart(context, state, part) {
+ let gpu = state.geometries.get(part.geometry);
+ if (!gpu) {
+ gpu = uploadGeometry(context, part.geometry);
+ state.geometries.set(part.geometry, gpu);
+ }
+ const matrix = part.matrix ?? [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
+ context.uniformMatrix4fv(state.uniforms.model, false, matrix);
+ context.frontFace(gpu.insideOut !== matrixDeterminant(matrix) < 0 ? context.CW : context.CCW);
+ context.bindVertexArray(gpu.vertexArray);
+ context.drawElements(context.TRIANGLES, gpu.count, gpu.indexType, 0);
+}
+
+/**
+ * Draws every fit "mesh" volume of the frame into the mesh buffer: layer n holds the volume with slice n, R the length inside along
+ * each pixel's ray and G the nearest entry. The caller rebinds the post framebuffer afterwards through the restore this makes.
+ * Returns the texture, or null when the frame has no such volume.
+ */
+export function drawMeshVolumes(renderer, frame) {
+ const meshParts = renderer.localVolumeBuffers?.meshParts ?? [];
+ if (meshParts.length === 0) return null;
+ const context = renderer.context;
+ const state = (renderer.meshVolumePass ??= createState(context));
+ for (const { part, label } of meshParts) assertClosedMesh(part, label);
+ allocateTarget(renderer, state, meshParts.length);
+ const { width, height } = renderer.canvas;
+ const blendWasOn = context.isEnabled(context.BLEND);
+ context.bindFramebuffer(context.FRAMEBUFFER, state.target.framebuffer);
+ context.colorMask(true, true, true, true);
+ context.clearBufferfv(context.COLOR, 0, [0, NO_ENTRY, 0, 0]);
+ context.useProgram(state.program);
+ context.disable(context.DEPTH_TEST);
+ context.disable(context.CULL_FACE);
+ context.enable(context.BLEND);
+ context.blendFunc(context.ONE, context.ONE);
+ context.activeTexture(context.TEXTURE1);
+ context.bindTexture(context.TEXTURE_2D, renderer.depthTexture);
+ context.uniform1i(state.uniforms.sceneDepth, 1);
+ context.uniformMatrix4fv(state.uniforms.viewProjection, false, renderer.viewProjection);
+ context.uniformMatrix4fv(state.uniforms.inverseViewProjection, false, renderer.inverseViewProjection);
+ context.uniform3fv(state.uniforms.eye, frame.eye);
+ context.uniform1f(state.uniforms.orthographic, frame.projection === 'orthographic' ? 1 : 0);
+ context.uniform2f(state.uniforms.resolution, width, height);
+ meshParts.forEach(({ part }, slice) => {
+ context.viewport(0, slice * height, width, height);
+ context.uniform1f(state.uniforms.layerOffset, slice * height);
+ context.uniform1f(state.uniforms.entryPass, 0);
+ context.blendEquation(context.FUNC_ADD);
+ context.colorMask(true, false, false, false);
+ drawPart(context, state, part);
+ context.uniform1f(state.uniforms.entryPass, 1);
+ context.blendEquation(context.MIN);
+ context.colorMask(false, true, false, false);
+ drawPart(context, state, part);
+ });
+ context.colorMask(true, true, true, true);
+ context.frontFace(context.CCW);
+ context.blendEquation(context.FUNC_ADD);
+ if (!blendWasOn) context.disable(context.BLEND);
+ context.bindVertexArray(null);
+ context.bindFramebuffer(context.FRAMEBUFFER, frame.outputFramebuffer ?? null);
+ context.viewport(0, 0, width, height);
+ return state.target.texture;
+}
+
+/** Frees the program, the buffer and the uploaded meshes. */
+export function disposeMeshVolumePass(renderer) {
+ const state = renderer.meshVolumePass;
+ if (!state) return;
+ const context = renderer.context;
+ releaseTarget(renderer, state);
+ for (const gpu of state.geometries.values()) {
+ context.deleteVertexArray(gpu.vertexArray);
+ gpu.buffers.forEach(buffer => context.deleteBuffer(buffer));
+ }
+ context.deleteProgram(state.program);
+ renderer.meshVolumePass = null;
+}
diff --git a/src/path-tracer-volume.js b/src/path-tracer-volume.js
index 8b005445..3e5faee8 100644
--- a/src/path-tracer-volume.js
+++ b/src/path-tracer-volume.js
@@ -1,5 +1,6 @@
import { resolveVolumeFog } from './volumetric.js';
import { localVolumeInterval, localVolumeProfile } from './local-volumes.js';
+import { countOpenEdges } from './local-volume-parts.js';
import { buildTriangleBvh } from './triangle-bvh.js';
import { createTracer } from './ray-query.js';
@@ -69,36 +70,7 @@ const MAXIMUM_MESH_CROSSINGS = 64;
const MAXIMUM_MESH_SEGMENTS = MAXIMUM_MESH_CROSSINGS / 2;
const IDENTITY_MATRIX = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
-function weldedVertexIds(positions, tolerance) {
- const ids = new Map();
- const welded = new Uint32Array(positions.length / 3);
- for (let vertex = 0; vertex < welded.length; vertex++) {
- const key = `${Math.round(positions[vertex * 3] / tolerance)},${Math.round(positions[vertex * 3 + 1] / tolerance)},${Math.round(positions[vertex * 3 + 2] / tolerance)}`;
- if (!ids.has(key)) ids.set(key, ids.size);
- welded[vertex] = ids.get(key);
- }
- return welded;
-}
-
-/** The number of edges that do not join exactly two triangles, with vertices at the same place counted as one. 0 for a closed mesh. */
-export function countOpenEdges(positions, indices, tolerance) {
- const welded = weldedVertexIds(positions, tolerance);
- const uses = new Map();
- for (let corner = 0; corner < indices.length; corner += 3) {
- for (let edge = 0; edge < 3; edge++) {
- const from = welded[indices[corner + edge]];
- const to = welded[indices[corner + (edge + 1) % 3]];
- if (from === to) continue;
- const key = from < to ? `${from}-${to}` : `${to}-${from}`;
- uses.set(key, (uses.get(key) ?? 0) + 1);
- }
- }
- let open = 0;
- for (const count of uses.values()) {
- if (count !== 2) open++;
- }
- return open;
-}
+export { countOpenEdges };
/**
* Gives a mesh volume (shape "mesh", bound to its part by bindLocalVolumeParts) what the tracer needs: the part's triangles in
diff --git a/src/renderer.js b/src/renderer.js
index 700ede71..6d767405 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -2045,6 +2045,7 @@ export class Renderer67 {
const localVolumes = this.prepareLocalVolumes(environment.localVolumes);
if (fogSupport) fogSupport.packLights(this);
else if (this.fogLightBuffers) this.fogLightBuffers.count = 0;
+ if (localVolumes) requireFeatureCode('volumeFog').support.drawMeshVolumes(this, frame);
const post = this.selectPostProgram(this.postStepsOf(frame, effects, volume, occluded, fogSky, localVolumes));
context.useProgram(post.program);
if (this.look) viewTransformShader.bindLook(this, post.program);
@@ -2575,6 +2576,7 @@ export class Renderer67 {
dispose() {
this.fogFieldTexture?.dispose();
+ if (this.meshVolumePass) requireFeatureCode('volumeFog').support.disposeMeshVolumes(this);
this.releaseFrameTargets();
this.shadowCascades?.dispose(this.context, this.renderTargets);
viewTransformShader.dispose?.(this);
diff --git a/tests/local-volume-mesh-raster.test.js b/tests/local-volume-mesh-raster.test.js
new file mode 100644
index 00000000..bb339dd2
--- /dev/null
+++ b/tests/local-volume-mesh-raster.test.js
@@ -0,0 +1,236 @@
+// Fit "mesh" volumes in the rasterizer (issue 807): the mesh pass finds how long each pixel's ray spends inside a closed part, and
+// the post shader integrates the volume over that length. Checked against the CPU path tracer on an L-shaped room and against the
+// analytic length through two shells. Draws in headless Chrome (SwiftShader) from the Vite dev server; skips when Chrome cannot start.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { spawn } from 'node:child_process';
+import { freePort } from './helpers/free-port.js';
+import { renderPathTracedImage } from '../src/path-tracer.js';
+import { applyViewTransform } from '../src/view-transform.js';
+
+async function startServer(port) {
+ const server = spawn(process.execPath, ['node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
+ await new Promise((resolve, reject) => {
+ const timeout = setTimeout(() => reject(new Error('The dev server did not start.')), 30000);
+ server.stdout.on('data', (data) => {
+ if (/ready in/.test(data.toString())) {
+ clearTimeout(timeout);
+ resolve();
+ }
+ });
+ server.on('exit', (code) => {
+ clearTimeout(timeout);
+ reject(new Error(`The dev server exited ${code}.`));
+ });
+ });
+ return server;
+}
+
+// An L-shaped prism: three unit squares in x and z (the notch is x in 1..2, z in 1..2), extruded in y from 0 to 1.
+function lPrism(reverseWinding = false) {
+ const outline = [[0, 0], [1, 0], [2, 0], [2, 1], [1, 1], [1, 2], [0, 2], [0, 1]];
+ const positions = [];
+ for (const height of [0, 1]) for (const [x, z] of outline) positions.push(x, height, z);
+ const indices = [];
+ const top = outline.length;
+ for (const [a, b, c, d] of [[0, 1, 4, 7], [1, 2, 3, 4], [7, 4, 5, 6]]) {
+ indices.push(a, b, c, a, c, d, top + a, top + c, top + b, top + a, top + d, top + c);
+ }
+ for (let corner = 0; corner < outline.length; corner++) {
+ const next = (corner + 1) % outline.length;
+ indices.push(corner, top + next, next, corner, top + corner, top + next);
+ }
+ if (reverseWinding) {
+ for (let corner = 0; corner < indices.length; corner += 3) [indices[corner + 1], indices[corner + 2]] = [indices[corner + 2], indices[corner + 1]];
+ }
+ return { positions, indices };
+}
+
+// Two unit boxes two units apart along x, one closed mesh in two shells.
+function dumbbell() {
+ const corners = [0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1];
+ const faces = [0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 1, 5, 0, 5, 4, 3, 7, 6, 3, 6, 2, 0, 4, 7, 0, 7, 3, 1, 2, 6, 1, 6, 5];
+ const positions = [...corners];
+ for (let index = 0; index < corners.length; index += 3) positions.push(corners[index] + 3, corners[index + 1], corners[index + 2]);
+ return { positions, indices: [...faces, ...faces.map(index => index + 8)] };
+}
+
+const OPEN_BOX = { positions: dumbbell().positions.slice(0, 24), indices: dumbbell().indices.slice(0, 33) };
+const LOOKING_DOWN = { eye: [0, 3, 0], target: [0, 0, 0], up: [0, 0, -1], fov: 40 };
+// The L covers x and z from -0.4 to 0.4 with its notch where both are positive, from y 0.2 to 1.2.
+const L_ROOM_MATRIX = [0.4, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.4, 0, -0.4, 0.2, -0.4, 1];
+const MIXED_WINDING = { positions: dumbbell().positions.slice(0, 24), indices: [0, 1, 2, ...dumbbell().indices.slice(3, 36)] };
+const IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
+const SIZE = 64;
+
+// Draws each scenario to a float target and returns the frame with the volume and the frame without it, as the display shows them.
+// A white ground and a white wall at x = 8 give every ray something to dim.
+async function drawInChrome(page, scenarios) {
+ const task = async (list) => {
+ const { box } = await import('/src/geometry.js');
+ const { Renderer67 } = await import('/src/renderer.js');
+ const { createFloatOutput, readFloatPixels } = await import('/src/float-output.js');
+ await import('/src/fog-shader.js');
+ const canvas = document.createElement('canvas');
+ canvas.width = list.size;
+ canvas.height = list.size;
+ canvas.style.width = list.size + 'px';
+ canvas.style.height = list.size + 'px';
+ document.body.append(canvas);
+ const material = { tag: 'unlit', color: [1, 1, 1], metallic: 0, roughness: 1, emission: 0 };
+ const ground = { name: 'ground', geometry: box(0, 1), matrix: new Float32Array([40, 0, 0, 0, 0, 0.2, 0, 0, 0, 0, 40, 0, 0, -0.1, 0, 1]), material };
+ const wall = { name: 'wall', geometry: box(0, 1), matrix: new Float32Array([0.2, 0, 0, 0, 0, 40, 0, 0, 0, 0, 40, 0, 8, 0, 0, 1]), material };
+ const results = [];
+ for (const scenario of list.scenarios) {
+ const geometry = { positions: new Float32Array(scenario.mesh.positions), normals: new Float32Array(scenario.mesh.positions.length), indices: new Uint32Array(scenario.mesh.indices) };
+ const room = { name: 'room', visible: false, geometry, matrix: new Float32Array(scenario.matrix), material };
+ const renderer = new Renderer67(canvas, [ground, wall, room], { hdr: true, depth: true, samples: 1, pixelRatio: 1, ground: false });
+ const output = createFloatOutput(renderer.context, renderer.canvas.width, renderer.canvas.height);
+ const draw = (localVolumes) => {
+ const environment = { keyIntensity: 0, fillIntensity: 0, ambientIntensity: 0, localVolumes };
+ renderer.render({ ...scenario.camera, time: 0, environment, outputFramebuffer: output.framebuffer });
+ return readFloatPixels(renderer.context, output.framebuffer, renderer.canvas.width, renderer.canvas.height);
+ };
+ let message = null;
+ let transmittance = null;
+ try {
+ const plain = draw([]);
+ const filled = draw(scenario.localVolumes);
+ transmittance = { plain: Array.from(plain), filled: Array.from(filled) };
+ } catch (error) {
+ message = error.message;
+ }
+ results.push({ message, transmittance, glError: renderer.context.getError() });
+ output.dispose();
+ renderer.dispose?.();
+ }
+ return results;
+ };
+ const frames = await page.evaluate(task, { scenarios, size: SIZE });
+ return frames.map(frame => (frame.transmittance ? { ...frame, transmittance: transmittanceOf(frame.transmittance) } : frame));
+}
+
+// The display shows aces(light) ^ (1 / 2.2); this undoes both for a channel, so the ratio of two frames is the light that got through.
+function lightOf(displayed) {
+ let low = 0;
+ let high = 64;
+ for (let round = 0; round < 40; round++) {
+ const middle = (low + high) / 2;
+ if (applyViewTransform('aces', [middle, middle, middle])[0] ** (1 / 2.2) < displayed) low = middle;
+ else high = middle;
+ }
+ return low;
+}
+
+function transmittanceOf({ plain, filled }) {
+ return Array.from(filled, (value, index) => (index % 4 === 3 || plain[index] <= 0 ? 1 : lightOf(value) / lightOf(plain[index])));
+}
+
+async function openPage(context) {
+ let chromium;
+ try {
+ ({ chromium } = await import('playwright'));
+ } catch {
+ context.skip('playwright is not installed');
+ return null;
+ }
+ const port = await freePort();
+ const server = await startServer(port);
+ try {
+ const browser = await chromium.launch({ headless: true, args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--enable-webgl', '--ignore-gpu-blocklist'] });
+ const page = await browser.newPage({ viewport: { width: SIZE, height: SIZE } });
+ await page.goto(`http://127.0.0.1:${port}/`);
+ return {
+ page,
+ async close() {
+ await browser.close();
+ server.kill();
+ },
+ };
+ } catch (error) {
+ server.kill();
+ context.skip(`Chrome did not start: ${error.message.split('\n')[0]}`);
+ return null;
+ }
+}
+
+function groundGeometry() {
+ const half = 20;
+ return {
+ positions: new Float32Array([-half, 0, half, half, 0, half, half, 0, -half, -half, 0, -half]),
+ normals: new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),
+ uvs: new Float32Array([0, 1, 1, 1, 1, 0, 0, 0]),
+ indices: new Uint32Array([0, 1, 2, 0, 2, 3]),
+ };
+}
+
+async function cpuTransmittance(fit) {
+ const material = { tag: 'surface', metallic: 0, roughness: 1, color: [0, 0, 0], ior: 1, emission: 1, emissiveColor: [1, 1, 1] };
+ const mesh = lPrism();
+ const geometry = { positions: new Float32Array(mesh.positions), indices: new Uint32Array(mesh.indices), normals: new Float32Array(mesh.positions.length) };
+ const room = { name: 'room', visible: false, material, matrix: L_ROOM_MATRIX, geometry };
+ const environment = { localVolumes: [{ shape: 'mesh', part: 'room', fit, density: 1, falloff: 0, steps: 32 }] };
+ const { linear } = await renderPathTracedImage({ parts: [{ name: 'ground', geometry: groundGeometry(), material }, room], environment, camera: LOOKING_DOWN }, { width: 16, height: 16, denoise: false, threads: 1, bounces: 1, samples: 1 });
+ return (column, row) => linear[(row * 16 + column) * 3];
+}
+
+// WebGL stores rows bottom to top, so the row is counted from the other end. The mean of the inner 2 by 2 pixels of the 4 by 4 block of the 64 by 64 frame that one pixel of the 16 by 16 CPU image covers.
+function blockMean(transmittance, column, row) {
+ let total = 0;
+ for (let y = row * 4 + 1; y < row * 4 + 3; y++) for (let x = column * 4 + 1; x < column * 4 + 3; x++) total += transmittance[((SIZE - 1 - y) * SIZE + x) * 4];
+ return total / 4;
+}
+
+test('the rasterizer fills an L-shaped closed mesh like the CPU path tracer: arms dimmed, notch clear', async (context) => {
+ const opened = await openPage(context);
+ if (!opened) return;
+ try {
+ const smoke = { shape: 'mesh', part: 'room', density: 1, falloff: 0, steps: 16 };
+ const [meshFit, boundsFit, reversed] = await drawInChrome(opened.page, [
+ { mesh: lPrism(), matrix: L_ROOM_MATRIX, camera: LOOKING_DOWN, localVolumes: [{ ...smoke, fit: 'mesh' }] },
+ { mesh: lPrism(), matrix: L_ROOM_MATRIX, camera: LOOKING_DOWN, localVolumes: [{ ...smoke, fit: 'bounds' }] },
+ { mesh: lPrism(true), matrix: L_ROOM_MATRIX, camera: LOOKING_DOWN, localVolumes: [{ ...smoke, fit: 'mesh' }] },
+ ]);
+ for (const result of [meshFit, boundsFit, reversed]) {
+ assert.equal(result.message, null);
+ assert.equal(result.glError, 0);
+ }
+ const cpu = await cpuTransmittance('mesh');
+ const regions = { arm: [5, 5], notch: [10, 10], outside: [1, 1] };
+ for (const [name, [column, row]] of Object.entries(regions)) {
+ const raster = blockMean(meshFit.transmittance, column, row);
+ assert.ok(Math.abs(raster - cpu(column, row)) < 0.03, `${name}: raster ${raster} against CPU ${cpu(column, row)}`);
+ assert.equal(blockMean(reversed.transmittance, column, row), raster, `${name}: a mesh wound the other way fills the same`);
+ }
+ assert.equal(blockMean(meshFit.transmittance, 10, 10), 1, 'the notch is clear');
+ assert.ok(blockMean(boundsFit.transmittance, 10, 10) < 0.5, 'bounds fill the notch');
+ } finally {
+ await opened.close();
+ }
+});
+
+test('the rasterizer sums every shell a ray crosses, from outside and from inside, and refuses an open or inconsistently wound mesh', async (context) => {
+ const opened = await openPage(context);
+ if (!opened) return;
+ try {
+ // A ray along +x at height 0.5 crosses 1 unit of each box of the dumbbell and a 2 unit gap.
+ const side = { eye: [-6, 0.5, 0.5], target: [0, 0.5, 0.5], up: [0, 1, 0], fov: 20 };
+ const smoke = { shape: 'mesh', part: 'room', fit: 'mesh', density: 0.7, falloff: 0, steps: 16 };
+ const [shells, inside, open, mixed] = await drawInChrome(opened.page, [
+ { mesh: dumbbell(), matrix: IDENTITY, camera: side, localVolumes: [smoke] },
+ // The camera sits in the middle of the first box, so the ray leaves it after half a unit and crosses the second box.
+ { mesh: dumbbell(), matrix: IDENTITY, camera: { eye: [0.5, 0.5, 0.5], target: [6, 0.5, 0.5], up: [0, 1, 0], fov: 20 }, localVolumes: [smoke] },
+ { mesh: OPEN_BOX, matrix: IDENTITY, camera: side, localVolumes: [smoke] },
+ { mesh: MIXED_WINDING, matrix: IDENTITY, camera: side, localVolumes: [smoke] },
+ ]);
+ const middle = (SIZE / 2) * SIZE + SIZE / 2;
+ assert.equal(shells.message, null);
+ assert.ok(Math.abs(shells.transmittance[middle * 4] - Math.exp(-0.7 * 2)) < 0.01, `two shells: ${shells.transmittance[middle * 4]}`);
+ assert.equal(inside.message, null);
+ assert.ok(Math.abs(inside.transmittance[middle * 4] - Math.exp(-0.7 * 1.5)) < 0.01, `from inside: ${inside.transmittance[middle * 4]}`);
+ assert.match(open.message, /environment\.localVolumes\[0\]\.part "room" is not a closed mesh \(\d+ open edges\)/);
+ assert.match(mixed.message, /environment\.localVolumes\[0\]\.part "room" has faces wound against each other/);
+ } finally {
+ await opened.close();
+ }
+});
diff --git a/tests/local-volume-parts-raster.test.js b/tests/local-volume-parts-raster.test.js
index 7cf9113a..6605ad78 100644
--- a/tests/local-volume-parts-raster.test.js
+++ b/tests/local-volume-parts-raster.test.js
@@ -60,7 +60,7 @@ async function drawInChrome(page, scenarios) {
return page.evaluate(task, scenarios);
}
-test('the rasterizer draws a mesh volume as the oriented bounds of its part and refuses a mesh fit', async (context) => {
+test('the rasterizer draws a mesh volume with fit bounds as the oriented bounds of its part', async (context) => {
let chromium;
try {
({ chromium } = await import('playwright'));
@@ -81,18 +81,16 @@ test('the rasterizer draws a mesh volume as the oriented bounds of its part and
const page = await browser.newPage({ viewport: { width: 64, height: 64 } });
await page.goto(`http://127.0.0.1:${port}/`);
const smoke = { density: 2, falloff: 0, steps: 24 };
- const [plain, byPart, byBox, wrongFit, unknown] = await drawInChrome(page, [
+ const [plain, byPart, byBox, unknown] = await drawInChrome(page, [
[],
[{ shape: 'mesh', part: 'room', ...smoke }],
[{ shape: 'box', center: [0, 0, 2], size: [3, 2, 1], ...smoke }],
- [{ shape: 'mesh', part: 'room', fit: 'mesh' }],
[{ shape: 'mesh', part: 'hall' }],
]);
assert.equal(byPart.message, null);
assert.equal(byPart.glError, 0);
assert.notDeepEqual(byPart.pixels, plain.pixels, 'the part fills a volume that dims the wall');
assert.deepEqual(byPart.pixels, byBox.pixels, 'the oriented bounds of the turned part draw as the box they equal');
- assert.match(wrongFit.message, /environment\.localVolumes\[0\]\.fit "mesh" is traced by the CPU path tracer only/);
assert.match(unknown.message, /environment\.localVolumes\[0\]\.part "hall" names no part in the scene; parts: wall, room/);
} finally {
await browser?.close();
diff --git a/tests/local-volume-parts.test.js b/tests/local-volume-parts.test.js
index 807d0d59..c7b1420a 100644
--- a/tests/local-volume-parts.test.js
+++ b/tests/local-volume-parts.test.js
@@ -104,9 +104,16 @@ test('the bounds of a rotated, scaled part become an oriented box that the inter
assert.throws(() => packLocalVolumes([volume], createLocalVolumeBuffers()), /bindLocalVolumeParts/);
});
-test('the rasterizer draws bounds only: a fit "mesh" volume throws a message that names the field', () => {
+test('a fit "mesh" volume packs for the rasterizer as its bounds plus a slot in the mesh buffer', () => {
const [volume] = resolveLocalVolumes([{ shape: 'mesh', part: 'room', fit: 'mesh' }]);
- assert.throws(() => bindLocalVolumeParts([volume], [part('room', unitBox())], { meshFit: false }), /environment\.localVolumes\[0\]\.fit "mesh" is traced by the CPU path tracer only/);
+ const room = part('room', unitBox());
+ const buffers = packLocalVolumes(bindLocalVolumeParts([volume], [room]), createLocalVolumeBuffers());
+ assert.equal(buffers.centers[3], 2, 'packed as a mesh shape');
+ assert.equal(buffers.steps[1], 0, 'the first slot');
+ assert.deepEqual(buffers.meshParts, [{ part: room, label: 'environment.localVolumes[0]' }]);
+ const bounds = packLocalVolumes(bindLocalVolumeParts(resolveLocalVolumes([{ shape: 'mesh', part: 'room' }]), [room]), createLocalVolumeBuffers());
+ assert.equal(bounds.centers[3], 0, 'fit bounds packs as a box');
+ assert.equal(bounds.meshParts.length, 0);
});
test('an open mesh has no inside: it throws with the open edge count, and a closed one reports none', () => {