Prompt specification
Build or customize the Three.js 3D scene "Workstation geometry configurator" 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: Workstation geometry configurator
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/workstation-geometry-configurator-x1040
Scene Description Prompt:
Adjust a low-poly desk, chair, and tools; flag collisions and unreachable objects using declared simplified body dimensions. One HTML file using Three.js from cdnjs.
Working Three.js Starter (workstation-geometry-configurator-x1040):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Desk Fit — Collision & Reach Checker</title>
<style>
:root {
--ink: #17262d;
--muted: #64747a;
--panel: rgba(250, 252, 251, 0.93);
--line: rgba(23, 38, 45, 0.12);
--teal: #147d75;
--teal-light: #d9f1ed;
--red: #d83c43;
--red-light: #ffe5e6;
--amber: #b66a00;
--amber-light: #fff0cf;
--green: #267a4d;
--green-light: #e0f3e7;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce6e7;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.brand {
position: fixed;
top: 18px;
left: 18px;
z-index: 5;
pointer-events: none;
letter-spacing: 0.02em;
}
.brand strong {
display: block;
font-size: 22px;
line-height: 1;
}
.brand span {
display: block;
margin-top: 7px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 10;
top: 82px;
bottom: 18px;
width: 326px;
padding: 16px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(25, 45, 52, 0.16);
backdrop-filter: blur(16px);
}
#left-panel {
left: 18px;
}
#right-panel {
right: 18px;
width: 310px;
}
.section + .section {
margin-top: 18px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 11px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.section-title small {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
text-transform: none;
}
label,
select,
input[type="number"] {
font: inherit;
}
select,
input[type="number"] {
width: 100%;
border: 1px solid var(--line);
border-radius: 9px;
background: white;
color: var(--ink);
}
select {
padding: 10px 11px;
font-weight: 700;
}
input[type="number"] {
padding: 7px 8px;
}
.control {
margin-top: 13px;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 6px;
font-size: 12px;
font-weight: 700;
}
.control-row output {
min-width: 62px;
color: var(--teal);
text-align: right;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--teal);
cursor: pointer;
}
input[type="range"]:disabled {
opacity: 0.35;
cursor: not-allowed;
}
.hint {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.body-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.body-grid label {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 700;
line-height: 1.3;
}
.body-grid input {
margin-top: 4px;
}
.toggle {
display: flex;
align-items: center;
gap: 9px;
margin: 10px 0;
color: #43565d;
font-size: 12px;
font-weight: 650;
cursor: pointer;
}
.toggle input {
width: 16px;
height: 16px;
accent-color: var(--teal);
}
.button-row {
display: flex;
gap: 8px;
margin-top: 12px;
}
button {
flex: 1;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: white;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
button:hover {
border-color: rgba(20, 125, 117, 0.45);
color: var(--teal);
}
.dimensions {
padding: 10px;
border-radius: 10px;
background: rgba(23, 38, 45, 0.045);
color: #52636a;
font-size: 11px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 12px;
}
.metric {
padding: 11px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.72);
}
.metric strong {
display: block;
font-size: 23px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.metric.bad strong {
color: var(--red);
}
.metric.warn strong {
color: var(--amber);
}
.status-list {
display: grid;
gap: 7px;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 9px 10px;
border-radius: 10px;
font-size: 11px;
font-weight: 700;
}
.status-row.good {
background: var(--green-light);
color: var(--green);
}
.status-row.bad {
background: var(--red-light);
color: var(--red);
}
.status-row.warn {
background: var(--amber-light);
color: var(--amber);
}
.status-row small {
display: block;
margin-top: 3px;
opacity: 0.76;
font-size: 9px;
font-weight: 650;
}
.pill {
flex: 0 0 auto;
padding: 4px 7px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.65);
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.empty {
padding: 12px;
border: 1px dashed rgba(23, 38, 45, 0.16);
border-radius: 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
text-align: center;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 12px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--green);
}
.dot.red {
background: var(--red);
}
.dot.amber {
background: var(--amber);
}
.dot.cyan {
background: #159eaa;
}
.camera-help {
position: fixed;
z-index: 4;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
padding: 8px 13px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 999px;
background: rgba(250, 252, 251, 0.82);
box-shadow: 0 8px 25px rgba(25, 45, 52, 0.1);
color: #52636a;
font-size: 10px;
font-weight: 700;
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 850px) {
.panel {
top: auto;
bottom: 12px;
max-height: 43vh;
width: calc(50% - 18px);
}
#left-panel {
left: 12px;
}
#right-panel {
right: 12px;
}
.brand {
top: 14px;
left: 14px;
}
.camera-help {
display: none;
}
}
@media (max-width: 580px) {
.panel {
width: calc(100% - 24px);
max-height: 38vh;
}
#right-panel {
display: none;
}
.brand strong {
font-size: 18px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="brand">
<strong>DESK FIT</strong>
<span>Spatial collision + reach lab</span>
</div>
<section class="panel" id="left-panel">
<div class="section">
<h2 class="section-title">
Adjust object
<small>metric transforms</small>
</h2>
<select id="object-select" aria-label="Object to adjust"></select>
<div class="control">
<div class="control-row">
<label for="slider-x">X position</label>
<output id="output-x"></output>
</div>
<input id="slider-x" type="range" min="-2.5" max="2.5" step="0.01">
</div>
<div class="control">
<div class="control-row">
<label for="slider-z">Z position</label>
<output id="output-z"></output>
</div>
<input id="slider-z" type="range" min="-2.5" max="2.5" step="0.01">
</div>
<div class="control">
<div class="control-row">
<label for="slider-y">Height</label>
<output id="output-y"></output>
</div>
<input id="slider-y" type="range" min="0" max="1.6" step="0.01">
</div>
<div class="control">
<div class="control-row">
<label for="slider-yaw">Rotation</label>
<output id="output-yaw"></output>
</div>
<input id="slider-yaw" type="range" min="-180" max="180" step="1">
</div>
<p class="hint" id="frame-hint"></p>
</div>
<div class="section">
<h2 class="section-title">
Declared body dimensions
<small>seated proxy</small>
</h2>
<div class="body-grid">
<label>
Seat → shoulder
<input id="body-shoulder" type="number" min="35" max="90" step="1" value="62">
</label>
<label>
Arm reach
<input id="body-reach" type="number" min="35" max="100" step="1" value="68">
</label>
<label>
Torso width
<input id="body-torso-w" type="number" min="25" max="70" step="1" value="42">
</label>
<label>
Torso depth
<input id="body-torso-d" type="number" min="18" max="60" step="1" value="28">
</label>
<label>
Thigh length
<input id="body-thigh" type="number" min="25" max="65" step="1" value="42">
</label>
<label>
Hand radius
<input id="body-hand" type="number" min="1" max="15" step="1" value="5">
</label>
</div>
<p class="hint">
Reach is a spherical shoulder-to-grasp envelope. It intentionally does
not perform posture, fatigue, or motion-path planning.
</p>
</div>
<div class="section">
<h2 class="section-title">Inspection</h2>
<label class="toggle">
<input id="show-proxies" type="checkbox">
Show collision proxies
</label>
<label class="toggle">
<input id="show-reach" type="checkbox" checked>
Show reach envelope and rays
</label>
<div class="button-row">
<button id="focus-button" type="button">Focus selected</button>
<button id="camera-button" type="button">Fit view</button>
</div>
<div class="button-row">
<button id="reset-button" type="button">Reset layout</button>
</div>
</div>
<div class="section">
<h2 class="section-title">
Declared selected geometry
<small>width × height × depth</small>
</h2>
<div id="selected-dimensions" class="dimensions"></div>
</div>
</section>
<section class="panel" id="right-panel">
<div class="section">
<h2 class="section-title">
Safety flags
<small>live analysis</small>
</h2>
<div class="metrics">
<div class="metric" id="collision-metric">
<strong id="collision-count">0</strong>
<span>Collisions</span>
</div>
<div class="metric" id="reach-metric">
<strong id="reach-count">0</strong>
<span>Unreachable</span>
</div>
</div>
<h3 class="section-title">
Collision pairs
<small>OBB / SAT</small>
</h3>
<div id="collision-list" class="status-list"></div>
</div>
<div class="section">
<h3 class="section-title">
Tool reach
<small>shoulder sphere</small>
</h3>
<div id="reach-list" class="status-list"></div>
</div>
<div class="section">
<h3 class="section-title">Legend</h3>
<div class="legend">
<span><i class="dot"></i>Reachable</span>
<span><i class="dot red"></i>Collision</span>
<span><i class="dot amber"></i>Out of reach</span>
<span><i class="dot cyan"></i>Selected</span>
</div>
<p class="hint">
Collision proxies are oriented boxes. A 6 mm penetration tolerance is
used so resting surfaces are not reported as collisions.
</p>
</div>
</section>
<div class="camera-help">
Drag: orbit · Right-drag: pan · Wheel: zoom · Click: select ·
Arrow keys: move 1 cm · Shift + arrows: move 10 cm
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Scene
// ---------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce6e7);
scene.fog = new THREE.Fog(0xdce6e7, 7, 16);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.05,
50
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
viewport.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf8ffff, 0x667476, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff3d7, 3.4);
sun.position.set(3.8, 6, 4.2);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -4;
sun.shadow.camera.right = 4;
sun.shadow.camera.top = 4;
sun.shadow.camera.bottom = -4;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 15;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8dd5df, 1.2);
fill.position.set(-4, 3, -2);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(20, 20),
new THREE.MeshStandardMaterial({
color: 0xd4dedf,
roughness: 1,
flatShading: true
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.012;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(10, 20, 0x8ca0a4, 0xb4c1c3);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
// ---------------------------------------------------------------------
// Declared simplified dimensions, in metres
// ---------------------------------------------------------------------
const BODY = {
shoulderRise: 0.62, // Seat surface to shoulder center.
armReach: 0.68, // Shoulder to hand center.
torsoWidth: 0.42,
torsoDepth: 0.28,
thighLength: 0.42,
handRadius: 0.05
};
const DEFAULT_BODY = { ...BODY };
const objectDefinitions = [
{
id: "desk",
label: "Desk",
pos: [0, 0, 0],
yaw: 0,
color: 0xc9965c,
proxies: [
{
label: "Desktop",
center: [0, 0.72, 0],
size: [1.60, 0.06, 0.80]
},
{
label: "Drawer",
center: [0, 0.60, 0.30],
size: [0.72, 0.18, 0.10]
},
{
label: "Leg FL",
center: [-0.69, 0.34, -0.28],
size: [0.08, 0.68, 0.08]
},
{
label: "Leg FR",
center: [0.69, 0.34, -0.28],
size: [0.08, 0.68, 0.08]
},
{
label: "Leg RL",
center: [-0.69, 0.34, 0.28],
size: [0.08, 0.68, 0.08]
},
{
label: "Leg RR",
center: [0.69, 0.34, 0.28],
size: [0.08, 0.68, 0.08]
}
]
},
{
id: "chair",
label: "Chair",
pos: [0, 0, 0.98],
yaw: Math.PI,
color: 0x347f86,
proxies: [
{
label: "Seat",
center: [0, 0.45, 0],
size: [0.50, 0.08, 0.50]
},
{
label: "Back",
center: [0, 0.73, -0.23],
size: [0.50, 0.56, 0.08]
},
{
label: "Stem",
center: [0, 0.22, 0],
size: [0.08, 0.40, 0.08]
},
{
label: "Base",
center: [0, 0.055, 0],
size: [0.58, 0.06, 0.10]
}
]
},
{
id: "toolbox",
label: "Toolbox",
parent: "desk",
tool: true,
pos: [-0.50, 0.83, 0.24],
yaw: 0,
color: 0xd45a45,
grasp: [0, 0.12, 0],
proxies: [
{
label: "Toolbox body",
center: [0, 0, 0],
size: [0.36, 0.16, 0.22]
},
{
label: "Toolbox handle",
center: [0, 0.105, 0],
size: [0.16, 0.05, 0.04]
}
]
},
{
id: "mug",
label: "Mug",
parent: "desk",
tool: true,
pos: [-0.08, 0.84, 0.28],
yaw: 0,
color: 0xe6b24c,
grasp: [0, 0.11, 0],
proxies: [
{
label: "Mug body",
center: [0, 0, 0],
size: [0.14, 0.18, 0.14]
}
]
},
{
id: "drill",
label: "Power drill",
parent: "desk",
tool: true,
pos: [0.18, 0.85, 0.25],
yaw: 0,
color: 0xe3b72f,
grasp: [0.02, 0.08, 0],
proxies: [
{
label: "Drill motor",
center: [0, 0, 0],
size: [0.28, 0.14, 0.12]
},
{
label: "Drill grip",
center: [0.08, -0.01, 0],
size: [0.07, 0.18, 0.10]
},
{
label: "Drill bit",
center: [0, 0.015, 0.12],
size: [0.045, 0.045, 0.18]
}
]
},
{
id: "radio",
label: "Radio",
parent: "desk",
tool: true,
pos: [0.53, 0.85, 0.22],
yaw: 0,
color: 0x465b69,
grasp: [0, 0.10, 0],
proxies: [
{
label: "Radio body",
center: [0, 0, 0],
size: [0.28, 0.18, 0.13]
},
{
label: "Radio antenna",
center: [0, 0.18, -0.04],
size: [0.02, 0.18, 0.02]
}
]
}
];
const objects = new Map();
const objectOrder = objectDefinitions.map((definition) => definition.id);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const DEG = Math.PI / 180;
const COLLISION_EPSILON = 0.006;
// ---------------------------------------------------------------------
// Low-poly visual construction
// ---------------------------------------------------------------------
function addEdge(mesh, object) {
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 20),
new THREE.LineBasicMaterial({
color: 0x23343a,
transparent: true,
opacity: 0.33
})
);
edge.userData.isEdge = true;
edge.renderOrder = 2;
mesh.add(edge);
object.edges.push(edge);
}
function addBox(parent, object, size, center, color, outlined = true) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.04,
flatShading: true
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(center[0], center[1], center[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.objectId = object.id;
parent.add(mesh);
object.materials.push(material);
if (outlined) {
addEdge(mesh, object);
}
return mesh;
}
function addCylinder(
parent,
object,
radiusTop,
radiusBottom,
height,
center,
color,
rotation = [0, 0, 0],
radialSegments = 8
) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.05,
flatShading: true
});
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
radialSegments
),
material
);
mesh.position.set(center[0], center[1], center[2]);
mesh.rotation.set(rotation[0], rotation[1], rotation[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.objectId = object.id;
parent.add(mesh);
object.materials.push(material);
addEdge(mesh, object);
return mesh;
}
function buildVisuals(object) {
const root = object.root;
switch (object.id) {
case "desk": {
addBox(root, object, [1.60, 0.06, 0.80], [0, 0.72, 0], 0xc9965c);
addBox(root, object, [0.72, 0.18, 0.10], [0, 0.60, 0.30], 0xa97848);
for (const x of [-0.69, 0.69]) {
for (const z of [-0.28, 0.28]) {
addBox(
root,
object,
[0.08, 0.68, 0.08],
[x, 0.34, z],
0x46565a
);
}
}
const drawerPull = addBox(
root,
object,
[0.18, 0.025, 0.025],
[0, 0.60, 0.365],
0x27363b
);
drawerPull.castShadow = false;
break;
}
case "chair": {
addBox(root, object, [0.50, 0.08, 0.50], [0, 0.45, 0], 0x347f86);
addBox(root, object, [0.50, 0.56, 0.08], [0, 0.73, -0.23], 0x2c6c73);
addBox(root, object, [0.08, 0.40, 0.08], [0, 0.22, 0], 0x3d494d);
addBox(root, object, [0.58, 0.06, 0.10], [0, 0.055, 0], 0x303d42);
break;
}
case "toolbox": {
addBox(root, object, [0.36, 0.16, 0.22], [0, 0, 0], 0xd45a45);
addBox(root, object, [0.16, 0.05, 0.04], [0, 0.105, 0], 0x27363b);
addBox(root, object, [0.27, 0.018, 0.015], [0, 0, 0.116], 0xf2bd74, false);
break;
}
case "mug": {
addCylinder(root, object, 0.07, 0.065, 0.18, [0, 0, 0], 0xe6b24c);
const material = new THREE.MeshStandardMaterial({
color: 0xe6b24c,
roughness: 0.72,
flatShading: true
});
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.052, 0.014, 6, 12, Math.PI * 1.45),
material
);
handle.position.set(0.075, 0, 0);
handle.rotation.y = Math.PI / 2;
handle.castShadow = true;
handle.userData.objectId = object.id;
root.add(handle);
object.materials.push(material);
addEdge(handle, object);
break;
}
case "drill": {
addBox(root, object, [0.28, 0.14, 0.12], [0, 0, 0], 0xe3b72f);
addBox(root, object, [0.07, 0.18, 0.10], [0.08, -0.01, 0], 0x35434a);
addBox(root, object, [0.045, 0.045, 0.18], [0, 0.015, 0.12], 0x9eabb0);
addCylinder(
root,
object,
0.048,
0.048,
0.055,
[0, 0.015, 0.055],
0x35434a,
[Math.PI / 2, 0, 0]
);
break;
}
case "radio": {
addBox(root, object, [0.28, 0.18, 0.13], [0, 0, 0], 0x465b69);
addCylinder(
root,
object,
0.009,
0.012,
0.18,
[0, 0.18, -0.04],
0x27363b,
[0, 0, 0],
6
);
addBox(root, object, [0.12, 0.065, 0.008], [-0.055, 0.025, 0.069], 0xb8c6c9, false);
addBox(root, object, [0.025, 0.025, 0.012], [0.075, -0.025, 0.069], 0xd98d45, false);
break;
}
}
}
function createProxyOverlay(object) {
for (const proxy of object.proxies) {
const material = new THREE.MeshBasicMaterial({
color: 0x7d8d91,
wireframe: true,
transparent: true,
opacity: 0.68,
depthTest: false
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(proxy.size[0], proxy.size[1], proxy.size[2]),
material
);
mesh.position.set(
proxy.center[0],
proxy.center[1],
proxy.center[2]
);
mesh.renderOrder = 10;
mesh.visible = false;
object.root.add(mesh);
object.overlays.push(mesh);
}
}
for (const definition of objectDefinitions) {
const object = {
...definition,
pos: new THREE.Vector3(...definition.pos),
initialPos: new THREE.Vector3(...definition.pos),
initialYaw: definition.yaw,
root: new THREE.Group(),
materials: [],
edges: [],
overlays: [],
reachMarker: null,
reachLine: null
};
objects.set(object.id, object);
if (object.parent) {
objects.get(object.parent).root.add(object.root);
} else {
scene.add(object.root);
}
buildVisuals(object);
createProxyOverlay(object);
if (object.tool) {
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.025, 8, 6),
new THREE.MeshBasicMaterial({
color: 0x267a4d,
depthTest: false
})
);
marker.position.set(...object.grasp);
marker.renderOrder = 12;
object.root.add(marker);
object.reachMarker = marker;
const lineGeometry = new THREE.BufferGeometry();
lineGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const line = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color: 0x267a4d,
transparent: true,
opacity: 0.7,
depthTest: false
})
);
line.renderOrder = 11;
scene.add(line);
object.reachLine = line;
}
}
// ---------------------------------------------------------------------
// Declared seated-body proxy
// ---------------------------------------------------------------------
const bodyGroup = new THREE.Group();
scene.add(bodyGroup);
let bodyDebugMeshes = [];
let reachEnvelope = null;
let shoulderMarker = null;
function clearBodyGroup() {
while (bodyGroup.children.length) {
const child = bodyGroup.children.pop();
child.traverse((node) => {
if (node.geometry) node.geometry.dispose();
if (node.material) node.material.dispose();
});
}
}
function bodyShoulderHeight() {
return 0.49 + BODY.shoulderRise;
}
function bodyShoulderLocal() {
return new THREE.Vector3(0, bodyShoulderHeight(), 0.12);
}
function rebuildBody() {
clearBodyGroup();
bodyDebugMeshes = [];
const torsoCenter = [0, bodyShoulderHeight() - 0.22, 0.05];
const thighCenter = [0, 0.40, 0.12 + BODY.thighLength / 2];
const shinCenter = [0, 0.18, 0.12 + BODY.thighLength];
const bodyProxies = [
{
label: "Torso",
center: torsoCenter,
size: [BODY.torsoWidth, 0.44, BODY.torsoDepth]
},
{
label: "Thighs",
center: thighCenter,
size: [BODY.torsoWidth * 0.80, 0.18, BODY.thighLength]
},
{
label: "Lower legs",
center: shinCenter,
size: [0.14, 0.34, 0.14]
}
];
for (const proxy of bodyProxies) {
const material = new THREE.MeshBasicMaterial({
color: 0x42a6a0,
transparent: true,
opacity: 0.16,
depthWrite: false
});
const visual = new THREE.Mesh(
new THREE.BoxGeometry(...proxy.size),
material
);
visual.position.set(...proxy.center);
bodyGroup.add(visual);
const debugMaterial = new THREE.MeshBasicMaterial({
color: 0x42a6a0,
wireframe: true,
transparent: true,
opacity: 0.6,
depthTest: false
});
const debug = new THREE.Mesh(
new THREE.BoxGeometry(...proxy.size),
debugMaterial
);
debug.position.set(...proxy.center);
debug.visible = false;
debug.renderOrder = 10;
bodyGroup.add(debug);
bodyDebugMeshes.push(debug);
}
reachEnvelope = new THREE.Mesh(
new THREE.SphereGeometry(
BODY.armReach + BODY.handRadius,
24,
12
),
new THREE.MeshBasicMaterial({
color: 0x2d9f98,
wireframe: true,
transparent: true,
opacity: 0.17,
depthWrite: false
})
);
reachEnvelope.position.copy(bodyShoulderLocal());
bodyGroup.add(reachEnvelope);
shoulderMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 10, 7),
new THREE.MeshBasicMaterial({
color: 0x147d75,
depthTest: false
})
);
shoulderMarker.position.copy(bodyShoulderLocal());
shoulderMarker.renderOrder = 12;
bodyGroup.add(shoulderMarker);
}
rebuildBody();
// ---------------------------------------------------------------------
// Transform and collision math
// ---------------------------------------------------------------------
function localPointToWorld(object, point) {
const result = new THREE.Vector3(...point);
result.applyAxisAngle(Y_AXIS, object.yaw);
if (object.parent) {
const parent = objects.get(object.parent);
result.applyAxisAngle(Y_AXIS, parent.yaw);
result.add(parent.pos);
}
result.add(object.pos);
return result;
}
function worldProxy(object, proxy) {
const center = localPointToWorld(object, proxy.center);
const parentYaw = object.parent
? objects.get(object.parent).yaw
: 0;
const yaw = parentYaw + object.yaw;
const cosine = Math.cos(yaw);
const sine = Math.sin(yaw);
return {
label: proxy.label,
center,
half: new THREE.Vector3(
proxy.size[0] / 2,
proxy.size[1] / 2,
proxy.size[2] / 2
),
axisX: new THREE.Vector3(cosine, 0, -sine),
axisZ: new THREE.Vector3(sine, 0, cosine)
};
}
function bodyWorldProxies() {
const chair = objects.get("chair");
const shoulder = bodyShoulderHeight();
const localProxies = [
{
label: "Torso",
center: [0, shoulder - 0.22, 0.05],
size: [BODY.torsoWidth, 0.44, BODY.torsoDepth]
},
{
label: "Thighs",
center: [0, 0.40, 0.12 + BODY.thighLength / 2],
size: [BODY.torsoWidth * 0.80, 0.18, BODY.thighLength]
},
{
label: "Lower legs",
center: [0, 0.18, 0.12 + BODY.thighLength],
size: [0.14, 0.34, 0.14]
}
];
return localProxies.map((proxy) => {
const center = localPointToWorld(chair, proxy.center);
const cosine = Math.cos(chair.yaw);
const sine = Math.sin(chair.yaw);
return {
label: proxy.label,
center,
half: new THREE.Vector3(
proxy.size[0] / 2,
proxy.size[1] / 2,
proxy.size[2] / 2
),
axisX: new THREE.Vector3(cosine, 0, -sine),
axisZ: new THREE.Vector3(sine, 0, cosine)
};
});
}
function boxesOverlap(a, b) {
const delta = new THREE.Vector3().subVectors(b.center, a.center);
const axes = [
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, 0, 1),
a.axisX,
a.axisZ,
b.axisX,
b.axisZ
];
for (const axis of axes) {
const radiusA =
a.half.x * Math.abs(a.axisX.dot(axis)) +
a.half.y * Math.abs(Y_AXIS.dot(axis)) +
a.half.z * Math.abs(a.axisZ.dot(axis));
const radiusB =
b.half.x * Math.abs(b.axisX.dot(axis)) +
b.half.y * Math.abs(Y_AXIS.dot(axis)) +
b.half.z * Math.abs(b.axisZ.dot(axis));
const separation = Math.abs(delta.dot(axis)) - radiusA - radiusB;
// A non-negative separation means touching or separated.
if (separation >= -COLLISION_EPSILON) {
return false;
}
}
return true;
}
let collisionPairs = [];
let collisionObjects = new Set();
let reachResults = new Map();
function computeCollisions() {
collisionPairs = [];
collisionObjects = new Set();
const definitions = objectOrder.map((id) => objects.get(id));
// Check every pair of physical objects.
for (let i = 0; i < definitions.length; i++) {
for (let j = i + 1; j < definitions.length; j++) {
const a = definitions[i];
const b = definitions[j];
const aProxies = a.proxies.map((proxy) => worldProxy(a, proxy));
const bProxies = b.proxies.map((proxy) => worldProxy(b, proxy));
for (const ap of aProxies) {
for (const bp of bProxies) {
if (boxesOverlap(ap, bp)) {
collisionPairs.push({
a,
b,
proxyA: ap.label,
proxyB: bp.label
});
collisionObjects.add(a.id);
collisionObjects.add(b.id);
break;
}
}
if (collisionObjects.has(a.id) && collisionObjects.has(b.id)) {
break;
}
}
}
}
// The body is checked against every object except the chair, because
// the seated body and chair are intentionally in contact.
const bodyProxies = bodyWorldProxies();
const chair = objects.get("chair");
for (const object of definitions) {
if (object.id === "chair") continue;
const objectProxies = object.proxies.map((proxy) =>
worldProxy(object, proxy)
);
for (const bp of bodyProxies) {
for (const op of objectProxies) {
if (boxesOverlap(bp, op)) {
collisionPairs.push({
a: {
id: "body",
label: "Seated body"
},
b: object,
proxyA: bp.label,
proxyB: op.label
});
collisionObjects.add(object.id);
break;
}
}
}
}
// Avoid duplicate body collision pairs caused by overlapping proxies.
const seen = new Set();
collisionPairs = collisionPairs.filter((pair) => {
const key = [
pair.a.id,
pair.b.id,
pair.proxyA,
pair.proxyB
].sort().join("|");
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
function computeReach() {
reachResults = new Map();
const chair = objects.get("chair");
const shoulder = localPointToWorld(chair, bodyShoulderLocal());
const threshold = BODY.armReach + BODY.handRadius;
for (const object of objects.values()) {
if (!object.tool) continue;
const target = localPointToWorld(object, object.grasp);
const distance = shoulder.distanceTo(target);
const reachable = distance <= threshold + 0.002;
reachResults.set(object.id, {
distance,
threshold,
reachable,
shoulder,
target
});
}
}
function updateTransforms() {
for (const object of objects.values()) {
object.root.position.copy(object.pos);
object.root.rotation.y = object.yaw;
}
const chair = objects.get("chair");
bodyGroup.position.copy(chair.pos);
bodyGroup.rotation.y = chair.yaw;
scene.updateMatrixWorld(true);
}
// ---------------------------------------------------------------------
// UI state and visual feedback
// ---------------------------------------------------------------------
const ui = {
objectSelect: document.getElementById("object-select"),
sliderX: document.getElementById("slider-x"),
sliderZ: document.getElementById("slider-z"),
sliderY: document.getElementById("slider-y"),
sliderYaw: document.getElementById("slider-yaw"),
outputX: document.getElementById("output-x"),
outputZ: document.getElementById("output-z"),
outputY: document.getElementById("output-y"),
outputYaw: document.getElementById("output-yaw"),
frameHint: document.getElementById("frame-hint"),
selectedDimensions: document.getElementById("selected-dimensions"),
collisionCount: document.getElementById("collision-count"),
reachCount: document.getElementById("reach-count"),
collisionMetric: document.getElementById("collision-metric"),
reachMetric: document.getElementById("reach-metric"),
collisionList: document.getElementById("collision-list"),
reachList: document.getElementById("reach-list"),
showProxies: document.getElementById("show-proxies"),
showReach: document.getElementById("show-reach")
};
let selectedId = "toolbox";
for (const object of objects.values()) {
const option = document.createElement("option");
option.value = object.id;
option.textContent = object.label;
ui.objectSelect.appendChild(option);
}
function formatCentimeters(value) {
return `${Math.round(value * 100)} cm`;
}
function syncControls() {
const object = objects.get(selectedId);
ui.objectSelect.value = selectedId;
ui.sliderX.value = object.pos.x;
ui.sliderZ.value = object.pos.z;
ui.sliderY.value = object.pos.y;
ui.sliderYaw.value = object.yaw / DEG;
ui.sliderY.disabled = !object.tool;
ui.outputX.textContent = formatCentimeters(object.pos.x);
ui.outputZ.textContent = formatCentimeters(object.pos.z);
ui.outputY.textContent = formatCentimeters(object.pos.y);
ui.outputYaw.textContent = `${Math.round(object.yaw / DEG)}°`;
ui.frameHint.textContent = object.parent
? "Tool transforms are relative to the desk frame."
: "Object transforms use the world floor frame.";
ui.selectedDimensions.innerHTML = object.proxies
.map((proxy) => {
const dimensions = proxy.size
.map((value) => Math.round(value * 100))
.join(" × ");
return `<div><strong>${proxy.label}</strong>: ${dimensions} cm</div>`;
})
.join("");
}
function selectObject(id) {
if (!objects.has(id)) return;
selectedId = id;
syncControls();
updateAnalysis();
}
function updateObjectVisuals() {
for (const object of objects.values()) {
const collision = collisionObjects.has(object.id);
const reach = reachResults.get(object.id);
const unreachable = reach ? !reach.reachable : false;
const selected = object.id === selectedId;
let color = 0x000000;
let intensity = 0;
if (collision) {
color = 0xd83c43;
intensity = 0.34;
} else if (unreachable) {
color = 0xb66a00;
intensity = 0.28;
} else if (selected) {
color = 0x159eaa;
intensity = 0.15;
}
for (const material of object.materials) {
material.emissive.setHex(color);
material.emissiveIntensity = intensity;
}
for (const edge of object.edges) {
if (collision) {
edge.material.color.setHex(0xd83c43);
edge.material.opacity = 0.72;
} else if (unreachable) {
edge.material.color.setHex(0xb66a00);
edge.material.opacity = 0.68;
} else if (selected) {
edge.material.color.setHex(0x159eaa);
edge.material.opacity = 0.62;
} else {
edge.material.color.setHex(0x23343a);
edge.material.opacity = 0.33;
}
}
for (const overlay of object.overlays) {
overlay.visible = ui.showProxies.checked;
if (collision) {
overlay.material.color.setHex(0xd83c43);
} else if (unreachable) {
overlay.material.color.setHex(0xb66a00);
} else if (selected) {
overlay.material.color.setHex(0x159eaa);
} else {
overlay.material.color.setHex(0x667b80);
}
}
}
for (const debug of bodyDebugMeshes) {
debug.visible = ui.showProxies.checked;
}
}
function updateReachVisuals() {
for (const object of objects.values()) {
if (!object.tool) continue;
const result = reachResults.get(object.id);
const color = result.reachable ? 0x267a4d : 0xb66a00;
object.reachMarker.material.color.setHex(color);
object.reachLine.material.color.setHex(color);
const positions = object.reachLine.geometry.attributes.position;
positions.setXYZ(0, result.shoulder.x, result.shoulder.y, result.shoulder.z);
positions.setXYZ(1, result.target.x, result.target.y, result.target.z);
positions.needsUpdate = true;
object.reachLine.geometry.computeBoundingSphere();
object.reachLine.visible = ui.showReach.checked;
}
if (reachEnvelope) {
reachEnvelope.visible = ui.showReach.checked;
}
}
function updateStatusPanel() {
ui.collisionCount.textContent = collisionPairs.length;
ui.reachCount.textContent =
[...reachResults.values()].filter((result) => !result.reachable).length;
ui.collisionMetric.classList.toggle(
"bad",
collisionPairs.length > 0
);
ui.reachMetric.classList.toggle(
"warn",
[...reachResults.values()].some((result) => !result.reachable)
);
if (collisionPairs.length === 0) {
ui.collisionList.innerHTML =
'<div class="empty">No inter-object penetrations detected.</div>';
} else {
ui.collisionList.innerHTML = collisionPairs
.map(
(pair) => `
<div class="status-row bad">
<span>
${pair.a.label} ↔ ${pair.b.label}
<small>${pair.proxyA} / ${pair.proxyB}</small>
</span>
<span class="pill">Collision</span>
</div>
`
)
.join("");
}
ui.reachList.innerHTML = [...reachResults.entries()]
.map(([id, result]) => {
const object = objects.get(id);
const distance = Math.round(result.distance * 100);
const threshold = Math.round(result.threshold * 100);
return `
<div class="status-row ${result.reachable ? "good" : "warn"}">
<span>
${object.label}
<small>${distance} cm / ${threshold} cm envelope</small>
</span>
<span class="pill">
${result.reachable ? "Reachable" : "Out of reach"}
</span>
</div>
`;
})
.join("");
}
function updateAnalysis() {
updateTransforms();
computeCollisions();
computeReach();
updateObjectVisuals();
updateReachVisuals();
updateStatusPanel();
}
ui.objectSelect.addEventListener("change", () => {
selectObject(ui.objectSelect.value);
});
ui.sliderX.addEventListener("input", () => {
objects.get(selectedId).pos.x = Number(ui.sliderX.value);
syncControls();
updateAnalysis();
});
ui.sliderZ.addEventListener("input", () => {
objects.get(selectedId).pos.z = Number(ui.sliderZ.value);
syncControls();
updateAnalysis();
});
ui.sliderY.addEventListener("input", () => {
objects.get(selectedId).pos.y = Number(ui.sliderY.value);
syncControls();
updateAnalysis();
});
ui.sliderYaw.addEventListener("input", () => {
objects.get(selectedId).yaw = Number(ui.sliderYaw.value) * DEG;
syncControls();
updateAnalysis();
});
ui.showProxies.addEventListener("change", updateObjectVisuals);
ui.showReach.addEventListener("change", updateReachVisuals);
// ---------------------------------------------------------------------
// Body dimension controls
// ---------------------------------------------------------------------
const bodyInputs = {
shoulderRise: document.getElementById("body-shoulder"),
armReach: document.getElementById("body-reach"),
torsoWidth: document.getElementById("body-torso-w"),
torsoDepth: document.getElementById("body-torso-d"),
thighLength: document.getElementById("body-thigh"),
handRadius: document.getElementById("body-hand")
};
function readBodyInputs() {
for (const [key, input] of Object.entries(bodyInputs)) {
const value = Number(input.value) / 100;
if (Number.isFinite(value)) {
BODY[key] = value;
}
}
rebuildBody();
updateAnalysis();
}
for (const input of Object.values(bodyInputs)) {
input.addEventListener("change", readBodyInputs);
}
// ---------------------------------------------------------------------
// Direct camera controls, implemented without Three.js addons
// ---------------------------------------------------------------------
const orbit = {
yaw: 0.64,
pitch: 0.48,
distance: 4.8,
target: new THREE.Vector3(0, 0.65, 0.10)
};
function applyCamera() {
const horizontal = Math.cos(orbit.pitch) * orbit.distance;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.yaw = 0.64;
orbit.pitch = 0.48;
orbit.distance = 4.8;
orbit.target.set(0, 0.65, 0.10);
applyCamera();
}
document.getElementById("camera-button").addEventListener("click", resetCamera);
document.getElementById("focus-button").addEventListener("click", () => {
const object = objects.get(selectedId);
const focus = new THREE.Vector3();
object.root.getWorldPosition(focus);
focus.y += object.tool ? 0.80 : 0.45;
orbit.target.copy(focus);
orbit.distance = Math.min(orbit.distance, 3.2);
applyCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragState = null;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
dragState = {
startX: event.clientX,
startY: event.clientY,
x: event.clientX,
y: event.clientY,
button: event.button,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragState) return;
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
const totalDistance = Math.hypot(
event.clientX - dragState.startX,
event.clientY - dragState.startY
);
if (totalDistance > 4) {
dragState.moved = true;
}
if (dragState.button === 2) {
const scale = orbit.distance * 0.0013;
const right = new THREE.Vector3(Math.cos(orbit.yaw), 0, -Math.sin(orbit.yaw));
const forward = new THREE.Vector3(
-Math.sin(orbit.yaw),
0,
-Math.cos(orbit.yaw)
);
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(forward, -dy * scale);
} else {
orbit.yaw -= dx * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.005,
0.12,
1.30
);
}
dragState.x = event.clientX;
dragState.y = event.clientY;
applyCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragState) return;
if (!dragState.moved && dragState.button === 0) {
const rect = renderer.domElement.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);
const intersections = raycaster.intersectObjects(
[...objects.values()].map((object) => object.root),
true
);
const hit = intersections.find(
(intersection) => intersection.object.userData.objectId
);
if (hit) {
selectObject(hit.object.userData.objectId);
}
}
dragState = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance * Math.exp(event.deltaY * 0.001),
2.0,
10.0
);
applyCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Keyboard adjustment
// ---------------------------------------------------------------------
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLSelectElement
) {
return;
}
const step = event.shiftKey ? 0.10 : 0.01;
const object = objects.get(selectedId);
let handled = true;
switch (event.key) {
case "ArrowLeft":
object.pos.x -= step;
break;
case "ArrowRight":
object.pos.x += step;
break;
case "ArrowUp":
object.pos.z -= step;
break;
case "ArrowDown":
object.pos.z += step;
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
syncControls();
updateAnalysis();
}
});
// ---------------------------------------------------------------------
// Reset
// ---------------------------------------------------------------------
document.getElementById("reset-button").addEventListener("click", () => {
for (const object of objects.values()) {
object.pos.copy(object.initialPos);
object.yaw = object.initialYaw;
}
Object.assign(BODY, DEFAULT_BODY);
bodyInputs.shoulderRise.value = Math.round(BODY.shoulderRise * 100);
bodyInputs.armReach.value = Math.round(BODY.armReach * 100);
bodyInputs.torsoWidth.value = Math.round(BODY.torsoWidth * 100);
bodyInputs.torsoDepth.value = Math.round(BODY.torsoDepth * 100);
bodyInputs.thighLength.value = Math.round(BODY.thighLength * 100);
bodyInputs.handRadius.value = Math.round(BODY.handRadius * 100);
rebuildBody();
selectedId = "toolbox";
syncControls();
updateAnalysis();
resetCamera();
});
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
syncControls();
updateAnalysis();
applyCamera();
animate();
</script>
</body>
</html>
```