Prompt specification
Build or customize the Three.js 3D scene "Clarification Before Action Game" 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: Clarification Before Action Game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/clarification-before-action-game-x3364
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Create a robot assistant puzzle with deliberately ambiguous object requests. Reward a correct clarification and grounded action, allowing refusal to guess when information remains insufficient.
Working Three.js Starter (clarification-before-action-game-x3364):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Assistant — Grounding Lab</title>
<style>
:root {
--ink: #12232d;
--muted: #62737d;
--paper: #f7f4ec;
--panel: #ffffff;
--line: #d9ded9;
--navy: #10232d;
--navy-2: #193540;
--mint: #6fe0b2;
--mint-dark: #178c68;
--amber: #f5b84b;
--red: #e46b61;
--blue: #4c8ed9;
--shadow: 0 12px 32px rgba(18, 35, 45, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
color: var(--ink);
background: var(--navy);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
overflow: hidden;
}
button,
textarea {
font: inherit;
}
button {
cursor: pointer;
}
#app {
width: 100vw;
height: 100vh;
display: grid;
grid-template-rows: 68px minmax(0, 1fr);
background: var(--paper);
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
color: white;
background: var(--navy);
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
z-index: 20;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 34px;
height: 34px;
display: grid;
place-items: center;
color: var(--navy);
background: var(--mint);
box-shadow: 5px 5px 0 rgba(111, 224, 178, 0.2);
font-weight: 900;
font-size: 14px;
letter-spacing: -1px;
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1.1;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand small {
display: block;
margin-top: 4px;
color: #9fb4bd;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.header-stats {
display: flex;
align-items: center;
gap: 10px;
}
.stat {
min-width: 88px;
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.05);
}
.stat span {
display: block;
color: #9fb4bd;
font-size: 9px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.stat strong {
display: block;
margin-top: 2px;
font-size: 16px;
color: white;
}
.icon-button,
.small-button {
color: white;
border: 1px solid rgba(255, 255, 255, 0.18);
background: rgba(255, 255, 255, 0.06);
transition: background 150ms ease, transform 150ms ease;
}
.icon-button {
width: 36px;
height: 36px;
font-size: 19px;
}
.icon-button:hover,
.small-button:hover {
background: rgba(255, 255, 255, 0.13);
transform: translateY(-1px);
}
main {
min-height: 0;
display: grid;
grid-template-columns: 250px minmax(360px, 1fr) 365px;
}
.task-rail {
min-height: 0;
overflow-y: auto;
color: #eef8f4;
background: var(--navy-2);
border-right: 1px solid rgba(0, 0, 0, 0.15);
padding: 22px 16px;
}
.rail-kicker {
margin: 0 0 8px;
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.task-rail h2 {
margin: 0 0 10px;
font-size: 23px;
line-height: 1.15;
}
.rail-copy {
margin: 0 0 20px;
color: #a9bdc5;
font-size: 12px;
line-height: 1.55;
}
#task-list {
display: grid;
gap: 8px;
}
.task-button {
width: 100%;
min-height: 67px;
display: grid;
grid-template-columns: 30px minmax(0, 1fr) 18px;
align-items: center;
gap: 8px;
padding: 10px;
color: #c8d6da;
text-align: left;
border: 1px solid rgba(255, 255, 255, 0.09);
background: rgba(255, 255, 255, 0.035);
transition: 150ms ease;
}
.task-button:hover {
background: rgba(255, 255, 255, 0.08);
}
.task-button.active {
color: white;
border-color: var(--mint);
background: rgba(111, 224, 178, 0.1);
box-shadow: inset 4px 0 0 var(--mint);
}
.task-button.completed {
border-color: rgba(111, 224, 178, 0.35);
}
.task-number {
display: grid;
place-items: center;
width: 26px;
height: 26px;
color: #8da6af;
border: 1px solid rgba(255, 255, 255, 0.14);
font-size: 10px;
font-weight: 900;
}
.task-button.active .task-number {
color: var(--navy);
border-color: var(--mint);
background: var(--mint);
}
.task-button.completed .task-number {
color: var(--mint);
border-color: var(--mint);
}
.task-name {
display: block;
font-size: 12px;
font-weight: 750;
line-height: 1.25;
}
.task-status {
display: block;
margin-top: 4px;
color: #849da6;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.task-check {
color: var(--mint);
font-weight: 900;
}
.rail-rules {
margin-top: 24px;
padding: 14px;
border: 1px solid rgba(111, 224, 178, 0.2);
background: rgba(0, 0, 0, 0.12);
}
.rail-rules strong {
display: block;
margin-bottom: 8px;
color: var(--mint);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.rail-rules ul {
margin: 0;
padding-left: 16px;
color: #9eb2ba;
font-size: 11px;
line-height: 1.55;
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dce9e4;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.viewport-top {
position: absolute;
top: 16px;
left: 16px;
right: 16px;
z-index: 5;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.view-chip,
.view-help {
color: #29434d;
border: 1px solid rgba(18, 35, 45, 0.13);
background: rgba(255, 255, 255, 0.84);
backdrop-filter: blur(8px);
box-shadow: 0 6px 20px rgba(18, 35, 45, 0.08);
}
.view-chip {
padding: 9px 12px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.view-chip::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 8px;
background: var(--mint-dark);
box-shadow: 0 0 0 3px rgba(23, 140, 104, 0.12);
}
.view-help {
max-width: 290px;
padding: 9px 12px;
color: #536a73;
font-size: 10px;
line-height: 1.45;
text-align: right;
}
#camera-reset {
pointer-events: auto;
padding: 6px 9px;
color: #29434d;
border: 1px solid rgba(18, 35, 45, 0.14);
background: rgba(255, 255, 255, 0.9);
font-size: 10px;
font-weight: 800;
}
#labels {
position: absolute;
inset: 0;
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 4px 7px;
color: #29434d;
border: 1px solid rgba(18, 35, 45, 0.12);
background: rgba(255, 255, 255, 0.82);
font-size: 9px;
font-weight: 750;
white-space: nowrap;
transform: translate(-50%, -100%);
box-shadow: 0 3px 10px rgba(18, 35, 45, 0.08);
transition: opacity 150ms ease, background 150ms ease, border-color 150ms ease;
}
.world-label.candidate {
color: #143a31;
border-color: rgba(23, 140, 104, 0.5);
background: rgba(214, 255, 239, 0.94);
}
.world-label.dimmed {
opacity: 0.38;
}
#robot-bubble {
position: absolute;
z-index: 6;
width: min(280px, 54%);
padding: 11px 13px;
color: #17343c;
border: 2px solid #17343c;
background: #ffffff;
box-shadow: 5px 5px 0 rgba(23, 52, 60, 0.13);
font-size: 11px;
font-weight: 650;
line-height: 1.45;
transform: translate(-50%, -100%);
pointer-events: none;
}
#robot-bubble::after {
content: "";
position: absolute;
left: 50%;
bottom: -9px;
width: 12px;
height: 12px;
border-right: 2px solid #17343c;
border-bottom: 2px solid #17343c;
background: white;
transform: translateX(-50%) rotate(45deg);
}
#toast {
position: absolute;
left: 50%;
bottom: 24px;
z-index: 8;
max-width: min(480px, calc(100% - 30px));
padding: 10px 15px;
color: white;
background: rgba(16, 35, 45, 0.92);
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 700;
text-align: center;
transform: translate(-50%, 20px);
opacity: 0;
pointer-events: none;
transition: 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.chat-panel {
min-height: 0;
display: grid;
grid-template-rows: auto minmax(0, 1fr) auto;
border-left: 1px solid var(--line);
background: var(--panel);
}
.request-card {
padding: 19px 20px 16px;
border-bottom: 1px solid var(--line);
}
.request-kicker {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
color: var(--mint-dark);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#phase-pill {
padding: 4px 7px;
color: #7b5a17;
background: #fff1c8;
letter-spacing: 0.07em;
}
#request-title {
margin: 9px 0 8px;
font-size: 18px;
line-height: 1.2;
}
#request-quote {
margin: 0;
padding: 11px 12px;
color: #28434d;
border-left: 4px solid var(--amber);
background: #f7f3e9;
font-size: 13px;
font-weight: 700;
line-height: 1.45;
}
#ambiguity-tags {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 10px;
}
.ambiguity-tag {
padding: 4px 7px;
color: #6b5632;
border: 1px solid #ead8ae;
background: #fffaf0;
font-size: 9px;
font-weight: 700;
}
#transcript {
min-height: 0;
overflow-y: auto;
padding: 18px 20px;
background:
linear-gradient(rgba(255, 255, 255, 0.91), rgba(255, 255, 255, 0.91)),
linear-gradient(90deg, #e7e7e7 1px, transparent 1px),
linear-gradient(#e7e7e7 1px, transparent 1px);
background-size: auto, 16px 16px, 16px 16px;
}
.message {
max-width: 91%;
margin-bottom: 12px;
padding: 10px 12px;
font-size: 11px;
line-height: 1.5;
animation: message-in 180ms ease;
}
@keyframes message-in {
from {
opacity: 0;
transform: translateY(5px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.message.robot {
color: #18343d;
border: 1px solid #d6dfdb;
border-radius: 3px 12px 12px 12px;
background: #f3f8f5;
}
.message.user {
margin-left: auto;
color: white;
border-radius: 12px 3px 12px 12px;
background: var(--navy-2);
}
.message.system {
max-width: 100%;
color: #61727a;
border: 1px dashed #cbd3cf;
background: #fafbf9;
text-align: center;
}
.message.reward {
color: #175a45;
border: 1px solid #a9ddc8;
background: #e9faf2;
font-weight: 700;
}
.composer {
padding: 14px 16px 16px;
border-top: 1px solid var(--line);
background: #fbfcfa;
}
.composer-label {
display: block;
margin-bottom: 7px;
color: #6d7c83;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.input-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 42px;
gap: 8px;
}
textarea {
width: 100%;
min-height: 67px;
max-height: 130px;
resize: vertical;
padding: 10px 11px;
color: var(--ink);
border: 1px solid #cbd3cf;
outline: none;
background: white;
font-size: 11px;
line-height: 1.45;
}
textarea:focus {
border-color: var(--mint-dark);
box-shadow: 0 0 0 3px rgba(111, 224, 178, 0.15);
}
#send {
align-self: stretch;
color: var(--navy);
border: 0;
background: var(--mint);
font-size: 19px;
font-weight: 900;
}
#send:hover {
background: #84e8bf;
}
.composer-actions {
display: flex;
gap: 7px;
margin-top: 8px;
}
.small-button {
flex: 1;
min-height: 32px;
color: #415861;
border-color: #d5dcd8;
background: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
}
.small-button:hover {
color: var(--ink);
background: #f1f5f2;
}
#next-task {
display: none;
width: 100%;
margin-top: 8px;
padding: 9px;
color: white;
border: 0;
background: var(--mint-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.05em;
text-transform: uppercase;
}
#loading {
position: absolute;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: var(--navy);
background: #dce9e4;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
@media (max-width: 1050px) {
main {
grid-template-columns: 210px minmax(320px, 1fr) 330px;
}
.task-rail {
padding-inline: 11px;
}
}
@media (max-width: 820px) {
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100vh;
grid-template-rows: 64px auto;
}
main {
display: grid;
grid-template-columns: 1fr;
}
.task-rail {
padding: 14px;
overflow: visible;
}
.task-rail h2,
.rail-copy,
.rail-rules {
display: none;
}
#task-list {
grid-template-columns: repeat(5, minmax(150px, 1fr));
overflow-x: auto;
padding-bottom: 4px;
}
.task-button {
min-width: 150px;
}
#viewport {
min-height: 58vh;
}
.chat-panel {
min-height: 560px;
}
.header-stats .stat:first-child {
display: none;
}
}
@media (max-width: 500px) {
header {
padding-inline: 12px;
}
.brand small {
display: none;
}
.stat {
min-width: 66px;
padding: 6px 8px;
}
.view-help {
display: none;
}
#robot-bubble {
width: 64%;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark">VA</div>
<div>
<h1>Voxel Assistant</h1>
<small>Grounding & clarification lab</small>
</div>
</div>
<div class="header-stats">
<div class="stat">
<span>Score</span>
<strong id="score">0</strong>
</div>
<div class="stat">
<span>Progress</span>
<strong id="progress">0 / 5</strong>
</div>
<button id="reset" class="icon-button" title="Reset lab" aria-label="Reset lab">
↺
</button>
</div>
</header>
<main>
<aside class="task-rail">
<p class="rail-kicker">Episode 01</p>
<h2>Resolve before acting.</h2>
<p class="rail-copy">
Each request contains more than one plausible object. Ask a precise
clarifying question, then point to the grounded object in the scene.
</p>
<div id="task-list"></div>
<div class="rail-rules">
<strong>Assistant protocol</strong>
<ul>
<li>Do not guess between visual matches.</li>
<li>Ask for a distinguishing detail.</li>
<li>Ground source and destination by clicking.</li>
<li>Refuse to act when evidence is insufficient.</li>
</ul>
</div>
</aside>
<section id="viewport" aria-label="Interactive voxel scene">
<div id="loading">Building voxel workspace…</div>
<div class="viewport-top">
<div class="view-chip">Live scene</div>
<div class="view-help">
Drag to orbit · Scroll to zoom · Click an object to ground
<button id="camera-reset" title="Reset camera">Reset view</button>
</div>
</div>
<div id="labels"></div>
<div id="robot-bubble">I’m ready. Let’s disambiguate your request.</div>
<div id="toast" role="status" aria-live="polite"></div>
</section>
<aside class="chat-panel">
<div class="request-card">
<div class="request-kicker">
<span id="task-number">Puzzle 01</span>
<span id="phase-pill">Clarify</span>
</div>
<h2 id="request-title">The writing tool</h2>
<p id="request-quote">“I need something to write with.”</p>
<div id="ambiguity-tags"></div>
</div>
<div id="transcript" aria-live="polite"></div>
<div class="composer">
<label class="composer-label" for="message-input">
Your clarification
</label>
<div class="input-row">
<textarea
id="message-input"
placeholder="Ask which object I mean…"
aria-label="Clarification message"
></textarea>
<button id="send" title="Send clarification" aria-label="Send clarification">
↑
</button>
</div>
<div class="composer-actions">
<button id="hint" class="small-button">Show example</button>
<button id="refuse" class="small-button">Ask without guessing</button>
</div>
<button id="next-task">Next puzzle →</button>
</div>
</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";
// ---------------------------------------------------------------------
// Puzzle data
// ---------------------------------------------------------------------
const tasks = [
{
id: "writing",
title: "The writing tool",
request: "I need something to write with.",
ambiguity: ["pen or pencil", "no color specified"],
source: ["bluePen", "yellowPencil"],
destination: [],
stages: 1,
concepts: [["pen", "pencil"]],
example: "Do you mean the blue pen or the yellow pencil?",
followup:
"Good question. Now point to the exact writing tool in the scene.",
action: "bring"
},
{
id: "stack",
title: "Cube on something round",
request: "Put the cube on the round thing.",
ambiguity: ["two cubes", "two round objects"],
source: ["redCube", "blueCube"],
destination: ["yellowBall", "orangeBowl"],
stages: 2,
concepts: [
["cube"],
["round", "ball", "bowl", "cylinder"]
],
example:
"Do you mean the red or blue cube, and the yellow ball or orange bowl?",
followup:
"Good distinction. First, point to the cube you want me to move.",
action: "stack"
},
{
id: "small",
title: "The small one",
request: "Bring me the small one.",
ambiguity: ["three small objects", "no category specified"],
source: ["smallMug", "brassKey", "blueCoin"],
destination: [],
stages: 1,
concepts: [
["small", "tiny"],
["mug", "key", "coin", "item", "object"]
],
example:
"Do you mean the small mug, the small brass key, or the small blue coin?",
followup:
"Good question. Please point to the exact small object.",
action: "bring"
},
{
id: "box",
title: "Box beside a ball",
request: "Move the box next to the ball.",
ambiguity: ["two boxes", "two balls"],
source: ["redBox", "greenBox"],
destination: ["yellowBall", "blueBall"],
stages: 2,
concepts: [
["box"],
["ball", "sphere"]
],
example:
"Do you mean the red or green box, and the yellow or blue ball?",
followup:
"Good distinction. First, point to the box you want moved.",
action: "beside"
},
{
id: "identical",
title: "The identical cups",
request: "Bring me the cup.",
ambiguity: ["two identical cups", "no visual distinction"],
source: ["whiteCupA", "whiteCupB"],
destination: [],
stages: 1,
concepts: [["cup", "mug"]],
example:
"There are two identical white cups. Can you point to the one you mean?",
followup:
"Understood. I cannot distinguish them safely, so please point to one.",
action: "bring"
}
];
const state = {
taskIndex: 0,
phase: "clarify",
score: 0,
completed: Array(tasks.length).fill(false),
selectedSource: null,
selectedDestination: null,
bubbleTimer: null
};
// ---------------------------------------------------------------------
// DOM references
// ---------------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const labelLayer = document.querySelector("#labels");
const transcript = document.querySelector("#transcript");
const input = document.querySelector("#message-input");
const sendButton = document.querySelector("#send");
const hintButton = document.querySelector("#hint");
const refuseButton = document.querySelector("#refuse");
const nextButton = document.querySelector("#next-task");
const resetButton = document.querySelector("#reset");
const cameraResetButton = document.querySelector("#camera-reset");
const robotBubble = document.querySelector("#robot-bubble");
const toast = document.querySelector("#toast");
const taskList = document.querySelector("#task-list");
const scoreElement = document.querySelector("#score");
const progressElement = document.querySelector("#progress");
const phasePill = document.querySelector("#phase-pill");
const requestTitle = document.querySelector("#request-title");
const requestQuote = document.querySelector("#request-quote");
const taskNumber = document.querySelector("#task-number");
const ambiguityTags = document.querySelector("#ambiguity-tags");
// ---------------------------------------------------------------------
// Three.js setup
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e4);
scene.fog = new THREE.Fog(0xdce9e4, 14, 25);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
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.domElement.setAttribute(
"aria-label",
"Voxel room. Drag to orbit, scroll to zoom, and click objects."
);
viewport.prepend(renderer.domElement);
const ambient = new THREE.HemisphereLight(0xf7fff9, 0x4b626d, 2.25);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff3d7, 3.1);
sun.position.set(5, 9, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8cc9ff, 1.15);
fill.position.set(-6, 4, -3);
scene.add(fill);
const materials = new Map();
function material(color, metalness = 0, roughness = 0.82) {
const key = `${color}-${metalness}-${roughness}`;
if (!materials.has(key)) {
materials.set(
key,
new THREE.MeshStandardMaterial({
color,
metalness,
roughness
})
);
}
return materials.get(key);
}
function addBox(
parent,
width,
height,
depth,
x,
y,
z,
color,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
options.material || material(color, options.metalness || 0, options.roughness)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Camera controls — implemented locally without addons
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.35, 1.15, -0.35);
const cameraState = {
yaw: 0.72,
pitch: 0.54,
distance: 11.8
};
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
const sp = Math.sin(cameraState.pitch);
const sy = Math.sin(cameraState.yaw);
const cy = Math.cos(cameraState.yaw);
camera.position.set(
cameraTarget.x + cameraState.distance * cp * sy,
cameraTarget.y + cameraState.distance * sp,
cameraTarget.z + cameraState.distance * cp * cy
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.yaw = 0.72;
cameraState.pitch = 0.54;
cameraState.distance = 11.8;
updateCamera();
}
resetCamera();
let pointerDown = false;
let pointerStart = { x: 0, y: 0 };
let pointerLast = { x: 0, y: 0 };
let dragDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
dragDistance = 0;
pointerStart = { x: event.clientX, y: event.clientY };
pointerLast = { ...pointerStart };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - pointerLast.x;
const dy = event.clientY - pointerLast.y;
dragDistance += Math.abs(dx) + Math.abs(dy);
pointerLast = { x: event.clientX, y: event.clientY };
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.22,
1.15
);
updateCamera();
} else {
updateHover(event);
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
const wasClick = dragDistance < 7;
pointerDown = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (wasClick) {
pickObject(event);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
dragDistance = 0;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.009,
7,
17
);
updateCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Voxel room
// ---------------------------------------------------------------------
const room = new THREE.Group();
scene.add(room);
addBox(room, 11.5, 0.3, 9.5, 0, -0.15, -0.4, 0xb9c9c1);
addBox(room, 11.5, 5.3, 0.28, 0, 2.65, -4.45, 0xc7d8d0);
addBox(room, 0.25, 5.3, 9.2, -5.7, 2.65, -0.4, 0xd7e3dc);
const grid = new THREE.GridHelper(11, 22, 0x91aaa0, 0xa9bbb3);
grid.position.set(0, 0.012, -0.4);
grid.scale.z = 0.82;
room.add(grid);
// Back-wall blocks and trim.
for (let i = -5; i <= 5; i++) {
addBox(room, 0.94, 0.95, 0.11, i, 0.52, -4.27, 0xb7cbc2);
}
for (let i = -4; i <= 4; i++) {
addBox(room, 0.82, 0.84, 0.12, i, 1.52, -4.25, 0xc4d4cc);
}
addBox(room, 10.8, 0.16, 0.18, 0, 2.12, -4.19, 0x7fa095);
// Window-like wall decoration.
addBox(room, 2.7, 1.7, 0.16, 2.1, 3.4, -4.19, 0x4d7d86);
addBox(room, 2.42, 1.42, 0.11, 2.1, 3.4, -4.08, 0x9cc8cb);
addBox(room, 0.12, 1.55, 0.14, 2.1, 3.4, -4.0, 0x4d7d86);
addBox(room, 2.55, 0.12, 0.14, 2.1, 3.4, -4.0, 0x4d7d86);
// Desk.
const desk = new THREE.Group();
desk.position.set(-2.6, 0, -0.3);
room.add(desk);
addBox(desk, 4.2, 0.24, 2.05, 0, 1.68, 0, 0x8a6952);
addBox(desk, 0.26, 1.58, 0.26, -1.72, 0.8, -0.72, 0x665446);
addBox(desk, 0.26, 1.58, 0.26, 1.72, 0.8, -0.72, 0x665446);
addBox(desk, 0.26, 1.58, 0.26, -1.72, 0.8, 0.72, 0x665446);
addBox(desk, 0.26, 1.58, 0.26, 1.72, 0.8, 0.72, 0x665446);
// Back console.
const consoleTable = new THREE.Group();
consoleTable.position.set(0.42, 0, -2.85);
room.add(consoleTable);
addBox(consoleTable, 3.2, 0.22, 1.0, 0, 1.62, 0, 0x6b8790);
addBox(consoleTable, 0.25, 1.52, 0.25, -1.25, 0.76, 0, 0x4f6870);
addBox(consoleTable, 0.25, 1.52, 0.25, 1.25, 0.76, 0, 0x4f6870);
addBox(consoleTable, 0.25, 1.52, 0.25, -1.25, 0.76, -0.31, 0x4f6870);
addBox(consoleTable, 0.25, 1.52, 0.25, 1.25, 0.76, -0.31, 0x4f6870);
// Shelf on the left wall.
const shelf = new THREE.Group();
shelf.position.set(-5.32, 0, -0.65);
room.add(shelf);
addBox(shelf, 0.28, 3.4, 2.7, 0, 1.7, 0, 0x718c84);
addBox(shelf, 1.3, 0.16, 2.7, 0.65, 2.7, 0, 0x9b7659);
addBox(shelf, 1.3, 0.16, 2.7, 0.65, 1.05, 0, 0x9b7659);
// Decorative books.
const bookColors = [0xd96f61, 0xf0b64e, 0x4f8cbd, 0x71b995];
for (let i = 0; i < 7; i++) {
addBox(
shelf,
0.78,
0.58 + (i % 3) * 0.08,
0.22,
0.75,
3.08 + (i % 3) * 0.04,
-0.83 + i * 0.27,
bookColors[i % bookColors.length]
);
}
// ---------------------------------------------------------------------
// Robot assistant
// ---------------------------------------------------------------------
const robot = new THREE.Group();
robot.position.set(-0.05, 0, 2.45);
scene.add(robot);
addBox(robot, 1.15, 0.3, 1.05, 0, 0.15, 0, 0x294651);
addBox(robot, 0.95, 0.26, 0.85, 0, 0.42, 0, 0x3f6470);
addBox(robot, 1.12, 1.2, 0.8, 0, 1.13, 0, 0x3e84a4);
addBox(robot, 0.72, 0.34, 0.16, 0, 1.22, 0.44, 0x274a58);
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0xb9ffe0,
emissive: 0x54e5a8,
emissiveIntensity: 1.5,
roughness: 0.4
});
addBox(robot, 0.18, 0.13, 0.08, -0.18, 1.24, 0.54, 0xffffff, {
material: eyeMaterial
});
addBox(robot, 0.18, 0.13, 0.08, 0.18, 1.24, 0.54, 0xffffff, {
material: eyeMaterial
});
addBox(robot, 0.92, 0.72, 0.78, 0, 2.06, 0, 0x72b8cc);
addBox(robot, 0.59, 0.18, 0.12, 0, 2.04, 0.42, 0x213e49);
addBox(robot, 0.08, 0.32, 0.08, 0, 2.57, 0, 0x375967);
addBox(robot, 0.2, 0.2, 0.2, 0, 2.77, 0, 0xf5b84b);
const armLeft = new THREE.Group();
armLeft.position.set(-0.67, 1.45, 0);
robot.add(armLeft);
addBox(armLeft, 0.27, 0.85, 0.27, 0, -0.33, 0, 0x72b8cc);
addBox(armLeft, 0.31, 0.28, 0.31, 0, -0.82, 0, 0x274a58);
const armRight = new THREE.Group();
armRight.position.set(0.67, 1.45, 0);
robot.add(armRight);
addBox(armRight, 0.27, 0.85, 0.27, 0, -0.33, 0, 0x72b8cc);
addBox(armRight, 0.31, 0.28, 0.31, 0, -0.82, 0, 0x274a58);
// Delivery tray.
addBox(robot, 0.9, 0.13, 0.8, 1.08, 1.05, 0, 0x21424d);
addBox(robot, 0.08, 0.35, 0.8, 0.66, 1.24, 0, 0x21424d);
addBox(robot, 0.08, 0.35, 0.8, 1.5, 1.24, 0, 0x21424d);
// ---------------------------------------------------------------------
// Interactive objects
// ---------------------------------------------------------------------
const objects = new Map();
const interactiveMeshes = [];
function registerObject(id, label, position, height, builder) {
const group = new THREE.Group();
group.position.copy(position);
group.userData.interactiveId = id;
builder(group);
scene.add(group);
const object = {
id,
label,
group,
height,
home: position.clone(),
homeRotation: new THREE.Euler()
};
objects.set(id, object);
group.traverse((child) => {
if (child.isMesh) {
child.userData.interactiveId = id;
interactiveMeshes.push(child);
}
});
const labelElement = document.createElement("div");
labelElement.className = "world-label";
labelElement.textContent = label;
labelLayer.appendChild(labelElement);
object.labelElement = labelElement;
return object;
}
function buildPen(group, bodyColor, capColor) {
addBox(group, 0.17, 0.17, 1.18, 0, 0.085, 0, bodyColor);
addBox(group, 0.21, 0.21, 0.23, 0, 0.085, 0.57, capColor);
addBox(group, 0.12, 0.12, 0.18, 0, 0.085, -0.66, 0xe8e2cd);
addBox(group, 0.06, 0.06, 0.12, 0, 0.085, -0.8, 0x3c4b50);
}
registerObject(
"bluePen",
"Blue pen",
new THREE.Vector3(-3.85, 1.81, -0.18),
0.22,
(group) => buildPen(group, 0x397fbd, 0x264e70)
);
registerObject(
"yellowPencil",
"Yellow pencil",
new THREE.Vector3(-2.55, 1.81, -0.42),
0.22,
(group) => buildPen(group, 0xf2b84a, 0xd46c58)
);
registerObject(
"smallMug",
"Small ceramic mug",
new THREE.Vector3(-1.18, 1.81, -0.03),
0.56,
(group) => {
addBox(group, 0.45, 0.48, 0.42, 0, 0.24, 0, 0xdce7df);
addBox(group, 0.2, 0.28, 0.12, 0.31, 0.25, 0, 0xdce7df);
addBox(group, 0.18, 0.08, 0.12, 0.4, 0.34, 0, 0xdce7df);
addBox(group, 0.18, 0.08, 0.12, 0.4, 0.15, 0, 0xdce7df);
addBox(group, 0.33, 0.06, 0.3, 0, 0.5, 0, 0x6f9e91);
}
);
registerObject(
"brassKey",
"Small brass key",
new THREE.Vector3(-3.45, 1.81, 0.39),
0.18,
(group) => {
addBox(group, 0.14, 0.12, 0.74, 0, 0.06, 0, 0xc99a35, {
metalness: 0.45
});
addBox(group, 0.34, 0.12, 0.16, 0, 0.06, -0.38, 0xc99a35, {
metalness: 0.45
});
addBox(group, 0.12, 0.12, 0.2, 0.1, 0.06, 0.25, 0xc99a35, {
metalness: 0.45
});
}
);
registerObject(
"blueCoin",
"Small blue coin",
new THREE.Vector3(-2.65, 1.81, 0.48),
0.14,
(group) => {
addBox(group, 0.34, 0.1, 0.34, 0, 0.05, 0, 0x4b8fc0, {
metalness: 0.28
});
addBox(group, 0.15, 0.03, 0.15, 0, 0.115, 0, 0x8cc9df, {
metalness: 0.28
});
}
);
registerObject(
"redCube",
"Red cube",
new THREE.Vector3(-4.12, 0, -1.62),
0.7,
(group) => {
addBox(group, 0.7, 0.7, 0.7, 0, 0.35, 0, 0xd96358);
addBox(group, 0.54, 0.05, 0.54, 0, 0.72, 0, 0xec887c);
}
);
registerObject(
"blueCube",
"Blue cube",
new THREE.Vector3(-3.52, 0, -2.4),
0.7,
(group) => {
addBox(group, 0.7, 0.7, 0.7, 0, 0.35, 0, 0x4f8fc0);
addBox(group, 0.54, 0.05, 0.54, 0, 0.72, 0, 0x78b3d8);
}
);
function buildVoxelBall(group, color) {
const block = 0.24;
for (let y = 0; y < 3; y++) {
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
if (
Math.abs(x) + Math.abs(z) + Math.abs(y - 1) <= 2
) {
addBox(
group,
block,
block,
block,
x * block,
0.12 + y * block,
z * block,
color
);
}
}
}
}
}
registerObject(
"yellowBall",
"Yellow ball",
new THREE.Vector3(0.12, 0, -0.48),
0.72,
(group) => buildVoxelBall(group, 0xf0bd4c)
);
registerObject(
"blueBall",
"Blue ball",
new THREE.Vector3(2.28, 0, -0.28),
0.72,
(group) => buildVoxelBall(group, 0x4f91c5)
);
registerObject(
"orangeBowl",
"Orange bowl",
new THREE.Vector3(1.1, 0, -1.48),
0.48,
(group) => {
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
addBox(group, 0.22, 0.18, 0.22, x * 0.22, 0.09, z * 0.22, 0xd9823f);
}
}
for (const x of [-1, 1]) {
for (const z of [-1, 1]) {
addBox(group, 0.2, 0.28, 0.2, x * 0.22, 0.27, z * 0.22, 0xe2954c);
}
}
}
);
registerObject(
"redBox",
"Red box",
new THREE.Vector3(1.18, 0, 1.22),
0.72,
(group) => {
addBox(group, 0.78, 0.72, 0.78, 0, 0.36, 0, 0xc96354);
addBox(group, 0.8, 0.08, 0.8, 0, 0.72, 0, 0xe07d6d);
addBox(group, 0.12, 0.5, 0.05, 0, 0.4, 0.405, 0xf3d7a2);
}
);
registerObject(
"greenBox",
"Green box",
new THREE.Vector3(2.68, 0, 1.32),
0.72,
(group) => {
addBox(group, 0.78, 0.72, 0.78, 0, 0.36, 0, 0x559b72);
addBox(group, 0.8, 0.08, 0.8, 0, 0.72, 0, 0x74b78f);
addBox(group, 0.12, 0.5, 0.05, 0, 0.4, 0.405, 0xf3d7a2);
}
);
function buildCup(group) {
addBox(group, 0.44, 0.5, 0.42, 0, 0.25, 0, 0xf0f1e9);
addBox(group, 0.18, 0.28, 0.11, 0.29, 0.25, 0, 0xf0f1e9);
addBox(group, 0.17, 0.08, 0.11, 0.37, 0.34, 0, 0xf0f1e9);
addBox(group, 0.17, 0.08, 0.11, 0.37, 0.16, 0, 0xf0f1e9);
addBox(group, 0.3, 0.06, 0.28, 0, 0.52, 0, 0xc8d8d5);
}
registerObject(
"whiteCupA",
"White cup",
new THREE.Vector3(-0.2, 1.74, -2.72),
0.56,
buildCup
);
registerObject(
"whiteCupB",
"White cup",
new THREE.Vector3(0.72, 1.74, -2.72),
0.56,
buildCup
);
// ---------------------------------------------------------------------
// Picking and hover
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const hoverHelper = new THREE.BoxHelper(new THREE.Object3D(), 0xf5b84b);
hoverHelper.visible = false;
scene.add(hoverHelper);
function setPointerFromEvent(event) {
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;
}
function findObjectAtEvent(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(interactiveMeshes, true);
if (!hits.length) return null;
let node = hits[0].object;
while (node && !node.userData.interactiveId) {
node = node.parent;
}
return node ? objects.get(node.userData.interactiveId) : null;
}
function updateHover(event) {
const object = findObjectAtEvent(event);
if (object) {
hoverHelper.setFromObject(object.group);
hoverHelper.visible = true;
renderer.domElement.style.cursor = "pointer";
} else {
hoverHelper.visible = false;
renderer.domElement.style.cursor = "grab";
}
}
function pickObject(event) {
const object = findObjectAtEvent(event);
if (!object) return;
handleObjectClick(object);
}
// ---------------------------------------------------------------------
// UI and conversation
// ---------------------------------------------------------------------
function currentTask() {
return tasks[state.taskIndex];
}
function addMessage(text, sender = "robot", type = sender) {
const message = document.createElement("div");
message.className = `message ${type}`;
message.textContent = text;
transcript.appendChild(message);
transcript.scrollTop = transcript.scrollHeight;
if (sender === "robot") {
setBubble(text);
}
return message;
}
function setBubble(text) {
robotBubble.textContent = text;
clearTimeout(state.bubbleTimer);
state.bubbleTimer = setTimeout(() => {
if (state.phase === "clarify") {
robotBubble.textContent =
"I can’t safely choose yet. What distinguishes the possible objects?";
} else if (state.phase === "ground-source") {
robotBubble.textContent = "Please point to the source object in the scene.";
} else if (state.phase === "ground-destination") {
robotBubble.textContent = "Now point to the destination object.";
} else if (state.phase === "done") {
robotBubble.textContent = "Task complete. Your grounding was unambiguous.";
}
}, 5000);
}
let toastTimer;
function showToast(text) {
toast.textContent = text;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
function updateStats() {
scoreElement.textContent = state.score;
const count = state.completed.filter(Boolean).length;
progressElement.textContent = `${count} / ${tasks.length}`;
}
function updateTaskList() {
[...taskList.children].forEach((button, index) => {
button.classList.toggle("active", index === state.taskIndex);
button.classList.toggle("completed", state.completed[index]);
const status = button.querySelector(".task-status");
const check = button.querySelector(".task-check");
if (state.completed[index]) {
status.textContent = "Grounded complete";
check.textContent = "✓";
} else if (index === state.taskIndex) {
status.textContent =
state.phase === "clarify"
? "Needs clarification"
: state.phase === "done"
? "Complete"
: "Needs grounding";
check.textContent = state.phase === "done" ? "✓" : "→";
} else {
status.textContent = "Queued";
check.textContent = "";
}
});
}
function updatePhaseUI() {
const task = currentTask();
const labels = {
clarify: "Clarify",
"ground-source": "Point to source",
"ground-destination": "Point to destination",
done: "Complete"
};
phasePill.textContent = labels[state.phase];
taskNumber.textContent = `Puzzle ${String(state.taskIndex + 1).padStart(2, "0")}`;
requestTitle.textContent = task.title;
requestQuote.textContent = `“${task.request}”`;
ambiguityTags.innerHTML = "";
for (const tag of task.ambiguity) {
const element = document.createElement("span");
element.className = "ambiguity-tag";
element.textContent = tag;
ambiguityTags.appendChild(element);
}
nextButton.style.display = state.phase === "done" ? "block" : "none";
nextButton.textContent =
state.taskIndex === tasks.length - 1
? "Restart the lab →"
: "Next puzzle →";
updateTaskList();
updateLabels();
}
function updateLabels() {
const task = currentTask();
const relevant = new Set([...task.source, ...task.destination]);
for (const object of objects.values()) {
object.labelElement.classList.toggle(
"candidate",
relevant.has(object.id)
);
object.labelElement.classList.toggle(
"dimmed",
relevant.size > 0 && !relevant.has(object.id)
);
}
}
function resetObjectPositions() {
for (const object of objects.values()) {
object.group.position.copy(object.home);
object.group.rotation.copy(object.homeRotation);
object.group.scale.set(1, 1, 1);
}
}
function selectTask(index) {
state.taskIndex = index;
state.phase = "clarify";
state.selectedSource = null;
state.selectedDestination = null;
resetObjectPositions();
transcript.innerHTML = "";
const task = currentTask();
addMessage(
`I heard your request. I can see multiple plausible matches, so I will not guess which one you mean.`
);
addMessage(
"Ask one precise disambiguating question first. Then I will wait for you to point to the object in the scene.",
"system"
);
input.value = "";
input.focus();
updatePhaseUI();
updateStats();
}
function evaluateClarification(text) {
const lower = text.toLowerCase();
const task = currentTask();
const hasQuestion =
lower.includes("?") ||
/\b(which|what|do you mean|can you point|could you point|where)\b/.test(
lower
);
const mentionsUncertainty =
/\b(can't|cannot|can not|not sure|unclear|ambiguous|multiple|too many|need more information|need a location)\b/.test(
lower
);
const asksForMore =
hasQuestion ||
/\b(point|color|location|clarif|specif|which|where)\b/.test(lower);
const safeRefusal =
task.allowRefusal !== false &&
mentionsUncertainty &&
asksForMore;
const coversConcepts = task.concepts.every((group) =>
group.some((term) => lower.includes(term))
);
if (safeRefusal) {
return { valid: true, refusal: true };
}
if (hasQuestion && coversConcepts) {
return { valid: true, refusal: false };
}
return { valid: false, refusal: false };
}
function sendMessage() {
const text = input.value.trim();
if (!text) return;
input.value = "";
addMessage(text, "user");
if (state.phase === "done") {
addMessage("This puzzle is already complete. Choose the next puzzle from the rail.");
return;
}
if (state.phase === "clarify") {
const evaluation = evaluateClarification(text);
if (!evaluation.valid) {
addMessage(
`That still leaves the scene ambiguous. Try a question such as: “${currentTask().example}” I will not act on an unsafe guess.`
);
return;
}
state.score += evaluation.refusal ? 8 : 10;
state.phase = "ground-source";
updatePhaseUI();
updateStats();
if (evaluation.refusal) {
addMessage(
"Understood. I will not guess. Please point directly to the exact source object in the scene.",
"reward"
);
addMessage(
"A pointer or a precise visible attribute will ground the action."
);
} else {
addMessage(
currentTask().followup,
"reward"
);
addMessage(
`Clarification accepted: +${evaluation.refusal ? 8 : 10} points.`
);
}
return;
}
if (
state.phase === "ground-source" ||
state.phase === "ground-destination"
) {
addMessage(
"I still need a grounded pointer. Click the object directly in the scene; I will not infer the target from another ambiguous description."
);
}
}
// ---------------------------------------------------------------------
// Grounded action logic
// ---------------------------------------------------------------------
function handleObjectClick(object) {
const task = currentTask();
if (state.phase === "done") {
showToast("This puzzle is complete. Select the next puzzle.");
return;
}
if (state.phase === "clarify") {
addMessage(
"Before I move anything, I need one disambiguating question. Please ask which visible object you mean."
);
showToast("Clarify first. I will not guess.");
return;
}
if (state.phase === "ground-source") {
if (!task.source.includes(object.id)) {
addMessage(
"That object does not match the possible source objects for this request. I will leave it untouched."
);
showToast("Not a valid source match.");
return;
}
state.selectedSource = object.id;
if (task.stages === 1) {
completeTask();
} else {
state.phase = "ground-destination";
state.score += 8;
updatePhaseUI();
updateStats();
addMessage(
`Source grounded: ${object.label}. Now point to the destination object.`,
"reward"
);
addMessage("Source grounding accepted: +8 points.");
showToast("Source grounded. Now choose the destination.");
}
return;
}
if (state.phase === "ground-destination") {
if (!task.destination.includes(object.id)) {
addMessage(
"That is not one of the requested destination objects. I will not place the source incorrectly."
);
showToast("Not a valid destination match.");
return;
}
state.selectedDestination = object.id;
completeTask();
}
}
const animations = [];
let robotWaveUntil = 0;
function animateObject(object, end, duration = 900, rotation = 0) {
animations.push({
object,
start: object.group.position.clone(),
end: end.clone(),
startTime: performance.now(),
duration,
startRotation: object.group.rotation.clone(),
rotation
});
}
function completeTask() {
const task = currentTask();
const source = objects.get(state.selectedSource);
const destination = state.selectedDestination
? objects.get(state.selectedDestination)
: null;
state.phase = "done";
state.completed[state.taskIndex] = true;
const actionPoints = task.stages === 2 ? 22 : 30;
state.score += actionPoints;
robotWaveUntil = performance.now() + 1300;
updatePhaseUI();
updateStats();
if (task.action === "bring") {
const end = new THREE.Vector3(1.08, 1.15, 2.45);
animateObject(source, end, 950, Math.PI * 0.5);
addMessage(
`Grounded action complete: I brought the ${source.label.toLowerCase()} to the tray. +${actionPoints} points.`,
"reward"
);
} else if (task.action === "stack") {
const end = destination.group.position.clone();
end.y += destination.height + 0.35;
animateObject(source, end, 900, Math.PI * 0.25);
addMessage(
`Grounded action complete: I placed the ${source.label.toLowerCase()} on the ${destination.label.toLowerCase()}. +${actionPoints} points.`,
"reward"
);
} else if (task.action === "beside") {
const end = destination.group.position.clone();
end.x += 0.92;
end.z += 0.18;
animateObject(source, end, 900, Math.PI * 0.25);
addMessage(
`Grounded action complete: I moved the ${source.label.toLowerCase()} beside the ${destination.label.toLowerCase()}. +${actionPoints} points.`,
"reward"
);
}
showToast(`Task complete · +${actionPoints} grounded action points`);
}
// ---------------------------------------------------------------------
// Task list and controls
// ---------------------------------------------------------------------
tasks.forEach((task, index) => {
const button = document.createElement("button");
button.className = "task-button";
button.innerHTML = `
<span class="task-number">${String(index + 1).padStart(2, "0")}</span>
<span>
<span class="task-name">${task.title}</span>
<span class="task-status">Queued</span>
</span>
<span class="task-check"></span>
`;
button.addEventListener("click", () => selectTask(index));
taskList.appendChild(button);
});
sendButton.addEventListener("click", sendMessage);
input.addEventListener("keydown", (event) => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
sendMessage();
}
});
hintButton.addEventListener("click", () => {
addMessage(
`Example clarification: “${currentTask().example}”`,
"system"
);
input.focus();
});
refuseButton.addEventListener("click", () => {
if (state.phase === "clarify") {
input.value =
"I’m not sure. Can you point to the exact object or give its color and location?";
sendMessage();
} else {
addMessage(
"Okay. I will not act without sufficient evidence. Please point to the exact source or destination in the scene."
);
showToast("No action taken. More grounding is required.");
}
});
nextButton.addEventListener("click", () => {
if (state.taskIndex === tasks.length - 1) {
resetLab();
} else {
selectTask(state.taskIndex + 1);
}
});
resetButton.addEventListener("click", resetLab);
function resetLab() {
state.score = 0;
state.completed.fill(false);
resetObjectPositions();
selectTask(0);
showToast("Lab reset.");
}
cameraResetButton.addEventListener("click", resetCamera);
// ---------------------------------------------------------------------
// Labels and render loop
// ---------------------------------------------------------------------
const projected = new THREE.Vector3();
const labelWorld = new THREE.Vector3();
function updateWorldLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
for (const object of objects.values()) {
labelWorld.set(0, object.height + 0.24, 0);
object.group.localToWorld(labelWorld);
projected.copy(labelWorld).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
object.labelElement.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
object.labelElement.style.left = `${x}px`;
object.labelElement.style.top = `${y}px`;
}
}
robot.getWorldPosition(labelWorld);
labelWorld.y += 3.25;
projected.copy(labelWorld).project(camera);
const bx = (projected.x * 0.5 + 0.5) * width;
const by = (-projected.y * 0.5 + 0.5) * height;
robotBubble.style.left = `${bx}px`;
robotBubble.style.top = `${by}px`;
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
function easeInOutCubic(value) {
return value < 0.5
? 4 * value * value * value
: 1 - Math.pow(-2 * value + 2, 3) / 2;
}
function animate(now) {
requestAnimationFrame(animate);
for (let index = animations.length - 1; index >= 0; index--) {
const animation = animations[index];
const progress = THREE.MathUtils.clamp(
(now - animation.startTime) / animation.duration,
0,
1
);
const eased = easeInOutCubic(progress);
animation.object.group.position.lerpVectors(
animation.start,
animation.end,
eased
);
animation.object.group.rotation.y =
animation.startRotation.y + eased * animation.rotation;
if (progress >= 1) {
animation.object.group.position.copy(animation.end);
animations.splice(index, 1);
}
}
if (now < robotWaveUntil) {
const phase = (robotWaveUntil - now) / 1300;
armRight.rotation.z = -0.55 - Math.sin(now * 0.018) * 0.35 * phase;
} else {
armRight.rotation.z = THREE.MathUtils.lerp(
armRight.rotation.z,
0,
0.08
);
}
if (hoverHelper.visible) {
hoverHelper.update();
}
updateWorldLabels();
renderer.render(scene, camera);
}
selectTask(0);
requestAnimationFrame(animate);
document.querySelector("#loading").style.display = "none";
</script>
</body>
</html>
```