Prompt specification
Build or customize the Three.js 3D scene "Table Setting From Composed Instructions" 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: Table Setting From Composed Instructions
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/table-setting-from-composed-instructions-x3387
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring ordered commands and composed instructions. Build a dining table where a robot follows short placement instructions with spatial references. Validate each step and stop when a needed reference object is absent. Voxel style.
Working Three.js Starter (table-setting-from-composed-instructions-x3387):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel TableTask</title>
<style>
:root {
--ink: #17202a;
--muted: #687582;
--panel: rgba(250, 252, 251, 0.94);
--line: #d9e0df;
--accent: #1f8f83;
--accent-dark: #11665f;
--danger: #c24f43;
--shadow: 0 18px 45px rgba(31, 48, 55, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: #dce5e4;
}
button, textarea, input { font: inherit; }
#app {
width: 100%;
height: 100%;
display: grid;
grid-template-rows: 64px 1fr;
}
header {
z-index: 5;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 22px;
color: #f7fffd;
background: #173b3b;
border-bottom: 4px solid #e7b958;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 34px;
height: 34px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 3px;
transform: rotate(-5deg);
}
.brand-mark i {
display: block;
background: #f5c65c;
border-radius: 2px;
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(6),
.brand-mark i:nth-child(8) {
background: #64d3c5;
}
.brand-mark i:nth-child(5) {
background: #f27b63;
}
h1 {
margin: 0;
font-size: 18px;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.subtitle {
color: #b8d0cd;
font-size: 12px;
letter-spacing: 0.04em;
}
.header-actions {
display: flex;
gap: 9px;
}
.header-actions button {
border: 1px solid rgba(255,255,255,0.24);
color: white;
background: rgba(255,255,255,0.08);
padding: 8px 12px;
border-radius: 7px;
cursor: pointer;
}
.header-actions button:hover {
background: rgba(255,255,255,0.16);
}
main {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dce5e4;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.scene-title {
position: absolute;
top: 18px;
left: 18px;
pointer-events: none;
padding: 10px 13px;
color: #173b3b;
background: rgba(255,255,255,0.8);
border: 1px solid rgba(255,255,255,0.85);
border-radius: 9px;
box-shadow: 0 8px 25px rgba(33,55,60,0.1);
backdrop-filter: blur(8px);
}
.scene-title strong {
display: block;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-title span {
display: block;
margin-top: 3px;
font-size: 11px;
color: #637674;
}
.camera-help {
position: absolute;
bottom: 16px;
left: 18px;
padding: 8px 11px;
border-radius: 8px;
color: #526562;
background: rgba(255,255,255,0.78);
font-size: 11px;
pointer-events: none;
}
aside {
min-height: 0;
overflow-y: auto;
padding: 18px;
background: var(--panel);
border-left: 1px solid #cad5d3;
box-shadow: -12px 0 35px rgba(32, 56, 60, 0.08);
}
.section + .section {
margin-top: 19px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#status {
max-width: 190px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 5px 8px;
border-radius: 999px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
color: #16675f;
background: #d9f2ed;
}
#status.working {
color: #805b0f;
background: #fff0c8;
}
#status.error {
color: #9a332a;
background: #fde2de;
}
label {
display: block;
margin-bottom: 7px;
font-size: 11px;
font-weight: 750;
color: #50605f;
}
textarea {
width: 100%;
min-height: 88px;
resize: vertical;
padding: 11px;
border: 1px solid #bdcac8;
border-radius: 9px;
outline: none;
color: #233130;
background: #fff;
line-height: 1.45;
font-size: 13px;
}
textarea:focus {
border-color: var(--accent);
box-shadow: 0 0 0 3px rgba(31,143,131,0.12);
}
.run-row {
display: flex;
gap: 8px;
margin-top: 9px;
}
.primary, .secondary {
border: 0;
border-radius: 8px;
padding: 10px 13px;
cursor: pointer;
font-weight: 800;
font-size: 12px;
}
.primary {
flex: 1;
color: white;
background: var(--accent);
}
.primary:hover { background: var(--accent-dark); }
.secondary {
color: #3f504e;
background: #e4ebe9;
}
.secondary:hover { background: #d7e1df; }
button:disabled {
opacity: 0.48;
cursor: not-allowed;
}
.examples {
display: grid;
gap: 7px;
}
.example {
width: 100%;
padding: 9px 10px;
text-align: left;
border: 1px solid #d7dfde;
border-radius: 8px;
color: #40514f;
background: #f7faf9;
cursor: pointer;
font-size: 11px;
line-height: 1.35;
}
.example:hover {
border-color: #8ebcb6;
background: #edf8f5;
}
.syntax {
margin: 0;
padding: 10px 11px;
color: #536260;
background: #edf2f1;
border-radius: 8px;
font: 11px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.inventory {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.chip {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 8px;
border: 1px solid #d1dcda;
border-radius: 999px;
color: #40524f;
background: #f6f9f8;
font-size: 11px;
font-weight: 700;
}
.chip::before {
content: "";
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--accent);
}
.chip.absent {
color: #9b5c55;
border-color: #e6c5c0;
background: #fff4f2;
}
.chip.absent::before { background: var(--danger); }
#log {
list-style: none;
margin: 0;
padding: 0;
display: grid;
gap: 7px;
max-height: 235px;
overflow-y: auto;
}
#log li {
padding: 8px 9px;
border-left: 3px solid #a4b7b4;
border-radius: 4px;
color: #50605e;
background: #f1f5f4;
font-size: 11px;
line-height: 1.4;
}
#log li.ok {
border-left-color: var(--accent);
background: #edf8f5;
}
#log li.warn {
border-left-color: #d39b25;
background: #fff8e7;
}
#log li.error {
border-left-color: var(--danger);
color: #88372f;
background: #fff0ed;
}
.note {
margin: 9px 0 0;
color: #788582;
font-size: 10px;
line-height: 1.45;
}
@media (max-width: 760px) {
html, body { overflow: auto; }
#app {
height: auto;
min-height: 100%;
grid-template-rows: 58px auto;
}
header {
position: sticky;
top: 0;
padding: 0 13px;
}
.subtitle { display: none; }
main {
grid-template-columns: 1fr;
grid-template-rows: 58vh auto;
}
aside {
overflow: visible;
border-left: 0;
border-top: 1px solid #cad5d3;
}
.scene-title { top: 10px; left: 10px; }
.camera-help { bottom: 9px; left: 9px; }
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</div>
<div>
<h1>TableTask</h1>
<div class="subtitle">Voxel robot placement laboratory</div>
</div>
</div>
<div class="header-actions">
<button id="cameraReset" type="button">Reset camera</button>
<button id="resetScene" type="button">Reset scene</button>
</div>
</header>
<main>
<section id="stage" aria-label="Interactive 3D voxel dining table">
<canvas id="scene"></canvas>
<div class="scene-title">
<strong>Dining table / command lane</strong>
<span>Every clause is checked before the robot moves.</span>
</div>
<div class="camera-help">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
</section>
<aside>
<section class="section">
<div class="section-head">
<h2>Instruction queue</h2>
<div id="status" role="status">Ready</div>
</div>
<label for="command">Command or composed instruction</label>
<textarea id="command" spellcheck="false">place apple on plate then place cup left of apple</textarea>
<div class="run-row">
<button id="run" class="primary" type="button">Run instructions</button>
<button id="clear" class="secondary" type="button">Clear</button>
</div>
<p class="note">Use Ctrl/⌘ + Enter to run. Later clauses are not executed after validation fails.</p>
</section>
<section class="section">
<div class="section-head">
<h2>Command examples</h2>
</div>
<div class="examples">
<button class="example" data-command="place apple on plate then place cup left of apple">
Composed order: apple on plate, then cup left of apple
</button>
<button class="example" data-command="place book in front of cup and place box behind book">
Alternate composition: use “and”
</button>
<button class="example" data-command="place cup left of fork">
Missing reference: fork is absent, so the robot stops
</button>
<button class="example" data-command="remove cup then place book left of cup">
Removal followed by a now-missing reference
</button>
</div>
</section>
<section class="section">
<div class="section-head">
<h2>Language</h2>
</div>
<pre class="syntax">place apple on plate
put cup left of apple
move book in front of cup
remove cup
Compose with: then | and | ;
Relations: left of, right of, in front of,
behind, on, above, near</pre>
</section>
<section class="section">
<div class="section-head">
<h2>Inventory</h2>
</div>
<div id="inventory" class="inventory"></div>
</section>
<section class="section">
<div class="section-head">
<h2>Validation log</h2>
</div>
<ol id="log" aria-live="polite"></ol>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, camera, lighting, and direct camera controls
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0xdce5e4);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce5e4);
scene.fog = new THREE.Fog(0xdce5e4, 18, 34);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 80);
const cameraState = {
target: new THREE.Vector3(0, 1.2, 0),
radius: 16.5,
theta: 0.72,
phi: 0.91
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.set(0, 1.2, 0);
cameraState.radius = 16.5;
cameraState.theta = 0.72;
cameraState.phi = 0.91;
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.32,
1.35
);
updateCamera();
});
const endDrag = () => { dragging = false; };
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
9,
27
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#cameraReset").addEventListener("click", resetCamera);
new ResizeObserver(() => {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}).observe(stage);
scene.add(new THREE.HemisphereLight(0xf8fff8, 0x71827c, 2.15));
const sun = new THREE.DirectionalLight(0xfff2cf, 3.2);
sun.position.set(-8, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbce7ea, 1.1);
fill.position.set(8, 7, -7);
scene.add(fill);
// ---------------------------------------------------------------------
// Voxel construction helpers
// ---------------------------------------------------------------------
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function addBox(parent, size, position, color) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createLabel(text, color = "#203c3a") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 64;
const ctx = labelCanvas.getContext("2d");
ctx.fillStyle = "rgba(255,255,255,0.91)";
ctx.fillRect(0, 0, 256, 64);
ctx.fillStyle = color;
ctx.font = "bold 29px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text.toUpperCase(), 128, 33);
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(1.12, 0.28, 1);
sprite.renderOrder = 20;
return sprite;
}
// Room and dining table.
addBox(scene, [30, 0.3, 28], [0, -0.15, 0], "#b9c7c1");
addBox(scene, [24, 8, 0.35], [0, 3.8, -7], "#d4dfda");
addBox(scene, [18, 0.035, 13], [0, 0.018, 0], "#a9beb5");
const table = new THREE.Group();
scene.add(table);
addBox(table, [11, 0.4, 7], [0, 0.95, 0], "#b97842");
addBox(table, [10.7, 0.08, 6.7], [0, 1.115, 0], "#d99a5d");
for (const x of [-4.6, 4.6]) {
for (const z of [-2.65, 2.65]) {
addBox(table, [0.45, 0.9, 0.45], [x, 0.45, z], "#6d4b36");
}
}
const SURFACE_Y = 1.17;
const ITEM_Y = 1.18;
// Table grid helps make left/right/front/behind references legible.
const gridMaterial = material("#c78b55");
for (let x = -5; x <= 5; x += 1) {
addBox(table, [0.018, 0.012, 6.55], [x, SURFACE_Y + 0.008, 0], "#c78b55");
}
for (let z = -3; z <= 3; z += 1) {
addBox(table, [10.55, 0.012, 0.018], [0, SURFACE_Y + 0.009, z], "#c78b55");
}
// Blocky placemats.
addBox(table, [3.0, 0.02, 2.25], [-2.45, SURFACE_Y + 0.01, -0.15], "#e8d7ae");
addBox(table, [3.0, 0.02, 2.25], [2.45, SURFACE_Y + 0.01, 0.25], "#d7e0c0");
// Voxel dining chairs.
function addChair(x, z, rotation) {
const chair = new THREE.Group();
chair.position.set(x, 0, z);
chair.rotation.y = rotation;
scene.add(chair);
addBox(chair, [1.45, 0.22, 1.35], [0, 1.03, 0], "#5e7772");
addBox(chair, [1.45, 1.35, 0.2], [0, 1.72, -0.58], "#6f8983");
for (const xx of [-0.53, 0.53]) {
for (const zz of [-0.45, 0.45]) {
addBox(chair, [0.18, 1.0, 0.18], [xx, 0.5, zz], "#465e59");
}
}
}
addChair(-3.4, -4.6, 0);
addChair(3.4, -4.6, 0);
addChair(-5.9, 0.2, Math.PI / 2);
addChair(5.9, 0.2, -Math.PI / 2);
// ---------------------------------------------------------------------
// Items and robot
// ---------------------------------------------------------------------
const itemDefinitions = {
plate: {
label: "plate",
aliases: ["plate", "dish"],
width: 1.48,
depth: 1.48,
height: 0.15,
color: "#eee8d8",
initial: new THREE.Vector3(-2.45, ITEM_Y, -0.2),
present: true
},
cup: {
label: "cup",
aliases: ["cup", "mug", "coffee cup"],
width: 0.78,
depth: 0.58,
height: 0.88,
color: "#418fc2",
initial: new THREE.Vector3(0.25, ITEM_Y, 0),
present: true
},
apple: {
label: "apple",
aliases: ["apple"],
width: 0.68,
depth: 0.68,
height: 0.86,
color: "#d95747",
initial: new THREE.Vector3(2.45, ITEM_Y, -0.25),
present: true
},
book: {
label: "book",
aliases: ["book"],
width: 0.92,
depth: 1.25,
height: 0.27,
color: "#6878bd",
initial: new THREE.Vector3(-3.7, ITEM_Y, 1.55),
present: true
},
box: {
label: "box",
aliases: ["box", "crate"],
width: 0.82,
depth: 0.82,
height: 0.78,
color: "#e0a83d",
initial: new THREE.Vector3(3.55, ITEM_Y, 1.35),
present: true
},
fork: {
label: "fork",
aliases: ["fork"],
width: 0.48,
depth: 0.85,
height: 0.12,
color: "#aebcba",
initial: new THREE.Vector3(4.1, ITEM_Y, -1.75),
present: false
}
};
const items = new Map();
function buildItem(id, definition) {
const group = new THREE.Group();
if (id === "plate") {
addBox(group, [1.48, 0.13, 1.48], [0, 0.065, 0], "#f1ead9");
addBox(group, [1.16, 0.035, 1.16], [0, 0.145, 0], "#d9ddcf");
}
if (id === "cup") {
addBox(group, [0.54, 0.8, 0.54], [-0.08, 0.4, 0], "#3c8fc4");
addBox(group, [0.45, 0.035, 0.43], [-0.08, 0.82, 0], "#244f70");
addBox(group, [0.12, 0.48, 0.13], [0.29, 0.48, 0], "#3c8fc4");
addBox(group, [0.23, 0.12, 0.13], [0.24, 0.69, 0], "#3c8fc4");
addBox(group, [0.23, 0.12, 0.13], [0.24, 0.27, 0], "#3c8fc4");
}
if (id === "apple") {
addBox(group, [0.66, 0.52, 0.66], [0, 0.26, 0], "#d85243");
addBox(group, [0.46, 0.22, 0.46], [0, 0.61, 0], "#e06451");
addBox(group, [0.11, 0.15, 0.11], [0.08, 0.79, 0], "#65523a");
addBox(group, [0.2, 0.07, 0.1], [0.16, 0.84, 0], "#719b53");
}
if (id === "book") {
addBox(group, [0.94, 0.22, 1.27], [0, 0.11, 0], "#6674b5");
addBox(group, [0.82, 0.15, 1.15], [0, 0.12, 0], "#e9e3cd");
addBox(group, [0.96, 0.055, 1.29], [0, 0.247, 0], "#475aa1");
}
if (id === "box") {
addBox(group, [0.82, 0.78, 0.82], [0, 0.39, 0], "#dba23c");
addBox(group, [0.86, 0.13, 0.86], [0, 0.38, 0], "#efd078");
addBox(group, [0.14, 0.82, 0.87], [0, 0.39, 0], "#f1c55f");
}
if (id === "fork") {
addBox(group, [0.1, 0.08, 0.72], [0, 0.04, 0.02], "#b8c4c2");
addBox(group, [0.48, 0.09, 0.16], [0, 0.04, -0.35], "#c7d0ce");
for (let i = 0; i < 3; i++) {
addBox(group, [0.08, 0.07, 0.22], [-0.15 + i * 0.15, 0.04, -0.5], "#c7d0ce");
}
}
const label = createLabel(definition.label);
label.position.set(0, definition.height + 0.38, 0);
group.add(label);
group.position.copy(definition.initial);
group.visible = definition.present;
scene.add(group);
const item = {
id,
definition,
group,
present: definition.present,
initial: definition.initial.clone()
};
items.set(id, item);
}
for (const [id, definition] of Object.entries(itemDefinitions)) {
buildItem(id, definition);
}
// Robot.
const robot = new THREE.Group();
robot.position.set(0, ITEM_Y, 2.25);
scene.add(robot);
addBox(robot, [0.34, 0.2, 0.52], [-0.25, 0.1, 0], "#314d4b");
addBox(robot, [0.34, 0.2, 0.52], [0.25, 0.1, 0], "#314d4b");
addBox(robot, [0.2, 0.42, 0.24], [-0.25, 0.39, 0], "#54716d");
addBox(robot, [0.2, 0.42, 0.24], [0.25, 0.39, 0], "#54716d");
addBox(robot, [0.86, 0.68, 0.64], [0, 0.91, 0], "#f1f3ea");
addBox(robot, [0.72, 0.16, 0.67], [0, 0.75, 0], "#2d8f84");
addBox(robot, [0.92, 0.58, 0.72], [0, 1.51, 0], "#e8ece3");
addBox(robot, [0.25, 0.24, 0.08], [-0.23, 1.53, 0.39], "#263a3d");
addBox(robot, [0.25, 0.24, 0.08], [0.23, 1.53, 0.39], "#263a3d");
addBox(robot, [0.07, 0.07, 0.03], [-0.22, 1.56, 0.44], "#9be8df");
addBox(robot, [0.07, 0.07, 0.03], [0.24, 1.56, 0.44], "#9be8df");
addBox(robot, [0.09, 0.3, 0.09], [0, 1.94, 0], "#59726e");
addBox(robot, [0.16, 0.16, 0.16], [0, 2.13, 0], "#e7b957");
const robotLabel = createLabel("robot", "#1f6e67");
robotLabel.position.set(0, 2.52, 0);
robot.add(robotLabel);
const gripper = new THREE.Group();
gripper.position.set(0, 1.18, 0.68);
robot.add(gripper);
addBox(gripper, [0.28, 0.12, 0.22], [0, 0, 0], "#d1a44a");
// ---------------------------------------------------------------------
// Instruction parsing and validation
// ---------------------------------------------------------------------
const commandInput = document.querySelector("#command");
const runButton = document.querySelector("#run");
const clearButton = document.querySelector("#clear");
const resetButton = document.querySelector("#resetScene");
const statusElement = document.querySelector("#status");
const logElement = document.querySelector("#log");
const inventoryElement = document.querySelector("#inventory");
let running = false;
function splitInstruction(text) {
return text
.replace(/\r/g, "")
.split(/\s*(?:;|\bthen\b|\band\b|,\s*(?=(?:place|put|move|remove)\b))\s*/i)
.map(part => part.trim())
.filter(Boolean);
}
function cleanName(name) {
return name
.trim()
.toLowerCase()
.replace(/^the\s+/, "")
.replace(/[.!?]+$/, "")
.trim();
}
function resolveItem(name) {
const cleaned = cleanName(name);
for (const item of items.values()) {
if (item.definition.aliases.includes(cleaned)) return item;
}
return null;
}
function parseClause(raw) {
let text = raw
.trim()
.toLowerCase()
.replace(/[.!?]+$/, "")
.replace(/\bon top of\b/g, "on")
.replace(/\bto the left of\b/g, "left of")
.replace(/\bto the right of\b/g, "right of");
const removeMatch = text.match(/^(?:remove|delete)\s+(.+)$/);
if (removeMatch) {
return { type: "remove", sourceName: removeMatch[1].trim() };
}
const placementMatch = text.match(
/^(?:place|put|move)\s+(.+?)\s+(left of|right of|in front of|behind|above|near|on)\s+(.+)$/
);
if (!placementMatch) {
return {
error: `Invalid syntax: “${raw}”. Expected “place <object> <relation> <reference>”.`
};
}
return {
type: "place",
sourceName: placementMatch[1].trim(),
relation: placementMatch[2].trim(),
referenceName: placementMatch[3].trim()
};
}
function computeTarget(source, reference, relation) {
const p = reference.group.position;
const s = source.definition;
const r = reference.definition;
const target = new THREE.Vector3(p.x, ITEM_Y, p.z);
if (relation === "left of") {
target.x = p.x - (s.width + r.width) / 2 - 0.3;
} else if (relation === "right of") {
target.x = p.x + (s.width + r.width) / 2 + 0.3;
} else if (relation === "in front of") {
target.z = p.z + (s.depth + r.depth) / 2 + 0.3;
} else if (relation === "behind") {
target.z = p.z - (s.depth + r.depth) / 2 - 0.3;
} else if (relation === "near") {
target.x = p.x + (s.width + r.width) / 2 + 0.2;
} else if (relation === "on" || relation === "above") {
target.set(p.x, p.y + r.height, p.z);
}
return target;
}
function validateClause(clause) {
const source = resolveItem(clause.sourceName);
if (!source || !source.present) {
return {
valid: false,
error: `Source object “${cleanName(clause.sourceName)}” is unknown or absent.`
};
}
if (clause.type === "remove") {
return { valid: true, source };
}
const reference = resolveItem(clause.referenceName);
if (!reference || !reference.present) {
return {
valid: false,
missingReference: true,
error: `Required reference “${cleanName(clause.referenceName)}” is absent. Execution stopped.`
};
}
if (source.id === reference.id) {
return {
valid: false,
error: "The source and reference cannot be the same object."
};
}
const target = computeTarget(source, reference, clause.relation);
if (Math.abs(target.x) > 4.82 || Math.abs(target.z) > 2.82) {
return {
valid: false,
error: "That placement would be outside the tabletop. No movement was made."
};
}
return { valid: true, source, reference, target };
}
// ---------------------------------------------------------------------
// Robot movement and placement animation
// ---------------------------------------------------------------------
function tween(duration, update) {
return new Promise((resolve) => {
const start = performance.now();
function frame(now) {
const linear = Math.min((now - start) / duration, 1);
const eased = linear * linear * (3 - 2 * linear);
update(eased);
if (linear < 1) requestAnimationFrame(frame);
else resolve();
}
requestAnimationFrame(frame);
});
}
function shortestAngle(from, to) {
let delta = (to - from + Math.PI) % (Math.PI * 2);
if (delta < 0) delta += Math.PI * 2;
return delta - Math.PI;
}
async function moveRobotTo(x, z, faceX, faceZ, duration = 650) {
const startX = robot.position.x;
const startZ = robot.position.z;
const startRotation = robot.rotation.y;
const desiredRotation = Math.atan2(faceX - x, faceZ - z);
await tween(duration, (t) => {
robot.position.x = THREE.MathUtils.lerp(startX, x, t);
robot.position.z = THREE.MathUtils.lerp(startZ, z, t);
robot.rotation.y = startRotation + shortestAngle(startRotation, desiredRotation) * t;
});
}
function standPositionFor(target) {
const offset = 1.08;
const candidates = [
new THREE.Vector3(target.x, ITEM_Y, target.z + offset),
new THREE.Vector3(target.x + offset, ITEM_Y, target.z),
new THREE.Vector3(target.x, ITEM_Y, target.z - offset),
new THREE.Vector3(target.x - offset, ITEM_Y, target.z)
].filter(point => Math.abs(point.x) <= 4.85 && Math.abs(point.z) <= 2.85);
candidates.sort((a, b) => {
const da = (a.x - robot.position.x) ** 2 + (a.z - robot.position.z) ** 2;
const db = (b.x - robot.position.x) ** 2 + (b.z - robot.position.z) ** 2;
return da - db;
});
return candidates[0] || new THREE.Vector3(0, ITEM_Y, 2.45);
}
async function pickUp(item) {
const stand = standPositionFor(item.group.position);
await moveRobotTo(
stand.x,
stand.z,
item.group.position.x,
item.group.position.z,
700
);
gripper.add(item.group);
item.group.position.set(0, 0, 0);
item.group.rotation.y = 0;
}
async function placeItem(item, target) {
await pickUp(item);
const stand = standPositionFor(target);
await moveRobotTo(
stand.x,
stand.z,
target.x,
target.z,
760
);
scene.add(item.group);
item.group.position.copy(target);
item.group.rotation.y = 0;
}
async function removeItem(item) {
await pickUp(item);
const edge = new THREE.Vector3(
THREE.MathUtils.clamp(robot.position.x, -4.2, 4.2),
ITEM_Y,
2.55
);
await moveRobotTo(edge.x, edge.z, edge.x, edge.z + 1, 700);
gripper.remove(item.group);
item.group.visible = false;
item.present = false;
}
// ---------------------------------------------------------------------
// UI and execution loop
// ---------------------------------------------------------------------
function setStatus(text, tone = "") {
statusElement.textContent = text;
statusElement.className = tone;
}
function addLog(message, kind = "") {
const li = document.createElement("li");
li.className = kind;
li.textContent = message;
logElement.appendChild(li);
logElement.scrollTop = logElement.scrollHeight;
}
function updateInventory() {
inventoryElement.replaceChildren();
for (const item of items.values()) {
const chip = document.createElement("span");
chip.className = item.present ? "chip" : "chip absent";
chip.textContent = item.definition.label;
chip.title = item.present ? "Present on the table" : "Absent";
inventoryElement.appendChild(chip);
}
}
async function runInstructions() {
if (running) return;
const clauses = splitInstruction(commandInput.value);
if (!clauses.length) {
setStatus("Empty instruction", "error");
addLog("Enter at least one command.", "error");
return;
}
running = true;
runButton.disabled = true;
resetButton.disabled = true;
clearButton.disabled = true;
let stopped = false;
for (let index = 0; index < clauses.length; index++) {
const raw = clauses[index];
setStatus(`Checking ${index + 1}/${clauses.length}`, "working");
addLog(`Step ${index + 1}: checking “${raw}”`, "warn");
const parsed = parseClause(raw);
if (parsed.error) {
setStatus("Stopped · invalid step", "error");
addLog(parsed.error, "error");
stopped = true;
break;
}
const validation = validateClause(parsed);
if (!validation.valid) {
setStatus(
validation.missingReference ? "Stopped · missing reference" : "Stopped · invalid step",
"error"
);
addLog(validation.error, "error");
addLog("Remaining instructions were cancelled.", "error");
stopped = true;
break;
}
if (parsed.type === "remove") {
addLog(
`Validated: remove ${validation.source.definition.label}. Robot is moving.`,
"ok"
);
await removeItem(validation.source);
addLog(`Removed ${validation.source.definition.label}.`, "ok");
} else {
addLog(
`Validated: place ${validation.source.definition.label} ${parsed.relation} ` +
`${validation.reference.definition.label}. Robot is moving.`,
"ok"
);
await placeItem(validation.source, validation.target);
addLog(
`Placed ${validation.source.definition.label} ${parsed.relation} ` +
`${validation.reference.definition.label}.`,
"ok"
);
}
updateInventory();
}
if (!stopped) {
setStatus("Complete");
addLog(`Completed ${clauses.length} ordered instruction${clauses.length === 1 ? "" : "s"}.`, "ok");
}
running = false;
runButton.disabled = false;
resetButton.disabled = false;
clearButton.disabled = false;
}
function resetScene() {
if (running) return;
for (const item of items.values()) {
if (item.group.parent !== scene) scene.add(item.group);
item.group.position.copy(item.initial);
item.group.rotation.set(0, 0, 0);
item.group.visible = item.definition.present;
item.present = item.definition.present;
}
robot.position.set(0, ITEM_Y, 2.25);
robot.rotation.set(0, 0, 0);
setStatus("Ready");
updateInventory();
addLog("Scene reset. Fork remains intentionally absent.", "warn");
}
runButton.addEventListener("click", runInstructions);
clearButton.addEventListener("click", () => {
commandInput.value = "";
commandInput.focus();
});
resetButton.addEventListener("click", resetScene);
commandInput.addEventListener("keydown", (event) => {
if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
event.preventDefault();
runInstructions();
}
});
document.querySelectorAll(".example").forEach(button => {
button.addEventListener("click", () => {
commandInput.value = button.dataset.command;
commandInput.focus();
});
});
updateInventory();
addLog("Ready. The robot validates each clause before moving.", "ok");
addLog("Fork is absent and can be used to test reference validation.", "warn");
resetCamera();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```