Prompt specification
Build or customize the Three.js 3D scene "Turning-space obstacle test" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Turning-space obstacle test
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/turning-space-obstacle-test-x0450
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts accessible public restrooms. Move a circular maneuvering template through a small room with movable bin and shelf; repositioning objects reveals where the available turning region becomes continuous. Low-poly.
Working Three.js Starter (turning-space-obstacle-test-x0450):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Restroom — Turning Space Study</title>
<style>
:root {
--ink: #173b38;
--muted: #66817b;
--panel: rgba(250, 252, 248, 0.93);
--line: rgba(23, 59, 56, 0.14);
--teal: #19aa8b;
--teal-soft: #a8e6d7;
--amber: #d89a43;
--red: #d45d52;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe9e2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
cursor: crosshair;
}
.panel {
position: fixed;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(30, 65, 57, 0.16);
backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 3px;
border-radius: 4px;
background: var(--teal);
content: "";
}
h1 {
margin: 0;
font-size: 24px;
font-weight: 760;
letter-spacing: -0.04em;
line-height: 1.08;
}
.subtitle {
margin: 9px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.status {
display: flex;
align-items: center;
gap: 9px;
min-height: 39px;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.55);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.05em;
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(25, 170, 139, 0.13);
}
.status-dot.warn {
background: var(--amber);
box-shadow: 0 0 0 4px rgba(216, 154, 67, 0.15);
}
.status-dot.danger {
background: var(--red);
box-shadow: 0 0 0 4px rgba(212, 93, 82, 0.15);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin: 12px 0;
}
.metric {
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.46);
}
.metric-label {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric-value {
font-size: 21px;
font-weight: 760;
letter-spacing: -0.04em;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px 13px;
margin: 12px 0 14px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--teal);
}
.swatch.amber {
background: var(--amber);
}
.check {
display: flex;
align-items: center;
gap: 8px;
margin: 12px 0 14px;
color: var(--muted);
font-size: 11px;
cursor: pointer;
}
.check input {
accent-color: var(--teal);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.buttons button {
min-height: 38px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: white;
color: var(--ink);
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
.buttons button:hover {
transform: translateY(-1px);
border-color: rgba(25, 170, 139, 0.42);
background: #f2fbf7;
}
.buttons button.primary {
border-color: var(--teal);
background: var(--teal);
color: white;
}
.buttons button.primary:hover {
background: #12977b;
}
.nudges {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-top: 8px;
}
.nudges button {
min-height: 31px;
padding: 6px 4px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.58);
color: var(--muted);
font-size: 9px;
font-weight: 750;
cursor: pointer;
}
.hint {
position: fixed;
right: 18px;
bottom: 18px;
max-width: min(430px, calc(100vw - 36px));
padding: 11px 14px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 12px;
background: rgba(250, 252, 248, 0.87);
box-shadow: 0 10px 30px rgba(30, 65, 57, 0.11);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
text-align: right;
backdrop-filter: blur(10px);
pointer-events: none;
}
.hint strong {
color: var(--ink);
}
.world-tag {
position: fixed;
z-index: 5;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 7px;
background: rgba(250, 252, 248, 0.91);
box-shadow: 0 5px 14px rgba(30, 65, 57, 0.12);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
transform: translate(-50%, -50%);
pointer-events: none;
white-space: nowrap;
}
.world-tag.template {
border-color: rgba(25, 170, 139, 0.45);
color: #087e67;
}
.world-tag.warn {
border-color: rgba(216, 154, 67, 0.55);
color: #9b641e;
}
.world-tag.danger {
border-color: rgba(212, 93, 82, 0.55);
color: #a33d35;
}
@media (max-width: 700px) {
.panel {
top: 10px;
left: 10px;
width: min(305px, calc(100vw - 20px));
padding: 14px;
}
h1 {
font-size: 21px;
}
.subtitle {
margin-bottom: 10px;
}
.hint {
right: 10px;
bottom: 10px;
left: 10px;
max-width: none;
text-align: center;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
transition: none !important;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive accessible restroom turning-space study"></canvas>
<section class="panel" aria-label="Turning-space controls and status">
<div class="eyebrow">Public restroom · Study 01</div>
<h1>Turning space,<br>made visible.</h1>
<p class="subtitle">
Drag the Ø 1.50 m maneuvering template, bin, or shelf. Teal floor cells show
valid turning positions continuously connected to the entry.
</p>
<div class="status" aria-live="polite">
<span id="statusDot" class="status-dot"></span>
<span id="statusText">CLEAR · CONTINUOUS</span>
</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Entry-linked field</span>
<span id="areaValue" class="metric-value">—</span>
</div>
<div class="metric">
<span class="metric-label">Disconnected zones</span>
<span id="zonesValue" class="metric-value">—</span>
</div>
</div>
<div class="legend" aria-label="Color legend">
<span><i class="swatch"></i> Continuous from entry</span>
<span><i class="swatch amber"></i> Valid but disconnected</span>
</div>
<label class="check">
<input id="showZones" type="checkbox" checked>
Show valid turning-position cells
</label>
<div class="buttons">
<button id="openPath" class="primary" type="button">Open centerline</button>
<button id="resetLayout" type="button">Reset layout</button>
</div>
<div class="nudges" aria-label="Nudge movable objects">
<button id="binLeft" type="button">BIN ←</button>
<button id="binRight" type="button">BIN →</button>
<button id="shelfLeft" type="button">SHELF ←</button>
<button id="shelfRight" type="button">SHELF →</button>
</div>
</section>
<div id="ringTag" class="world-tag template">Ø 1.50 m · drag</div>
<div id="binTag" class="world-tag">Bin · drag</div>
<div id="shelfTag" class="world-tag">Shelf · drag</div>
<div class="hint">
<strong>Drag</strong> the ring or objects ·
<strong>Click floor</strong> to route the ring ·
<strong>Arrow keys</strong> nudge the ring ·
<strong>Right-drag</strong> to orbit · <strong>Wheel</strong> to zoom
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const W = 6.6;
const D = 5.6;
const R = 0.75;
const CELL = 0.2;
const BIN_RADIUS = 0.36;
const SHELF_WIDTH = 2.3;
const SHELF_DEPTH = 0.52;
const initial = {
bin: { x: 1.75, z: 0.65 },
shelf: { x: -0.35, z: -0.45 },
template: { x: 0, z: 1.55 }
};
const openLayout = {
bin: { x: 2.65, z: 1.85 },
shelf: { x: -1.55, z: -0.45 }
};
const palette = {
floor: 0xe7e4d9,
grout: 0xc8cbc3,
wall: 0xe7eee8,
wainscot: 0xb8d2c8,
dark: 0x244d48,
teal: 0x19aa8b,
amber: 0xd89a43,
red: 0xd45d52,
white: 0xf8f8ef,
wood: 0xb78b61,
coral: 0xd56d5d
};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe9e2);
scene.fog = new THREE.Fog(0xdfe9e2, 14, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraTarget = new THREE.Vector3(0, 0.5, -0.05);
let cameraYaw = 0.72;
let cameraPitch = 0.72;
let cameraRadius = 10.8;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xffffff, 0x71887d, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff8e9, 3.1);
sun.position.set(5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xcde8ff, 0.8);
fill.position.set(-6, 5, 1);
scene.add(fill);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
const matFloor = material(palette.floor);
const matWall = material(palette.wall);
const matWainscot = material(palette.wainscot);
const matWhite = material(palette.white);
const matDark = material(palette.dark);
const matWood = material(palette.wood);
const matCoral = material(palette.coral);
const matMetal = material(0xa8b8b3, {
metalness: 0.55,
roughness: 0.34
});
function addBox(w, h, d, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinderBetween(a, b, radius, mat, parent = scene) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
mat
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addFloor() {
addBox(W, 0.18, D, matFloor, 0, -0.09, 0);
const points = [];
for (let x = -W / 2; x <= W / 2 + 0.001; x += 0.55) {
points.push(
new THREE.Vector3(x, 0.006, -D / 2),
new THREE.Vector3(x, 0.006, D / 2)
);
}
for (let z = -D / 2; z <= D / 2 + 0.001; z += 0.55) {
points.push(
new THREE.Vector3(-W / 2, 0.006, z),
new THREE.Vector3(W / 2, 0.006, z)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const lines = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: palette.grout,
transparent: true,
opacity: 0.7
})
);
scene.add(lines);
}
function addRoom() {
// Cutaway back and left walls.
addBox(W + 0.28, 2.85, 0.14, matWall, 0, 1.425, -D / 2 - 0.07);
addBox(0.14, 2.85, D, matWall, -W / 2 - 0.07, 1.425, 0);
// Colored lower wall panels.
addBox(W, 1.02, 0.035, matWainscot, 0, 0.51, -D / 2 + 0.018);
addBox(0.035, 1.02, D, matWainscot, -W / 2 + 0.018, 0.51, 0);
// Sparse wall seams.
const points = [];
for (let x = -W / 2 + 0.55; x < W / 2; x += 0.55) {
points.push(
new THREE.Vector3(x, 1.04, -D / 2 + 0.04),
new THREE.Vector3(x, 2.82, -D / 2 + 0.04)
);
}
for (let z = -D / 2 + 0.55; z < D / 2; z += 0.55) {
points.push(
new THREE.Vector3(-W / 2 + 0.04, 1.04, z),
new THREE.Vector3(-W / 2 + 0.04, 2.82, z)
);
}
scene.add(
new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: 0xbccdc6,
transparent: true,
opacity: 0.42
})
)
);
// Low cutaway curb on the open right/front side.
addBox(0.12, 0.28, D, matWainscot, W / 2 + 0.06, 0.14, 0);
// Entrance jambs and threshold.
addBox(0.13, 1.0, 0.16, matDark, -1.1, 0.5, D / 2 - 0.03);
addBox(0.13, 1.0, 0.16, matDark, 1.1, 0.5, D / 2 - 0.03);
addBox(2.05, 0.025, 0.16, matDark, 0, 0.018, D / 2 - 0.02);
}
function makeAccessibleSignTexture() {
const c = document.createElement("canvas");
c.width = 768;
c.height = 360;
const ctx = c.getContext("2d");
ctx.fillStyle = "#1f6f64";
ctx.fillRect(0, 0, c.width, c.height);
ctx.strokeStyle = "#ffffff";
ctx.fillStyle = "#ffffff";
ctx.lineWidth = 16;
ctx.lineCap = "round";
ctx.lineJoin = "round";
// Head.
ctx.beginPath();
ctx.arc(180, 86, 30, 0, Math.PI * 2);
ctx.fill();
// Torso and arm.
ctx.beginPath();
ctx.moveTo(180, 125);
ctx.lineTo(180, 218);
ctx.moveTo(180, 150);
ctx.lineTo(238, 185);
ctx.moveTo(180, 180);
ctx.lineTo(240, 180);
ctx.stroke();
// Leg and wheel.
ctx.beginPath();
ctx.moveTo(180, 215);
ctx.lineTo(238, 255);
ctx.lineTo(285, 255);
ctx.stroke();
ctx.beginPath();
ctx.arc(180, 235, 59, 0, Math.PI * 2);
ctx.stroke();
ctx.font = "800 48px Arial";
ctx.fillText("ACCESSIBLE", 340, 135);
ctx.font = "700 29px Arial";
ctx.fillText("PUBLIC RESTROOM", 342, 194);
ctx.globalAlpha = 0.72;
ctx.font = "600 23px Arial";
ctx.fillText("TURNING SPACE · Ø 1.50 m", 342, 245);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
function addSign() {
const frame = addBox(
1.65,
0.79,
0.07,
matDark,
-1.15,
1.84,
-D / 2 + 0.055
);
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(1.57, 0.72),
new THREE.MeshBasicMaterial({ map: makeAccessibleSignTexture() })
);
sign.position.set(-1.15, 1.84, -D / 2 + 0.094);
scene.add(sign);
frame.castShadow = false;
}
function makeEntryTexture() {
const c = document.createElement("canvas");
c.width = 768;
c.height = 180;
const ctx = c.getContext("2d");
ctx.fillStyle = "rgba(31,111,100,0.88)";
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = "#ffffff";
ctx.font = "800 64px Arial";
ctx.textAlign = "center";
ctx.fillText("ENTRY", c.width / 2, 91);
ctx.font = "700 28px Arial";
ctx.fillText("KEEP TURNING SPACE CLEAR", c.width / 2, 137);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
function addEntryMat() {
const mat = new THREE.Mesh(
new THREE.PlaneGeometry(1.85, 0.46),
new THREE.MeshBasicMaterial({
map: makeEntryTexture(),
transparent: true,
depthWrite: false
})
);
mat.rotation.x = -Math.PI / 2;
mat.position.set(0, 0.015, 2.37);
scene.add(mat);
}
function addToilet() {
const group = new THREE.Group();
group.position.set(2.15, 0, -2.12);
scene.add(group);
addBox(0.43, 0.39, 0.56, matWhite, 0, 0.195, 0.05, group);
const bowl = new THREE.Mesh(
new THREE.SphereGeometry(0.34, 12, 8),
matWhite
);
bowl.position.set(0, 0.46, 0.04);
bowl.scale.set(1, 0.58, 1.25);
bowl.castShadow = true;
group.add(bowl);
const seat = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.3, 0.075, 12),
matWhite
);
seat.position.set(0, 0.615, 0.04);
seat.scale.z = 1.22;
seat.castShadow = true;
group.add(seat);
const seatInset = new THREE.Mesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.012, 12),
material(0x9ec8c1)
);
seatInset.position.set(0, 0.658, 0.04);
seatInset.scale.z = 1.18;
group.add(seatInset);
addBox(0.63, 0.68, 0.24, matWhite, 0, 0.56, -0.39, group);
addBox(0.12, 0.035, 0.035, matMetal, 0.18, 0.72, -0.255, group);
// Grab bars behind and beside the toilet.
addCylinderBetween(
new THREE.Vector3(1.52, 0.9, -2.68),
new THREE.Vector3(2.74, 0.9, -2.68),
0.035,
matMetal
);
addCylinderBetween(
new THREE.Vector3(2.75, 0.9, -2.66),
new THREE.Vector3(2.75, 0.9, -1.63),
0.035,
matMetal
);
addCylinderBetween(
new THREE.Vector3(2.75, 0.48, -1.63),
new THREE.Vector3(2.75, 0.9, -1.63),
0.035,
matMetal
);
}
function addSink() {
const group = new THREE.Group();
group.position.set(-2.62, 0, -2.22);
scene.add(group);
addBox(0.86, 0.67, 0.58, matWainscot, 0, 0.335, 0, group);
addBox(0.94, 0.11, 0.68, matWhite, 0, 0.72, 0, group);
const basin = new THREE.Mesh(
new THREE.CylinderGeometry(0.27, 0.21, 0.11, 10),
matWhite
);
basin.position.set(0, 0.82, 0.02);
basin.castShadow = true;
group.add(basin);
const basinInset = new THREE.Mesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.012, 10),
material(0x83b9b2)
);
basinInset.position.set(0, 0.88, 0.02);
group.add(basinInset);
addCylinderBetween(
new THREE.Vector3(0, 0.83, -0.24),
new THREE.Vector3(0, 1.03, -0.24),
0.035,
matMetal,
group
);
addCylinderBetween(
new THREE.Vector3(0, 1.03, -0.24),
new THREE.Vector3(0, 1.03, -0.08),
0.035,
matMetal,
group
);
// Mirror.
addBox(0.84, 0.73, 0.05, matDark, 0, 1.48, -0.49, group);
const mirror = new THREE.Mesh(
new THREE.PlaneGeometry(0.75, 0.64),
material(0x8aa8a4, {
metalness: 0.7,
roughness: 0.18
})
);
mirror.position.set(0, 1.48, -0.458);
group.add(mirror);
// Wall soap dispenser.
addBox(0.2, 0.28, 0.12, matWhite, 0.68, 1.22, -0.43, group);
}
function addBin() {
const group = new THREE.Group();
group.position.set(initial.bin.x, 0, initial.bin.z);
group.userData.draggable = "bin";
scene.add(group);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(BIN_RADIUS, 0.31, 0.62, 8),
matCoral
);
body.position.y = 0.31;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const lid = new THREE.Mesh(
new THREE.CylinderGeometry(0.385, 0.385, 0.075, 8),
matDark
);
lid.position.y = 0.66;
lid.castShadow = true;
group.add(lid);
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.12, 0.025, 6, 12, Math.PI),
matMetal
);
handle.position.set(0, 0.69, 0);
handle.rotation.x = Math.PI / 2;
group.add(handle);
// Small recycling mark.
for (let i = -1; i <= 1; i++) {
const mark = addBox(
0.035,
0.16,
0.015,
matWhite,
i * 0.075,
0.38,
0.33,
group
);
mark.rotation.z = i * 0.25;
mark.castShadow = false;
}
return group;
}
function addShelf() {
const group = new THREE.Group();
group.position.set(initial.shelf.x, 0, initial.shelf.z);
group.userData.draggable = "shelf";
scene.add(group);
const uprightMaterial = material(0x537b73);
addBox(
0.075,
1.74,
SHELF_DEPTH,
uprightMaterial,
-SHELF_WIDTH / 2 + 0.04,
0.87,
0,
group
);
addBox(
0.075,
1.74,
SHELF_DEPTH,
uprightMaterial,
SHELF_WIDTH / 2 - 0.04,
0.87,
0,
group
);
const boardYs = [0.16, 0.7, 1.24, 1.72];
for (const y of boardYs) {
addBox(
SHELF_WIDTH,
0.075,
SHELF_DEPTH,
matWood,
0,
y,
0,
group
);
}
// Folded towels.
const towelMat = material(0xd7e6df);
for (let i = 0; i < 3; i++) {
addBox(
0.5,
0.11,
0.34,
towelMat,
-0.62,
0.805 + i * 0.105,
0,
group
);
}
// Supply boxes.
addBox(
0.48,
0.33,
0.36,
material(0xe3bd78),
0.15,
0.89,
0,
group
);
addBox(
0.44,
0.3,
0.34,
material(0x8fb8b0),
-0.5,
1.4,
0,
group
);
// Bottles.
for (let i = 0; i < 3; i++) {
const bottle = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.085, 0.28, 8),
material(i === 1 ? 0xc6796b : 0x5e9488)
);
bottle.position.set(0.34 + i * 0.19, 1.39, 0);
bottle.castShadow = true;
group.add(bottle);
}
return group;
}
function addTemplate() {
const group = new THREE.Group();
group.position.set(initial.template.x, 0, initial.template.z);
group.userData.draggable = "template";
scene.add(group);
const fillMaterial = new THREE.MeshBasicMaterial({
color: palette.teal,
transparent: true,
opacity: 0.12,
depthWrite: false,
side: THREE.DoubleSide
});
const fill = new THREE.Mesh(
new THREE.CircleGeometry(R, 64),
fillMaterial
);
fill.rotation.x = -Math.PI / 2;
fill.position.y = 0.027;
group.add(fill);
const ringMaterial = new THREE.MeshStandardMaterial({
color: palette.teal,
emissive: palette.teal,
emissiveIntensity: 0.25,
roughness: 0.4,
metalness: 0.05
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(R, 0.025, 8, 64),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.045;
ring.castShadow = true;
group.add(ring);
const center = new THREE.Mesh(
new THREE.RingGeometry(0.055, 0.078, 24),
new THREE.MeshBasicMaterial({
color: palette.teal,
side: THREE.DoubleSide
})
);
center.rotation.x = -Math.PI / 2;
center.position.y = 0.047;
group.add(center);
return {
group,
fill,
ring,
ringMaterial,
center
};
}
addFloor();
addRoom();
addSign();
addEntryMat();
addToilet();
addSink();
const binGroup = addBin();
const shelfGroup = addShelf();
const template = addTemplate();
const staticObstacles = [
{
type: "obb",
name: "sink",
x: -2.62,
z: -2.22,
w: 0.96,
d: 0.7,
angle: 0
},
{
type: "obb",
name: "toilet",
x: 2.15,
z: -2.12,
w: 0.72,
d: 1.1,
angle: 0
}
];
let activeObstacles = [];
function rebuildObstacles() {
activeObstacles = staticObstacles.concat([
{
type: "circle",
name: "bin",
x: binGroup.position.x,
z: binGroup.position.z,
r: BIN_RADIUS
},
{
type: "obb",
name: "shelf",
x: shelfGroup.position.x,
z: shelfGroup.position.z,
w: SHELF_WIDTH,
d: SHELF_DEPTH,
angle: 0
}
]);
}
function circleHitsObstacle(x, z, obstacle) {
if (obstacle.type === "circle") {
const dx = x - obstacle.x;
const dz = z - obstacle.z;
const clearance = R + obstacle.r;
return dx * dx + dz * dz < clearance * clearance;
}
const dx = x - obstacle.x;
const dz = z - obstacle.z;
const cos = Math.cos(obstacle.angle || 0);
const sin = Math.sin(obstacle.angle || 0);
// Rotate world offset into the obstacle's local space.
const localX = cos * dx - sin * dz;
const localZ = sin * dx + cos * dz;
const halfW = obstacle.w / 2;
const halfD = obstacle.d / 2;
const closestX = THREE.MathUtils.clamp(localX, -halfW, halfW);
const closestZ = THREE.MathUtils.clamp(localZ, -halfD, halfD);
const distanceX = localX - closestX;
const distanceZ = localZ - closestZ;
return distanceX * distanceX + distanceZ * distanceZ < R * R;
}
function getCircleCollision(x, z) {
if (
x - R < -W / 2 ||
x + R > W / 2 ||
z - R < -D / 2 ||
z + R > D / 2
) {
return "wall or room edge";
}
for (const obstacle of activeObstacles) {
if (circleHitsObstacle(x, z, obstacle)) {
return obstacle.name;
}
}
return null;
}
function isCircleClear(x, z) {
return getCircleCollision(x, z) === null;
}
const zoneGeometry = new THREE.PlaneGeometry(CELL * 0.82, CELL * 0.82);
zoneGeometry.rotateX(-Math.PI / 2);
const zoneMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0.2,
depthWrite: false,
side: THREE.DoubleSide,
vertexColors: true
});
const zoneMesh = new THREE.InstancedMesh(
zoneGeometry,
zoneMaterial,
1000
);
zoneMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
zoneMesh.frustumCulled = false;
zoneMesh.renderOrder = 2;
scene.add(zoneMesh);
const pathMaterial = new THREE.LineBasicMaterial({
color: palette.teal,
transparent: true,
opacity: 0.9
});
const pathLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
pathMaterial
);
pathLine.visible = false;
pathLine.renderOrder = 4;
scene.add(pathLine);
let grid = null;
let entryComponent = -1;
let componentCount = 0;
let templateComponent = -1;
let entryArea = 0;
let disconnectedZones = 0;
const dummy = new THREE.Object3D();
const zoneColorContinuous = new THREE.Color(0x20b99a);
const zoneColorDisconnected = new THREE.Color(0xe0a64d);
const statusDot = document.querySelector("#statusDot");
const statusText = document.querySelector("#statusText");
const areaValue = document.querySelector("#areaValue");
const zonesValue = document.querySelector("#zonesValue");
const showZones = document.querySelector("#showZones");
const ringTag = document.querySelector("#ringTag");
const binTag = document.querySelector("#binTag");
const shelfTag = document.querySelector("#shelfTag");
function gridCoordinates(index) {
const ix = index % grid.nx;
const iz = Math.floor(index / grid.nx);
return {
x: grid.startX + ix * CELL,
z: grid.startZ + iz * CELL
};
}
function nearestValidIndex(x, z) {
if (!grid) return -1;
let best = -1;
let bestDistance = Infinity;
for (let i = 0; i < grid.valid.length; i++) {
if (!grid.valid[i]) continue;
const p = gridCoordinates(i);
const distance = (p.x - x) ** 2 + (p.z - z) ** 2;
if (distance < bestDistance) {
bestDistance = distance;
best = i;
}
}
return best;
}
function exactOrNearestGridIndex(x, z) {
if (!grid) return -1;
const ix = Math.round((x - grid.startX) / CELL);
const iz = Math.round((z - grid.startZ) / CELL);
if (ix >= 0 && ix < grid.nx && iz >= 0 && iz < grid.nz) {
const index = iz * grid.nx + ix;
if (grid.valid[index]) {
const p = gridCoordinates(index);
if (Math.hypot(p.x - x, p.z - z) < CELL * 0.72) {
return index;
}
}
}
return nearestValidIndex(x, z);
}
function updateValidity() {
rebuildObstacles();
const nx = Math.floor((W - 2 * R) / CELL);
const nz = Math.floor((D - 2 * R) / CELL);
grid = {
nx,
nz,
startX: -W / 2 + R + CELL / 2,
startZ: -D / 2 + R + CELL / 2,
valid: new Uint8Array(nx * nz),
component: new Int32Array(nx * nz).fill(-1)
};
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const index = iz * nx + ix;
const x = grid.startX + ix * CELL;
const z = grid.startZ + iz * CELL;
grid.valid[index] = isCircleClear(x, z) ? 1 : 0;
}
}
const componentSizes = [];
const stack = [];
for (let start = 0; start < grid.valid.length; start++) {
if (!grid.valid[start] || grid.component[start] !== -1) continue;
const componentId = componentSizes.length;
let size = 0;
stack.push(start);
grid.component[start] = componentId;
while (stack.length) {
const index = stack.pop();
size++;
const ix = index % nx;
const iz = Math.floor(index / nx);
const neighbors = [];
if (ix > 0) neighbors.push(index - 1);
if (ix < nx - 1) neighbors.push(index + 1);
if (iz > 0) neighbors.push(index - nx);
if (iz < nz - 1) neighbors.push(index + nx);
for (const neighbor of neighbors) {
if (grid.valid[neighbor] && grid.component[neighbor] === -1) {
grid.component[neighbor] = componentId;
stack.push(neighbor);
}
}
}
componentSizes.push(size);
}
componentCount = componentSizes.length;
const entryIndex = nearestValidIndex(0, D / 2 - R - 0.08);
entryComponent = entryIndex >= 0 ? grid.component[entryIndex] : -1;
const templateX = template.group.position.x;
const templateZ = template.group.position.z;
const templateIndex = nearestValidIndex(templateX, templateZ);
templateComponent =
isCircleClear(templateX, templateZ) && templateIndex >= 0
? grid.component[templateIndex]
: -1;
entryArea =
entryComponent >= 0
? componentSizes[entryComponent] * CELL * CELL
: 0;
disconnectedZones = Math.max(0, componentCount - 1);
let instance = 0;
for (let index = 0; index < grid.valid.length; index++) {
if (!grid.valid[index]) continue;
const p = gridCoordinates(index);
dummy.position.set(p.x, 0.016, p.z);
dummy.updateMatrix();
zoneMesh.setMatrixAt(instance, dummy.matrix);
zoneMesh.setColorAt(
instance,
grid.component[index] === entryComponent
? zoneColorContinuous
: zoneColorDisconnected
);
instance++;
}
zoneMesh.count = instance;
zoneMesh.instanceMatrix.needsUpdate = true;
if (zoneMesh.instanceColor) {
zoneMesh.instanceColor.needsUpdate = true;
}
updateUI();
}
function updateUI() {
const collision = getCircleCollision(
template.group.position.x,
template.group.position.z
);
let color = palette.teal;
let label = "CLEAR · CONTINUOUS";
let tagClass = "";
if (collision) {
color = palette.red;
label = "COLLISION · " + collision.toUpperCase();
tagClass = "danger";
} else if (templateComponent !== entryComponent) {
color = palette.amber;
label = "CLEAR · DISCONNECTED POCKET";
tagClass = "warn";
}
template.fill.material.color.set(color);
template.ring.material.color.set(color);
template.ring.material.emissive.set(color);
template.center.material.color.set(color);
statusText.textContent = label;
statusDot.className =
"status-dot" +
(collision ? " danger" : templateComponent !== entryComponent ? " warn" : "");
ringTag.className =
"world-tag template" + (tagClass ? " " + tagClass : "");
areaValue.textContent = entryArea.toFixed(1) + " m²";
zonesValue.textContent = disconnectedZones;
}
function findGridPath(start, target) {
if (!grid || start < 0 || target < 0) return null;
const total = grid.valid.length;
const previous = new Int32Array(total).fill(-1);
const queue = new Int32Array(total);
let head = 0;
let tail = 0;
queue[tail++] = start;
previous[start] = start;
while (head < tail) {
const index = queue[head++];
if (index === target) break;
const ix = index % grid.nx;
const iz = Math.floor(index / grid.nx);
const neighbors = [];
if (ix > 0) neighbors.push(index - 1);
if (ix < grid.nx - 1) neighbors.push(index + 1);
if (iz > 0) neighbors.push(index - grid.nx);
if (iz < grid.nz - 1) neighbors.push(index + grid.nx);
for (const neighbor of neighbors) {
if (grid.valid[neighbor] && previous[neighbor] === -1) {
previous[neighbor] = index;
queue[tail++] = neighbor;
}
}
}
if (previous[target] === -1) return null;
const path = [];
let current = target;
while (current !== start) {
path.push(current);
current = previous[current];
}
path.push(start);
path.reverse();
return path;
}
let pathAnimation = null;
function moveTemplateTo(x, z) {
x = THREE.MathUtils.clamp(x, -W / 2 + 0.03, W / 2 - 0.03);
z = THREE.MathUtils.clamp(z, -D / 2 + 0.03, D / 2 - 0.03);
pathAnimation = null;
const targetIsClear = isCircleClear(x, z);
const targetIndex = exactOrNearestGridIndex(x, z);
const currentIndex = nearestValidIndex(
template.group.position.x,
template.group.position.z
);
if (targetIsClear && targetIndex >= 0 && currentIndex >= 0) {
const path = findGridPath(currentIndex, targetIndex);
if (path && path.length > 1) {
const points = [
new THREE.Vector3(
template.group.position.x,
0.025,
template.group.position.z
)
];
for (const index of path) {
const p = gridCoordinates(index);
points.push(new THREE.Vector3(p.x, 0.025, p.z));
}
points.push(new THREE.Vector3(x, 0.025, z));
pathLine.geometry.dispose();
pathLine.geometry = new THREE.BufferGeometry().setFromPoints(points);
pathLine.visible = true;
pathAnimation = {
points,
start: performance.now(),
duration: Math.min(2400, Math.max(500, points.length * 115))
};
return;
}
}
template.group.position.x = x;
template.group.position.z = z;
pathLine.visible = false;
updateUI();
}
const objectTweens = new Map();
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function moveObjectTo(type, endX, endZ, duration = 700) {
const group = type === "bin" ? binGroup : shelfGroup;
objectTweens.set(type, {
group,
startX: group.position.x,
startZ: group.position.z,
endX,
endZ,
start: performance.now(),
duration: reducedMotion ? 1 : duration
});
}
function clampObject(type, x, z) {
if (type === "bin") {
return {
x: THREE.MathUtils.clamp(x, -W / 2 + BIN_RADIUS, W / 2 - BIN_RADIUS),
z: THREE.MathUtils.clamp(z, -D / 2 + BIN_RADIUS, D / 2 - BIN_RADIUS)
};
}
return {
x: THREE.MathUtils.clamp(
x,
-W / 2 + SHELF_WIDTH / 2,
W / 2 - SHELF_WIDTH / 2
),
z: THREE.MathUtils.clamp(
z,
-D / 2 + SHELF_DEPTH / 2,
D / 2 - SHELF_DEPTH / 2
)
};
}
document.querySelector("#openPath").addEventListener("click", () => {
moveObjectTo("shelf", openLayout.shelf.x, openLayout.shelf.z, 900);
moveObjectTo("bin", openLayout.bin.x, openLayout.bin.z, 900);
});
document.querySelector("#resetLayout").addEventListener("click", () => {
moveObjectTo("shelf", initial.shelf.x, initial.shelf.z, 700);
moveObjectTo("bin", initial.bin.x, initial.bin.z, 700);
moveTemplateTo(initial.template.x, initial.template.z);
});
document.querySelector("#binLeft").addEventListener("click", () => {
moveObjectTo(
"bin",
binGroup.position.x - 0.22,
binGroup.position.z,
260
);
});
document.querySelector("#binRight").addEventListener("click", () => {
moveObjectTo(
"bin",
binGroup.position.x + 0.22,
binGroup.position.z,
260
);
});
document.querySelector("#shelfLeft").addEventListener("click", () => {
moveObjectTo(
"shelf",
shelfGroup.position.x - 0.22,
shelfGroup.position.z,
260
);
});
document.querySelector("#shelfRight").addEventListener("click", () => {
moveObjectTo(
"shelf",
shelfGroup.position.x + 0.22,
shelfGroup.position.z,
260
);
});
showZones.addEventListener("change", () => {
zoneMesh.visible = showZones.checked;
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const draggableRoots = [template.group, binGroup, shelfGroup];
let dragState = null;
let orbitState = null;
let clickState = null;
function setPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function floorPoint(event) {
setPointer(event);
return raycaster.ray.intersectPlane(groundPlane, new THREE.Vector3());
}
function findDraggableRoot(object) {
let current = object;
while (current) {
if (current.userData.draggable) return current;
current = current.parent;
}
return null;
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.focus();
if (event.button === 2 || event.altKey) {
orbitState = {
x: event.clientX,
y: event.clientY
};
canvas.setPointerCapture(event.pointerId);
return;
}
if (event.button !== 0) return;
setPointer(event);
const hits = raycaster.intersectObjects(draggableRoots, true);
const root = hits.length ? findDraggableRoot(hits[0].object) : null;
if (root) {
const point = floorPoint(event);
if (!point) return;
const type = root.userData.draggable;
const group =
type === "template"
? template.group
: type === "bin"
? binGroup
: shelfGroup;
objectTweens.delete(type);
pathAnimation = null;
dragState = {
type,
group,
pointerId: event.pointerId,
offsetX: group.position.x - point.x,
offsetZ: group.position.z - point.z
};
clickState = null;
canvas.setPointerCapture(event.pointerId);
} else {
clickState = {
x: event.clientX,
y: event.clientY,
moved: false
};
}
});
canvas.addEventListener("pointermove", (event) => {
if (orbitState) {
const dx = event.clientX - orbitState.x;
const dy = event.clientY - orbitState.y;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.28,
1.3
);
orbitState.x = event.clientX;
orbitState.y = event.clientY;
updateCamera();
return;
}
if (dragState) {
const point = floorPoint(event);
if (!point) return;
const desiredX = point.x + dragState.offsetX;
const desiredZ = point.z + dragState.offsetZ;
if (dragState.type === "template") {
dragState.group.position.x = THREE.MathUtils.clamp(
desiredX,
-W / 2 + 0.02,
W / 2 - 0.02
);
dragState.group.position.z = THREE.MathUtils.clamp(
desiredZ,
-D / 2 + 0.02,
D / 2 - 0.02
);
updateUI();
} else {
const clamped = clampObject(dragState.type, desiredX, desiredZ);
dragState.group.position.x = clamped.x;
dragState.group.position.z = clamped.z;
updateValidity();
}
return;
}
if (clickState) {
const distance = Math.hypot(
event.clientX - clickState.x,
event.clientY - clickState.y
);
if (distance > 5) clickState.moved = true;
}
});
canvas.addEventListener("pointerup", (event) => {
if (orbitState) {
orbitState = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
return;
}
if (dragState) {
dragState = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
return;
}
if (clickState && !clickState.moved && event.button === 0) {
const point = floorPoint(event);
if (point) {
moveTemplateTo(point.x, point.z);
}
}
clickState = null;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
6.5,
15
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
const step = event.shiftKey ? 0.25 : 0.1;
let handled = true;
pathAnimation = null;
switch (event.key) {
case "ArrowLeft":
template.group.position.x -= step;
break;
case "ArrowRight":
template.group.position.x += step;
break;
case "ArrowUp":
template.group.position.z -= step;
break;
case "ArrowDown":
template.group.position.z += step;
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
template.group.position.x = THREE.MathUtils.clamp(
template.group.position.x,
-W / 2 + 0.02,
W / 2 - 0.02
);
template.group.position.z = THREE.MathUtils.clamp(
template.group.position.z,
-D / 2 + 0.02,
D / 2 - 0.02
);
updateUI();
}
});
const projected = new THREE.Vector3();
function positionTag(element, worldPosition) {
projected.copy(worldPosition).project(camera);
if (projected.z > 1 || projected.z < -1) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left =
(projected.x * 0.5 + 0.5) * window.innerWidth + "px";
element.style.top =
(-projected.y * 0.5 + 0.5) * window.innerHeight + "px";
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
updateValidity();
let lastValidityUpdate = 0;
let validityDirty = false;
function animate(now) {
for (const [type, tween] of objectTweens) {
const progress = THREE.MathUtils.clamp(
(now - tween.start) / tween.duration,
0,
1
);
const eased =
progress < 0.5
? 4 * progress * progress * progress
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
tween.group.position.x = THREE.MathUtils.lerp(
tween.startX,
tween.endX,
eased
);
tween.group.position.z = THREE.MathUtils.lerp(
tween.startZ,
tween.endZ,
eased
);
if (progress >= 1) {
objectTweens.delete(type);
}
validityDirty = true;
}
if (validityDirty && now - lastValidityUpdate > 70) {
updateValidity();
lastValidityUpdate = now;
validityDirty = false;
}
if (pathAnimation) {
const progress = THREE.MathUtils.clamp(
(now - pathAnimation.start) / pathAnimation.duration,
0,
1
);
const points = pathAnimation.points;
const scaled = progress * (points.length - 1);
const segment = Math.min(
Math.floor(scaled),
points.length - 2
);
const localProgress = scaled - segment;
const a = points[segment];
const b = points[segment + 1];
template.group.position.x = THREE.MathUtils.lerp(
a.x,
b.x,
localProgress
);
template.group.position.z = THREE.MathUtils.lerp(
a.z,
b.z,
localProgress
);
updateUI();
if (progress >= 1) {
pathAnimation = null;
pathLine.visible = false;
}
}
positionTag(
ringTag,
new THREE.Vector3(
template.group.position.x,
0.11,
template.group.position.z
)
);
positionTag(
binTag,
new THREE.Vector3(
binGroup.position.x,
0.94,
binGroup.position.z
)
);
positionTag(
shelfTag,
new THREE.Vector3(
shelfGroup.position.x,
1.98,
shelfGroup.position.z
)
);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```