Prompt specification
Build or customize the Three.js 3D scene "Belt pin and hole engagement" 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: Belt pin and hole engagement
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/belt-pin-and-hole-engagement-x0565
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Model a belt passing through a simple buckle with one selected hole engaged by the pin; show the free tail, keeper, and loop continuity clearly.
Working Three.js Starter (belt-pin-and-hole-engagement-x0565):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Belt & Buckle</title>
<style>
:root {
color-scheme: dark;
--ink: #f4eadc;
--muted: #aaa5a4;
--amber: #f0bd58;
--panel: rgba(14, 19, 31, 0.82);
--line: rgba(255, 255, 255, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1020;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
top: 24px;
left: 24px;
width: min(340px, calc(100vw - 48px));
padding: 18px 20px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 8px;
color: var(--amber);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
position: absolute;
right: 24px;
top: 24px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
color: var(--muted);
font-size: 11px;
line-height: 1.8;
backdrop-filter: blur(12px);
}
.controls b {
display: inline-block;
min-width: 78px;
color: var(--ink);
font-weight: 650;
}
.status-panel {
position: absolute;
left: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 16px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.status-copy {
min-width: 148px;
}
.status-label {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#status {
margin-top: 3px;
color: var(--amber);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.06em;
}
#holeButtons {
display: flex;
gap: 6px;
}
.hole-button {
width: 32px;
height: 32px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 7px;
background: rgba(255, 255, 255, 0.04);
color: var(--muted);
font: 800 11px/1 inherit;
cursor: pointer;
transition: 150ms ease;
}
.hole-button:hover {
border-color: rgba(240, 189, 88, 0.6);
color: var(--ink);
}
.hole-button.active {
border-color: var(--amber);
background: var(--amber);
color: #17130d;
box-shadow: 0 0 20px rgba(240, 189, 88, 0.2);
}
#reset {
border: 0;
border-left: 1px solid var(--line);
padding: 4px 0 4px 14px;
background: transparent;
color: var(--muted);
font: 700 10px/1 inherit;
letter-spacing: 0.08em;
cursor: pointer;
}
#reset:hover {
color: var(--ink);
}
.scene-label {
position: absolute;
z-index: 2;
transform: translate(-50%, -100%);
padding: 6px 8px;
border: 1px solid rgba(240, 189, 88, 0.35);
border-radius: 5px;
background: rgba(12, 16, 27, 0.84);
color: #f3d491;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
white-space: nowrap;
pointer-events: none;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 15px;
background: linear-gradient(var(--amber), transparent);
}
.scene-label::before {
content: "";
position: absolute;
left: calc(50% - 2px);
top: calc(100% + 13px);
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 9px var(--amber);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #0b1020;
color: var(--amber);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.done {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.controls {
display: none;
}
.status-panel {
right: 12px;
left: 12px;
bottom: 12px;
justify-content: space-between;
}
.scene-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="loading">Building voxel assembly</div>
<div class="hud">
<section class="panel">
<div class="eyebrow">Voxel Product Study / 01</div>
<h1>Belt & Buckle</h1>
<p class="subtitle">
A continuous leather strap folds around the buckle frame.
The brass pin is seated in one selected hole.
</p>
</section>
<section class="controls">
<div><b>Drag</b> orbit view</div>
<div><b>Wheel</b> zoom</div>
<div><b>Click</b> select a hole</div>
<div><b>1–4</b> move the pin</div>
<div><b>R</b> reset camera</div>
</section>
<section class="status-panel">
<div class="status-copy">
<div class="status-label">Pin state</div>
<div id="status">Engaged · Hole 01</div>
</div>
<div id="holeButtons" aria-label="Select buckle hole"></div>
<button id="reset" type="button">RESET</button>
</section>
</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(0x0b1020);
scene.fog = new THREE.Fog(0x0b1020, 16, 31);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xc5dcff, 0x25151a, 1.65);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffedcf, 3.25);
key.position.set(-3.5, 7.5, 9);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -9;
key.shadow.camera.right = 9;
key.shadow.camera.top = 7;
key.shadow.camera.bottom = -5;
key.shadow.camera.near = 1;
key.shadow.camera.far = 25;
key.shadow.bias = -0.00035;
scene.add(key);
const rim = new THREE.DirectionalLight(0x78aaff, 1.75);
rim.position.set(6, 3, -7);
scene.add(rim);
const warmFill = new THREE.PointLight(0xffad5c, 16, 18, 2);
warmFill.position.set(-3, 1.5, 4);
scene.add(warmFill);
// ------------------------------------------------------------
// Shared geometry and voxel helper
// ------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function colorVariant(hex, amount = 0) {
return new THREE.Color(hex).offsetHSL(0, 0, amount);
}
function addVoxels(items, material, castShadow = true) {
const mesh = new THREE.InstancedMesh(
cubeGeometry,
material,
items.length
);
items.forEach((item, index) => {
dummy.position.set(item.x, item.y, item.z);
dummy.scale.set(item.sx, item.sy, item.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (item.color) {
mesh.setColorAt(index, item.color);
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const leatherMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.79,
metalness: 0.02
});
const goldMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.28,
metalness: 0.78
});
const pinMaterial = new THREE.MeshStandardMaterial({
color: 0xf2c465,
roughness: 0.22,
metalness: 0.88,
emissive: 0x5b3508,
emissiveIntensity: 0.16
});
const stitchMaterial = new THREE.MeshStandardMaterial({
color: 0xc58b59,
roughness: 0.9
});
const keeperMaterial = new THREE.MeshStandardMaterial({
color: 0x7b4930,
roughness: 0.78,
metalness: 0.01
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
});
// ------------------------------------------------------------
// Voxel floor
// ------------------------------------------------------------
const floorTiles = [];
for (let ix = -10; ix <= 10; ix++) {
for (let iz = -5; iz <= 5; iz++) {
const variation = ((ix * 17 + iz * 29) % 7) * 0.004 - 0.012;
floorTiles.push({
x: ix * 0.72,
y: -1.52,
z: iz * 0.72,
sx: 0.695,
sy: 0.055,
sz: 0.695,
color: colorVariant(0x182338, variation)
});
}
}
addVoxels(floorTiles, floorMaterial, false);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x0c1323,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.555;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Continuous leather belt
//
// Main section: behind the buckle.
// Fold: steps around the buckle's left side.
// Tail: in front of the buckle and extending to the free end.
// ------------------------------------------------------------
const step = 0.32;
const rowStep = 0.28;
const rowCount = 7;
const tailStart = -1.44;
const tailColumns = 25;
const holeColumns = [5, 10, 13, 16];
const holes = holeColumns.map((column, index) => ({
index,
column,
x: tailStart + column * step,
y: 0
}));
const tailColors = [
0x5a3023,
0x633526,
0x4d281e,
0x6d3a29,
0x542a20
];
const mainColors = [
0x45251e,
0x4e2b21,
0x3d211b,
0x573024,
0x49261e
];
const tailVoxels = [];
const mainVoxels = [];
const foldVoxels = [];
const stitchVoxels = [];
// Tail, including true rectangular holes.
for (let column = 0; column < tailColumns; column++) {
const x = tailStart + column * step;
for (let row = 0; row < rowCount; row++) {
const y = -0.84 + row * rowStep;
if (holeColumns.includes(column) && row === 3) {
continue;
}
const colorIndex =
(column * 13 + row * 7 + (column % 3)) % tailColors.length;
tailVoxels.push({
x,
y,
z: 0.30,
sx: 0.316,
sy: 0.276,
sz: 0.28,
color: colorVariant(tailColors[colorIndex], row === 0 || row === 6 ? -0.018 : 0)
});
}
// Raised saddle stitching on the visible face.
for (const y of [-0.73, 0.73]) {
stitchVoxels.push({
x,
y,
z: 0.457,
sx: 0.105,
sy: 0.047,
sz: 0.035
});
}
}
// Rounded, stepped free tip.
for (let row = 1; row <= 5; row++) {
tailVoxels.push({
x: 6.40,
y: -0.84 + row * rowStep,
z: 0.30,
sx: 0.316,
sy: 0.276,
sz: 0.28,
color: colorVariant(0x5a3023, row === 3 ? 0.01 : -0.01)
});
}
// Main belt section behind the buckle.
for (let column = 0; column < 15; column++) {
const x = -6.24 + column * step;
for (let row = 0; row < rowCount; row++) {
const y = -0.84 + row * rowStep;
const colorIndex =
(column * 11 + row * 5 + (column % 4)) % mainColors.length;
mainVoxels.push({
x,
y,
z: -0.30,
sx: 0.316,
sy: 0.276,
sz: 0.28,
color: colorVariant(mainColors[colorIndex], row === 0 || row === 6 ? -0.015 : 0)
});
}
for (const y of [-0.73, 0.73]) {
stitchVoxels.push({
x,
y,
z: -0.137,
sx: 0.105,
sy: 0.047,
sz: 0.035
});
}
}
// Stepped far end of the main section.
for (let row = 1; row <= 5; row++) {
mainVoxels.push({
x: -6.40,
y: -0.84 + row * rowStep,
z: -0.30,
sx: 0.316,
sy: 0.276,
sz: 0.28,
color: colorVariant(0x45251e, -0.01)
});
}
// The visible U-fold: the belt travels behind the frame,
// around the left side, and returns in front as the tail.
const foldPath = [
{ x: -1.76, z: -0.30 },
{ x: -1.61, z: -0.24 },
{ x: -1.49, z: -0.08 },
{ x: -1.49, z: 0.11 },
{ x: -1.60, z: 0.25 },
{ x: -1.44, z: 0.30 }
];
for (const y of [-0.84, -0.56, -0.28, 0, 0.28, 0.56, 0.84]) {
for (const point of foldPath) {
foldVoxels.push({
x: point.x,
y,
z: point.z,
sx: 0.285,
sy: 0.276,
sz: 0.285,
color: colorVariant(0x4d281e, ((Math.round(y * 10) + 8) % 5) * 0.004)
});
}
}
addVoxels(tailVoxels, leatherMaterial);
addVoxels(mainVoxels, leatherMaterial);
addVoxels(foldVoxels, leatherMaterial);
addVoxels(stitchVoxels, stitchMaterial, false);
// ------------------------------------------------------------
// Voxel buckle frame
// ------------------------------------------------------------
const frameVoxels = [];
const frameColors = [
0xd2a03e,
0xe1b54f,
0xbf8a31,
0xeac363
];
function addFrameBlock(x, y, index) {
frameVoxels.push({
x,
y,
z: 0,
sx: 0.205,
sy: 0.205,
sz: 0.38,
color: colorVariant(
frameColors[index % frameColors.length],
(index % 3) * 0.006 - 0.006
)
});
}
let frameIndex = 0;
// Top and bottom rails.
for (let i = 0; i <= 12; i++) {
const x = -1.15 + i * 0.22;
addFrameBlock(x, 1.22, frameIndex++);
addFrameBlock(x, -1.22, frameIndex++);
}
// Side rails.
for (let i = 1; i <= 9; i++) {
const y = -1.00 + i * 0.25;
addFrameBlock(-1.15, y, frameIndex++);
addFrameBlock(1.49, y, frameIndex++);
}
addVoxels(frameVoxels, goldMaterial);
// ------------------------------------------------------------
// Pivot, prong, and selected-hole halo
// ------------------------------------------------------------
const pivot = new THREE.Mesh(cubeGeometry, pinMaterial);
pivot.position.set(1.39, 0.27, 0.43);
pivot.scale.set(0.38, 0.38, 0.30);
pivot.castShadow = true;
scene.add(pivot);
const pivotInset = new THREE.Mesh(
cubeGeometry,
new THREE.MeshStandardMaterial({
color: 0x6d471c,
roughness: 0.38,
metalness: 0.7
})
);
pivotInset.position.set(1.39, 0.27, 0.595);
pivotInset.scale.set(0.115, 0.115, 0.035);
scene.add(pivotInset);
const pinGroup = new THREE.Group();
scene.add(pinGroup);
const haloMaterial = new THREE.MeshStandardMaterial({
color: 0xf3b84f,
emissive: 0xf09a21,
emissiveIntensity: 1.1,
roughness: 0.38,
metalness: 0.35
});
const haloGroup = new THREE.Group();
scene.add(haloGroup);
function rebuildHalo(x) {
haloGroup.clear();
const pieces = [
{ x: 0, y: 0.185, sx: 0.42, sy: 0.045 },
{ x: 0, y: -0.185, sx: 0.42, sy: 0.045 },
{ x: -0.19, y: 0, sx: 0.045, sy: 0.325 },
{ x: 0.19, y: 0, sx: 0.045, sy: 0.325 }
];
for (const piece of pieces) {
const mesh = new THREE.Mesh(cubeGeometry, haloMaterial);
mesh.position.set(x + piece.x, piece.y, -0.115);
mesh.scale.set(piece.sx, piece.sy, 0.055);
haloGroup.add(mesh);
}
}
function rebuildPin(hole) {
pinGroup.clear();
const start = new THREE.Vector3(1.38, 0.27, 0.55);
const end = new THREE.Vector3(hole.x, 0, 0.245);
const count = 15;
for (let i = 0; i <= count; i++) {
const t = i / count;
const point = start.clone().lerp(end, t);
const size = THREE.MathUtils.lerp(0.155, 0.075, t);
const voxel = new THREE.Mesh(cubeGeometry, pinMaterial);
voxel.position.copy(point);
voxel.scale.set(size, size, size);
voxel.castShadow = true;
pinGroup.add(voxel);
}
const tip = new THREE.Mesh(cubeGeometry, pinMaterial);
tip.position.copy(end);
tip.scale.set(0.10, 0.10, 0.13);
tip.castShadow = true;
pinGroup.add(tip);
}
// ------------------------------------------------------------
// Numbered hole badges
// ------------------------------------------------------------
function makeBadge(number) {
const canvas = document.createElement("canvas");
canvas.width = 96;
canvas.height = 96;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(16, 18, 26, 0.94)";
context.fillRect(5, 5, 86, 86);
context.strokeStyle = "#f0bd58";
context.lineWidth = 5;
context.strokeRect(7, 7, 82, 82);
context.fillStyle = "#f6d487";
context.font = "800 46px sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(String(number).padStart(2, "0"), 48, 49);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.27, 0.27, 1);
sprite.renderOrder = 20;
return sprite;
}
const badges = holes.map((hole, index) => {
const badge = makeBadge(index + 1);
badge.position.set(hole.x, 0.52, 0.48);
scene.add(badge);
return badge;
});
// ------------------------------------------------------------
// Leather keeper around the free tail
// ------------------------------------------------------------
const keeperX = 4.72;
const keeperParts = [
{ x: keeperX, y: 1.04, z: 0.30, sx: 0.52, sy: 0.17, sz: 0.76 },
{ x: keeperX, y: -1.04, z: 0.30, sx: 0.52, sy: 0.17, sz: 0.76 },
{ x: keeperX, y: 0, z: 0.58, sx: 0.52, sy: 1.94, sz: 0.14 },
{ x: keeperX, y: 0, z: 0.02, sx: 0.52, sy: 1.94, sz: 0.14 }
];
addVoxels(
keeperParts.map(part => ({
...part,
color: colorVariant(0x7b4930, part.z > 0.5 ? 0.015 : -0.015)
})),
keeperMaterial
);
const keeperStitches = [];
for (const y of [-0.79, 0.79]) {
keeperStitches.push({
x: keeperX,
y,
z: 0.666,
sx: 0.055,
sy: 0.09,
sz: 0.025
});
}
for (const y of [-0.66, -0.33, 0, 0.33, 0.66]) {
keeperStitches.push({
x: keeperX - 0.205,
y,
z: 0.665,
sx: 0.035,
sy: 0.075,
sz: 0.025
});
}
addVoxels(keeperStitches, stitchMaterial, false);
const keeperTag = new THREE.Mesh(cubeGeometry, goldMaterial);
keeperTag.position.set(keeperX, 0.59, 0.69);
keeperTag.scale.set(0.25, 0.19, 0.07);
keeperTag.castShadow = true;
scene.add(keeperTag);
// ------------------------------------------------------------
// Invisible raycast targets for hole selection
// ------------------------------------------------------------
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const hitboxes = holes.map((hole, index) => {
const hitbox = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.30, 0.42),
hitMaterial
);
hitbox.position.set(hole.x, 0, 0.30);
hitbox.userData.holeIndex = index;
scene.add(hitbox);
return hitbox;
});
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const labelDefinitions = [
{
id: "buckleLabel",
text: "VOXEL BUCKLE",
anchor: new THREE.Vector3(-0.55, 1.65, 0.50),
offset: [-18, -8]
},
{
id: "selectedLabel",
text: "PIN ENGAGED",
anchor: new THREE.Vector3(holes[0].x, 0.78, 0.80),
offset: [36, -18]
},
{
id: "foldLabel",
text: "THREADED FOLD",
anchor: new THREE.Vector3(-1.55, 0.88, 0.72),
offset: [-60, -18]
},
{
id: "keeperLabel",
text: "KEEPER",
anchor: new THREE.Vector3(keeperX, 1.42, 0.60),
offset: [0, -10]
},
{
id: "tailLabel",
text: "FREE TAIL",
anchor: new THREE.Vector3(5.55, -1.15, 0.70),
offset: [12, 34]
}
];
const labels = labelDefinitions.map(definition => {
const element = document.createElement("div");
element.className = "scene-label";
element.id = definition.id;
element.textContent = definition.text;
document.querySelector(".hud").appendChild(element);
return {
...definition,
element
};
});
// ------------------------------------------------------------
// UI state and selection
// ------------------------------------------------------------
const statusElement = document.getElementById("status");
const holeButtons = document.getElementById("holeButtons");
let selectedHole = 0;
holes.forEach((hole, index) => {
const button = document.createElement("button");
button.type = "button";
button.className = "hole-button";
button.textContent = String(index + 1).padStart(2, "0");
button.title = `Select hole ${index + 1}`;
button.addEventListener("click", () => selectHole(index));
holeButtons.appendChild(button);
});
function selectHole(index) {
selectedHole = index;
const hole = holes[index];
rebuildPin(hole);
rebuildHalo(hole.x);
statusElement.textContent = `Engaged · Hole ${String(index + 1).padStart(2, "0")}`;
[...holeButtons.children].forEach((button, buttonIndex) => {
button.classList.toggle("active", buttonIndex === index);
});
badges.forEach((badge, badgeIndex) => {
const active = badgeIndex === index;
badge.material.opacity = active ? 1 : 0.62;
badge.scale.set(active ? 0.32 : 0.27, active ? 0.32 : 0.27, 1);
});
const selectedLabel = labels.find(label => label.id === "selectedLabel");
selectedLabel.anchor.set(hole.x, 0.78, 0.80);
}
selectHole(0);
// ------------------------------------------------------------
// Camera controls implemented directly
// ------------------------------------------------------------
const target = new THREE.Vector3(0, -0.08, 0);
const initialCamera = {
theta: 0.095,
phi: 1.31,
radius: 13.8
};
let theta = initialCamera.theta;
let phi = initialCamera.phi;
let radius = initialCamera.radius;
function updateCamera() {
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
function resetCamera() {
theta = initialCamera.theta;
phi = initialCamera.phi;
radius = initialCamera.radius;
updateCamera();
}
updateCamera();
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let pointerDown = false;
let pointerId = null;
let previousX = 0;
let previousY = 0;
let dragDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown = true;
pointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
dragDistance = 0;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerDown || event.pointerId !== pointerId) {
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
dragDistance += Math.hypot(dx, dy);
theta -= dx * 0.006;
phi -= dy * 0.005;
phi = THREE.MathUtils.clamp(phi, 0.67, 1.48);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
if (event.pointerId !== pointerId) {
return;
}
if (dragDistance < 5) {
pointer.x = (event.clientX / innerWidth) * 2 - 1;
pointer.y = -(event.clientY / innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(hitboxes, false)[0];
if (hit) {
selectHole(hit.object.userData.holeIndex);
}
}
pointerDown = false;
pointerId = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
pointerId = null;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
9.2,
19
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", event => {
if (/^[1-4]$/.test(event.key)) {
selectHole(Number(event.key) - 1);
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
// ------------------------------------------------------------
// Responsive resize and label projection
// ------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener("resize", resize);
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labels) {
projected.copy(label.anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * innerWidth + label.offset[0];
const y = (-projected.y * 0.5 + 0.5) * innerHeight + label.offset[1];
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
label.element.style.display =
projected.z > -1 && projected.z < 1 ? "block" : "none";
}
}
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
haloMaterial.emissiveIntensity = 0.85 + Math.sin(elapsed * 3.2) * 0.24;
updateLabels();
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("done");
});
</script>
</body>
</html>
```