Prompt specification
Build or customize the Three.js 3D scene "Tree-directory exploration desk" 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: Tree-directory exploration desk
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/tree-directory-exploration-desk-x1899
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a voxel desk that represents a directory tree. A broad shelf holds three folder boxes; opening one reveals smaller boxes inside, while another is empty. Give every folder its own simple name and keep the hierarchy visible as boxes open. Animate a small cursor visiting children recursively, returning to each parent before moving to the next sibling. A narrow strip beside the desk records entry and return events. Pause briefly at the empty folder to show the stopping case. Use blocky wood shelves, colored folder tabs, and a front-facing camera that can also inspect the nesting from above.
Working Three.js Starter (tree-directory-exploration-desk-x1899):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Directory Desk</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d7c8aa;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
#hud {
position: fixed;
top: 18px;
left: 18px;
z-index: 10;
max-width: min(430px, calc(100vw - 36px));
padding: 15px 17px;
color: #f8f1df;
background: rgba(23, 31, 27, 0.88);
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 12px;
box-shadow: 0 12px 35px rgba(30, 24, 15, 0.22);
backdrop-filter: blur(8px);
pointer-events: none;
}
#hud h1 {
margin: 0 0 7px;
font-size: 17px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#hud p {
margin: 0;
color: #bbc7be;
font-size: 12px;
line-height: 1.5;
}
#status {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid rgba(255, 255, 255, 0.12);
color: #83e7d1;
font-size: 12px;
min-height: 18px;
}
#controls {
position: fixed;
left: 50%;
bottom: 20px;
z-index: 10;
display: flex;
gap: 8px;
padding: 8px;
transform: translateX(-50%);
background: rgba(23, 31, 27, 0.88);
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 12px;
box-shadow: 0 12px 35px rgba(30, 24, 15, 0.24);
backdrop-filter: blur(8px);
}
button {
min-width: 76px;
padding: 9px 12px;
border: 1px solid #53645a;
border-radius: 7px;
color: #f8f1df;
background: #34433a;
font: 600 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
button:hover {
background: #45594c;
transform: translateY(-1px);
}
button:active { transform: translateY(0); }
#play {
min-width: 92px;
color: #10251f;
background: #83e7d1;
border-color: #83e7d1;
}
#help {
position: fixed;
right: 18px;
bottom: 20px;
z-index: 10;
padding: 8px 11px;
color: #e8e3d7;
background: rgba(23, 31, 27, 0.74);
border-radius: 8px;
font-size: 11px;
line-height: 1.5;
text-align: right;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
color: #f8f1df;
background: #29342e;
font-size: 14px;
letter-spacing: 0.08em;
}
@media (max-width: 720px) {
#hud {
top: 10px;
left: 10px;
right: 10px;
max-width: none;
padding: 11px 13px;
}
#hud h1 { font-size: 14px; }
#hud p { display: none; }
#controls {
bottom: 10px;
width: calc(100% - 20px);
justify-content: center;
}
button {
flex: 1;
min-width: 0;
padding: 9px 6px;
font-size: 11px;
}
#help { display: none; }
}
</style>
</head>
<body>
<div id="hud">
<h1>Voxel Directory Desk</h1>
<p>
A depth-first traversal visits every nested folder, returns to each parent
before taking the next sibling, and pauses at the empty Archive.
</p>
<div id="status" aria-live="polite">Preparing traversal…</div>
</div>
<div id="controls">
<button id="play" type="button">Pause</button>
<button id="restart" type="button">Restart</button>
<button id="front" type="button">Front</button>
<button id="above" type="button">Above</button>
</div>
<div id="help">
Drag to orbit · Scroll to zoom<br>
Space: play / pause
</div>
<div id="loading">LOADING VOXEL DESK…</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 scene = new THREE.Scene();
scene.background = new THREE.Color(0xd7c8aa);
scene.fog = new THREE.Fog(0xd7c8aa, 28, 52);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const clock = new THREE.Clock();
const hemisphere = new THREE.HemisphereLight(
0xfff4d6,
0x5b6254,
2.1
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.1);
sun.position.set(-8, 17, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0006;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x9ecfff, 1.1);
rim.position.set(13, 8, -10);
scene.add(rim);
// -------------------------------------------------------------------------
// Small construction helpers
// -------------------------------------------------------------------------
function makeMaterial(color, roughness = 0.78) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0.02
});
}
function addBlock(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextPlane(
text,
width,
height,
options = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const ctx = canvas.getContext("2d");
ctx.fillStyle = options.background || "#f3e3bf";
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (options.border) {
ctx.strokeStyle = options.border;
ctx.lineWidth = 12;
ctx.strokeRect(6, 6, canvas.width - 12, canvas.height - 12);
}
ctx.fillStyle = options.color || "#26332c";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
let fontSize = options.fontSize || 66;
ctx.font = `700 ${fontSize}px ui-monospace, monospace`;
const measured = ctx.measureText(text).width;
const maximum = canvas.width - 54;
if (measured > maximum) {
fontSize *= maximum / measured;
ctx.font = `700 ${fontSize}px ui-monospace, monospace`;
}
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 3);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
mesh.renderOrder = 3;
return mesh;
}
// -------------------------------------------------------------------------
// Voxel desk
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 45),
makeMaterial(0xb8aa8d, 0.95)
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.52;
floor.receiveShadow = true;
scene.add(floor);
const desk = new THREE.Group();
scene.add(desk);
const darkWood = makeMaterial(0x704a2b);
const woodMaterials = [
makeMaterial(0xa8733d),
makeMaterial(0xb47c43),
makeMaterial(0x9d6936),
makeMaterial(0xba8249),
makeMaterial(0xa8733d),
makeMaterial(0xb98249)
];
// Thick shelf support and individual blocky top planks.
addBlock(
desk,
[17.8, 0.38, 6.9],
[0.4, 2.32, 0],
darkWood
);
for (let i = 0; i < 6; i++) {
addBlock(
desk,
[17.9, 0.25, 1.08],
[0.4, 2.64, -2.87 + i * 1.15],
woodMaterials[i]
);
}
const legPositions = [
[-7.65, -0.12, -2.45],
[-7.65, -0.12, 2.45],
[8.4, -0.12, -2.45],
[8.4, -0.12, 2.45]
];
for (const position of legPositions) {
addBlock(
desk,
[0.72, 4.75, 0.72],
position,
darkWood
);
}
addBlock(
desk,
[16.7, 0.38, 0.48],
[0.4, -1.72, -2.45],
darkWood
);
addBlock(
desk,
[16.7, 0.38, 0.48],
[0.4, -1.72, 2.45],
darkWood
);
const deskLabel = makeTextPlane(
"DIRECTORY TREE / VOXEL DESK",
4.2,
0.52,
{
background: "#513722",
border: "#bd8a50",
color: "#f4dfb7",
fontSize: 49
}
);
deskLabel.position.set(0.4, 2.31, 3.512);
desk.add(deskLabel);
const SHELF_TOP = 2.765;
// -------------------------------------------------------------------------
// Folder boxes
// -------------------------------------------------------------------------
const folders = [];
const folderBodyMaterial = makeMaterial(0xd3a564);
const folderSideMaterial = makeMaterial(0xbc8849);
const folderBaseMaterial = makeMaterial(0x9e6a35);
const paperMaterial = makeMaterial(0xf8edcf, 0.9);
function createFolderBox({
name,
width,
depth,
height,
color,
parent = null,
x = 0,
z = 0
}) {
const group = new THREE.Group();
const w = width;
const d = depth;
const h = height;
const wall = Math.max(0.09, w * 0.055);
const frontHeight = h * 0.68;
// Base.
addBlock(
group,
[w, 0.14, d],
[0, 0.07, 0],
folderBaseMaterial
);
// Tall back and sides; the lower front wall keeps nested boxes visible.
addBlock(
group,
[w, h, wall],
[0, h / 2, -d / 2 + wall / 2],
folderSideMaterial
);
addBlock(
group,
[wall, h, d - wall * 2],
[-w / 2 + wall / 2, h / 2, 0],
folderSideMaterial
);
addBlock(
group,
[wall, h, d - wall * 2],
[w / 2 - wall / 2, h / 2, 0],
folderSideMaterial
);
addBlock(
group,
[w, frontHeight, wall],
[0, frontHeight / 2, d / 2 - wall / 2],
folderBodyMaterial
);
// Hinged lid.
const lidPivot = new THREE.Group();
lidPivot.position.set(0, h, -d / 2);
group.add(lidPivot);
addBlock(
lidPivot,
[w, 0.12, d],
[0, 0, d / 2],
folderBodyMaterial
);
// A colored, blocky folder tab.
const tabMaterial = makeMaterial(color, 0.65);
const tabWidth = Math.min(0.9, w * 0.3);
const tabHeight = Math.max(0.27, h * 0.25);
addBlock(
group,
[tabWidth, tabHeight, 0.18],
[
-w / 2 + tabWidth / 2 + 0.16,
frontHeight + tabHeight / 2 - 0.03,
d / 2 - 0.02
],
tabMaterial
);
// Front-facing name plate.
const labelWidth = Math.min(w * 0.78, 2.95);
const labelHeight = THREE.MathUtils.clamp(
labelWidth / 3.25,
0.22,
0.52
);
const label = makeTextPlane(
name,
labelWidth,
labelHeight,
{
background: "#f5e8ca",
border: `#${new THREE.Color(color).getHexString()}`,
color: "#26332c",
fontSize: 61
}
);
label.position.set(
0,
frontHeight * 0.53,
d / 2 + 0.012
);
group.add(label);
// Children are placed into this content group.
const content = new THREE.Group();
content.position.y = 0.14;
group.add(content);
// Logical hover target for the animated cursor.
const anchor = new THREE.Object3D();
anchor.position.set(0, h + 0.38, 0.04);
group.add(anchor);
const folder = {
name,
group,
lidPivot,
content,
anchor,
openTarget: 0,
color
};
folders.push(folder);
if (parent) {
parent.folder.content.add(group);
group.position.set(x, 0.02, z);
} else {
scene.add(group);
group.position.set(x, SHELF_TOP, z);
}
return folder;
}
function addPaperStack(folder, count = 2) {
for (let i = 0; i < count; i++) {
const paper = addBlock(
folder.content,
[
Math.min(0.55, folder.group.children.length ? 0.52 : 0.52),
0.035,
0.36
],
[
(i - (count - 1) / 2) * 0.035,
0.035 + i * 0.04,
0.08
],
paperMaterial
);
paper.rotation.y = (i - 0.5) * 0.08;
}
}
function createNode(name, options) {
const folder = createFolderBox({
name,
width: options.w,
depth: options.d,
height: options.h,
color: options.color,
parent: options.parent || null,
x: options.x || 0,
z: options.z || 0
});
const node = {
name,
depth: options.parent ? options.parent.depth + 1 : 0,
parent: options.parent || null,
children: [],
folder,
empty: Boolean(options.empty)
};
folder.node = node;
if (node.parent) {
node.parent.children.push(node);
}
return node;
}
const projects = createNode("Projects", {
x: -5.25,
z: 0.35,
w: 4.25,
d: 3.4,
h: 1.9,
color: 0xe67e22
});
const builds = createNode("Builds", {
parent: projects,
x: -1.02,
z: -0.58,
w: 1.82,
d: 1.48,
h: 1.18,
color: 0x3b82f6
});
const finalFolder = createNode("Final", {
parent: builds,
x: 0,
z: 0,
w: 0.95,
d: 0.82,
h: 0.72,
color: 0xf4c542
});
addPaperStack(finalFolder.folder);
const drafts = createNode("Drafts", {
parent: projects,
x: 1.02,
z: 0.55,
w: 1.82,
d: 1.48,
h: 1.18,
color: 0x8b5cf6
});
const review = createNode("Review", {
parent: drafts,
x: 0,
z: 0,
w: 0.95,
d: 0.82,
h: 0.72,
color: 0xec4899
});
addPaperStack(review.folder);
const archive = createNode("Archive", {
x: 0,
z: 0.35,
w: 4.25,
d: 3.4,
h: 1.9,
color: 0x22c55e,
empty: true
});
// A plaque makes the stopping case unmistakable once Archive opens.
const emptyPlaque = makeTextPlane(
"EMPTY",
1.25,
0.39,
{
background: "#fee2e2",
border: "#dc2626",
color: "#991b1b",
fontSize: 70
}
);
emptyPlaque.rotation.x = -Math.PI / 2;
emptyPlaque.position.set(0, 0.012, 0.15);
archive.folder.content.add(emptyPlaque);
const files = createNode("Files", {
x: 5.25,
z: 0.35,
w: 4.25,
d: 3.4,
h: 1.9,
color: 0x14b8a6
});
const images = createNode("Images", {
parent: files,
x: -1.02,
z: -0.58,
w: 1.82,
d: 1.48,
h: 1.18,
color: 0x06b6d4
});
const logo = createNode("Logo", {
parent: images,
x: 0,
z: 0,
w: 0.95,
d: 0.82,
h: 0.72,
color: 0xf97316
});
addPaperStack(logo.folder);
const photos = createNode("Photos", {
parent: files,
x: 1.02,
z: 0.55,
w: 1.82,
d: 1.48,
h: 1.18,
color: 0x6366f1
});
const raw = createNode("Raw", {
parent: photos,
x: 0,
z: 0,
w: 0.95,
d: 0.82,
h: 0.72,
color: 0x84cc16
});
addPaperStack(raw.folder);
const roots = [projects, archive, files];
// -------------------------------------------------------------------------
// Physical event strip beside the desk
// -------------------------------------------------------------------------
const logCanvas = document.createElement("canvas");
logCanvas.width = 512;
logCanvas.height = 1024;
const logContext = logCanvas.getContext("2d");
const logTexture = new THREE.CanvasTexture(logCanvas);
logTexture.colorSpace = THREE.SRGBColorSpace;
const board = new THREE.Group();
board.position.set(10.9, 1.05, -1.05);
scene.add(board);
const boardFrameMaterial = makeMaterial(0x593b24);
const boardScreenMaterial = new THREE.MeshBasicMaterial({
map: logTexture,
toneMapped: false
});
addBlock(
board,
[3.25, 7.05, 0.24],
[0, 0, 0],
boardFrameMaterial
);
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(2.94, 6.65),
boardScreenMaterial
);
screen.position.z = 0.126;
board.add(screen);
for (const x of [-1.56, 1.56]) {
addBlock(
board,
[0.18, 7.22, 0.34],
[x, 0, 0.02],
darkWood
);
}
addBlock(
board,
[3.28, 0.18, 0.34],
[0, 3.54, 0.02],
darkWood
);
addBlock(
board,
[3.28, 0.18, 0.34],
[0, -3.54, 0.02],
darkWood
);
addBlock(
scene,
[0.32, 0.55, 0.5],
[10.9, -2.22, -1.05],
darkWood
);
const logEntries = [];
function drawLog() {
const ctx = logContext;
const width = logCanvas.width;
const height = logCanvas.height;
ctx.fillStyle = "#14201b";
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#83e7d1";
ctx.fillRect(28, 34, 12, 42);
ctx.fillStyle = "#f7edda";
ctx.font = "700 34px ui-monospace, monospace";
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillText("TRAVERSAL LOG", 58, 55);
ctx.strokeStyle = "#4a6155";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(28, 91);
ctx.lineTo(width - 28, 91);
ctx.stroke();
ctx.font = "18px ui-monospace, monospace";
ctx.fillStyle = "#8ba095";
ctx.fillText("DEPTH-FIRST / LIVE", 30, 120);
const visible = logEntries.slice(-19);
let y = 165;
for (const entry of visible) {
const indent = 26 + entry.depth * 21;
const prefix = entry.type === "ENTER"
? "> "
: entry.type === "RETURN"
? "< "
: "";
if (entry.type === "ENTER") {
ctx.fillStyle = "#83e7d1";
} else if (entry.type === "RETURN") {
ctx.fillStyle = "#f2b56c";
} else {
ctx.fillStyle = "#9baaa1";
}
ctx.font = "700 23px ui-monospace, monospace";
ctx.fillText(prefix + entry.text, indent, y);
y += 39;
}
ctx.fillStyle = "#71857a";
ctx.font = "17px ui-monospace, monospace";
ctx.fillText("SPACE: PLAY / PAUSE", 30, height - 34);
logTexture.needsUpdate = true;
}
function addLog(type, node = null) {
if (type === "READY") {
logEntries.push({
type: "READY",
depth: 0,
text: "READY"
});
} else if (type === "DONE") {
logEntries.push({
type: "DONE",
depth: 0,
text: "DONE"
});
} else {
logEntries.push({
type,
depth: node.depth,
text: `${type} ${node.name}`
});
}
drawLog();
}
drawLog();
// -------------------------------------------------------------------------
// Voxel cursor
// -------------------------------------------------------------------------
const cursor = new THREE.Group();
const cursorMaterial = new THREE.MeshStandardMaterial({
color: 0x55f0d2,
emissive: 0x0d6657,
emissiveIntensity: 0.8,
roughness: 0.45
});
const cursorBlocks = [
[-0.09, 0.20, 0.12, 0.12],
[0.00, 0.11, 0.12, 0.12],
[0.00, 0.02, 0.12, 0.12],
[0.09, -0.07, 0.12, 0.12],
[0.18, -0.07, 0.12, 0.12]
];
for (const [x, y, sx, sy] of cursorBlocks) {
addBlock(
cursor,
[sx, sy, 0.13],
[x, y, 0],
cursorMaterial
);
}
const homeAnchor = new THREE.Object3D();
homeAnchor.position.set(-7.65, 4.8, 2.65);
scene.add(homeAnchor);
cursor.position.copy(homeAnchor.position);
scene.add(cursor);
// -------------------------------------------------------------------------
// Camera controls implemented without addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(1.3, 1.0, 0);
const desiredCamera = {
yaw: 0,
pitch: 0.19,
distance: 23
};
const actualCamera = {
yaw: desiredCamera.yaw,
pitch: desiredCamera.pitch,
distance: desiredCamera.distance
};
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredCamera.yaw -= dx * 0.006;
desiredCamera.pitch = THREE.MathUtils.clamp(
desiredCamera.pitch + dy * 0.004,
0.08,
1.35
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
desiredCamera.distance = THREE.MathUtils.clamp(
desiredCamera.distance + event.deltaY * 0.015,
11,
35
);
}, { passive: false });
function updateCamera(dt) {
actualCamera.yaw = THREE.MathUtils.damp(
actualCamera.yaw,
desiredCamera.yaw,
7,
dt
);
actualCamera.pitch = THREE.MathUtils.damp(
actualCamera.pitch,
desiredCamera.pitch,
7,
dt
);
actualCamera.distance = THREE.MathUtils.damp(
actualCamera.distance,
desiredCamera.distance,
7,
dt
);
const horizontalDistance =
Math.cos(actualCamera.pitch) * actualCamera.distance;
camera.position.set(
cameraTarget.x +
Math.sin(actualCamera.yaw) * horizontalDistance,
cameraTarget.y +
Math.sin(actualCamera.pitch) * actualCamera.distance,
cameraTarget.z +
Math.cos(actualCamera.yaw) * horizontalDistance
);
camera.lookAt(cameraTarget);
}
// -------------------------------------------------------------------------
// Depth-first traversal
// -------------------------------------------------------------------------
const events = [];
function buildEvents(node) {
events.push({ type: "enter", node });
for (const child of node.children) {
buildEvents(child);
}
// Returning means moving back to this node's parent.
events.push({ type: "return", node });
}
for (const root of roots) {
buildEvents(root);
}
const statusElement = document.getElementById("status");
const playButton = document.getElementById("play");
let tourStarted = false;
let tourPaused = false;
let tourComplete = false;
let startAt = 0;
let eventIndex = 0;
let phase = "idle";
let dwellRemaining = 0;
const movement = {
from: new THREE.Vector3(),
to: new THREE.Vector3(),
elapsed: 0,
duration: 1,
arc: 0
};
function setStatus(text) {
statusElement.textContent = text;
}
function resetTour() {
tourStarted = false;
tourPaused = false;
tourComplete = false;
eventIndex = 0;
phase = "idle";
dwellRemaining = 0;
startAt = clock.getElapsedTime() + 0.8;
for (const folder of folders) {
folder.openTarget = 0;
folder.lidPivot.rotation.x = 0;
}
cursor.position.copy(homeAnchor.position);
logEntries.length = 0;
addLog("READY");
setStatus("Ready — depth-first traversal");
playButton.textContent = "Pause";
}
function beginEvent() {
if (eventIndex >= events.length) {
tourComplete = true;
phase = "done";
addLog("DONE");
setStatus("Traversal complete — all folders visited");
playButton.textContent = "Replay";
return;
}
const event = events[eventIndex];
scene.updateMatrixWorld(true);
let destination;
if (event.type === "enter") {
destination = new THREE.Vector3();
event.node.folder.anchor.getWorldPosition(destination);
} else {
destination = new THREE.Vector3();
if (event.node.parent) {
event.node.parent.folder.anchor.getWorldPosition(destination);
} else {
homeAnchor.getWorldPosition(destination);
}
}
movement.from.copy(cursor.position);
movement.to.copy(destination);
movement.elapsed = 0;
movement.duration = THREE.MathUtils.clamp(
movement.from.distanceTo(movement.to) * 0.055,
0.48,
1.08
);
movement.arc = 0.35 + event.node.depth * 0.12;
phase = "moving";
}
function arriveAtEvent(event) {
if (event.type === "enter") {
event.node.folder.openTarget = -1.38;
addLog("ENTER", event.node);
if (event.node.empty) {
dwellRemaining = 2.05;
setStatus("ENTER Archive — empty folder; recursion stops here");
} else if (event.node.children.length === 0) {
dwellRemaining = 0.68;
setStatus(`ENTER ${event.node.name} — leaf folder`);
} else {
dwellRemaining = 0.52;
setStatus(`ENTER ${event.node.name} — opening folder`);
}
} else {
addLog("RETURN", event.node);
const parentName = event.node.parent
? event.node.parent.name
: "Desk";
dwellRemaining = 0.23;
setStatus(`RETURN ${event.node.name} → ${parentName}`);
}
phase = "dwell";
}
function updateTour(dt) {
const elapsed = clock.getElapsedTime();
if (!tourStarted) {
if (elapsed >= startAt) {
tourStarted = true;
beginEvent();
}
return;
}
if (tourPaused || tourComplete) return;
if (phase === "moving") {
movement.elapsed += dt;
const t = THREE.MathUtils.clamp(
movement.elapsed / movement.duration,
0,
1
);
const eased = t * t * (3 - 2 * t);
cursor.position.lerpVectors(
movement.from,
movement.to,
eased
);
cursor.position.y += Math.sin(Math.PI * t) * movement.arc;
if (t >= 1) {
cursor.position.copy(movement.to);
arriveAtEvent(events[eventIndex]);
}
} else if (phase === "dwell") {
dwellRemaining -= dt;
if (dwellRemaining <= 0) {
eventIndex++;
beginEvent();
}
}
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
playButton.addEventListener("click", () => {
if (tourComplete) {
resetTour();
tourPaused = false;
playButton.textContent = "Pause";
return;
}
tourPaused = !tourPaused;
playButton.textContent = tourPaused ? "Play" : "Pause";
if (!tourPaused) {
setStatus("Traversal resumed");
} else {
setStatus("Paused");
}
});
document.getElementById("restart").addEventListener("click", () => {
resetTour();
});
document.getElementById("front").addEventListener("click", () => {
desiredCamera.yaw = 0;
desiredCamera.pitch = 0.19;
desiredCamera.distance = 23;
});
document.getElementById("above").addEventListener("click", () => {
desiredCamera.yaw = 0;
desiredCamera.pitch = 1.23;
desiredCamera.distance = 21;
});
addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
resetTour();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
updateTour(dt);
updateCamera(dt);
for (const folder of folders) {
folder.lidPivot.rotation.x = THREE.MathUtils.damp(
folder.lidPivot.rotation.x,
folder.openTarget,
8,
dt
);
}
// The block cursor always faces the current camera.
cursor.quaternion.copy(camera.quaternion);
renderer.render(scene, camera);
}
document.getElementById("loading").remove();
animate();
</script>
</body>
</html>
```