Prompt specification
Build or customize the Three.js 3D scene "Upstream Workshop Downstream Garden" 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: Upstream Workshop Downstream Garden
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/upstream-workshop-downstream-garden-x3151
Scene Description Prompt:
Follow a harmless illustrative marker downstream from a fictional voxel workshop toward a community garden. One HTML file using Three.js from cdnjs.
Working Three.js Starter (upstream-workshop-downstream-garden-x3151):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Downstream — A Voxel Journey</title>
<style>
:root {
color-scheme: light;
--ink: #18352f;
--paper: rgba(255, 252, 240, 0.92);
--accent: #f4b942;
--water: #429fbe;
--leaf: #4f8a58;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9dce0;
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;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(24, 53, 47, 0.14);
background: var(--paper);
color: var(--ink);
box-shadow: 0 14px 38px rgba(24, 53, 47, 0.14);
backdrop-filter: blur(12px);
}
.story {
top: 18px;
left: 18px;
width: min(380px, calc(100vw - 36px));
padding: 18px 19px 16px;
border-radius: 18px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
color: #46766a;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(244, 185, 66, 0.2);
content: "";
}
h1 {
margin: 0 0 8px;
font-size: clamp(24px, 4vw, 34px);
line-height: 1.02;
letter-spacing: -0.04em;
}
.story p {
margin: 0;
color: #49675f;
font-size: 13px;
line-height: 1.55;
}
.tags {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 14px;
}
.tag {
padding: 6px 9px;
border: 1px solid rgba(24, 53, 47, 0.12);
border-radius: 999px;
background: rgba(255, 255, 255, 0.58);
color: #41685e;
font-size: 11px;
font-weight: 700;
}
.controls {
right: 18px;
bottom: 58px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
padding: 8px;
border-radius: 15px;
}
button {
appearance: none;
border: 1px solid rgba(24, 53, 47, 0.14);
border-radius: 10px;
padding: 10px 13px;
background: rgba(255, 255, 255, 0.82);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-1px);
background: white;
}
button.active {
border-color: transparent;
background: var(--ink);
color: white;
}
button:focus-visible {
outline: 3px solid rgba(244, 185, 66, 0.7);
outline-offset: 2px;
}
.journey {
right: 18px;
bottom: 18px;
left: 18px;
display: grid;
grid-template-columns: auto minmax(120px, 1fr) auto;
align-items: center;
gap: 14px;
padding: 11px 14px;
border-radius: 14px;
}
.journey-label,
.journey-status {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
}
.journey-status {
color: #5c776f;
text-align: right;
}
.track {
position: relative;
height: 7px;
overflow: hidden;
border-radius: 999px;
background: #dbe5d9;
}
.fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #e8a936, #4b9eb8);
transition: width 80ms linear;
}
.hint {
position: fixed;
z-index: 5;
bottom: 75px;
left: 18px;
padding: 7px 10px;
border-radius: 9px;
background: rgba(24, 53, 47, 0.75);
color: white;
font-size: 10px;
font-weight: 650;
pointer-events: none;
}
@media (max-width: 680px) {
.story {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 14px;
}
.story p {
font-size: 12px;
}
.tags { display: none; }
.controls {
right: 10px;
bottom: 58px;
}
.hint { display: none; }
.journey {
right: 10px;
bottom: 10px;
left: 10px;
grid-template-columns: auto 1fr;
}
.journey-status { display: none; }
}
</style>
</head>
<body>
<section class="panel story" aria-labelledby="title">
<div class="eyebrow">A small downstream story</div>
<h1 id="title">One marker.<br>Two welcoming places.</h1>
<p>
A harmless visual cube travels from the voxel workshop, along the
workshop channel, toward the community garden. It carries nothing but
a painted leaf.
</p>
<div class="tags">
<span class="tag">Visual marker only</span>
<span class="tag">Voxel diorama</span>
<span class="tag">No external assets</span>
</div>
</section>
<div class="hint">Drag to orbit · wheel/pinch to zoom · shift-drag to pan</div>
<nav class="panel controls" aria-label="Scene controls">
<button id="playButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay</button>
<button id="overviewButton" class="active" type="button">Overview</button>
<button id="followButton" type="button">Follow marker</button>
</nav>
<section class="panel journey" aria-label="Marker journey progress">
<span class="journey-label">Workshop</span>
<div
id="progressTrack"
class="track"
role="progressbar"
aria-label="Marker progress to the community garden"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="0"
>
<div id="progressFill" class="fill"></div>
</div>
<span id="journeyStatus" class="journey-status" aria-live="polite">
Marker entering the channel
</span>
</section>
<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 renderer = new THREE.WebGLRenderer({ antialias: true });
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 voxel landscape showing a marker traveling from a workshop to a community garden"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9dce0);
scene.fog = new THREE.Fog(0xb9dce0, 40, 76);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const hemisphere = new THREE.HemisphereLight(0xdaf4ff, 0x66734a, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2cc, 3.2);
sun.position.set(-16, 28, 15);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const clock = new THREE.Clock();
// -------------------------------------------------------------------------
// Materials and reusable helpers
// -------------------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
grass: material(0x7ea866),
grassDark: material(0x668c53),
earth: material(0x8d6847),
soil: material(0x684734),
wood: material(0x9b7047),
woodDark: material(0x5d4938),
wall: material(0xe4c89a),
wallShade: material(0xcda979),
roof: material(0x527d83),
roofDark: material(0x385b61),
window: material(0x86cbd5, { roughness: 0.2, metalness: 0.08 }),
water: material(0x429fbe, { roughness: 0.25, metalness: 0.06 }),
waterLight: material(0xa6e0e6),
bank: material(0xd2b98a),
leaf: material(0x4d8a57),
leafLight: material(0x78aa5c),
leafDark: material(0x346848),
cream: material(0xf4ead0),
orange: material(0xf4b942),
charcoal: material(0x344640),
red: material(0xbf6555),
blue: material(0x4f7892),
skin: material(0xd9a477),
white: material(0xf7f5e9)
};
function box(w, h, d, mat, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function labelSprite(text, width = 6.2) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 192;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(255, 252, 240, 0.94)";
ctx.fillRect(10, 20, 1004, 140);
ctx.strokeStyle = "rgba(24, 53, 47, 0.24)";
ctx.lineWidth = 8;
ctx.strokeRect(14, 24, 996, 132);
ctx.fillStyle = "#244b41";
ctx.font = "800 56px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 512, 91);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
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 * 192 / 1024, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Terrain
// -------------------------------------------------------------------------
box(38, 1.1, 28, materials.earth, 0, -0.65, 0);
box(37.6, 0.22, 27.6, materials.grass, 0, -0.03, 0);
// Quiet checkerboard-like patches give the ground a handmade voxel texture.
for (let x = -17; x <= 17; x += 4) {
for (let z = -11; z <= 11; z += 4) {
const patch = box(
2.3,
0.025,
2.3,
((x + z) / 4) % 2 === 0 ? materials.grassDark : materials.grass,
x,
0.095,
z
);
patch.castShadow = false;
}
}
// -------------------------------------------------------------------------
// Downstream route
// -------------------------------------------------------------------------
const routePoints = [
new THREE.Vector3(-12.1, 0.27, -5.05),
new THREE.Vector3(-10.0, 0.27, -4.15),
new THREE.Vector3(-7.5, 0.27, -2.8),
new THREE.Vector3(-4.2, 0.27, -1.15),
new THREE.Vector3(-1.0, 0.27, 1.05),
new THREE.Vector3(2.1, 0.27, 2.75),
new THREE.Vector3(5.2, 0.27, 4.35),
new THREE.Vector3(8.2, 0.27, 5.85),
new THREE.Vector3(10.8, 0.27, 6.65),
new THREE.Vector3(12.8, 0.27, 5.85),
new THREE.Vector3(14.25, 0.27, 4.85)
];
const route = new THREE.CatmullRomCurve3(
routePoints,
false,
"catmullrom",
0.38
);
const segmentCount = 76;
for (let i = 0; i < segmentCount; i++) {
const t0 = i / segmentCount;
const t1 = (i + 1) / segmentCount;
const a = route.getPointAt(t0);
const b = route.getPointAt(t1);
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz) * 1.18;
const angle = Math.atan2(dx, dz);
const nx = -dz / Math.hypot(dx, dz);
const nz = dx / Math.hypot(dx, dz);
const water = box(
1.84,
0.12,
length,
materials.water,
(a.x + b.x) * 0.5,
0.17,
(a.z + b.z) * 0.5
);
water.rotation.y = angle;
water.castShadow = false;
for (const side of [-1, 1]) {
const bank = box(
0.48,
0.18,
length,
materials.bank,
(a.x + b.x) * 0.5 + nx * 1.15 * side,
0.11,
(a.z + b.z) * 0.5 + nz * 1.15 * side
);
bank.rotation.y = angle;
bank.castShadow = false;
}
}
// Small downstream glints make the direction of the water readable.
const flowGlints = [];
const glintGeometry = new THREE.BoxGeometry(0.42, 0.025, 0.08);
for (let i = 0; i < 25; i++) {
const glint = new THREE.Mesh(glintGeometry, materials.waterLight);
glint.castShadow = false;
glint.receiveShadow = false;
scene.add(glint);
flowGlints.push(glint);
}
// -------------------------------------------------------------------------
// Voxel workshop
// -------------------------------------------------------------------------
const workshop = new THREE.Group();
scene.add(workshop);
box(8.2, 0.28, 6.4, materials.woodDark, -15.1, 0.17, -6.0, workshop);
box(5.7, 3.4, 4.6, materials.wall, -15.1, 2.05, -6.0, workshop);
// Stepped voxel roof.
box(6.4, 0.48, 5.25, materials.roofDark, -15.1, 3.98, -6.0, workshop);
box(5.35, 0.48, 4.3, materials.roof, -15.1, 4.46, -6.0, workshop);
box(4.15, 0.48, 3.25, materials.roofDark, -15.1, 4.94, -6.0, workshop);
box(2.9, 0.48, 2.2, materials.roof, -15.1, 5.42, -6.0, workshop);
// Front door and windows.
box(1.25, 2.15, 0.12, materials.woodDark, -15.1, 1.4, -3.66, workshop);
box(0.86, 0.34, 0.15, materials.window, -15.1, 1.8, -3.58, workshop);
box(0.12, 0.18, 0.1, materials.orange, -14.65, 1.2, -3.56, workshop);
for (const x of [-16.65, -13.55]) {
box(1.18, 1.05, 0.12, materials.woodDark, x, 2.2, -3.65, workshop);
box(0.93, 0.8, 0.14, materials.window, x, 2.2, -3.57, workshop);
box(0.08, 0.83, 0.17, materials.cream, x, 2.2, -3.48, workshop);
box(0.96, 0.08, 0.17, materials.cream, x, 2.2, -3.47, workshop);
}
// Chimney, outlet pipe, and hanging workshop sign.
box(0.8, 1.8, 0.8, materials.wallShade, -16.1, 5.2, -6.6, workshop);
box(1.05, 0.3, 1.05, materials.charcoal, -16.1, 6.15, -6.6, workshop);
box(1.25, 0.48, 0.65, materials.roofDark, -11.95, 0.48, -5.05, workshop);
box(1.3, 0.14, 0.72, materials.waterLight, -11.65, 0.31, -5.05, workshop);
const workshopLabel = labelSprite("VOXEL WORKSHOP", 6.1);
workshopLabel.position.set(-15.1, 6.85, -6.0);
scene.add(workshopLabel);
// Crates, timber, and a barrel beside the workshop.
function crate(x, y, z, size = 0.85, parent = scene) {
box(size, size, size, materials.wood, x, y + size / 2, z, parent);
box(size * 1.03, 0.1, size * 1.03, materials.woodDark, x, y + size * 0.77, z, parent);
box(size * 1.03, 0.1, size * 1.03, materials.woodDark, x, y + size * 0.23, z, parent);
}
crate(-11.3, 0.1, -7.25, 0.9, workshop);
crate(-12.0, 0.1, -7.1, 0.72, workshop);
crate(-11.85, 1.0, -7.18, 0.64, workshop);
for (let i = 0; i < 3; i++) {
box(1.7, 0.28, 0.34, materials.woodDark, -17.6, 0.28 + i * 0.3, -2.5, workshop);
}
// -------------------------------------------------------------------------
// Community garden
// -------------------------------------------------------------------------
const garden = new THREE.Group();
scene.add(garden);
// A soft garden lawn.
box(9.5, 0.12, 8.2, materials.grassLight ?? materials.grass, 14.2, 0.13, 8.3, garden);
// Raised beds.
function raisedBed(x, z, width = 3.3, depth = 1.8) {
box(width, 0.35, depth, materials.woodDark, x, 0.32, z, garden);
box(width - 0.25, 0.22, depth - 0.25, materials.soil, x, 0.48, z, garden);
const columns = Math.max(3, Math.floor(width / 0.65));
const rows = 2;
for (let i = 0; i < columns; i++) {
for (let row = 0; row < rows; row++) {
const px = x - width * 0.38 + (i / (columns - 1)) * width * 0.76;
const pz = z - depth * 0.22 + row * depth * 0.44;
const height = 0.28 + ((i + row) % 3) * 0.08;
box(0.08, height, 0.08, materials.leafDark, px, 0.59 + height / 2, pz, garden);
box(0.34, 0.13, 0.22, materials.leaf, px - 0.08, 0.66 + height * 0.7, pz, garden);
box(0.26, 0.12, 0.22, materials.leafLight, px + 0.11, 0.76 + height * 0.7, pz, garden);
}
}
}
raisedBed(10.2, 9.0, 3.6, 1.8);
raisedBed(14.2, 9.0, 3.6, 1.8);
raisedBed(17.0, 7.0, 1.7, 3.6);
// Back and side fences.
function fencePost(x, z) {
box(0.18, 1.3, 0.18, materials.cream, x, 0.72, z, garden);
box(0.25, 0.12, 0.25, materials.woodDark, x, 1.38, z, garden);
}
for (let x = 8.4; x <= 19; x += 1.45) fencePost(x, 11.3);
box(10.9, 0.13, 0.13, materials.cream, 13.7, 0.65, 11.3, garden);
box(10.9, 0.13, 0.13, materials.cream, 13.7, 1.08, 11.3, garden);
for (let z = 5.4; z <= 11.3; z += 1.45) fencePost(19, z);
box(0.13, 0.13, 6.0, materials.cream, 19, 0.65, 8.4, garden);
box(0.13, 0.13, 6.0, materials.cream, 19, 1.08, 8.4, garden);
// Rain barrel marks the end of the illustrated route.
box(1.18, 1.45, 1.18, materials.blue, 14.35, 0.8, 4.05, garden);
box(1.32, 0.16, 1.32, materials.charcoal, 14.35, 1.58, 4.05, garden);
box(1.28, 0.12, 1.28, materials.cream, 14.35, 1.0, 4.05, garden);
box(0.16, 0.9, 0.16, materials.charcoal, 14.85, 1.65, 3.72, garden);
// Small bench.
box(2.7, 0.18, 0.65, materials.wood, 8.9, 0.62, 6.2, garden);
box(2.7, 0.72, 0.16, materials.wood, 8.9, 1.05, 6.48, garden);
for (const x of [7.85, 9.95]) {
box(0.18, 0.65, 0.48, materials.woodDark, x, 0.36, 6.2, garden);
}
const gardenLabel = labelSprite("COMMUNITY GARDEN", 6.5);
gardenLabel.position.set(14.1, 3.05, 10.6);
scene.add(gardenLabel);
// -------------------------------------------------------------------------
// Trees, flowers, and small voxel people
// -------------------------------------------------------------------------
function tree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.08, z);
group.scale.setScalar(scale);
scene.add(group);
box(0.5, 2.1, 0.5, materials.woodDark, 0, 1.05, 0, group);
box(2.25, 1.35, 1.75, materials.leafDark, 0, 2.45, 0, group);
box(1.65, 1.2, 1.55, materials.leaf, -0.3, 3.15, 0.05, group);
box(1.25, 1.0, 1.25, materials.leafLight, 0.3, 3.65, -0.05, group);
box(1.1, 0.8, 1.0, materials.leaf, -0.75, 2.75, 0.35, group);
}
[
[-6.5, -9.7, 1.1],
[-0.8, -10.2, 0.85],
[5.1, -9.6, 1.05],
[8.0, -7.5, 0.78],
[-14.0, 4.9, 1.15],
[-8.4, 7.8, 1.0],
[-3.5, 9.5, 0.8],
[5.9, 10.4, 0.9]
].forEach(([x, z, scale]) => tree(x, z, scale));
function flower(x, z, colorMaterial) {
box(0.05, 0.28, 0.05, materials.leafDark, x, 0.23, z);
box(0.18, 0.12, 0.18, colorMaterial, x, 0.4, z);
}
const flowerMaterials = [
material(0xe28d75),
material(0xf0d15f),
material(0xd98ab4)
];
[
[8.1, 8.0], [8.7, 8.5], [9.4, 8.2],
[11.9, 7.4], [12.6, 7.8], [17.8, 9.8],
[18.3, 10.5], [7.5, 10.4]
].forEach(([x, z], i) => flower(x, z, flowerMaterials[i % 3]));
function person(x, z, shirt, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.1, z);
group.rotation.y = rotation;
scene.add(group);
box(0.2, 0.58, 0.22, materials.charcoal, -0.16, 0.32, 0, group);
box(0.2, 0.58, 0.22, materials.charcoal, 0.16, 0.32, 0, group);
box(0.58, 0.68, 0.36, shirt, 0, 0.94, 0, group);
box(0.16, 0.62, 0.18, shirt, -0.4, 0.92, 0, group);
box(0.16, 0.62, 0.18, shirt, 0.4, 0.92, 0, group);
box(0.44, 0.44, 0.42, materials.skin, 0, 1.52, 0, group);
box(0.47, 0.12, 0.45, materials.woodDark, 0, 1.78, 0, group);
box(0.07, 0.08, 0.03, materials.charcoal, -0.1, 1.53, 0.22, group);
box(0.07, 0.08, 0.03, materials.charcoal, 0.1, 1.53, 0.22, group);
}
person(11.2, 6.8, materials.red, -0.7);
person(16.6, 9.7, materials.blue, 2.4);
person(-10.2, -7.8, materials.orange, 0.5);
// -------------------------------------------------------------------------
// The harmless illustrative marker
// -------------------------------------------------------------------------
const marker = new THREE.Group();
scene.add(marker);
const markerBody = box(0.58, 0.58, 0.58, materials.orange, 0, 0.36, 0, marker);
box(0.61, 0.12, 0.61, materials.cream, 0, 0.36, 0, marker);
box(0.16, 0.15, 0.1, materials.leaf, -0.1, 0.43, 0.32, marker);
box(0.16, 0.15, 0.1, materials.leafLight, 0.1, 0.5, 0.32, marker);
box(0.07, 0.38, 0.07, materials.woodDark, 0, 0.82, 0, marker);
box(0.38, 0.23, 0.05, materials.white, 0.2, 0.96, 0, marker);
// A soft halo helps the tiny marker remain visible against the water.
const halo = new THREE.Mesh(
new THREE.RingGeometry(0.48, 0.62, 24),
new THREE.MeshBasicMaterial({
color: 0xffe5a3,
transparent: true,
opacity: 0.72,
side: THREE.DoubleSide,
depthWrite: false
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = 0.08;
marker.add(halo);
// -------------------------------------------------------------------------
// Animation and UI state
// -------------------------------------------------------------------------
const playButton = document.getElementById("playButton");
const replayButton = document.getElementById("replayButton");
const overviewButton = document.getElementById("overviewButton");
const followButton = document.getElementById("followButton");
const progressFill = document.getElementById("progressFill");
const progressTrack = document.getElementById("progressTrack");
const journeyStatus = document.getElementById("journeyStatus");
let playing = true;
let markerT = 0;
let endpointHold = 0;
let simulationTime = 0;
let cameraMode = "overview";
const journeyDuration = 34;
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "Pause" : "Play";
}
function replay() {
markerT = 0;
endpointHold = 0;
setPlaying(true);
journeyStatus.textContent = "Marker entering the channel";
}
function setCameraMode(mode) {
cameraMode = mode;
overviewButton.classList.toggle("active", mode === "overview");
followButton.classList.toggle("active", mode === "follow");
if (mode === "overview") {
orbit.radius = 34;
orbit.phi = 0.9;
} else {
orbit.radius = 12.5;
orbit.phi = 0.96;
}
}
playButton.addEventListener("click", () => setPlaying(!playing));
replayButton.addEventListener("click", replay);
overviewButton.addEventListener("click", () => setCameraMode("overview"));
followButton.addEventListener("click", () => setCameraMode("follow"));
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
}
if (event.key.toLowerCase() === "r") replay();
if (event.key.toLowerCase() === "f") {
setCameraMode(cameraMode === "follow" ? "overview" : "follow");
}
});
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, pan, and touch pinch
// -------------------------------------------------------------------------
const orbit = {
theta: 0.57,
phi: 0.9,
radius: 34
};
const cameraTarget = new THREE.Vector3(0, 0.7, 1.2);
const desiredTarget = cameraTarget.clone();
const overviewTarget = new THREE.Vector3(0, 0.7, 1.2);
function updateCamera(dt) {
desiredTarget.copy(
cameraMode === "follow" ? marker.position : overviewTarget
);
const damping = 1 - Math.exp(-dt * 5);
cameraTarget.lerp(desiredTarget, damping);
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
const pointers = new Map();
let panMode = false;
let previousX = 0;
let previousY = 0;
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
panMode = event.button === 2 || event.shiftKey;
previousX = event.clientX;
previousY = event.clientY;
previousPinchDistance = pointerDistance();
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size >= 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * previousPinchDistance / distance,
7,
55
);
}
previousPinchDistance = distance;
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panMode) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
const right = new THREE.Vector3().crossVectors(forward, camera.up).normalize();
const scale = orbit.radius * 0.0015;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.y += dy * scale * 0.65;
desiredTarget.copy(cameraTarget);
} else {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.28, 1.42);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointerDistance();
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("lostpointercapture", endPointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
7,
55
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const tangent = new THREE.Vector3();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
simulationTime += dt;
if (markerT < 1) {
markerT = Math.min(1, markerT + dt / journeyDuration);
endpointHold = 0;
if (markerT < 0.25) {
journeyStatus.textContent = "Leaving the workshop outflow";
} else if (markerT < 0.72) {
journeyStatus.textContent = "Traveling through the channel";
} else {
journeyStatus.textContent = "Approaching the garden";
}
} else {
endpointHold += dt;
journeyStatus.textContent = "Safely arrived at the garden";
if (endpointHold > 2.8) {
markerT = 0;
endpointHold = 0;
}
}
for (let i = 0; i < flowGlints.length; i++) {
const t = (i / flowGlints.length + simulationTime * 0.035) % 1;
const point = route.getPointAt(t);
const next = route.getPointAt(Math.min(1, t + 0.002));
const side = Math.sin(i * 12.7) * 0.56;
point.x += -(next.z - point.z) * side;
point.z += (next.x - point.x) * side;
point.y = 0.245;
flowGlints[i].position.copy(point);
flowGlints[i].rotation.y = Math.atan2(next.x - point.x, next.z - point.z);
}
}
const point = route.getPointAt(markerT);
route.getTangentAt(markerT, tangent);
marker.position.copy(point);
marker.position.y += Math.sin(simulationTime * 4.5) * 0.055;
marker.rotation.y = Math.atan2(tangent.x, tangent.z);
marker.rotation.z = Math.sin(simulationTime * 3.2) * 0.035;
const percentage = Math.round(markerT * 100);
progressFill.style.width = `${percentage}%`;
progressTrack.setAttribute("aria-valuenow", String(percentage));
updateCamera(dt);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
animate();
</script>
</body>
</html>
```