Prompt specification
Build or customize the Three.js 3D scene "Product category and search-tree gallery" 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: Product category and search-tree gallery
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/product-category-and-search-tree-gallery-x1915
Scene Description Prompt:
A voxel product-category hierarchy beside a numeric search tree, with their different organizing rules. One HTML file using Three.js from cdnjs.
Working Three.js Starter (product-category-and-search-tree-gallery-x1915):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One Catalog, Two Organizing Rules</title>
<style>
:root {
color-scheme: dark;
--bg: #06101c;
--panel: rgba(9, 23, 39, 0.76);
--line: rgba(147, 206, 231, 0.18);
--text: #e7f4fa;
--muted: #8da8b8;
--cyan: #48d7e8;
--gold: #ffc66d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#app,
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
canvas {
display: block;
touch-action: none;
outline: none;
}
.glass {
border: 1px solid var(--line);
background: var(--panel);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.25),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
header {
position: fixed;
top: 26px;
left: 28px;
z-index: 10;
width: min(440px, calc(100vw - 56px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 30px;
height: 1px;
content: "";
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
h1 {
max-width: 420px;
margin: 0;
font-size: clamp(34px, 4vw, 58px);
font-weight: 650;
line-height: 0.98;
letter-spacing: -0.055em;
text-wrap: balance;
}
h1 span {
color: var(--cyan);
}
header p {
max-width: 410px;
margin: 16px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.rules {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 17px;
}
.rule {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
color: #bdd2dd;
background: rgba(7, 19, 32, 0.6);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.rule.semantic {
border-color: rgba(72, 215, 232, 0.32);
}
.rule.numeric {
border-color: rgba(255, 198, 109, 0.35);
}
.search-panel {
position: fixed;
top: 26px;
right: 28px;
z-index: 20;
width: 300px;
padding: 16px;
border-radius: 15px;
}
.panel-label {
margin-bottom: 11px;
color: var(--gold);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
form {
display: flex;
gap: 8px;
}
input {
min-width: 0;
flex: 1;
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
outline: none;
color: var(--text);
background: rgba(2, 9, 17, 0.68);
font: inherit;
font-size: 13px;
}
input:focus {
border-color: rgba(255, 198, 109, 0.6);
box-shadow: 0 0 0 3px rgba(255, 198, 109, 0.08);
}
button {
border: 0;
border-radius: 9px;
padding: 10px 13px;
color: #08131e;
background: var(--gold);
font: inherit;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
cursor: pointer;
transition:
transform 160ms ease,
filter 160ms ease;
}
button:hover {
transform: translateY(-1px);
filter: brightness(1.08);
}
button:active {
transform: translateY(0);
}
#searchStatus {
min-height: 30px;
margin-top: 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
#searchStatus strong {
color: #e9f7fb;
font-weight: 650;
}
.inspector {
position: fixed;
bottom: 26px;
left: 28px;
z-index: 15;
width: min(340px, calc(100vw - 56px));
padding: 16px 18px;
border-radius: 14px;
pointer-events: none;
}
#inspectType {
color: var(--cyan);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.18em;
text-transform: uppercase;
}
#inspectTitle {
display: block;
margin-top: 5px;
font-size: 19px;
font-weight: 650;
letter-spacing: -0.02em;
}
#inspectCopy {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.hint {
position: fixed;
bottom: 28px;
left: 50%;
z-index: 10;
transform: translateX(-50%);
color: rgba(183, 208, 220, 0.65);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
}
#resetView {
position: fixed;
right: 28px;
bottom: 26px;
z-index: 20;
border: 1px solid var(--line);
color: #b9d0dc;
background: rgba(9, 23, 39, 0.76);
backdrop-filter: blur(12px);
}
@media (max-width: 760px) {
header {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
h1 {
max-width: 330px;
font-size: 36px;
}
header p {
max-width: 330px;
font-size: 11px;
}
.rules {
display: none;
}
.search-panel {
top: auto;
right: 18px;
bottom: 68px;
width: calc(100vw - 36px);
}
.inspector {
bottom: 18px;
left: 18px;
width: calc(100vw - 168px);
padding: 12px 14px;
}
#inspectCopy {
display: none;
}
.hint {
display: none;
}
#resetView {
right: 18px;
bottom: 18px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header>
<div class="eyebrow">Information architecture / 01</div>
<h1>One catalog.<br><span>Two logics.</span></h1>
<p>
A voxel taxonomy groups products by meaning and category.
Beside it, a binary search tree organizes numeric keys by comparison.
</p>
<div class="rules">
<span class="rule semantic">Semantic · category-first</span>
<span class="rule numeric">Numeric · ordered</span>
</div>
</header>
<aside class="search-panel glass">
<div class="panel-label">Trace the numeric search tree</div>
<form id="searchForm">
<input
id="numberInput"
type="number"
min="1"
max="15"
step="1"
placeholder="Key 1–15"
aria-label="Numeric key to trace"
>
<button type="button" id="traceNumberButton">Trace</button>
</form>
<div id="searchStatus" aria-live="polite">
Enter a key to reveal its comparison path.
</div>
</aside>
<aside class="inspector glass">
<span id="inspectType">Explore the structures</span>
<strong id="inspectTitle">Select any node</strong>
<p id="inspectCopy">
Click a voxel, category, or numeric node to inspect its organizing role.
</p>
</aside>
<div class="hint">Drag to orbit · wheel or pinch to zoom · click to inspect</div>
<button id="resetView" type="button">Reset view</button>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, scene, and camera
// ---------------------------------------------------------------------
const app = document.querySelector("#app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06101c);
scene.fog = new THREE.FogExp2(0x06101c, 0.015);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive comparison of a voxel product taxonomy and a numeric binary search tree"
);
app.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
180
);
const cameraState = {
target: new THREE.Vector3(0, 5.5, 0),
radius: 24,
theta: 0,
phi: 1.36,
userZoomed: false
};
function fittedRadius() {
const halfFov = THREE.MathUtils.degToRad(camera.fov * 0.5);
const required = 25 / (2 * Math.tan(halfFov) * camera.aspect);
return Math.max(24, required);
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * cosPhi,
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
cameraState.radius = fittedRadius();
updateCamera();
// ---------------------------------------------------------------------
// Lighting and floor
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9ccfff, 0x07101b, 1.45));
const keyLight = new THREE.DirectionalLight(0xd9f2ff, 3.2);
keyLight.position.set(-8, 16, 12);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -20;
keyLight.shadow.camera.right = 20;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x30d8ef, 35, 24, 2);
cyanLight.position.set(-8, 7, 5);
scene.add(cyanLight);
const goldLight = new THREE.PointLight(0xffbf62, 32, 22, 2);
goldLight.position.set(9, 7, 5);
scene.add(goldLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 45),
new THREE.MeshStandardMaterial({
color: 0x071321,
roughness: 0.9,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = 1;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(40, 40, 0x1a4560, 0x10283a);
grid.position.y = 1.012;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const centerDivider = new THREE.Mesh(
new THREE.BoxGeometry(0.022, 0.018, 18),
new THREE.MeshBasicMaterial({
color: 0x3b7289,
transparent: true,
opacity: 0.42
})
);
centerDivider.position.set(0, 1.025, 0);
scene.add(centerDivider);
function addFloorHalo(x, color, radius) {
const halo = new THREE.Mesh(
new THREE.RingGeometry(radius, radius + 0.035, 80),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.24,
side: THREE.DoubleSide
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.set(x, 1.025, 0);
scene.add(halo);
}
addFloorHalo(-7.1, 0x48d7e8, 4.95);
addFloorHalo(7.2, 0xffc66d, 4.35);
// ---------------------------------------------------------------------
// Canvas-texture labels
// ---------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeLabel(text, options = {}) {
const {
foreground = "#e7f4fa",
background = null,
border = null,
height = 0.45,
fontSize = 42,
fontWeight = 700,
depthTest = true
} = options;
const measureCanvas = document.createElement("canvas");
const measureContext = measureCanvas.getContext("2d");
const font = `${fontWeight} ${fontSize}px Inter, system-ui, sans-serif`;
measureContext.font = font;
const measuredWidth = measureContext.measureText(text).width;
const paddingX = background ? 30 : 4;
const canvasWidth = Math.ceil(measuredWidth + paddingX * 2);
const canvasHeight = background ? 92 : 70;
const canvas = document.createElement("canvas");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
const context = canvas.getContext("2d");
if (background) {
roundedRect(
context,
2,
2,
canvasWidth - 4,
canvasHeight - 4,
18
);
context.fillStyle = background;
context.fill();
if (border) {
context.lineWidth = 2;
context.strokeStyle = border;
context.stroke();
}
}
context.font = font;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = foreground;
context.fillText(text, canvasWidth / 2, canvasHeight / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(
height * (canvasWidth / canvasHeight),
height,
1
);
return sprite;
}
// ---------------------------------------------------------------------
// Geometry helpers
// ---------------------------------------------------------------------
function createBeam(start, end, thickness, material) {
const direction = end.clone().sub(start);
const length = direction.length();
const beam = new THREE.Mesh(
new THREE.BoxGeometry(thickness, length, thickness),
material
);
beam.position.copy(start).add(end).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
beam.castShadow = true;
return beam;
}
function createRod(start, end, radius, material) {
const direction = end.clone().sub(start);
const length = direction.length();
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
material
);
rod.position.copy(start).add(end).multiplyScalar(0.5);
rod.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return rod;
}
const interactiveObjects = [];
const semanticObjects = [];
// ---------------------------------------------------------------------
// Left structure: voxel product/category hierarchy
// ---------------------------------------------------------------------
const semanticGroup = new THREE.Group();
semanticGroup.position.x = -7.1;
scene.add(semanticGroup);
const semanticTitle = makeLabel("VOXEL PRODUCT TREE", {
foreground: "#8eefff",
background: "rgba(5, 22, 34, 0.88)",
border: "rgba(72, 215, 232, 0.34)",
height: 0.58,
fontSize: 39
});
semanticTitle.position.set(0, 10.65, 0.2);
semanticGroup.add(semanticTitle);
const rootPosition = new THREE.Vector3(0, 8.55, 0);
const rootVoxel = new THREE.Mesh(
new THREE.BoxGeometry(0.68, 0.68, 0.68),
new THREE.MeshStandardMaterial({
color: 0xf4c46b,
emissive: 0x6a4210,
emissiveIntensity: 0.45,
roughness: 0.3,
metalness: 0.2
})
);
rootVoxel.position.copy(rootPosition);
rootVoxel.castShadow = true;
rootVoxel.receiveShadow = true;
rootVoxel.userData = {
kind: "semantic-root",
title: "CATALOG",
type: "Semantic root",
copy: "The catalog is the common parent. Every product belongs to a semantic family, but no numeric ordering is required here."
};
semanticGroup.add(rootVoxel);
interactiveObjects.push(rootVoxel);
const rootEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(rootVoxel.geometry),
new THREE.LineBasicMaterial({
color: 0xffe0a3,
transparent: true,
opacity: 0.8
})
);
rootVoxel.add(rootEdges);
const rootLabel = makeLabel("CATALOG", {
foreground: "#ffe0a3",
background: "rgba(43, 30, 10, 0.86)",
border: "rgba(255, 198, 109, 0.34)",
height: 0.52,
fontSize: 38
});
rootLabel.position.set(0, 9.42, 0.2);
semanticGroup.add(rootLabel);
const categoryDefinitions = [
{
name: "ELECTRONICS",
x: -3.5,
color: 0x48d7e8,
products: ["Laptops", "Phones", "Audio"]
},
{
name: "HOME",
x: 0,
color: 0xff7d72,
products: ["Furniture", "Kitchen", "Decor"]
},
{
name: "APPAREL",
x: 3.5,
color: 0xa98cff,
products: ["Wear", "Shoes", "Bags"]
}
];
const voxelGeometry = new THREE.BoxGeometry(0.25, 0.25, 0.25);
for (const category of categoryDefinitions) {
const categoryPosition = new THREE.Vector3(category.x, 6.2, 0);
const color = new THREE.Color(category.color);
const branchMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.24,
transparent: true,
opacity: 0.5,
roughness: 0.45,
metalness: 0.12
});
const rootBranch = createBeam(
rootPosition,
categoryPosition,
0.075,
branchMaterial
);
semanticGroup.add(rootBranch);
// Each category is a true voxel cluster rather than a single symbol.
const cluster = new THREE.InstancedMesh(
voxelGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.36,
metalness: 0.16
}),
27
);
const dummy = new THREE.Object3D();
let instance = 0;
for (let z = 0; z < 3; z++) {
for (let y = 0; y < 3; y++) {
for (let x = 0; x < 3; x++) {
dummy.position.set(
(x - 1) * 0.285,
(y - 1) * 0.285,
(z - 1) * 0.285
);
dummy.updateMatrix();
cluster.setMatrixAt(instance, dummy.matrix);
const variation = color.clone().offsetHSL(
(x - 1) * 0.008,
0,
(y - 1) * 0.055 + (z - 1) * 0.025
);
cluster.setColorAt(instance, variation);
instance++;
}
}
}
cluster.instanceMatrix.needsUpdate = true;
cluster.instanceColor.needsUpdate = true;
cluster.position.copy(categoryPosition);
cluster.castShadow = true;
cluster.receiveShadow = true;
cluster.userData = {
kind: "category",
title: category.name,
type: "Semantic category",
copy: "A semantic family. Products live here because they share purpose, use, or meaning—not because their names happen to sort together."
};
semanticGroup.add(cluster);
interactiveObjects.push(cluster);
semanticObjects.push(cluster);
const categoryLabel = makeLabel(category.name, {
foreground: `#${color.getHexString()}`,
background: "rgba(5, 19, 31, 0.88)",
border: `#${color.getHexString()}55`,
height: 0.5,
fontSize: 35
});
categoryLabel.position.set(category.x, 7.1, 0.28);
semanticGroup.add(categoryLabel);
const productOffsets = [-1.05, 0, 1.05];
category.products.forEach((productName, productIndex) => {
const productPosition = new THREE.Vector3(
category.x + productOffsets[productIndex],
3.55,
0
);
const productBranch = createBeam(
categoryPosition,
productPosition,
0.055,
branchMaterial
);
semanticGroup.add(productBranch);
const productMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.34, 0.34),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.08,
roughness: 0.32,
metalness: 0.18
})
);
productMesh.position.copy(productPosition);
productMesh.castShadow = true;
productMesh.userData = {
kind: "product",
title: productName,
type: `${category.name} / Product leaf`,
copy: `This leaf inherits meaning from ${category.name}. Its location communicates category membership; it does not encode numeric order.`
};
semanticGroup.add(productMesh);
interactiveObjects.push(productMesh);
semanticObjects.push(productMesh);
const productLabel = makeLabel(productName, {
foreground: "#c8dce5",
height: 0.34,
fontSize: 33,
fontWeight: 650,
depthTest: false
});
productLabel.position.set(productPosition.x, 2.92, 0.3);
semanticGroup.add(productLabel);
});
}
// ---------------------------------------------------------------------
// Right structure: ordered numeric binary search tree
// ---------------------------------------------------------------------
const numericGroup = new THREE.Group();
numericGroup.position.x = 7.2;
scene.add(numericGroup);
const numericTitle = makeLabel("ORDERED NUMERIC TREE", {
foreground: "#ffd58f",
background: "rgba(31, 23, 10, 0.88)",
border: "rgba(255, 198, 109, 0.34)",
height: 0.58,
fontSize: 37
});
numericTitle.position.set(0, 10.65, 0.2);
numericGroup.add(numericTitle);
const bstModel = {
8: { left: 4, right: 12 },
4: { left: 2, right: 6 },
12: { left: 10, right: 14 },
2: { left: 1, right: 3 },
6: { left: 5, right: 7 },
10: { left: 9, right: 11 },
14: { left: 13, right: 15 },
1: { left: null, right: null },
3: { left: null, right: null },
5: { left: null, right: null },
7: { left: null, right: null },
9: { left: null, right: null },
11: { left: null, right: null },
13: { left: null, right: null },
15: { left: null, right: null }
};
const numericLayout = {
8: [0, 8.55],
4: [-2, 6.7],
12: [2, 6.7],
2: [-3, 5.05],
6: [-1, 5.05],
10: [1, 5.05],
14: [3, 5.05],
1: [-3.5, 3.4],
3: [-2.5, 3.4],
5: [-1.5, 3.4],
7: [-0.5, 3.4],
9: [0.5, 3.4],
11: [1.5, 3.4],
13: [2.5, 3.4],
15: [3.5, 3.4]
};
const numericNodes = new Map();
const numericRodMaterial = new THREE.MeshBasicMaterial({
color: 0x2b6178,
transparent: true,
opacity: 0.62
});
function nodeDepth(key) {
let depth = 0;
let current = 8;
while (current !== key) {
current = key < current
? bstModel[current].left
: bstModel[current].right;
depth++;
}
return depth;
}
for (const [key, layout] of Object.entries(numericLayout)) {
const [x, y] = layout;
const disc = new THREE.Mesh(
new THREE.CircleGeometry(0.34, 40),
new THREE.MeshStandardMaterial({
color: 0x10283e,
emissive: 0x03111d,
emissiveIntensity: 0.55,
roughness: 0.42,
metalness: 0.12,
side: THREE.DoubleSide
})
);
disc.position.set(x, y, 0.08);
disc.userData = {
kind: "numeric",
title: String(key),
type: "Ordered numeric node",
copy: `Keys smaller than ${key} branch left; larger keys branch right. Its depth is ${nodeDepth(Number(key))}, so only ${nodeDepth(Number(key)) + 1} comparisons are needed to find it.`
};
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.4, 0.022, 8, 40),
new THREE.MeshBasicMaterial({
color: 0x2b718c,
transparent: true,
opacity: 0.92
})
);
ring.position.set(x, y, 0.13);
const label = makeLabel(String(key), {
foreground: "#eafcff",
height: 0.43,
fontSize: 42,
fontWeight: 750,
depthTest: false
});
label.position.set(x, y, 0.25);
numericGroup.add(disc, ring, label);
interactiveObjects.push(disc);
numericNodes.set(Number(key), {
key: Number(key),
disc,
ring,
pathIndex: -1,
isTarget: false
});
}
for (const [keyText, children] of Object.entries(bstModel)) {
const key = Number(keyText);
const [x, y] = numericLayout[key];
for (const childKey of [children.left, children.right]) {
if (childKey === null) continue;
const [childX, childY] = numericLayout[childKey];
const rod = createRod(
new THREE.Vector3(x, y, -0.04),
new THREE.Vector3(childX, childY, -0.04),
0.018,
numericRodMaterial
);
numericGroup.add(rod);
}
}
// ---------------------------------------------------------------------
// Search path interaction
// ---------------------------------------------------------------------
const searchForm = document.querySelector("#searchForm");
const numberInput = document.querySelector("#numberInput");
const searchStatus = document.querySelector("#searchStatus");
function resetNumericPath() {
for (const node of numericNodes.values()) {
node.pathIndex = -1;
node.isTarget = false;
node.ring.material.color.setHex(0x2b718c);
node.disc.material.color.setHex(0x10283e);
node.disc.material.emissive.setHex(0x03111d);
node.disc.material.emissiveIntensity = 0.55;
}
}
function traceNumber(value) {
resetNumericPath();
if (!Number.isInteger(value) || value < 1 || value > 15) {
searchStatus.innerHTML =
"Use an integer from <strong>1 through 15</strong>.";
return [];
}
const path = [];
let current = 8;
while (current !== null) {
path.push(current);
if (value === current) break;
current = value < current
? bstModel[current].left
: bstModel[current].right;
}
path.forEach((key, index) => {
const node = numericNodes.get(key);
node.pathIndex = index;
node.ring.material.color.setHex(0x48d7e8);
node.disc.material.color.setHex(0x104858);
node.disc.material.emissive.setHex(0x0b6973);
node.disc.material.emissiveIntensity = 0.85;
});
const target = numericNodes.get(value);
target.isTarget = true;
target.ring.material.color.setHex(0xffc66d);
target.disc.material.color.setHex(0x5a421c);
target.disc.material.emissive.setHex(0x8b5917);
target.disc.material.emissiveIntensity = 0.95;
searchStatus.innerHTML =
`<strong>${path.join(" → ")}</strong><br>` +
`${path.length} comparison${path.length === 1 ? "" : "s"} from the root.`;
return path;
}
const traceEntry = () => traceNumber(Number(numberInput.value));
searchForm.querySelector("#traceNumberButton").addEventListener("click", traceEntry);
numberInput.addEventListener("keydown", event => {
if (event.key === "Enter") { event.preventDefault(); traceEntry(); }
});
// ---------------------------------------------------------------------
// Inspector and raycasting
// ---------------------------------------------------------------------
const inspectType = document.querySelector("#inspectType");
const inspectTitle = document.querySelector("#inspectTitle");
const inspectCopy = document.querySelector("#inspectCopy");
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function showInspector(object) {
if (!object?.userData?.title) return;
inspectType.textContent = object.userData.type;
inspectTitle.textContent = object.userData.title;
inspectCopy.textContent = object.userData.copy;
}
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 pick(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(interactiveObjects, false);
return intersections.length ? intersections[0].object : null;
}
let hoveredObject = null;
function setHovered(object) {
hoveredObject = object;
renderer.domElement.style.cursor = object ? "pointer" : "grab";
}
// ---------------------------------------------------------------------
// Custom orbit and pinch controls; no addons are used.
// ---------------------------------------------------------------------
const pointers = new Map();
let dragDistance = 0;
let previousPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragDistance = 0;
renderer.domElement.style.cursor = "grabbing";
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) {
const hovered = pick(event);
setHovered(hovered);
return;
}
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragDistance += Math.abs(dx) + Math.abs(dy);
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.72,
1.7
);
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0 && distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinchDistance / distance),
13,
70
);
cameraState.userZoomed = true;
}
previousPinchDistance = distance;
}
});
function finishPointer(event) {
const wasSinglePointer = pointers.size === 1;
pointers.delete(event.pointerId);
if (wasSinglePointer && dragDistance < 7) {
const selected = pick(event);
setHovered(selected);
if (selected) {
showInspector(selected);
if (selected.userData.kind === "numeric") {
traceNumber(Number(selected.userData.title));
}
}
}
if (pointers.size === 0) {
renderer.domElement.style.cursor = hoveredObject ? "pointer" : "grab";
}
}
renderer.domElement.addEventListener("pointerup", finishPointer);
renderer.domElement.addEventListener("pointercancel", finishPointer);
renderer.domElement.addEventListener("pointerleave", () => {
if (pointers.size === 0) setHovered(null);
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
13,
70
);
cameraState.userZoomed = true;
},
{ passive: false }
);
document.querySelector("#resetView").addEventListener("click", () => {
cameraState.theta = 0;
cameraState.phi = 1.36;
cameraState.radius = fittedRadius();
cameraState.target.set(0, 5.5, 0);
cameraState.userZoomed = false;
resetNumericPath();
updateCamera();
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
if (!cameraState.userZoomed) {
cameraState.radius = fittedRadius();
}
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const easing = Math.min(1, delta * 10);
updateCamera();
// Smooth hover feedback for voxel nodes and clusters.
for (const object of semanticObjects) {
const isHovered = object === hoveredObject;
const targetScale = isHovered
? object.userData.kind === "product" ? 1.18 : 1.08
: 1;
object.scale.x += (targetScale - object.scale.x) * easing;
object.scale.y += (targetScale - object.scale.y) * easing;
object.scale.z += (targetScale - object.scale.z) * easing;
}
// Numeric path nodes pulse, while the target key uses a warm accent.
for (const node of numericNodes.values()) {
let targetScale = 1;
if (node.isTarget) {
targetScale = 1.11 + Math.sin(elapsed * 4.4) * 0.045;
} else if (node.pathIndex >= 0) {
targetScale = 1.05 + Math.sin(elapsed * 3.6 - node.pathIndex * 0.35) * 0.04;
} else if (node.disc === hoveredObject) {
targetScale = 1.09;
}
node.ring.scale.x += (targetScale - node.ring.scale.x) * easing;
node.ring.scale.y += (targetScale - node.ring.scale.y) * easing;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```