Prompt specification
Build or customize the Three.js 3D scene "Recover From a Missing Part" 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: Recover From a Missing Part
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/recover-from-a-missing-part-x2064
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a small low-poly robot assembly cell that must handle a missing part. Put an input tray, a short articulated arm, an assembly fixture, and an output shelf on one compact bench. Run a short sequence in which the first transfer succeeds, but the next expected input slot is empty. The robot should enter a visible waiting or retry state instead of closing its gripper on imaginary cargo. Let the user place the missing part back into the tray and resume the sequence. Track the actual objects through every pickup and placement, with clear differences between waiting and active motion. Keep the previously completed object on the output shelf during the interruption, so waiting changes the process state without erasing earlier work.
Working Three.js Starter (recover-from-a-missing-part-x2064):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Three.js Robot Assembly Cell</title>
<style>
:root {
color-scheme: dark;
--bg: #07111c;
--panel: rgba(9, 20, 33, 0.91);
--border: rgba(148, 163, 184, 0.23);
--text: #e5eef9;
--muted: #94a3b8;
--cyan: #55e6ff;
--green: #64f5a6;
--amber: #ffbd59;
--violet: #c084fc;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
border-radius: 16px;
}
#hud {
position: fixed;
z-index: 5;
top: 18px;
left: 18px;
width: min(365px, calc(100vw - 36px));
padding: 18px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 5px;
font-size: 22px;
line-height: 1.1;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.state-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 16px;
}
#stateBadge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
border-radius: 999px;
color: var(--cyan);
border: 1px solid rgba(85, 230, 255, 0.28);
background: rgba(85, 230, 255, 0.09);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.08em;
white-space: nowrap;
}
#stateBadge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
#stateBadge.waiting {
color: var(--amber);
border-color: rgba(255, 189, 89, 0.32);
background: rgba(255, 189, 89, 0.1);
animation: waitingPulse 1.2s ease-in-out infinite;
}
#stateBadge.complete {
color: var(--green);
border-color: rgba(100, 245, 166, 0.32);
background: rgba(100, 245, 166, 0.1);
}
#stateBadge.fault {
color: #fb7185;
border-color: rgba(251, 113, 133, 0.35);
background: rgba(251, 113, 133, 0.1);
}
@keyframes waitingPulse {
50% {
box-shadow: 0 0 22px rgba(255, 189, 89, 0.18);
}
}
#gripperState {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-align: right;
}
#phaseTitle {
margin-top: 15px;
font-size: 15px;
font-weight: 750;
}
#phaseDetail {
min-height: 38px;
margin-top: 5px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.divider {
height: 1px;
margin: 15px 0;
background: var(--border);
}
.section-title {
margin-bottom: 9px;
color: #cbd5e1;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#steps {
display: grid;
gap: 7px;
}
.step {
display: grid;
grid-template-columns: 24px 1fr;
align-items: center;
gap: 8px;
color: #64748b;
font-size: 11px;
transition: color 180ms ease;
}
.step .number {
display: grid;
width: 23px;
height: 23px;
place-items: center;
border: 1px solid rgba(148, 163, 184, 0.2);
border-radius: 7px;
font-size: 9px;
font-weight: 900;
}
.step.current {
color: var(--cyan);
}
.step.current .number {
border-color: rgba(85, 230, 255, 0.45);
background: rgba(85, 230, 255, 0.09);
}
.step.done {
color: var(--green);
}
.step.done .number {
border-color: rgba(100, 245, 166, 0.36);
background: rgba(100, 245, 166, 0.08);
}
.object-row {
display: grid;
grid-template-columns: 13px 1fr auto;
align-items: center;
gap: 9px;
margin-top: 8px;
font-size: 11px;
}
.object-dot {
width: 10px;
height: 10px;
border-radius: 3px;
}
.object-name {
color: #cbd5e1;
}
.object-state {
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.07em;
}
#instructions {
position: fixed;
z-index: 5;
left: 18px;
bottom: 18px;
width: min(440px, calc(100vw - 36px));
padding: 16px 18px;
pointer-events: none;
}
#instructionTitle {
font-size: 14px;
font-weight: 800;
}
#instructionText {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.button-row {
display: flex;
gap: 8px;
margin-top: 12px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid rgba(85, 230, 255, 0.28);
border-radius: 10px;
padding: 9px 12px;
color: #dffaff;
background: rgba(85, 230, 255, 0.09);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover:not(:disabled) {
background: rgba(85, 230, 255, 0.17);
transform: translateY(-1px);
}
button:disabled {
cursor: not-allowed;
opacity: 0.38;
}
#placeButton {
border-color: rgba(192, 132, 252, 0.35);
color: #f1e4ff;
background: rgba(192, 132, 252, 0.1);
}
#cameraHelp {
position: fixed;
z-index: 5;
right: 18px;
bottom: 18px;
padding: 10px 13px;
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
#toast {
position: fixed;
z-index: 10;
top: 20px;
left: 50%;
max-width: min(460px, calc(100vw - 40px));
padding: 11px 15px;
border: 1px solid rgba(255, 189, 89, 0.35);
border-radius: 12px;
color: #ffe4b5;
background: rgba(42, 28, 12, 0.92);
font-size: 12px;
opacity: 0;
transform: translate(-50%, -10px);
transition:
opacity 180ms ease,
transform 180ms ease;
pointer-events: none;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 720px) {
#hud {
top: 10px;
left: 10px;
width: min(330px, calc(100vw - 20px));
padding: 13px;
}
#instructions {
left: 10px;
bottom: 10px;
width: min(390px, calc(100vw - 20px));
}
#cameraHelp {
display: none;
}
h1 {
font-size: 18px;
}
#steps {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section id="hud" class="panel">
<div class="eyebrow">Robotics / Assembly Cell 04</div>
<h1>Missing-Input Recovery</h1>
<p class="subtitle">
A live object-tracking demonstration. Completed work remains on the output shelf.
</p>
<div class="state-row">
<div id="stateBadge">ACTIVE MOTION</div>
<div id="gripperState">GRIPPER OPEN</div>
</div>
<div id="phaseTitle">Initializing cell</div>
<div id="phaseDetail">Preparing the robot and input tray.</div>
<div class="divider"></div>
<div class="section-title">Sequence</div>
<div id="steps">
<div class="step current" id="step0">
<span class="number">01</span><span>Pick drive housing</span>
</div>
<div class="step" id="step1">
<span class="number">02</span><span>Process in fixture</span>
</div>
<div class="step" id="step2">
<span class="number">03</span><span>Output completed housing</span>
</div>
<div class="step" id="step3">
<span class="number">04</span><span>Check input slot 2</span>
</div>
<div class="step" id="step4">
<span class="number">05</span><span>Restore core and resume</span>
</div>
</div>
<div class="divider"></div>
<div class="section-title">Tracked physical objects</div>
<div class="object-row">
<span class="object-dot" style="background:#f6c453"></span>
<span class="object-name">Drive housing · H-01</span>
<span class="object-state" id="housingState">TRAY</span>
</div>
<div class="object-row">
<span class="object-dot" style="background:#c084fc"></span>
<span class="object-name">Core module · C-02</span>
<span class="object-state" id="coreState">RETURN</span>
</div>
</section>
<section id="instructions" class="panel">
<div id="instructionTitle">Sequence running</div>
<div id="instructionText">
The robot will process the first unit, then inspect the next input slot.
</div>
<div class="button-row">
<button id="placeButton" disabled>Place missing core into tray</button>
<button id="viewButton">Reset camera</button>
<button id="restartButton">Restart run</button>
</div>
</section>
<div id="cameraHelp" class="panel">
Drag empty space to orbit · Scroll to zoom · Drag violet core to input
</div>
<div id="toast"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const stateBadge = document.querySelector("#stateBadge");
const gripperState = document.querySelector("#gripperState");
const phaseTitle = document.querySelector("#phaseTitle");
const phaseDetail = document.querySelector("#phaseDetail");
const instructionTitle = document.querySelector("#instructionTitle");
const instructionText = document.querySelector("#instructionText");
const placeButton = document.querySelector("#placeButton");
const viewButton = document.querySelector("#viewButton");
const restartButton = document.querySelector("#restartButton");
const housingState = document.querySelector("#housingState");
const coreState = document.querySelector("#coreState");
const toast = document.querySelector("#toast");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111c);
scene.fog = new THREE.Fog(0x07111c, 18, 34);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const canvas = renderer.domElement;
const ambient = new THREE.HemisphereLight(0xb9ddff, 0x17202b, 1.8);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55e6ff, 1.8);
rimLight.position.set(-7, 5, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb45f, 12, 8, 2);
warmLight.position.set(3.7, 2.2, 1.5);
scene.add(warmLight);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.12,
flatShading: true,
...options
});
}
const mat = {
floor: material(0x0a1420, { roughness: 1 }),
bench: material(0x263646, { metalness: 0.28 }),
benchEdge: material(0x476176, { metalness: 0.45 }),
dark: material(0x111b27, { metalness: 0.4 }),
tray: material(0x3f5364, { metalness: 0.36 }),
trayInside: material(0x152231),
robot: material(0xe6edf4, { metalness: 0.35 }),
robotDark: material(0x243447, { metalness: 0.55 }),
joint: material(0x66798d, { metalness: 0.68 }),
cyan: material(0x30d9f5, {
emissive: 0x0b7890,
emissiveIntensity: 0.65,
metalness: 0.28
}),
amber: material(0xffb84f, {
emissive: 0x9c4e00,
emissiveIntensity: 0.7
}),
green: material(0x56e39a, {
emissive: 0x0a7b4a,
emissiveIntensity: 0.8
}),
yellowPart: material(0xf0bd4d, { metalness: 0.28 }),
violetPart: material(0xa965e8, {
emissive: 0x37145f,
emissiveIntensity: 0.35,
metalness: 0.3
}),
whitePart: material(0xcbd5e1, { metalness: 0.45 }),
rubber: material(0x101722, { roughness: 0.95 })
};
function box(width, height, depth, matObject, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
matObject
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
radiusTop,
radiusBottom,
height,
segments,
matObject,
x,
y,
z,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
matObject
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, width = 1.6, height = 0.3, foreground = "#dbeafe") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const context = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
function setText(nextText) {
context.clearRect(0, 0, 512, 128);
context.fillStyle = "rgba(5, 12, 21, 0.9)";
context.fillRect(8, 12, 496, 104);
context.strokeStyle = "rgba(130, 170, 200, 0.42)";
context.lineWidth = 3;
context.strokeRect(8, 12, 496, 104);
context.fillStyle = foreground;
context.font = "800 34px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(nextText, 256, 65, 470);
texture.needsUpdate = true;
}
setText(text);
return { sprite, setText };
}
// Floor and bench.
box(38, 0.18, 38, mat.floor, 0, -1.28, 0);
const grid = new THREE.GridHelper(34, 34, 0x274156, 0x142536);
grid.position.y = -1.18;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
box(10.8, 0.32, 4.1, mat.bench, 0, -0.16, 0);
box(10.85, 0.08, 0.12, mat.benchEdge, 0, 0.015, 2.02);
box(10.85, 0.08, 0.12, mat.benchEdge, 0, 0.015, -2.02);
for (const x of [-4.8, 4.8]) {
for (const z of [-1.55, 1.55]) {
box(0.28, 1.1, 0.28, mat.robotDark, x, -0.71, z);
cylinder(0.2, 0.23, 0.12, 10, mat.rubber, x, -1.2, z);
}
}
// Input tray.
const slot1Position = new THREE.Vector3(-3.72, 0.15, -0.45);
const slot2Position = new THREE.Vector3(-2.55, 0.15, -0.45);
const returnPosition = new THREE.Vector3(-4.62, 0.14, 1.12);
box(2.45, 0.14, 1.35, mat.tray, -3.14, 0.07, -0.45);
box(2.15, 0.035, 1.05, mat.trayInside, -3.14, 0.152, -0.45);
box(2.45, 0.25, 0.08, mat.tray, -3.14, 0.24, -1.075);
box(2.45, 0.25, 0.08, mat.tray, -3.14, 0.24, 0.175);
box(0.08, 0.25, 1.25, mat.tray, -4.325, 0.24, -0.45);
box(0.08, 0.25, 1.25, mat.tray, -1.955, 0.24, -0.45);
const slot1Material = material(0x225268, {
emissive: 0x0b596b,
emissiveIntensity: 0.7
});
const slot2Material = material(0x253241, {
emissive: 0x000000,
emissiveIntensity: 0
});
box(0.88, 0.035, 0.88, slot1Material, slot1Position.x, 0.177, slot1Position.z);
box(0.88, 0.035, 0.88, slot2Material, slot2Position.x, 0.177, slot2Position.z);
// Parts return dock.
box(1.35, 0.11, 0.86, mat.dark, returnPosition.x, 0.055, returnPosition.z);
const returnRingMaterial = new THREE.MeshBasicMaterial({
color: 0xc084fc,
transparent: true,
opacity: 0.68
});
const returnRing = new THREE.Mesh(
new THREE.TorusGeometry(0.53, 0.025, 6, 32),
returnRingMaterial
);
returnRing.rotation.x = Math.PI / 2;
returnRing.position.set(returnPosition.x, 0.13, returnPosition.z);
scene.add(returnRing);
// Assembly fixture.
const fixturePosition = new THREE.Vector3(0.92, 0.15, -0.45);
box(1.35, 0.14, 1.12, mat.dark, fixturePosition.x, 0.07, fixturePosition.z);
box(0.85, 0.035, 0.78, mat.trayInside, fixturePosition.x, 0.157, fixturePosition.z);
for (const dx of [-0.49, 0.49]) {
for (const dz of [-0.38, 0.38]) {
box(0.16, 0.29, 0.16, mat.joint, fixturePosition.x + dx, 0.29, fixturePosition.z + dz);
}
}
const fixtureLampMaterial = material(0x193b31, {
emissive: 0x000000,
emissiveIntensity: 0
});
const fixtureLamp = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 10, 6),
fixtureLampMaterial
);
fixtureLamp.position.set(fixturePosition.x, 0.37, fixturePosition.z + 0.48);
scene.add(fixtureLamp);
// Output shelf.
const outputShelfY = 0.55;
box(1.95, 0.12, 1.15, mat.tray, 3.37, outputShelfY, -0.3);
box(0.12, 1.45, 0.12, mat.robotDark, 2.48, 0.64, -0.76);
box(0.12, 1.45, 0.12, mat.robotDark, 4.26, 0.64, -0.76);
box(1.9, 0.08, 0.08, mat.benchEdge, 3.37, 0.15, -0.76);
const housingOutputPosition = new THREE.Vector3(3.02, 0.61, -0.3);
const coreOutputPosition = new THREE.Vector3(3.72, 0.61, -0.3);
// Low-poly robot.
const robotRoot = new THREE.Group();
robotRoot.position.set(0, 0, 0.45);
scene.add(robotRoot);
cylinder(0.58, 0.65, 0.3, 12, mat.robotDark, 0, 0.15, 0, robotRoot);
cylinder(0.48, 0.56, 0.08, 12, mat.joint, 0, 0.32, 0, robotRoot);
const turret = new THREE.Group();
turret.position.y = 0.36;
robotRoot.add(turret);
cylinder(0.34, 0.4, 0.32, 10, mat.robot, 0, 0.13, 0, turret);
const upperLength = 2.0;
const lowerLength = 1.9;
const shoulder = new THREE.Group();
shoulder.position.y = 0.34;
turret.add(shoulder);
const shoulderJoint = cylinder(
0.28,
0.28,
0.42,
10,
mat.joint,
0,
0,
0,
shoulder
);
shoulderJoint.rotation.x = Math.PI / 2;
box(0.3, upperLength, 0.36, mat.robot, 0, upperLength / 2, 0, shoulder);
box(0.08, upperLength * 0.72, 0.385, mat.cyan, 0.09, upperLength / 2, 0, shoulder);
const elbow = new THREE.Group();
elbow.position.y = upperLength;
shoulder.add(elbow);
const elbowJoint = cylinder(
0.25,
0.25,
0.4,
10,
mat.joint,
0,
0,
0,
elbow
);
elbowJoint.rotation.x = Math.PI / 2;
box(0.27, lowerLength, 0.3, mat.robot, 0, lowerLength / 2, 0, elbow);
box(0.075, lowerLength * 0.65, 0.315, mat.robotDark, -0.08, lowerLength / 2, 0, elbow);
const wrist = new THREE.Group();
wrist.position.y = lowerLength;
elbow.add(wrist);
const wristJoint = cylinder(
0.2,
0.2,
0.32,
10,
mat.joint,
0,
0,
0,
wrist
);
wristJoint.rotation.x = Math.PI / 2;
box(0.48, 0.15, 0.34, mat.robotDark, 0, -0.075, 0, wrist);
const leftFinger = box(0.075, 0.42, 0.12, mat.joint, -0.31, -0.33, 0, wrist);
const rightFinger = box(0.075, 0.42, 0.12, mat.joint, 0.31, -0.33, 0, wrist);
box(0.095, 0.09, 0.14, mat.rubber, -0.31, -0.53, 0, wrist);
box(0.095, 0.09, 0.14, mat.rubber, 0.31, -0.53, 0, wrist);
const carryAnchor = new THREE.Object3D();
carryAnchor.position.y = -0.42;
wrist.add(carryAnchor);
const beaconMaterial = material(0x55e6ff, {
emissive: 0x1d9bb5,
emissiveIntensity: 1.3
});
const robotBeacon = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 10, 6),
beaconMaterial
);
robotBeacon.position.set(0.36, 0.5, 0);
robotRoot.add(robotBeacon);
// Direct two-link IK; no controls or addons are imported.
const shoulderWorldY = 0.7;
const toolTarget = new THREE.Vector3();
function setRobotTarget(target) {
const dx = target.x - robotRoot.position.x;
const dz = target.z - robotRoot.position.z;
const radial = Math.hypot(dx, dz);
const vertical = target.y - shoulderWorldY;
turret.rotation.y = Math.atan2(-dz, dx);
const distance = THREE.MathUtils.clamp(
Math.hypot(radial, vertical),
Math.abs(upperLength - lowerLength) + 0.01,
upperLength + lowerLength - 0.01
);
const baseAngle = Math.atan2(radial, vertical);
const elbowAngle = Math.acos(
THREE.MathUtils.clamp(
(distance * distance - upperLength * upperLength - lowerLength * lowerLength) /
(2 * upperLength * lowerLength),
-1,
1
)
);
const shoulderAngle =
baseAngle -
Math.atan2(
lowerLength * Math.sin(elbowAngle),
upperLength + lowerLength * Math.cos(elbowAngle)
);
shoulder.rotation.z = -shoulderAngle;
elbow.rotation.z = -elbowAngle;
wrist.rotation.z = shoulderAngle + elbowAngle;
}
// Labels.
const inputLabel = makeLabel("INPUT TRAY", 1.55, 0.3);
inputLabel.sprite.position.set(-3.2, 1.35, -1.05);
scene.add(inputLabel.sprite);
const returnLabel = makeLabel("PARTS RETURN", 1.55, 0.3, "#e9d5ff");
returnLabel.sprite.position.set(-4.58, 1.12, 1.35);
scene.add(returnLabel.sprite);
const fixtureLabel = makeLabel("ASSEMBLY FIXTURE", 1.75, 0.3);
fixtureLabel.sprite.position.set(0.9, 1.35, -1.04);
scene.add(fixtureLabel.sprite);
const outputLabel = makeLabel("OUTPUT SHELF", 1.65, 0.3, "#baffd8");
outputLabel.sprite.position.set(3.36, 1.75, -0.9);
scene.add(outputLabel.sprite);
const robotLabel = makeLabel("ROBOT ACTIVE", 2.2, 0.34);
robotLabel.sprite.position.set(0, 3.02, 0.42);
scene.add(robotLabel.sprite);
// Real scene objects. These exact meshes are moved throughout the run.
function createHousing() {
const group = new THREE.Group();
box(0.48, 0.39, 0.46, mat.yellowPart, 0, 0.22, 0, group);
box(0.34, 0.09, 0.34, mat.whitePart, 0, 0.46, 0, group);
box(0.52, 0.07, 0.16, mat.robotDark, 0, 0.27, 0, group);
for (const side of [-1, 1]) {
box(0.06, 0.25, 0.32, mat.robotDark, side * 0.25, 0.19, 0, group);
}
const ledMaterial = material(0x6dffac, {
emissive: 0x20a563,
emissiveIntensity: 1.4
});
box(0.08, 0.08, 0.025, ledMaterial, 0.13, 0.35, 0.242, group);
scene.add(group);
return group;
}
function createCore() {
const group = new THREE.Group();
cylinder(0.22, 0.25, 0.4, 8, mat.violetPart, 0, 0.21, 0, group);
cylinder(0.14, 0.18, 0.09, 8, mat.whitePart, 0, 0.45, 0, group);
box(0.12, 0.1, 0.1, mat.cyan, 0, 0.54, 0, group);
for (let i = 0; i < 4; i += 1) {
const angle = (i / 4) * Math.PI * 2;
box(
0.07,
0.16,
0.07,
mat.robotDark,
Math.cos(angle) * 0.23,
0.1,
Math.sin(angle) * 0.23,
group
);
}
scene.add(group);
return group;
}
const housingPart = {
id: "H-01",
name: "Drive housing",
mesh: createHousing(),
state: "tray"
};
const corePart = {
id: "C-02",
name: "Core module",
mesh: createCore(),
state: "return"
};
housingPart.mesh.position.copy(slot1Position);
corePart.mesh.position.copy(returnPosition);
// Runtime state.
let processState = "active";
let currentStep = -1;
let waitingResolver = null;
let robotMotion = null;
let gripperMotion = null;
let partTween = null;
let gripperOpen = 1;
let toastTimer = null;
const steps = [
document.querySelector("#step0"),
document.querySelector("#step1"),
document.querySelector("#step2"),
document.querySelector("#step3"),
document.querySelector("#step4")
];
function showToast(message) {
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("visible"), 3400);
}
function setPhase(index, title, detail) {
currentStep = index;
phaseTitle.textContent = title;
phaseDetail.textContent = detail;
steps.forEach((element, stepIndex) => {
element.classList.toggle("done", stepIndex < index);
element.classList.toggle("current", stepIndex === index);
});
}
function partStateLabel(state) {
const labels = {
tray: "TRAY",
return: "RETURN",
held: "IN GRIPPER",
fixture: "FIXTURE",
shelf: "OUTPUT",
placing: "MOVING TO TRAY"
};
return labels[state] || state.toUpperCase();
}
function updateUI() {
housingState.textContent = partStateLabel(housingPart.state);
coreState.textContent = partStateLabel(corePart.state);
stateBadge.className = "";
if (processState === "waiting") {
stateBadge.textContent = "WAITING • INPUT MISSING";
stateBadge.classList.add("waiting");
robotLabel.setText("WAITING • SLOT 2 EMPTY");
instructionTitle.textContent = "Missing input detected";
instructionText.textContent =
"Drag the violet core module from PARTS RETURN into the glowing input slot 2, or use the button below. The gripper remains open.";
placeButton.disabled = false;
} else if (processState === "complete") {
stateBadge.textContent = "SEQUENCE COMPLETE";
stateBadge.classList.add("complete");
robotLabel.setText("SEQUENCE COMPLETE");
instructionTitle.textContent = "Recovery successful";
instructionText.textContent =
"The restored core was processed and output. Unit 01 remained on the output shelf throughout the interruption.";
placeButton.disabled = true;
} else if (processState === "fault") {
stateBadge.textContent = "CELL FAULT";
stateBadge.classList.add("fault");
robotLabel.setText("CELL FAULT");
instructionTitle.textContent = "Unexpected cell state";
instructionText.textContent = "Restart the demonstration to reset every tracked object.";
placeButton.disabled = true;
} else {
stateBadge.textContent = "ACTIVE MOTION";
robotLabel.setText("ROBOT ACTIVE");
instructionTitle.textContent = "Sequence running";
instructionText.textContent =
"The robot is tracking and transferring the actual physical part meshes.";
placeButton.disabled = true;
}
if (housingPart.state === "held" || corePart.state === "held") {
gripperState.textContent = "GRIPPER CLOSED · PART HELD";
} else if (processState === "waiting") {
gripperState.textContent = "GRIPPER OPEN · NOT CLOSED";
} else {
gripperState.textContent = "GRIPPER OPEN";
}
}
function delay(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function moveTool(target, duration = 1000) {
return new Promise((resolve) => {
robotMotion = {
from: toolTarget.clone(),
to: target.clone(),
start: performance.now(),
duration,
resolve
};
});
}
function setGripper(openness, duration = 320) {
return new Promise((resolve) => {
gripperMotion = {
from: gripperOpen,
to: openness,
start: performance.now(),
duration,
resolve
};
});
}
function exactToolPosition(basePosition) {
// The carry anchor and the part's local origin total 0.57 units.
return basePosition.clone().add(new THREE.Vector3(0, 0.57, 0));
}
function approachToolPosition(basePosition) {
return basePosition.clone().add(new THREE.Vector3(0, 1.25, 0));
}
async function pickPart(part, expectedState, pickupPosition) {
// Critical safety check: never close on an absent or incorrectly located part.
if (part.state !== expectedState || !part.mesh.visible) {
throw new Error(
`${part.id} is not in the expected ${expectedState} state; gripper was not closed.`
);
}
await moveTool(approachToolPosition(pickupPosition), 1050);
await moveTool(exactToolPosition(pickupPosition), 850);
// Re-check after reaching the pickup pose.
if (part.state !== expectedState) {
throw new Error(`${part.id} changed state before pickup.`);
}
// Attach the exact existing mesh to the tool. No clone or imaginary cargo.
carryAnchor.attach(part.mesh);
part.mesh.position.set(0, -0.15, 0);
part.mesh.quaternion.identity();
part.state = "held";
updateUI();
await setGripper(0, 300);
await moveTool(approachToolPosition(pickupPosition), 750);
}
async function placePart(part, destination, destinationState) {
await moveTool(approachToolPosition(destination), 1100);
await moveTool(exactToolPosition(destination), 800);
await setGripper(1, 280);
// Detach the same tracked object and place it at the real destination.
scene.attach(part.mesh);
part.mesh.position.copy(destination);
part.mesh.quaternion.identity();
part.state = destinationState;
updateUI();
await moveTool(approachToolPosition(destination), 650);
}
function beginPartPlacement(destination, finalState) {
if (processState !== "waiting" || partTween) return;
corePart.state = "placing";
updateUI();
partTween = {
from: corePart.mesh.position.clone(),
to: destination.clone(),
finalState,
start: performance.now(),
duration: 700
};
}
function completePartPlacement() {
const finalState = partTween.finalState;
corePart.mesh.position.copy(partTween.to);
corePart.state = finalState;
partTween = null;
if (finalState === "tray") {
processState = "active";
slot2Material.color.setHex(0x285748);
slot2Material.emissive.setHex(0x0b6b46);
slot2Material.emissiveIntensity = 0.8;
showToast("Core module detected in input slot 2. Sequence resuming.");
if (waitingResolver) {
const resolve = waitingResolver;
waitingResolver = null;
resolve();
}
} else {
showToast("The core was returned to PARTS RETURN.");
}
updateUI();
}
async function runSequence() {
try {
processState = "active";
updateUI();
// Unit 01: one real mesh moves through tray -> fixture -> output.
setPhase(
0,
"Picking drive housing from input slot 1",
"The arm approaches the occupied slot while the gripper remains open."
);
await pickPart(housingPart, "tray", slot1Position);
setPhase(
1,
"Placing drive housing in the assembly fixture",
"The same H-01 mesh is released onto the fixture. Its state is updated only after release."
);
await placePart(housingPart, fixturePosition, "fixture");
fixtureLampMaterial.color.setHex(0x56e39a);
fixtureLampMaterial.emissive.setHex(0x0a7b4a);
fixtureLampMaterial.emissiveIntensity = 1.4;
await delay(700);
setPhase(
2,
"Transferring completed housing to the output shelf",
"H-01 is picked back up from the fixture and moved to a permanent output position."
);
await pickPart(housingPart, "fixture", fixturePosition);
await placePart(housingPart, housingOutputPosition, "shelf");
showToast("Unit 01 completed and safely stored on the output shelf.");
// The next expected input position is intentionally empty.
setPhase(
3,
"Checking input slot 2",
"The robot moves above the next expected slot and checks its real occupancy."
);
await moveTool(approachToolPosition(slot2Position), 1100);
if (corePart.state !== "tray") {
processState = "waiting";
slot2Material.color.setHex(0x9a6427);
slot2Material.emissive.setHex(0x8f5100);
slot2Material.emissiveIntensity = 1.2;
updateUI();
showToast("Input slot 2 is empty. The gripper will not close.");
await new Promise((resolve) => {
waitingResolver = resolve;
});
if (corePart.state !== "tray") {
throw new Error("The restored core is not in input slot 2.");
}
}
processState = "active";
updateUI();
// Unit 02: the restored core follows the same tracked transfer route.
setPhase(
4,
"Picking restored core module from slot 2",
"Occupancy has been confirmed. The robot resumes from its waiting pose."
);
await pickPart(corePart, "tray", slot2Position);
setPhase(
4,
"Assembling restored core module",
"The core is transferred to the fixture using the same actual scene mesh."
);
await placePart(corePart, fixturePosition, "fixture");
fixtureLampMaterial.color.setHex(0x56e39a);
fixtureLampMaterial.emissive.setHex(0x0a7b4a);
fixtureLampMaterial.emissiveIntensity = 1.4;
await delay(700);
setPhase(
4,
"Transferring restored core to output",
"The final unit is moved to the second position on the output shelf."
);
await pickPart(corePart, "fixture", fixturePosition);
await placePart(corePart, coreOutputPosition, "shelf");
processState = "complete";
steps.forEach((element) => {
element.classList.remove("current");
element.classList.add("done");
});
updateUI();
showToast("Recovery complete. Both tracked units are on the output shelf.");
} catch (error) {
console.error(error);
processState = "fault";
updateUI();
showToast(error.message || "The cell encountered an unexpected state.");
}
}
// Custom camera controls.
const cameraTarget = new THREE.Vector3(0, 0.68, 0);
let cameraAzimuth = 0.68;
let cameraPolar = 0.92;
let cameraRadius = 13.4;
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraAzimuth = 0.68;
cameraPolar = 0.92;
cameraRadius = 13.4;
updateCamera();
}
viewButton.addEventListener("click", resetCamera);
restartButton.addEventListener("click", () => location.reload());
placeButton.addEventListener("click", () => {
if (processState === "waiting") {
beginPartPlacement(slot2Position, "tray");
}
});
// Dragging the missing part.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.15);
const dragIntersection = new THREE.Vector3();
let draggingPart = false;
let orbiting = false;
let pointerId = null;
let previousPointerX = 0;
let previousPointerY = 0;
function updatePointer(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);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
updatePointer(event);
pointerId = event.pointerId;
canvas.setPointerCapture(pointerId);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
const coreHit =
processState === "waiting" &&
corePart.state === "return" &&
raycaster.intersectObject(corePart.mesh, true).length > 0;
if (coreHit) {
draggingPart = true;
orbiting = false;
canvas.style.cursor = "grabbing";
} else {
draggingPart = false;
orbiting = true;
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", (event) => {
if (event.pointerId !== pointerId) return;
updatePointer(event);
if (draggingPart) {
if (raycaster.ray.intersectPlane(dragPlane, dragIntersection)) {
corePart.mesh.position.set(
THREE.MathUtils.clamp(dragIntersection.x, -4.95, 4.95),
0.15,
THREE.MathUtils.clamp(dragIntersection.z, -1.45, 1.6)
);
corePart.mesh.rotation.y = 0;
}
return;
}
if (orbiting) {
cameraAzimuth -= (event.clientX - previousPointerX) * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - (event.clientY - previousPointerY) * 0.005,
0.48,
1.22
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
}
});
function finishPointer(event) {
if (event.pointerId !== pointerId) return;
updatePointer(event);
if (draggingPart) {
const dx = corePart.mesh.position.x - slot2Position.x;
const dz = corePart.mesh.position.z - slot2Position.z;
if (Math.hypot(dx, dz) <= 0.68) {
beginPartPlacement(slot2Position, "tray");
} else {
beginPartPlacement(returnPosition, "return");
}
}
draggingPart = false;
orbiting = false;
pointerId = null;
canvas.style.cursor = "default";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", finishPointer);
canvas.addEventListener("pointercancel", finishPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
8.5,
17
);
},
{ passive: false }
);
// Animation loop.
function animate(now) {
requestAnimationFrame(animate);
if (robotMotion) {
const motion = robotMotion;
const t = THREE.MathUtils.clamp(
(now - motion.start) / motion.duration,
0,
1
);
toolTarget.lerpVectors(motion.from, motion.to, easeInOutCubic(t));
setRobotTarget(toolTarget);
if (t >= 1) {
robotMotion = null;
motion.resolve();
}
} else {
setRobotTarget(toolTarget);
}
if (gripperMotion) {
const motion = gripperMotion;
const t = THREE.MathUtils.clamp(
(now - motion.start) / motion.duration,
0,
1
);
gripperOpen = THREE.MathUtils.lerp(
motion.from,
motion.to,
easeInOutCubic(t)
);
if (t >= 1) {
gripperMotion = null;
motion.resolve();
}
}
leftFinger.position.x = -(0.19 + 0.12 * gripperOpen);
rightFinger.position.x = 0.19 + 0.12 * gripperOpen;
if (partTween) {
const t = THREE.MathUtils.clamp(
(now - partTween.start) / partTween.duration,
0,
1
);
corePart.mesh.position.lerpVectors(
partTween.from,
partTween.to,
easeInOutCubic(t)
);
if (t >= 1) {
const finishedTween = partTween;
partTween = null;
corePart.mesh.position.copy(finishedTween.to);
corePart.state = finishedTween.finalState;
if (finishedTween.finalState === "tray") {
processState = "active";
slot2Material.color.setHex(0x285748);
slot2Material.emissive.setHex(0x0b6b46);
slot2Material.emissiveIntensity = 0.8;
showToast("Core module detected in input slot 2. Sequence resuming.");
if (waitingResolver) {
const resolve = waitingResolver;
waitingResolver = null;
resolve();
}
} else {
showToast("The core was returned to PARTS RETURN.");
}
updateUI();
}
}
const pulse = 0.5 + 0.5 * Math.sin(now * 0.006);
returnRing.scale.setScalar(1 + pulse * 0.055);
returnRingMaterial.opacity = 0.45 + pulse * 0.28;
if (processState === "waiting") {
beaconMaterial.color.setHex(0xffb84f);
beaconMaterial.emissive.setHex(0xb35b00);
beaconMaterial.emissiveIntensity = 1.2 + pulse * 1.4;
} else if (processState === "complete") {
beaconMaterial.color.setHex(0x56e39a);
beaconMaterial.emissive.setHex(0x0a7b4a);
beaconMaterial.emissiveIntensity = 1.8;
} else {
beaconMaterial.color.setHex(0x55e6ff);
beaconMaterial.emissive.setHex(0x1d9bb5);
beaconMaterial.emissiveIntensity = 1.1 + pulse * 0.5;
}
updateCamera();
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
resetCamera();
toolTarget.copy(approachToolPosition(slot1Position));
setRobotTarget(toolTarget);
updateUI();
requestAnimationFrame(animate);
// The short sequence begins automatically.
setTimeout(runSequence, 650);
</script>
</body>
</html>
```