Prompt specification
Build or customize the Three.js 3D scene "Silent Cooperative Search Scene" 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: Silent Cooperative Search Scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/silent-cooperative-search-scene-x3383
Scene Description Prompt:
Two voxel characters search a room using gaze and pointing, with partitions hiding different objects from each. One HTML file with Three.js from cdnjs.
Working Three.js Starter (silent-cooperative-search-scene-x3383):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Parallel Room — Voxel Search</title>
<style>
:root {
--bg: #09111d;
--panel: rgba(13, 23, 38, 0.88);
--line: rgba(155, 190, 220, 0.16);
--text: #edf7ff;
--muted: #8fa4ba;
--cyan: #67e8f9;
--yellow: #ffd166;
--red: #ff6b7a;
--green: #73efb0;
}
* { 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;
}
button { font: inherit; }
#app, #scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene {
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 5px 8px;
border: 1px solid rgba(180, 215, 240, 0.17);
border-radius: 7px;
background: rgba(8, 17, 29, 0.78);
box-shadow: 0 5px 16px rgba(0, 0, 0, 0.2);
color: #d8e8f5;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.045em;
white-space: nowrap;
will-change: transform;
}
.world-label.object {
border-color: rgba(103, 232, 249, 0.22);
}
.world-label.character-a {
color: #b8f7ff;
border-color: rgba(103, 232, 249, 0.4);
}
.world-label.character-b {
color: #ffe1a5;
border-color: rgba(255, 209, 102, 0.4);
}
.world-label.partition {
color: #a7b9c9;
font-size: 9px;
letter-spacing: 0.12em;
}
.header {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 4px;
font-size: clamp(22px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
color: var(--muted);
font-size: 12px;
}
.toolbar {
position: fixed;
top: 20px;
right: 22px;
z-index: 6;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: min(620px, 62vw);
}
.toolbar button {
min-height: 36px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(12, 22, 36, 0.82);
color: #b8c9d9;
cursor: pointer;
transition: 160ms ease;
backdrop-filter: blur(12px);
}
.toolbar button:hover {
border-color: rgba(103, 232, 249, 0.5);
color: white;
transform: translateY(-1px);
}
.toolbar button.active {
border-color: rgba(103, 232, 249, 0.7);
background: rgba(38, 190, 218, 0.16);
color: #c9f9ff;
box-shadow: inset 0 0 0 1px rgba(103, 232, 249, 0.08);
}
.toolbar button.auto.active {
border-color: rgba(255, 209, 102, 0.75);
background: rgba(255, 209, 102, 0.14);
color: #ffe7a8;
}
.inspector {
position: fixed;
left: 22px;
bottom: 22px;
z-index: 5;
display: flex;
gap: 12px;
pointer-events: none;
}
.card {
width: 245px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(15px);
}
.card.a { border-top-color: rgba(103, 232, 249, 0.72); }
.card.b { border-top-color: rgba(255, 209, 102, 0.72); }
.card-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.dot {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 7px;
border-radius: 50%;
box-shadow: 0 0 13px currentColor;
}
.a .dot { color: var(--cyan); background: var(--cyan); }
.b .dot { color: var(--yellow); background: var(--yellow); }
.found {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
}
.row {
display: grid;
grid-template-columns: 55px 1fr;
gap: 8px;
align-items: baseline;
margin: 8px 0;
font-size: 11px;
}
.row-label {
color: var(--muted);
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.target-name {
overflow: hidden;
color: #e7f3fc;
font-weight: 650;
text-overflow: ellipsis;
white-space: nowrap;
}
.state {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
.state.clear { color: var(--green); }
.state.blocked { color: var(--red); }
.legend {
position: fixed;
right: 22px;
bottom: 24px;
z-index: 5;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(12, 22, 36, 0.76);
color: var(--muted);
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(12px);
}
.legend div { margin: 3px 0; }
.legend .cyan { color: var(--cyan); }
.legend .yellow { color: var(--yellow); }
.legend .red { color: var(--red); }
.help {
position: fixed;
left: 50%;
bottom: 25px;
z-index: 4;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(160, 200, 225, 0.12);
border-radius: 999px;
background: rgba(9, 17, 29, 0.65);
color: #8298ad;
font-size: 10px;
pointer-events: none;
white-space: nowrap;
backdrop-filter: blur(10px);
}
#toast {
position: fixed;
left: 50%;
top: 86px;
z-index: 9;
transform: translate(-50%, -10px);
padding: 10px 15px;
border: 1px solid rgba(103, 232, 249, 0.3);
border-radius: 9px;
background: rgba(10, 24, 38, 0.94);
color: #d8f9ff;
font-size: 11px;
font-weight: 650;
opacity: 0;
pointer-events: none;
transition: 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: var(--bg);
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
@media (max-width: 900px) {
.toolbar {
top: 105px;
left: 20px;
right: 20px;
max-width: none;
justify-content: flex-start;
}
.legend { display: none; }
.help { display: none; }
.inspector {
right: 16px;
bottom: 16px;
left: 16px;
}
.card { width: 50%; min-width: 0; }
}
@media (max-width: 580px) {
.header { left: 16px; top: 16px; }
.subtitle { display: none; }
.toolbar { top: 82px; left: 12px; right: 12px; }
.toolbar button { padding: 0 9px; font-size: 11px; }
.card {
width: 50%;
padding: 10px;
}
.card-title { font-size: 10px; }
.row { grid-template-columns: 43px 1fr; gap: 4px; font-size: 10px; }
.found { display: none; }
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene"></canvas>
<div id="labels"></div>
<header class="header">
<div class="eyebrow">Parallel Room / Voxel Search</div>
<h1>Different views. Different finds.</h1>
<div class="subtitle">Two agents scan the same room from opposite sides.</div>
</header>
<nav class="toolbar" aria-label="Targeting controls">
<button class="channel active" data-channel="0">A · Gaze</button>
<button class="channel" data-channel="1">A · Point</button>
<button class="channel" data-channel="2">B · Gaze</button>
<button class="channel" data-channel="3">B · Point</button>
<button id="searchButton">Auto search</button>
<button id="resetButton">Reset</button>
<button id="cameraButton">Camera</button>
</nav>
<section class="inspector">
<article class="card a">
<div class="card-title">
<span><i class="dot"></i>Agent A</span>
<span class="found" id="foundA">0 / 6 found</span>
</div>
<div class="row">
<span class="row-label">Gaze</span>
<span>
<span class="target-name" id="gazeTargetA">—</span>
<span class="state" id="gazeStateA">No target</span>
</span>
</div>
<div class="row">
<span class="row-label">Point</span>
<span>
<span class="target-name" id="pointTargetA">—</span>
<span class="state" id="pointStateA">No target</span>
</span>
</div>
</article>
<article class="card b">
<div class="card-title">
<span><i class="dot"></i>Agent B</span>
<span class="found" id="foundB">0 / 6 found</span>
</div>
<div class="row">
<span class="row-label">Gaze</span>
<span>
<span class="target-name" id="gazeTargetB">—</span>
<span class="state" id="gazeStateB">No target</span>
</span>
</div>
<div class="row">
<span class="row-label">Point</span>
<span>
<span class="target-name" id="pointTargetB">—</span>
<span class="state" id="pointStateB">No target</span>
</span>
</div>
</article>
</section>
<div class="legend">
<div><span class="cyan">━━</span> Gaze / visible</div>
<div><span class="yellow">━━</span> Point / visible</div>
<div><span class="red">━━</span> Blocked by partition</div>
</div>
<div class="help">Drag to orbit · Right-drag to pan · Scroll to zoom · Click an object to assign it</div>
<div id="toast"></div>
</div>
<div id="loading">Building room…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const labelLayer = document.querySelector("#labels");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09111d);
scene.fog = new THREE.Fog(0x09111d, 24, 42);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xb9ddff, 0x18202c, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3dc, 3.1);
sun.position.set(-7, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -11;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x5cd9ff, 1.15);
rimLight.position.set(9, 6, -8);
scene.add(rimLight);
const room = new THREE.Group();
scene.add(room);
// -------------------------------------------------------------------------
// Room floor and walls
// -------------------------------------------------------------------------
const floorBase = new THREE.Mesh(
new THREE.BoxGeometry(18.4, 0.22, 12.4),
new THREE.MeshStandardMaterial({ color: 0x182434, roughness: 0.93 })
);
floorBase.position.y = -0.2;
floorBase.receiveShadow = true;
room.add(floorBase);
const tileGeometry = new THREE.BoxGeometry(0.965, 0.11, 0.965);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
vertexColors: true
});
const tileCountX = 18;
const tileCountZ = 12;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
const matrix = new THREE.Matrix4();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let z = 0; z < tileCountZ; z++) {
for (let x = 0; x < tileCountX; x++) {
matrix.makeTranslation(
-8.5 + x,
-0.07,
-5.5 + z
);
tiles.setMatrixAt(tileIndex, matrix);
const checker = (x + z) % 2 === 0;
tileColor.setHSL(
0.57,
0.19,
checker ? 0.145 + Math.random() * 0.018 : 0.12 + Math.random() * 0.015
);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.receiveShadow = true;
room.add(tiles);
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0x27384a,
roughness: 0.8
});
function addWall(width, height, depth, x, y, z) {
const wall = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
wallMaterial
);
wall.position.set(x, y, z);
wall.castShadow = true;
wall.receiveShadow = true;
room.add(wall);
return wall;
}
addWall(18.5, 0.95, 0.22, 0, 0.475, -6.08);
addWall(0.22, 0.8, 12.2, -9.08, 0.4, 0);
// -------------------------------------------------------------------------
// Labels
// -------------------------------------------------------------------------
function createLabel(text, className = "") {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
labelLayer.appendChild(element);
return element;
}
const labelEntries = [];
function addLabel(text, className, position) {
const element = createLabel(text, className);
const entry = { element, position };
labelEntries.push(entry);
return entry;
}
// -------------------------------------------------------------------------
// Partitions: these are the actual line-of-sight occluders
// -------------------------------------------------------------------------
const occluders = [];
const partitionSpecs = [
{
name: "Partition 01",
size: [0.3, 2.7, 3.4],
position: [-1.2, 1.35, 1.7],
color: 0x41586c
},
{
name: "Partition 02",
size: [0.3, 2.7, 3.6],
position: [1.2, 1.35, -1.8],
color: 0x4c526c
},
{
name: "Partition 03",
size: [2.8, 2.4, 0.3],
position: [0, 1.2, 0],
color: 0x3e5965
}
];
partitionSpecs.forEach((spec, index) => {
const material = new THREE.MeshStandardMaterial({
color: spec.color,
roughness: 0.82,
metalness: 0.05
});
const partition = new THREE.Mesh(
new THREE.BoxGeometry(...spec.size),
material
);
partition.position.set(...spec.position);
partition.castShadow = true;
partition.receiveShadow = true;
partition.userData.partition = true;
partition.userData.name = spec.name;
room.add(partition);
occluders.push(partition);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(partition.geometry),
new THREE.LineBasicMaterial({
color: 0x90a9bb,
transparent: true,
opacity: 0.28
})
);
partition.add(edges);
const top = new THREE.Mesh(
new THREE.BoxGeometry(
spec.size[0] + 0.04,
0.07,
spec.size[2] + 0.04
),
new THREE.MeshStandardMaterial({
color: 0x8ca5b5,
roughness: 0.55
})
);
top.position.y = spec.size[1] / 2 + 0.01;
top.castShadow = true;
partition.add(top);
addLabel(
`P${String(index + 1).padStart(2, "0")}`,
"partition",
new THREE.Vector3(
spec.position[0],
spec.position[1] + spec.size[1] / 2 + 0.36,
spec.position[2]
)
);
});
// -------------------------------------------------------------------------
// Search objects
// -------------------------------------------------------------------------
const objectConfigs = [
{ name: "Red Stack", color: 0xff5f6d, x: -6.3, z: 3.6, y: 1.00, type: "stack" },
{ name: "Blue Cube", color: 0x5790ff, x: -3.2, z: 4.15, y: 0.78, type: "cube" },
{ name: "Gold Stack", color: 0xffc857, x: 3.0, z: 4.25, y: 1.05, type: "stack" },
{ name: "Green Totem", color: 0x68df87, x: -6.3, z: -3.6, y: 1.10, type: "totem" },
{ name: "Purple Cube", color: 0xb477ff, x: -2.8, z: -3.7, y: 0.78, type: "cube" },
{ name: "Cyan Stack", color: 0x48d8e8, x: 3.2, z: -3.55, y: 1.05, type: "stack" }
];
const objects = [];
const clickableMeshes = [];
function makeVoxelMaterial(color) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.12,
emissive: new THREE.Color(color).multiplyScalar(0.08)
});
}
function addVoxel(group, size, position, material, objectIndex) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(...size),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.objectIndex = objectIndex;
group.add(mesh);
clickableMeshes.push(mesh);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.11
})
);
mesh.add(outline);
return mesh;
}
objectConfigs.forEach((config, index) => {
const group = new THREE.Group();
group.position.set(config.x, 0, config.z);
room.add(group);
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(0.95, 0.16, 0.95),
new THREE.MeshStandardMaterial({
color: 0x253548,
roughness: 0.72,
metalness: 0.16
})
);
pedestal.position.y = 0.08;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.64, 0.027, 8, 36),
new THREE.MeshBasicMaterial({
color: 0x334155,
transparent: true,
opacity: 0.9
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.18;
group.add(ring);
const material = makeVoxelMaterial(config.color);
if (config.type === "stack") {
addVoxel(group, [0.68, 0.42, 0.68], [0, 0.42, 0], material, index);
addVoxel(group, [0.56, 0.42, 0.56], [0.04, 0.82, 0], material, index);
addVoxel(group, [0.42, 0.42, 0.42], [-0.04, 1.22, 0.03], material, index);
} else if (config.type === "cube") {
addVoxel(group, [0.86, 0.86, 0.86], [0, 0.63, 0], material, index);
addVoxel(group, [0.34, 0.22, 0.34], [0, 1.14, 0], material, index);
} else {
addVoxel(group, [0.72, 0.40, 0.72], [0, 0.40, 0], material, index);
addVoxel(group, [0.56, 0.52, 0.56], [0.05, 0.83, 0], material, index);
addVoxel(group, [0.38, 0.40, 0.38], [-0.05, 1.28, 0.02], material, index);
}
const target = new THREE.Vector3(config.x, config.y, config.z);
const object = {
index,
name: config.name,
group,
target,
ring,
material
};
objects.push(object);
addLabel(
config.name,
"object",
new THREE.Vector3(config.x, config.y + 0.82, config.z)
);
});
// -------------------------------------------------------------------------
// Voxel agents
// -------------------------------------------------------------------------
function createCharacter({ name, color, x, z, labelClass }) {
const root = new THREE.Group();
root.position.set(x, 0, z);
room.add(root);
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.56,
metalness: 0.08
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x182333,
roughness: 0.72
});
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xf0c7a5,
roughness: 0.7
});
function bodyBox(size, position, material = bodyMaterial) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(...size),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
root.add(mesh);
return mesh;
}
bodyBox([0.22, 0.42, 0.25], [-0.18, 0.25, 0], darkMaterial);
bodyBox([0.22, 0.42, 0.25], [ 0.18, 0.25, 0], darkMaterial);
bodyBox([0.66, 0.72, 0.42], [0, 0.79, 0]);
bodyBox([0.54, 0.18, 0.46], [0, 1.12, 0], darkMaterial);
const headYaw = new THREE.Group();
headYaw.position.set(0, 1.34, 0);
root.add(headYaw);
const headPitch = new THREE.Group();
headYaw.add(headPitch);
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.55, 0.56),
skinMaterial
);
head.position.y = 0.22;
head.castShadow = true;
headPitch.add(head);
const hair = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 0.13, 0.59),
darkMaterial
);
hair.position.set(0, 0.51, -0.015);
hair.castShadow = true;
headPitch.add(hair);
const eyeMaterial = new THREE.MeshBasicMaterial({ color: 0x111b28 });
const eyeLeft = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.075, 0.035),
eyeMaterial
);
eyeLeft.position.set(-0.13, 0.25, 0.292);
headPitch.add(eyeLeft);
const eyeRight = eyeLeft.clone();
eyeRight.position.x = 0.13;
headPitch.add(eyeRight);
// Idle arm.
const idleArm = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.62, 0.18),
bodyMaterial
);
idleArm.position.set(-0.44, 0.83, 0);
idleArm.castShadow = true;
root.add(idleArm);
// Pointing arm: yaw group -> pitch group -> arm.
const pointYaw = new THREE.Group();
pointYaw.position.set(0.44, 1.04, 0);
root.add(pointYaw);
const pointPitch = new THREE.Group();
pointYaw.add(pointPitch);
const pointArm = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.16, 0.58),
bodyMaterial
);
pointArm.position.z = 0.25;
pointArm.castShadow = true;
pointPitch.add(pointArm);
const hand = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.19, 0.19),
skinMaterial
);
hand.position.z = 0.58;
hand.castShadow = true;
pointPitch.add(hand);
const label = addLabel(
name,
labelClass,
new THREE.Vector3(x, 2.22, z)
);
return {
name,
root,
headYaw,
headPitch,
eye: eyeRight,
pointYaw,
pointPitch,
hand,
idleArm,
label,
gazeTarget: null,
pointTarget: null,
gazeTrace: null,
pointTrace: null,
search: {
active: false,
index: 0,
phase: 0,
evaluated: false,
found: new Set()
}
};
}
const agentA = createCharacter({
name: "AGENT A",
color: 0x38c9df,
x: -5.2,
z: 1.2,
labelClass: "character-a"
});
const agentB = createCharacter({
name: "AGENT B",
color: 0xf2b84b,
x: 5.2,
z: -1.4,
labelClass: "character-b"
});
const agents = [agentA, agentB];
// -------------------------------------------------------------------------
// Gaze cones and pointing beams
// -------------------------------------------------------------------------
const UP = new THREE.Vector3(0, 1, 0);
function createBeam(radius, color, opacity = 0.9) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 10),
material
);
mesh.visible = false;
mesh.renderOrder = 3;
scene.add(mesh);
return mesh;
}
function createGazeCone() {
const material = new THREE.MeshBasicMaterial({
color: 0x67e8f9,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false
});
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(0, 0.48, 1, 24, 1, true),
material
);
mesh.visible = false;
mesh.renderOrder = 2;
scene.add(mesh);
return mesh;
}
agentA.gazeBeam = createBeam(0.026, 0x67e8f9, 0.82);
agentB.gazeBeam = createBeam(0.026, 0x67e8f9, 0.82);
agentA.pointBeam = createBeam(0.018, 0xffd166, 0.95);
agentB.pointBeam = createBeam(0.018, 0xffd166, 0.95);
agentA.gazeCone = createGazeCone();
agentB.gazeCone = createGazeCone();
function setBeam(mesh, start, end, clearColor, blockedColor) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
if (length < 0.001) {
mesh.visible = false;
return;
}
direction.normalize();
mesh.visible = true;
mesh.position.copy(start).addScaledVector(direction, length / 2);
mesh.quaternion.setFromUnitVectors(UP, direction);
mesh.scale.set(1, length, 1);
mesh.material.color.setHex(clearColor);
}
function setBlockedBeamColor(mesh, blocked) {
mesh.material.color.setHex(blocked ? 0xff6b7a : 0xffd166);
}
function setGazeCone(mesh, start, end, blocked) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
if (length < 0.001) {
mesh.visible = false;
return;
}
direction.normalize();
// The cone's apex is its +Y end, so +Y is aligned opposite the ray.
mesh.visible = true;
mesh.position.copy(start).addScaledVector(direction, length / 2);
mesh.quaternion.setFromUnitVectors(UP, direction.clone().negate());
mesh.scale.set(1, length, 1);
mesh.material.color.setHex(blocked ? 0xff6b7a : 0x67e8f9);
}
// -------------------------------------------------------------------------
// Line-of-sight tracing
// -------------------------------------------------------------------------
const traceRaycaster = new THREE.Raycaster();
const traceDirection = new THREE.Vector3();
function traceVisibility(source, targetIndex) {
if (targetIndex === null || targetIndex === undefined) return null;
const target = objects[targetIndex].target;
traceDirection.subVectors(target, source);
const targetDistance = traceDirection.length();
traceDirection.normalize();
traceRaycaster.set(source, traceDirection);
traceRaycaster.near = 0;
traceRaycaster.far = targetDistance + 0.2;
const hits = traceRaycaster.intersectObjects(occluders, false);
const hit = hits.find(candidate => candidate.distance < targetDistance - 0.1);
if (hit) {
return {
clear: false,
end: hit.point.clone(),
blocker: hit.object.userData.name,
targetDistance
};
}
return {
clear: true,
end: target.clone(),
blocker: null,
targetDistance
};
}
// -------------------------------------------------------------------------
// Agent animation and target updates
// -------------------------------------------------------------------------
const tempEye = new THREE.Vector3();
const tempHand = new THREE.Vector3();
const targetDirection = new THREE.Vector3();
function updateAgent(agent, time) {
agent.root.updateMatrixWorld(true);
let gazeYaw = Math.sin(time * 0.7 + agent.root.position.x) * 0.32;
let gazePitch = 0;
if (agent.gazeTarget !== null) {
const target = objects[agent.gazeTarget].target;
targetDirection.set(
target.x - agent.root.position.x,
0,
target.z - agent.root.position.z
);
gazeYaw = Math.atan2(targetDirection.x, targetDirection.z);
const horizontal = Math.hypot(
target.x - agent.root.position.x,
target.z - agent.root.position.z
);
gazePitch = Math.atan2(
target.y - 1.58,
horizontal
);
}
agent.headYaw.rotation.y = gazeYaw;
agent.headPitch.rotation.x = -gazePitch;
agent.root.updateMatrixWorld(true);
agent.eye.getWorldPosition(tempEye);
agent.gazeTrace = traceVisibility(tempEye, agent.gazeTarget);
if (agent.gazeTrace) {
setBeam(
agent.gazeBeam,
tempEye,
agent.gazeTrace.end,
0x67e8f9,
0xff6b7a
);
if (!agent.gazeTrace.clear) {
agent.gazeBeam.material.color.setHex(0xff6b7a);
}
setGazeCone(
agent.gazeCone,
tempEye,
agent.gazeTrace.end,
!agent.gazeTrace.clear
);
} else {
agent.gazeBeam.visible = false;
agent.gazeCone.visible = false;
}
if (agent.pointTarget !== null) {
const target = objects[agent.pointTarget].target;
const yaw = Math.atan2(
target.x - agent.root.position.x,
target.z - agent.root.position.z
);
const horizontal = Math.hypot(
target.x - agent.root.position.x,
target.z - agent.root.position.z
);
const pitch = Math.atan2(target.y - 1.04, horizontal);
agent.pointYaw.rotation.y = yaw;
agent.pointPitch.rotation.x = -pitch;
} else {
agent.pointYaw.rotation.y = 0;
agent.pointPitch.rotation.x = 1.35;
}
agent.root.updateMatrixWorld(true);
agent.hand.getWorldPosition(tempHand);
agent.pointTrace = traceVisibility(tempHand, agent.pointTarget);
if (agent.pointTrace) {
setBeam(
agent.pointBeam,
tempHand,
agent.pointTrace.end,
0xffd166,
0xff6b7a
);
if (!agent.pointTrace.clear) {
agent.pointBeam.material.color.setHex(0xff6b7a);
}
} else {
agent.pointBeam.visible = false;
}
agent.idleArm.rotation.z = Math.sin(time * 2 + agent.root.position.x) * 0.035;
agent.root.position.y = Math.sin(time * 2.2 + agent.root.position.z) * 0.008;
}
// -------------------------------------------------------------------------
// UI and target assignment
// -------------------------------------------------------------------------
const channelButtons = [...document.querySelectorAll(".channel")];
const channels = [
{ agent: 0, type: "gaze", title: "Agent A gaze" },
{ agent: 0, type: "point", title: "Agent A pointing" },
{ agent: 1, type: "gaze", title: "Agent B gaze" },
{ agent: 1, type: "point", title: "Agent B pointing" }
];
let activeChannel = 0;
function setActiveChannel(index) {
activeChannel = index;
channelButtons.forEach((button, buttonIndex) => {
button.classList.toggle("active", buttonIndex === index);
});
}
channelButtons.forEach((button, index) => {
button.addEventListener("click", () => setActiveChannel(index));
});
const toast = document.querySelector("#toast");
let toastTimer;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 1600);
}
function stopSearch() {
agents.forEach(agent => {
agent.search.active = false;
});
updateSearchButton();
}
function assignTarget(objectIndex) {
stopSearch();
const channel = channels[activeChannel];
const agent = agents[channel.agent];
if (channel.type === "gaze") {
agent.gazeTarget = objectIndex;
} else {
agent.pointTarget = objectIndex;
}
showToast(
`${channel.title} → ${objects[objectIndex].name}`
);
}
// -------------------------------------------------------------------------
// Autonomous search
// -------------------------------------------------------------------------
const searchButton = document.querySelector("#searchButton");
function updateSearchButton() {
const searching = agents.some(agent => agent.search.active);
searchButton.textContent = searching ? "Stop search" : "Auto search";
searchButton.classList.toggle("active", searching);
}
function startSearch() {
agents.forEach(agent => {
agent.search.active = true;
agent.search.index = 0;
agent.search.phase = 0;
agent.search.evaluated = false;
agent.search.found.clear();
agent.gazeTarget = 0;
agent.pointTarget = null;
});
updateSearchButton();
showToast("Both agents are scanning the room");
}
searchButton.addEventListener("click", () => {
const searching = agents.some(agent => agent.search.active);
if (searching) {
stopSearch();
showToast("Search paused");
} else {
startSearch();
}
});
function updateSearch(dt) {
agents.forEach(agent => {
if (!agent.search.active) return;
agent.search.phase += dt;
agent.gazeTarget = agent.search.index;
if (agent.search.phase >= 1.8) {
agent.search.index = (agent.search.index + 1) % objects.length;
agent.search.phase = 0;
agent.search.evaluated = false;
agent.gazeTarget = agent.search.index;
}
});
}
function evaluateSearch() {
agents.forEach(agent => {
if (!agent.search.active || agent.search.evaluated) return;
if (agent.search.phase >= 0.78) {
agent.search.evaluated = true;
if (agent.gazeTrace?.clear) {
agent.search.found.add(agent.search.index);
agent.pointTarget = agent.search.index;
}
}
});
}
// -------------------------------------------------------------------------
// Inspector readouts
// -------------------------------------------------------------------------
function getElement(id) {
return document.getElementById(id);
}
function updateInspector() {
agents.forEach((agent, index) => {
const prefix = index === 0 ? "A" : "B";
const gazeTargetElement = getElement(`gazeTarget${prefix}`);
const pointTargetElement = getElement(`pointTarget${prefix}`);
const gazeStateElement = getElement(`gazeState${prefix}`);
const pointStateElement = getElement(`pointState${prefix}`);
const foundElement = getElement(`found${prefix}`);
gazeTargetElement.textContent =
agent.gazeTarget === null ? "—" : objects[agent.gazeTarget].name;
pointTargetElement.textContent =
agent.pointTarget === null ? "—" : objects[agent.pointTarget].name;
if (agent.gazeTrace) {
gazeStateElement.textContent = agent.gazeTrace.clear
? "Visible"
: `Blocked by ${agent.gazeTrace.blocker}`;
gazeStateElement.className =
`state ${agent.gazeTrace.clear ? "clear" : "blocked"}`;
} else {
gazeStateElement.textContent = "No target";
gazeStateElement.className = "state";
}
if (agent.pointTrace) {
pointStateElement.textContent = agent.pointTrace.clear
? "Visible"
: `Blocked by ${agent.pointTrace.blocker}`;
pointStateElement.className =
`state ${agent.pointTrace.clear ? "clear" : "blocked"}`;
} else {
pointStateElement.textContent = "No target";
pointStateElement.className = "state";
}
foundElement.textContent = `${agent.search.found.size} / ${objects.length} found`;
});
objects.forEach((object, index) => {
const usedAsGaze = agents.some(agent => agent.gazeTarget === index);
const usedAsPoint = agents.some(agent => agent.pointTarget === index);
object.ring.visible = usedAsGaze || usedAsPoint;
object.ring.material.color.setHex(
usedAsGaze ? 0x67e8f9 : usedAsPoint ? 0xffd166 : 0x334155
);
});
}
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.65, 0);
const cameraState = {
radius: 22,
theta: 0.67,
phi: 0.8
};
function resetCamera() {
cameraTarget.set(0, 0.65, 0);
cameraState.radius = 22;
cameraState.theta = 0.67;
cameraState.phi = 0.8;
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
document.querySelector("#cameraButton").addEventListener("click", () => {
resetCamera();
showToast("Camera reset");
});
const drag = {
active: false,
button: 0,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
canvas.addEventListener("pointerdown", event => {
drag.active = true;
drag.button = event.button;
drag.startX = drag.lastX = event.clientX;
drag.startY = drag.lastY = event.clientY;
drag.moved = false;
canvas.setPointerCapture(event.pointerId);
});
const cameraForward = new THREE.Vector3();
const cameraRight = new THREE.Vector3();
canvas.addEventListener("pointermove", event => {
if (drag.active) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
const totalDistance = Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
);
if (totalDistance > 4) drag.moved = true;
if (drag.button === 0) {
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.004,
0.35,
1.35
);
} else if (drag.button === 2) {
camera.getWorldDirection(cameraForward);
cameraForward.y = 0;
cameraForward.normalize();
cameraRight.crossVectors(cameraForward, UP).normalize();
const panScale = cameraState.radius * 0.0012;
cameraTarget.addScaledVector(cameraRight, -dx * panScale);
cameraTarget.addScaledVector(cameraForward, dy * panScale);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -7, 7);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -5, 5);
}
drag.lastX = event.clientX;
drag.lastY = event.clientY;
return;
}
// Hover feedback.
const hover = pickObject(event);
canvas.style.cursor = hover ? "pointer" : "grab";
});
canvas.addEventListener("pointerup", event => {
const shouldSelect =
drag.active &&
drag.button === 0 &&
!drag.moved &&
Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
) < 5;
drag.active = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (shouldSelect) {
const hit = pickObject(event);
if (hit !== null) assignTarget(hit);
}
});
canvas.addEventListener("pointercancel", () => {
drag.active = false;
});
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
12,
32
);
}, { passive: false });
// -------------------------------------------------------------------------
// Object picking
// -------------------------------------------------------------------------
const pickRaycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickObject(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;
pickRaycaster.setFromCamera(pointer, camera);
const hits = pickRaycaster.intersectObjects(clickableMeshes, false);
return hits.length ? hits[0].object.userData.objectIndex : null;
}
// -------------------------------------------------------------------------
// Reset and keyboard shortcuts
// -------------------------------------------------------------------------
function resetScene() {
stopSearch();
agentA.gazeTarget = 2;
agentA.pointTarget = 0;
agentB.gazeTarget = 4;
agentB.pointTarget = 2;
agentA.search.found.clear();
agentB.search.found.clear();
setActiveChannel(0);
resetCamera();
showToast("Scene reset");
}
document.querySelector("#resetButton").addEventListener("click", resetScene);
window.addEventListener("keydown", event => {
if (event.key >= "1" && event.key <= "4") {
setActiveChannel(Number(event.key) - 1);
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
if (event.code === "Space") {
event.preventDefault();
searchButton.click();
}
});
// -------------------------------------------------------------------------
// Label projection
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
labelEntries.forEach(entry => {
projected.copy(entry.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.12 &&
Math.abs(projected.y) < 1.12;
entry.element.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
entry.element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
});
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateSearch(dt);
updateAgent(agentA, time);
updateAgent(agentB, time);
evaluateSearch();
updateCamera();
updateInspector();
updateLabels();
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
// Initial demonstration:
// A cannot see Gold Stack through P1; B cannot see Purple Cube through P2.
agentA.gazeTarget = 2;
agentA.pointTarget = 0;
agentB.gazeTarget = 4;
agentB.pointTarget = 2;
resetCamera();
updateCamera();
updateInspector();
document.querySelector("#loading").classList.add("hidden");
animate();
</script>
</body>
</html>
```