3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
import { CLOSED_ROOM_MAXIMUM_TRIANGLES, CLOSED_ROOM_SHARE, closedRoomShare, freeOrigin, hitsFrontFace, worldTriangles } from './closed-room.js';
// A closed-room test for rooms with a garden outside the window and heavy props inside. closedRoomShare (which the inspect
// diagnostic uses) shoots from the middle of the bounds, so a lawn beside the room moves the start out of it, and it gives up
// above CLOSED_ROOM_MAXIMUM_TRIANGLES. This one leaves out the heaviest parts and the parts far from the rest, then runs the
// same test; it lives in a module of its own so the core bundle does not carry it.
function triangleCountOf(part) {
return part.geometry.indices.length / 3;
}
// The lightest parts that fit the triangle budget: walls and floors are a few triangles, a scanned prop is thousands.
function lightestParts(parts) {
const kept = new Set();
let total = 0;
for (const part of [...parts].sort((first, second) => triangleCountOf(first) - triangleCountOf(second))) {
total += triangleCountOf(part);
if (total > CLOSED_ROOM_MAXIMUM_TRIANGLES) {
break;
}
kept.add(part);
}
return parts.filter(part => kept.has(part));
}
// The middle of a part's local bounds, in world space.
function partCentre(part, matrix) {
const { positions } = part.geometry;
const minimum = [Infinity, Infinity, Infinity];
const maximum = [-Infinity, -Infinity, -Infinity];
for (let index = 0; index < positions.length; index++) {
minimum[index % 3] = Math.min(minimum[index % 3], positions[index]);
maximum[index % 3] = Math.max(maximum[index % 3], positions[index]);
}
const middle = minimum.map((value, axis) => (value + maximum[axis]) / 2);
return [0, 1, 2].map(axis => matrix[axis] * middle[0] + matrix[4 + axis] * middle[1] + matrix[8 + axis] * middle[2] + matrix[12 + axis]);
}
// The parts whose centre lies within 1.5 times the widest spread of the middle 60 percent of the centres (20th to 80th
// percentile) from that spread, on every axis. A lawn or a hedge beside a room is farther out than that.
function partsNearTheRest(parts, worldMatrices) {
const centres = parts.map(part => partCentre(part, worldMatrices.get(part) ?? worldMatrices.get(part.name)));
const spreads = [0, 1, 2].map(axis => {
const values = centres.map(centre => centre[axis]).sort((first, second) => first - second);
return [values[Math.floor((values.length - 1) * 0.2)], values[Math.ceil((values.length - 1) * 0.8)]];
});
const margin = 1.5 * Math.max(...spreads.map(([low, high]) => high - low));
return parts.filter((part, index) => spreads.every(([low, high], axis) => centres[index][axis] >= low - margin && centres[index][axis] <= high + margin));
}
const CLOSED_ROOM_WALL_RAYS = 36;
// A room has walls when this share of the level rays around the origin hit one; a canopy on columns hits a column now and then.
const CLOSED_ROOM_WALL_SHARE = 0.8;
/**
* The share of level rays (a ring around the up axis at the origin's height) that hit a part's inner face, or null under the same conditions as
* closedRoomShare. A roof over a floor on a few columns has a high closedRoomShare, because the rays up and down hit the roof and the floor, but few
* level rays hit anything, so this tells a room with walls from a canopy or a colonnade.
*/
function closedRoomWallShare(parts, worldMatrices) {
const triangles = worldTriangles(parts.filter(part => !['blend', 'add'].includes(part.material?.alphaMode)), worldMatrices);
if (!triangles) {
return null;
}
const origin = freeOrigin(triangles);
if (!origin) {
return null;
}
const direction = [0, 0, 0];
let hits = 0;
for (let ray = 0; ray < CLOSED_ROOM_WALL_RAYS; ray++) {
const angle = ((ray + 0.5) / CLOSED_ROOM_WALL_RAYS) * Math.PI * 2;
direction[0] = Math.cos(angle);
direction[1] = 0;
direction[2] = Math.sin(angle);
for (let triangle = 0; triangle < triangles.triangleCount; triangle++) {
if (hitsFrontFace(triangles.corners, triangle * 9, origin, direction)) {
hits++;
break;
}
}
}
return hits / CLOSED_ROOM_WALL_RAYS;
}
/**
* The share of rays from the middle of the bounds that hit a part's inner face (closedRoomShare), or, when that is not a room,
* the same test on the parts near the rest, if that is larger. The heaviest parts are left out when the scene has more than
* CLOSED_ROOM_MAXIMUM_TRIANGLES triangles. Null when nothing is left to test. `worldMatrices` maps each part (or its name) to its world matrix.
*/
export function fittedClosedRoomShare(parts, worldMatrices) {
const kept = lightestParts(parts.filter(part => !['blend', 'add'].includes(part.material?.alphaMode)));
const share = closedRoomShare(kept, worldMatrices);
if (share === null || share >= CLOSED_ROOM_SHARE) {
return share;
}
return Math.max(share, closedRoomShare(partsNearTheRest(kept, worldMatrices), worldMatrices) ?? 0);
}
/**
* Whether the scene has walls all round (closedRoomWallShare of at least CLOSED_ROOM_WALL_SHARE), tested on the same parts as fittedClosedRoomShare:
* all of them, or the ones near the rest when a lawn beside the room moves the origin out of it. False when nothing can be tested.
*/
export function fittedRoomHasWalls(parts, worldMatrices) {
const kept = lightestParts(parts.filter(part => !['blend', 'add'].includes(part.material?.alphaMode)));
const share = Math.max(closedRoomWallShare(kept, worldMatrices) ?? 0, closedRoomWallShare(partsNearTheRest(kept, worldMatrices), worldMatrices) ?? 0);
return share >= CLOSED_ROOM_WALL_SHARE;
}