Prompt specification
Build or customize the Three.js 3D scene "Bench between a gate and tree" 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: Bench between a gate and tree
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bench-between-a-gate-and-tree-x1337
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a voxel bench between a gate and a tree on a small plaza. Use a low garden gate at one side, a broad-canopied tree at the other, and a simple bench clearly occupying the space between them. Put an observer marker near the front edge with a visible facing arrow. Add left, right, and between labels interpreted from that observer's position, not from whichever direction the camera happens to face. The gate and tree should have different silhouettes so their roles remain easy to follow when the scene is viewed from the opposite side.
Working Three.js Starter (bench-between-a-gate-and-tree-x1337):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Plaza — Between the Gate and Tree</title>
<style>
:root {
--ink: #18312d;
--paper: #fffaf0;
--left: #55c6b8;
--between: #f5a15f;
--right: #9bd36b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e2;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(24, 49, 45, 0.16);
background: rgba(255, 250, 240, 0.91);
box-shadow: 0 12px 36px rgba(26, 57, 50, 0.13);
backdrop-filter: blur(10px);
}
#title {
top: 20px;
left: 20px;
max-width: min(390px, calc(100vw - 40px));
padding: 17px 19px 16px;
border-radius: 18px;
pointer-events: none;
}
#title .eyebrow {
margin-bottom: 5px;
color: #58726b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
#title h1 {
margin: 0;
font-size: clamp(20px, 3vw, 31px);
line-height: 1.04;
letter-spacing: -0.045em;
}
#title p {
margin: 9px 0 0;
max-width: 330px;
color: #4e6861;
font-size: 12px;
line-height: 1.45;
}
#controls {
top: 20px;
right: 20px;
display: flex;
gap: 7px;
padding: 7px;
border-radius: 15px;
}
button {
appearance: none;
border: 0;
border-radius: 10px;
padding: 10px 13px;
background: #e8eee8;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.02em;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
button:hover {
background: #dce8df;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
background: var(--ink);
color: var(--paper);
}
button.primary:hover {
background: #294a43;
}
#observer-key {
left: 20px;
bottom: 20px;
width: min(350px, calc(100vw - 40px));
padding: 14px 16px 15px;
border-radius: 18px;
pointer-events: none;
}
.key-title {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: #617871;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.key-title::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--between);
box-shadow: 0 0 0 4px rgba(245, 161, 95, 0.16);
content: "";
}
.key-grid {
display: grid;
grid-template-columns: 1fr 54px 1fr;
align-items: center;
gap: 8px;
}
.key-side {
padding: 9px 10px;
border-radius: 12px;
background: rgba(24, 49, 45, 0.055);
}
.key-side.left {
border-left: 4px solid var(--left);
}
.key-side.right {
border-right: 4px solid var(--right);
text-align: right;
}
.key-side span,
#between-key span {
display: block;
margin-bottom: 2px;
color: #647a73;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.key-side strong,
#between-key strong {
font-size: 14px;
}
.facing {
color: var(--between);
text-align: center;
font-size: 28px;
font-weight: 900;
line-height: 1;
}
.facing small {
display: block;
margin-top: 3px;
color: #647a73;
font-size: 7px;
letter-spacing: 0.12em;
}
#between-key {
margin-top: 8px;
padding: 7px 10px;
border-left: 4px solid var(--between);
border-radius: 8px;
background: rgba(245, 161, 95, 0.09);
}
#hint {
right: 20px;
bottom: 20px;
max-width: 310px;
padding: 10px 13px;
border-radius: 13px;
color: #536b64;
font-size: 10px;
font-weight: 700;
text-align: right;
pointer-events: none;
}
@media (max-width: 760px) {
#title {
top: 12px;
left: 12px;
max-width: calc(100vw - 24px);
}
#title p {
display: none;
}
#controls {
top: 104px;
right: 12px;
}
#observer-key {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" tabindex="0" aria-label="Interactive voxel plaza with a gate, bench, and tree"></canvas>
<section id="title" class="panel">
<div class="eyebrow">Observer-relative wayfinding</div>
<h1>Between the Gate & Tree</h1>
<p>
The labels belong to the observer at the front edge. They stay fixed even when
the camera moves to the opposite side.
</p>
</section>
<nav id="controls" class="panel" aria-label="Camera views">
<button id="front-view" class="primary" type="button">Observer View</button>
<button id="opposite-view" type="button">Opposite View</button>
</nav>
<aside id="observer-key" class="panel" aria-label="Observer-relative labels">
<div class="key-title">Fixed observer frame</div>
<div class="key-grid">
<div class="key-side left">
<span>Left</span>
<strong>Gate</strong>
</div>
<div class="facing">
↑
<small>Facing</small>
</div>
<div class="key-side right">
<span>Right</span>
<strong>Tree</strong>
</div>
</div>
<div id="between-key">
<span>Between</span>
<strong>Bench</strong>
</div>
</aside>
<div id="hint" class="panel">
Drag to orbit · Scroll or pinch to zoom · Arrow keys also work
</div>
<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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e2);
scene.fog = new THREE.Fog(0xdce9e2, 19, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
70
);
const orbit = {
theta: 0.28,
phi: 1.05,
radius: 14.8,
target: new THREE.Vector3(0, 1.45, 0),
thetaTarget: 0.28,
phiTarget: 1.05,
radiusTarget: 14.8
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf6fff5, 0x68816c, 2.25));
const sun = new THREE.DirectionalLight(0xfff1d1, 3.4);
sun.position.set(-7, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbfe5e0, 0.75);
fill.position.set(9, 6, -7);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const mats = {
grass: material(0x87b66f),
grassDark: material(0x699b58),
plazaEdge: material(0xc4b69e),
paverA: material(0xe8dfca),
paverB: material(0xddd1b9),
paverC: material(0xf0e6d0),
gate: material(0x3f7166),
gateLight: material(0x619487),
gateDark: material(0x294d47),
metal: material(0x304641, { metalness: 0.25, roughness: 0.62 }),
wood: material(0xb96739),
woodLight: material(0xd28a4b),
woodDark: material(0x8f4d31),
trunk: material(0x795039),
trunkLight: material(0x95674b),
leafA: material(0x568e49),
leafB: material(0x67a454),
leafC: material(0x7db65f),
leafD: material(0x477d43),
observer: material(0xf39b54),
observerLight: material(0xffc77f),
markerWhite: material(0xfffaf0),
soil: material(0x675044),
flowerA: material(0xf5cf5d),
flowerB: material(0xe98978),
flowerC: material(0xf4eee0)
};
function box(parent, sx, sy, sz, x, y, z, mat, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(sx, sy, sz), mat);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Small plaza and voxel landscaping
// -------------------------------------------------------------------------
box(scene, 20, 0.4, 15, 0, -0.45, 0, mats.grass);
box(scene, 14.4, 0.3, 9.5, 0, -0.14, 0, mats.plazaEdge);
// Individually tiled paving keeps the scene visibly voxel-built.
const tileSize = 0.88;
const tileGap = 0.055;
const tilePitch = tileSize + tileGap;
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 14; col++) {
const palette = [mats.paverA, mats.paverB, mats.paverC];
const mat = palette[(row * 2 + col) % palette.length];
const x = (col - 6.5) * tilePitch;
const z = (row - 4) * tilePitch;
box(scene, tileSize, 0.075, tileSize, x, 0.0125, z, mat, false);
}
}
// Dark grass patches around the plaza.
[
[-8.2, -0.16, -4.6, 2.8, 0.08, 1.8],
[8.1, -0.16, 4.8, 2.6, 0.08, 1.5],
[-8.0, -0.16, 4.6, 2.2, 0.08, 1.7],
[8.0, -0.16, -4.8, 2.4, 0.08, 1.5]
].forEach(([x, y, z, sx, sy, sz]) => {
box(scene, sx, sy, sz, x, y, z, mats.grassDark, false);
});
function addShrub(x, z, scale = 1) {
box(scene, 1.15 * scale, 0.48 * scale, 1.0 * scale, x, 0.28 * scale, z, mats.leafD);
box(scene, 0.85 * scale, 0.62 * scale, 0.78 * scale, x - 0.12 * scale, 0.64 * scale, z + 0.03 * scale, mats.leafA);
box(scene, 0.58 * scale, 0.44 * scale, 0.62 * scale, x + 0.35 * scale, 0.93 * scale, z - 0.04 * scale, mats.leafB);
}
addShrub(-7.3, 3.3, 0.9);
addShrub(7.25, 3.35, 0.95);
addShrub(-7.3, -3.7, 0.85);
addShrub(7.25, -3.75, 0.9);
// Small deterministic flower blocks.
const flowerSpots = [
[-7.55, 2.55], [-7.05, 2.25], [7.35, 2.45], [6.95, 2.85],
[-7.4, -2.9], [7.35, -3.0], [6.85, 3.65], [-6.8, 3.85]
];
flowerSpots.forEach(([x, z], i) => {
const flowerMat = [mats.flowerA, mats.flowerB, mats.flowerC][i % 3];
box(scene, 0.07, 0.35, 0.07, x, 0.2, z, mats.leafD, false);
box(scene, 0.2, 0.18, 0.2, x, 0.45, z, flowerMat, false);
});
// -------------------------------------------------------------------------
// LEFT: Low garden gate, with a rectangular slatted silhouette
// -------------------------------------------------------------------------
const gate = new THREE.Group();
gate.position.set(-4.35, 0.06, -1.2);
scene.add(gate);
// Gate posts and cap stones.
box(gate, 0.28, 2.12, 0.3, -1.16, 1.06, 0, mats.gateDark);
box(gate, 0.28, 2.12, 0.3, 1.16, 1.06, 0, mats.gateDark);
box(gate, 0.42, 0.16, 0.42, -1.16, 2.16, 0, mats.gateLight);
box(gate, 0.42, 0.16, 0.42, 1.16, 2.16, 0, mats.gateLight);
// Strong horizontal rails create an unmistakable gate/fence silhouette.
box(gate, 2.42, 0.18, 0.2, 0, 0.48, 0.02, mats.gate);
box(gate, 2.42, 0.18, 0.2, 0, 1.56, 0.02, mats.gate);
// Vertical gate slats.
for (let i = -2; i <= 2; i++) {
box(gate, 0.19, 1.55, 0.18, i * 0.43, 0.98, 0.03, mats.gateLight);
}
// Short fence wings distinguish this from the central bench.
for (const side of [-1, 1]) {
box(gate, 1.22, 0.15, 0.18, side * 1.86, 0.5, 0.02, mats.gate);
box(gate, 1.22, 0.15, 0.18, side * 1.86, 1.22, 0.02, mats.gate);
box(gate, 0.24, 1.58, 0.25, side * 2.48, 0.79, 0.02, mats.gateDark);
box(gate, 0.34, 0.13, 0.34, side * 2.48, 1.61, 0.02, mats.gateLight);
}
// Hinges and a bright latch.
box(gate, 0.14, 0.28, 0.28, -1.04, 0.57, 0, mats.metal);
box(gate, 0.14, 0.28, 0.28, -1.04, 1.47, 0, mats.metal);
box(gate, 0.42, 0.13, 0.13, 0.94, 1.02, 0.19, mats.observer);
// -------------------------------------------------------------------------
// RIGHT: Broad-canopied voxel tree, intentionally different in silhouette
// -------------------------------------------------------------------------
const tree = new THREE.Group();
tree.position.set(4.25, 0.06, -0.55);
scene.add(tree);
box(tree, 1.4, 0.14, 1.35, 0, 0.07, 0, mats.soil);
box(tree, 0.68, 2.65, 0.68, 0, 1.33, 0, mats.trunk);
box(tree, 0.82, 0.56, 0.78, -0.06, 2.1, 0, mats.trunkLight);
// Chunky branches.
const branchA = box(tree, 1.75, 0.38, 0.42, -0.65, 2.48, 0.02, mats.trunk);
branchA.rotation.z = 0.18;
const branchB = box(tree, 1.55, 0.35, 0.4, 0.66, 2.58, -0.04, mats.trunk);
branchB.rotation.z = -0.22;
// Stepped cubic canopy: broad and irregular rather than gate-like.
const canopy = [
[0.00, 3.20, 0.00, 2.35, 1.80, 2.10, mats.leafA],
[-1.30, 3.10, 0.05, 1.75, 1.55, 1.72, mats.leafB],
[1.32, 3.12, -0.06, 1.82, 1.52, 1.78, mats.leafA],
[-0.45, 4.22, -0.18, 2.05, 1.22, 1.76, mats.leafB],
[0.92, 4.13, 0.22, 1.78, 1.18, 1.64, mats.leafC],
[-1.22, 3.87, -0.70, 1.34, 1.10, 1.28, mats.leafD],
[1.25, 3.79, -0.68, 1.36, 1.12, 1.34, mats.leafB],
[-0.35, 2.72, 0.42, 2.38, 1.17, 1.90, mats.leafD],
[0.48, 2.77, -0.63, 1.82, 1.15, 1.53, mats.leafA]
];
canopy.forEach(([x, y, z, sx, sy, sz, mat]) => {
box(tree, sx, sy, sz, x, y, z, mat);
});
// A few brighter top voxels suggest sunlit leaves.
box(tree, 0.82, 0.34, 0.82, -0.55, 4.82, -0.12, mats.leafC);
box(tree, 0.92, 0.30, 0.78, 0.95, 4.73, 0.22, mats.leafB);
// -------------------------------------------------------------------------
// BETWEEN: Clearly readable bench
// -------------------------------------------------------------------------
const bench = new THREE.Group();
bench.position.set(0, 0.07, 0.12);
scene.add(bench);
// Four chunky supports.
for (const x of [-1.35, 1.35]) {
for (const z of [-0.28, 0.28]) {
box(bench, 0.18, 0.73, 0.18, x, 0.365, z, mats.metal);
}
}
// Seat slats run toward the observer.
for (let i = 0; i < 4; i++) {
box(
bench,
3.38,
0.17,
0.16,
0,
0.78,
-0.27 + i * 0.18,
i % 2 === 0 ? mats.woodLight : mats.wood
);
}
// Back supports and horizontal backrest slats.
box(bench, 0.17, 1.42, 0.17, -1.34, 1.12, -0.43, mats.metal);
box(bench, 0.17, 1.42, 0.17, 1.34, 1.12, -0.43, mats.metal);
box(bench, 3.38, 0.18, 0.16, 0, 1.05, -0.44, mats.wood);
box(bench, 3.38, 0.18, 0.16, 0, 1.37, -0.44, mats.woodLight);
box(bench, 3.38, 0.18, 0.16, 0, 1.68, -0.44, mats.wood);
// Lower stretcher.
box(bench, 2.75, 0.12, 0.13, 0, 0.38, 0, mats.metal);
// -------------------------------------------------------------------------
// Observer marker and a large ground-facing arrow
// -------------------------------------------------------------------------
const observer = new THREE.Group();
observer.position.set(0, 0.07, 4.03);
scene.add(observer);
box(observer, 1.18, 0.13, 1.05, 0, 0.065, 0, mats.gateDark);
box(observer, 0.78, 0.10, 0.74, 0, 0.16, 0, mats.observer);
// Marker post, white head, and a small forward-facing nose.
box(observer, 0.16, 0.95, 0.16, 0, 0.68, 0, mats.observer);
box(observer, 0.39, 0.39, 0.39, 0, 1.30, 0, mats.markerWhite);
box(observer, 0.13, 0.13, 0.25, 0, 1.30, -0.26, mats.observer);
// Flat voxel-style arrow shape, pointing from the observer toward -Z.
function makeArrowShape() {
const shape = new THREE.Shape();
shape.moveTo(0, 1.0);
shape.lineTo(-0.58, 0.10);
shape.lineTo(-0.22, 0.10);
shape.lineTo(-0.22, -0.86);
shape.lineTo(0.22, -0.86);
shape.lineTo(0.22, 0.10);
shape.lineTo(0.58, 0.10);
shape.closePath();
return shape;
}
const arrowGeometry = new THREE.ShapeGeometry(makeArrowShape());
const arrowOutline = new THREE.Mesh(
arrowGeometry,
material(0x8c4b2d, { side: THREE.DoubleSide })
);
arrowOutline.rotation.x = -Math.PI / 2;
arrowOutline.position.set(0, 0.055, -0.85);
arrowOutline.scale.set(1.08, 1.08, 1.08);
arrowOutline.receiveShadow = true;
scene.add(arrowOutline);
const arrowFill = new THREE.Mesh(
arrowGeometry,
material(0xf39b54, {
emissive: 0x6b2d08,
emissiveIntensity: 0.16,
side: THREE.DoubleSide
})
);
arrowFill.rotation.x = -Math.PI / 2;
arrowFill.position.set(0, 0.069, -0.85);
arrowFill.scale.set(0.88, 0.88, 0.88);
arrowFill.receiveShadow = true;
scene.add(arrowFill);
// -------------------------------------------------------------------------
// World-space labels
//
// These are deliberately tied to world objects and to the observer's fixed
// frame. Orbiting the camera never changes LEFT, BETWEEN, or RIGHT.
// -------------------------------------------------------------------------
function roundedRectPath(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 createLabel(kicker, title, accent, width = 3.45) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 180;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 180);
ctx.save();
ctx.shadowColor = "rgba(20, 40, 35, 0.22)";
ctx.shadowBlur = 20;
ctx.shadowOffsetY = 9;
roundedRectPath(ctx, 10, 8, 492, 158, 28);
ctx.fillStyle = "rgba(24, 49, 45, 0.95)";
ctx.fill();
ctx.restore();
roundedRectPath(ctx, 10, 8, 492, 158, 28);
ctx.strokeStyle = accent;
ctx.lineWidth = 8;
ctx.stroke();
ctx.fillStyle = accent;
roundedRectPath(ctx, 30, 31, 10, 24, 5);
ctx.fill();
ctx.textAlign = "center";
ctx.fillStyle = "#bfd1ca";
ctx.font = "800 22px system-ui, sans-serif";
ctx.fillText(kicker.toUpperCase(), 271, 51);
ctx.fillStyle = "#fffaf0";
ctx.font = "900 58px system-ui, sans-serif";
ctx.fillText(title.toUpperCase(), 256, 119);
ctx.strokeStyle = "rgba(255,255,255,0.13)";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(65, 141);
ctx.lineTo(447, 141);
ctx.stroke();
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width * (180 / 512), 1);
sprite.renderOrder = 30;
return sprite;
}
const gateLabel = createLabel("Observer left", "Gate", "#55c6b8", 3.35);
gateLabel.position.set(-4.35, 2.92, -1.2);
scene.add(gateLabel);
const benchLabel = createLabel("Observer between", "Bench", "#f5a15f", 3.65);
benchLabel.position.set(0, 2.48, 0.12);
scene.add(benchLabel);
const treeLabel = createLabel("Observer right", "Tree", "#9bd36b", 3.35);
treeLabel.position.set(4.25, 5.62, -0.55);
scene.add(treeLabel);
const observerLabel = createLabel("Front edge", "Observer", "#f5a15f", 2.55);
observerLabel.position.set(0, 2.02, 4.03);
scene.add(observerLabel);
// -------------------------------------------------------------------------
// Custom camera controls — no addons or examples modules
// -------------------------------------------------------------------------
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
const pointers = new Map();
let previousPinchDistance = 0;
function pointerDistance() {
const points = [...pointers.values()];
if (points.length < 2) return 0;
return Math.hypot(points[0].x - points[1].x, points[0].y - points[1].y);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.focus({ preventScroll: true });
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const current = { x: event.clientX, y: event.clientY };
pointers.set(event.pointerId, current);
if (pointers.size === 1) {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
orbit.thetaTarget -= dx * 0.006;
orbit.phiTarget = clamp(orbit.phiTarget - dy * 0.005, 0.45, 1.38);
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
orbit.radiusTarget = clamp(
orbit.radiusTarget * (previousPinchDistance / distance),
8.2,
23
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
if (pointers.size === 0) canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radiusTarget = clamp(
orbit.radiusTarget * Math.exp(event.deltaY * 0.001),
8.2,
23
);
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") orbit.thetaTarget -= 0.12;
else if (key === "ArrowRight") orbit.thetaTarget += 0.12;
else if (key === "ArrowUp") orbit.phiTarget = clamp(orbit.phiTarget - 0.08, 0.45, 1.38);
else if (key === "ArrowDown") orbit.phiTarget = clamp(orbit.phiTarget + 0.08, 0.45, 1.38);
else if (key === "+" || key === "=") orbit.radiusTarget = clamp(orbit.radiusTarget - 0.8, 8.2, 23);
else if (key === "-") orbit.radiusTarget = clamp(orbit.radiusTarget + 0.8, 8.2, 23);
else return;
event.preventDefault();
});
document.querySelector("#front-view").addEventListener("click", () => {
orbit.thetaTarget = 0;
orbit.phiTarget = 1.05;
orbit.radiusTarget = 14.8;
});
document.querySelector("#opposite-view").addEventListener("click", () => {
orbit.thetaTarget = Math.PI;
orbit.phiTarget = 1.05;
orbit.radiusTarget = 14.8;
});
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
function animate() {
const easing = 0.105;
orbit.theta += (orbit.thetaTarget - orbit.theta) * easing;
orbit.phi += (orbit.phiTarget - orbit.phi) * easing;
orbit.radius += (orbit.radiusTarget - orbit.radius) * easing;
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```