3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Read buried wall ends and floor rims as occluded in the room sky trace (#2166)
Nguyen Xuan An30362da1e3aa
diff --git a/changes/2166.md b/changes/2166.mdnew file mode 100644index 00000000..5313857f--- /dev/null+++ b/changes/2166.md@@ -0,0 +1 @@+- The window room's floor sky trace no longer lights bright slivers at the back wall's corners: a wall end or floor rim buried in the wall beside it is read as occluded instead of smearing a half-lit value onto the visible face (#2166).diff --git a/changes/2166.size.md b/changes/2166.size.mdnew file mode 100644index 00000000..7c460ca2--- /dev/null+++ b/changes/2166.size.md@@ -0,0 +1,2 @@+- entry:floor-sky-visibility: the burial test (a few extra rays per room vertex, run once when the parts are prepared) removes the bright slivers the window room drew at the back wall's corners.+- scene:agent-scene+lazy: the same code, counted again in the scene that loads this module lazily.diff --git a/src/floor-sky-visibility.js b/src/floor-sky-visibility.jsindex f7864dfe..ed34d29c 100644--- a/src/floor-sky-visibility.js+++ b/src/floor-sky-visibility.js@@ -213,6 +213,63 @@ function cutGeometry(geometry, matrix, maximumEdge, onlyUpward = true) {return { builder, indices, tracedVertices };}+// Directions that no wall or floor lines up with, so a point on a face is never tested along that face.+const BURIAL_DIRECTIONS = [[0.3713, 0.8123, 0.4521], [-0.6431, 0.2917, 0.7102]].map(raw => {+ const length = Math.hypot(...raw);+ return raw.map(value => value / length);+});++function triangleSeenFromBehind(world, triangle, direction) {+ const [first, second, third] = [0, 1, 2].map(slot => world.indices[triangle * 3 + slot] * 3);+ const { positions } = world;+ const edgeOne = [0, 1, 2].map(axis => positions[second + axis] - positions[first + axis]);+ const edgeTwo = [0, 1, 2].map(axis => positions[third + axis] - positions[first + axis]);+ const normal = [edgeOne[1] * edgeTwo[2] - edgeOne[2] * edgeTwo[1], edgeOne[2] * edgeTwo[0] - edgeOne[0] * edgeTwo[2], edgeOne[0] * edgeTwo[1] - edgeOne[1] * edgeTwo[0]];+ return normal[0] * direction[0] + normal[1] * direction[1] + normal[2] * direction[2] > 0;+}++/**+ * True when the point lies inside a solid: along one of a few fixed directions, or its opposite, the first surface met is seen from+ * behind. Touching boxes share their faces, so one direction can stop at the neighbour's face from outside; any direction that+ * leaves through the back of a surface is enough. A wall's end or a floor's rim that runs into the wall beside it is buried this way,+ * and its traced value is meaningless: the triangles around it are large, so it would smear a bright share onto the visible face (#2166).+ */+function isBuried(tracer, world, point) {+ for (const base of BURIAL_DIRECTIONS) {+ for (const sign of [1, -1]) {+ const direction = base.map(value => value * sign);+ const hit = tracer.trace(point[0], point[1], point[2], direction[0], direction[1], direction[2], Infinity, false);+ if (hit.nearestTriangle >= 0 && triangleSeenFromBehind(world, hit.nearestTriangle, direction)) return true;+ }+ }+ return false;+}++// The mean of the triangle centres around each vertex, in the part's own space: a vertex on the rim of a face moves toward its own face from there.+function incidentCentres(positions, indices, vertexCount) {+ const sums = new Float64Array(vertexCount * 3);+ const counts = new Uint32Array(vertexCount);+ for (let corner = 0; corner < indices.length; corner += 3) {+ for (let axis = 0; axis < 3; axis++) {+ const centre = (positions[indices[corner] * 3 + axis] + positions[indices[corner + 1] * 3 + axis] + positions[indices[corner + 2] * 3 + axis]) / 3;+ for (let slot = 0; slot < 3; slot++) sums[indices[corner + slot] * 3 + axis] += centre;+ }+ for (let slot = 0; slot < 3; slot++) counts[indices[corner + slot]]++;+ }+ return { sums, counts };+}++// The vertex moved a little off its face and toward the middle of its own triangles, so a vertex exactly on another part's face is tested inside that part.+function isBuriedInside(tracer, world, point, worldNormal, offset, centres, vertex, matrix) {+ const count = centres.counts[vertex] || 1;+ const local = [0, 1, 2].map(axis => centres.sums[vertex * 3 + axis] / count);+ const middle = [0, 1, 2].map(axis => matrix[axis] * local[0] + matrix[4 + axis] * local[1] + matrix[8 + axis] * local[2] + matrix[12 + axis]);+ const toMiddle = [0, 1, 2].map(axis => middle[axis] - point[axis]);+ const length = Math.hypot(...toMiddle) || 1;+ const probe = [0, 1, 2].map(axis => point[axis] + worldNormal[axis] * offset + (toMiddle[axis] / length) * offset * 2);+ return isBuried(tracer, world, probe);+}+function fixedSamples(sampleCount) {return Array.from({ length: sampleCount }, (unused, sample) => [(sample + 0.5) / sampleCount, (radicalInverse(sample) + FIXED_ROTATION) % 1]);}@@ -272,6 +329,7 @@ export function applyFloorSkyVisibility(parts, { raysPerTexel = FLOOR_SKY_RAYS,if (totalRays + tracedVertices.size * raysPerTexel > maximumRays) return unchanged('too many rays');const vertexCount = builder.positions.length / 3;const skyVisibility = new Float32Array(vertexCount).fill(1);+ const centres = everySurface ? incidentCentres(builder.positions, indices, vertexCount) : null;for (const vertex of tracedVertices) {if (performance.now() - started > maxSeconds * 1000) return unchanged('passed maxSeconds');const local = builder.positions.slice(vertex * 3, vertex * 3 + 3);@@ -282,6 +340,10 @@ export function applyFloorSkyVisibility(parts, { raysPerTexel = FLOOR_SKY_RAYS,worldNormal[0] /= length;worldNormal[1] /= length;worldNormal[2] /= length;+ if (centres && isBuriedInside(tracer, world, point, worldNormal, offset, centres, vertex, matrix)) {+ skyVisibility[vertex] = 1 - strength;+ continue;+ }let escaped = 0;for (const [firstUnit, secondUnit] of samples) {cosineDirection(worldNormal, firstUnit, secondUnit, direction);diff --git a/tests/floor-sky-visibility.test.js b/tests/floor-sky-visibility.test.jsindex a592b60b..8063a436 100644--- a/tests/floor-sky-visibility.test.js+++ b/tests/floor-sky-visibility.test.js@@ -113,3 +113,60 @@ test('a trace that passes its time limit returns the parts as they were', () =>assert.equal(result.parts, parts);assert.equal(result.report.reason, 'passed maxSeconds');});++// A box of unit size with flat faces, six faces of four vertices each, scaled and moved by its matrix.+function boxPart(name, [x, y, z], [width, height, depth]) {+ const faces = [[[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[-1, 0, 0], [0, 0, 1], [0, 1, 0]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]], [[0, -1, 0], [1, 0, 0], [0, 0, 1]], [[0, 0, 1], [1, 0, 0], [0, 1, 0]], [[0, 0, -1], [0, 1, 0], [1, 0, 0]]];+ const positions = [];+ const normals = [];+ const indices = [];+ for (const [normal, tangent, bitangent] of faces) {+ const first = positions.length / 3;+ for (const [along, across] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {+ positions.push(...[0, 1, 2].map(axis => 0.5 * (normal[axis] + along * tangent[axis] + across * bitangent[axis])));+ normals.push(...normal);+ }+ indices.push(first, first + 1, first + 2, first, first + 2, first + 3);+ }+ const matrix = [width, 0, 0, 0, 0, height, 0, 0, 0, 0, depth, 0, x, y, z, 1];+ const geometry = { positions: new Float32Array(positions), normals: new Float32Array(normals), indices: new Uint32Array(indices) };+ return { name, geometry, matrix, material: { color: [0.7, 0.7, 0.7] } };+}++// The window room of the interior exposure probe (#1334): walls of 0.2 m that overlap at the corners, a window only in the front wall.+function windowRoomParts() {+ return [+ boxPart('floor', [0, -0.1, 0.25], [8.4, 0.2, 6.9]),+ boxPart('ceiling', [0, 3.1, 0.25], [8.4, 0.2, 6.9]),+ boxPart('wall-left', [-4.1, 1.5, 0.25], [0.2, 3, 6.9]),+ boxPart('wall-right', [4.1, 1.5, 0.25], [0.2, 3, 6.9]),+ boxPart('wall-back', [0, 1.5, 3.6], [8, 3, 0.2]),+ boxPart('front-left', [-2.7, 1.5, -3.1], [2.6, 3, 0.2]),+ boxPart('front-right', [2.7, 1.5, -3.1], [2.6, 3, 0.2]),+ boxPart('front-sill', [0, 0.45, -3.1], [2.8, 0.9, 0.2]),+ boxPart('front-head', [0, 2.7, -3.1], [2.8, 0.6, 0.2]),+ ];+}++// The brightest sky visibility among the vertices of one face of a part whose world depth is at least `minimumDepth`: the half of the room away from the window.+function brightestRoomSide(result, partName, normalAxis, normalSign, minimumDepth = 2) {+ const part = result.parts.find(candidate => candidate.name === partName);+ const { positions, normals, skyVisibility } = part.geometry;+ const { matrix } = part;+ let brightest = 0;+ for (let vertex = 0; vertex < skyVisibility.length; vertex++) {+ const depth = matrix[2] * positions[vertex * 3] + matrix[6] * positions[vertex * 3 + 1] + matrix[10] * positions[vertex * 3 + 2] + matrix[14];+ if (depth >= minimumDepth && normals[vertex * 3 + normalAxis] * normalSign > 0.9) brightest = Math.max(brightest, skyVisibility[vertex]);+ }+ return brightest;+}++test('walls that run into the back wall do not glow near its corners: their buried end vertices read as occluded (#2166)', () => {+ const result = applyFloorSkyVisibility(windowRoomParts(), { onlyClosedRooms: true, surfaces: 'all' });+ assert.equal(result.report.reason, null);+ // The far half of the room sees the sky only through a 2.8 x 1.5 m window at least 5 m away, so these inner faces stay dark.+ assert.ok(brightestRoomSide(result, 'wall-left', 0, 1) < 0.1, 'left wall inner face');+ assert.ok(brightestRoomSide(result, 'wall-right', 0, -1) < 0.1, 'right wall inner face');+ assert.ok(brightestRoomSide(result, 'ceiling', 1, -1) < 0.1, 'ceiling underside');+ assert.ok(brightestRoomSide(result, 'wall-back', 2, -1) < 0.1, 'back wall inner face');+});