Prompt specification
Build or customize the Three.js 3D scene "Sand fence accumulation demonstration" 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: Sand fence accumulation demonstration
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sand-fence-accumulation-demonstration-x1134
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a shallow sand tray and a porous fence in a clean stylized style. Make the fence from spaced slats with visible gaps, supported by two short posts crossing the middle of the tray. A fixed arrow indicates one wind direction throughout the animation. Show three or four authored stages of sand accumulating around the obstacle, with the original tray profile faintly visible for comparison. Individual grains are unnecessary; broad smooth ridges can communicate the changing shape. Identify the sequence as qualitative, and keep the wind arrow unchanged as the stages advance.
Working Three.js Starter (sand-fence-accumulation-demonstration-x1134):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Qualitative Sand Accumulation Around a Porous Fence</title>
<style>
:root {
--ink: #24383b;
--muted: #667678;
--teal: #167f86;
--teal-soft: #dbeaE8;
--sand: #dfb978;
--paper: rgba(251, 250, 246, 0.92);
--line: rgba(36, 56, 59, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f1eee7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene,
#scene canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene canvas {
display: block;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 20px;
border: 1px solid rgba(36, 56, 59, 0.12);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 18px 50px rgba(55, 63, 58, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 7px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
font-weight: 720;
letter-spacing: -0.035em;
line-height: 1.08;
}
.intro {
margin: 10px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.qualitative {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 3px;
padding: 5px 8px;
border-radius: 999px;
background: #eef2ed;
color: #53645d;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.qualitative::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #72977c;
content: "";
}
.wind-strip {
display: flex;
align-items: center;
justify-content: space-between;
margin: 16px 0 14px;
padding: 11px 13px;
border: 1px solid rgba(22, 127, 134, 0.19);
border-radius: 12px;
background: var(--teal-soft);
}
.wind-strip span:first-child {
font-size: 12px;
font-weight: 750;
}
.wind-arrow {
color: var(--teal);
font-size: 30px;
font-weight: 500;
line-height: 0.7;
transform: translateY(-1px);
}
.wind-strip small {
display: block;
margin-top: 2px;
color: #5f7373;
font-size: 10px;
font-weight: 600;
}
.stage-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
#stageCount {
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
#play {
border: 0;
border-radius: 9px;
padding: 7px 10px;
background: var(--ink);
color: white;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
}
#play:hover {
background: #315256;
}
.stage-list {
display: grid;
gap: 6px;
}
.stage-step {
display: grid;
grid-template-columns: 32px 1fr;
align-items: center;
gap: 10px;
width: 100%;
padding: 9px 10px;
border: 1px solid transparent;
border-radius: 11px;
background: transparent;
color: var(--ink);
text-align: left;
font: inherit;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease;
}
.stage-step:hover {
background: rgba(22, 127, 134, 0.06);
}
.stage-step.active {
border-color: rgba(22, 127, 134, 0.2);
background: rgba(22, 127, 134, 0.09);
}
.step-number {
display: grid;
width: 29px;
height: 29px;
place-items: center;
border: 1px solid var(--line);
border-radius: 50%;
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.stage-step.active .step-number {
border-color: var(--teal);
background: var(--teal);
color: white;
}
.step-copy strong,
.step-copy small {
display: block;
}
.step-copy strong {
font-size: 12px;
font-weight: 720;
}
.step-copy small {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.progress-track {
height: 3px;
margin: 15px 0 13px;
overflow: hidden;
border-radius: 999px;
background: rgba(36, 56, 59, 0.1);
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--teal);
transition: width 80ms linear;
}
#stageDescription {
min-height: 53px;
margin: 0;
color: #59696b;
font-size: 12px;
line-height: 1.48;
}
.legend {
display: flex;
align-items: center;
gap: 9px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: #667779;
font-size: 10px;
}
.legend-line {
position: relative;
width: 35px;
height: 1px;
background: repeating-linear-gradient(
90deg,
#668b8d 0 6px,
transparent 6px 10px
);
}
.note {
margin: 10px 0 0;
color: #7b8788;
font-size: 10px;
line-height: 1.45;
}
#windTag {
position: fixed;
z-index: 8;
left: 0;
top: 0;
padding: 5px 8px;
border: 1px solid rgba(22, 127, 134, 0.2);
border-radius: 999px;
background: rgba(248, 250, 247, 0.9);
color: var(--teal);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
#windTag::after {
display: block;
margin-top: 2px;
color: #617779;
content: "→";
font-size: 18px;
line-height: 0.7;
text-align: center;
}
.orbit-hint {
position: fixed;
z-index: 8;
right: 18px;
bottom: 16px;
padding: 7px 10px;
border: 1px solid rgba(36, 56, 59, 0.11);
border-radius: 999px;
background: rgba(251, 250, 246, 0.78);
color: #687879;
font-size: 10px;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #f1eee7;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 700px) {
#hud {
top: 12px;
left: 12px;
width: min(300px, calc(100vw - 24px));
padding: 15px;
}
h1 {
font-size: 21px;
}
.intro {
display: none;
}
.qualitative {
margin-bottom: 10px;
}
.stage-step {
padding: 6px 8px;
}
#stageDescription {
min-height: 42px;
}
.orbit-hint {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional sand accumulation scene"></div>
<aside id="hud">
<div class="eyebrow">Wind-driven form study</div>
<h1>Sand around a porous fence</h1>
<p class="intro">
Four authored stages show a smooth sand surface developing around
spaced slats. The geometry is deliberately simplified.
</p>
<div class="qualitative">Qualitative sequence</div>
<div class="wind-strip" aria-label="Fixed wind direction">
<span>
Fixed wind direction
<small>Unchanged through all stages</small>
</span>
<span class="wind-arrow" aria-hidden="true">→</span>
</div>
<div class="stage-heading">
<span id="stageCount">Stage 1 / 4</span>
<button id="play" type="button">▶ Play sequence</button>
</div>
<nav id="stageList" class="stage-list" aria-label="Sand accumulation stages"></nav>
<div class="progress-track" aria-hidden="true">
<div id="progressFill"></div>
</div>
<p id="stageDescription" aria-live="polite"></p>
<div class="legend">
<span class="legend-line" aria-hidden="true"></span>
<span>Original tray profile — faint reference</span>
</div>
<p class="note">
Broad ridges represent spatial trends rather than individual grains.
The wind arrow remains fixed in the scene.
</p>
</aside>
<div id="windTag">FIXED WIND</div>
<div class="orbit-hint">Drag to orbit · Scroll to zoom</div>
<div id="loading">Loading scene…</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, lighting
// -------------------------------------------------------------------------
const mount = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf1eee7);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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.03;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
mount.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Camera controls are implemented directly, without Three.js addons.
const orbit = {
radius: 14.5,
theta: 0.72,
phi: 1.08,
target: new THREE.Vector3(0, 0.22, 0)
};
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();
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.43,
1.43
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.009,
8.5,
21
);
},
{ passive: false }
);
const hemisphere = new THREE.HemisphereLight(
0xffffff,
0x9b8d75,
2.25
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d8, 3.15);
keyLight.position.set(-4.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc4e1e3, 0.85);
fillLight.position.set(6, 4, -5);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Ground and shallow tray
// -------------------------------------------------------------------------
function addBox(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xefede6,
roughness: 1
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.225;
ground.receiveShadow = true;
scene.add(ground);
const trayBaseMaterial = new THREE.MeshStandardMaterial({
color: 0xc9c1b3,
roughness: 0.9
});
const trayRimMaterial = new THREE.MeshStandardMaterial({
color: 0xeee9df,
roughness: 0.78
});
const sandSideMaterial = new THREE.MeshStandardMaterial({
color: 0xcda568,
roughness: 1
});
addBox(8.75, 0.22, 4.9, trayBaseMaterial, 0, -0.11, 0);
// Four low walls create the shallow tray.
addBox(8.75, 0.3, 0.18, trayRimMaterial, 0, 0.11, 2.35);
addBox(8.75, 0.3, 0.18, trayRimMaterial, 0, 0.11, -2.35);
addBox(0.18, 0.3, 4.7, trayRimMaterial, 4.285, 0.11, 0);
addBox(0.18, 0.3, 4.7, trayRimMaterial, -4.285, 0.11, 0);
const SAND_WIDTH = 8.2;
const SAND_DEPTH = 4.35;
const SAND_BASE_Y = 0.09;
addBox(
SAND_WIDTH,
0.12,
SAND_DEPTH,
sandSideMaterial,
0,
0.02,
0
);
// -------------------------------------------------------------------------
// Porous fence: separated vertical slats, two support posts, thin rails
// -------------------------------------------------------------------------
const fenceMaterial = new THREE.MeshStandardMaterial({
color: 0x527b7a,
roughness: 0.66,
metalness: 0.02
});
const postMaterial = new THREE.MeshStandardMaterial({
color: 0x34575a,
roughness: 0.7,
metalness: 0.02
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x426968,
roughness: 0.74
});
const fence = new THREE.Group();
scene.add(fence);
const slatPositions = [-1.65, -1.1, -0.55, 0, 0.55, 1.1, 1.65];
for (const z of slatPositions) {
addBox(0.16, 1.12, 0.16, fenceMaterial, 0, 0.65, z, fence);
}
// The rails sit just behind the slats, leaving the gaps clearly visible.
addBox(0.09, 0.08, 4.08, railMaterial, -0.115, 0.4, 0, fence);
addBox(0.09, 0.08, 4.08, railMaterial, -0.115, 1.08, 0, fence);
// Exactly two short posts support the fence across the tray's middle.
addBox(0.28, 1.35, 0.28, postMaterial, 0.01, 0.765, 1.9, fence);
addBox(0.28, 1.35, 0.28, postMaterial, 0.01, 0.765, -1.9, fence);
// -------------------------------------------------------------------------
// Authored, deterministic sand profiles
// -------------------------------------------------------------------------
const stageProfiles = [
{
upstream: 0.0,
apron: 0.0,
downstream: 0.0,
downstreamCenter: 0.7,
downstreamWidth: 0.55,
poreStrength: 0.0
},
{
upstream: 0.11,
apron: 0.025,
downstream: 0.075,
downstreamCenter: 0.68,
downstreamWidth: 0.58,
poreStrength: 0.015
},
{
upstream: 0.29,
apron: 0.06,
downstream: 0.25,
downstreamCenter: 0.96,
downstreamWidth: 0.8,
poreStrength: 0.045
},
{
upstream: 0.43,
apron: 0.095,
downstream: 0.49,
downstreamCenter: 1.18,
downstreamWidth: 1.05,
poreStrength: 0.08
}
];
function smoothstep(min, max, value) {
const t = THREE.MathUtils.clamp((value - min) / (max - min), 0, 1);
return t * t * (3 - 2 * t);
}
function profileHeight(x, z, profile) {
const halfWidth = SAND_WIDTH / 2;
const halfDepth = SAND_DEPTH / 2;
// Keep the sand surface flat where it meets the tray rim.
const edgeFade =
smoothstep(0, 0.38, halfWidth - Math.abs(x)) *
smoothstep(0, 0.32, halfDepth - Math.abs(z));
const crossShelf = Math.exp(-Math.pow(z / 2.12, 2));
// A broad windward bank pressed against the upstream slat faces.
const upstream =
profile.upstream *
Math.exp(-Math.pow((x + 0.38) / 0.5, 2)) *
crossShelf;
// A subtle upstream approach apron.
const apron =
profile.apron *
Math.exp(-Math.pow((x + 1.12) / 0.88, 2)) *
crossShelf;
// Broad downstream accumulation. The smooth periodic component suggests
// aligned flow through the fence openings without modeling individual grains.
const gapPhase =
2 * Math.PI * (z + 1.325) / 0.55;
const poreVariation =
1 -
profile.poreStrength +
profile.poreStrength *
(0.5 + 0.5 * Math.cos(gapPhase));
const downstream =
profile.downstream *
Math.exp(
-Math.pow(
(x - profile.downstreamCenter) / profile.downstreamWidth,
2
)
) *
crossShelf *
poreVariation;
// A low tail makes the final leeward mound broader.
const tail =
profile.downstream *
0.2 *
Math.exp(
-Math.pow(
(x - (profile.downstreamCenter + 1.02)) / 1.22,
2
)
) *
crossShelf;
return edgeFade * (upstream + apron + downstream + tail);
}
const NX = 96;
const NZ = 48;
const sandPositions = [];
const sandUVs = [];
const sandIndices = [];
const sandCoordinates = [];
for (let iz = 0; iz <= NZ; iz++) {
const z =
(iz / NZ - 0.5) * SAND_DEPTH;
for (let ix = 0; ix <= NX; ix++) {
const x =
(ix / NX - 0.5) * SAND_WIDTH;
sandCoordinates.push({ x, z });
sandPositions.push(x, SAND_BASE_Y, z);
sandUVs.push(ix / NX, iz / NZ);
}
}
for (let iz = 0; iz < NZ; iz++) {
for (let ix = 0; ix < NX; ix++) {
const a = iz * (NX + 1) + ix;
const b = a + 1;
const c = a + NX + 1;
const d = c + 1;
// Counter-clockwise when viewed from above.
sandIndices.push(a, c, b, b, c, d);
}
}
const sandGeometry = new THREE.BufferGeometry();
sandGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sandPositions, 3)
);
sandGeometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(sandUVs, 2)
);
sandGeometry.setIndex(sandIndices);
sandGeometry.computeVertexNormals();
const sandMaterial = new THREE.MeshStandardMaterial({
color: 0xdfb978,
roughness: 0.96,
metalness: 0
});
const sandMesh = new THREE.Mesh(sandGeometry, sandMaterial);
sandMesh.castShadow = true;
sandMesh.receiveShadow = true;
sandMesh.frustumCulled = false;
scene.add(sandMesh);
function updateSand(fromIndex, toIndex, amount) {
const from = stageProfiles[fromIndex];
const to = stageProfiles[toIndex];
const position = sandGeometry.attributes.position.array;
for (let i = 0; i < sandCoordinates.length; i++) {
const { x, z } = sandCoordinates[i];
const height =
THREE.MathUtils.lerp(
profileHeight(x, z, from),
profileHeight(x, z, to),
amount
);
position[i * 3 + 1] = SAND_BASE_Y + height;
}
sandGeometry.attributes.position.needsUpdate = true;
sandGeometry.computeVertexNormals();
}
// -------------------------------------------------------------------------
// Faint, stationary reference for the original tray profile
// -------------------------------------------------------------------------
const referencePlane = new THREE.Mesh(
new THREE.PlaneGeometry(SAND_WIDTH, SAND_DEPTH),
new THREE.MeshBasicMaterial({
color: 0x6c9697,
transparent: true,
opacity: 0.032,
side: THREE.DoubleSide,
depthTest: false,
depthWrite: false
})
);
referencePlane.rotation.x = -Math.PI / 2;
referencePlane.position.y = SAND_BASE_Y + 0.002;
referencePlane.renderOrder = 4;
scene.add(referencePlane);
const referenceLineMaterial = new THREE.LineDashedMaterial({
color: 0x628789,
transparent: true,
opacity: 0.2,
dashSize: 0.17,
gapSize: 0.11,
depthTest: false,
depthWrite: false
});
const halfW = SAND_WIDTH / 2;
const halfD = SAND_DEPTH / 2;
const referenceY = SAND_BASE_Y + 0.006;
const perimeterPoints = [
new THREE.Vector3(-halfW, referenceY, -halfD),
new THREE.Vector3(halfW, referenceY, -halfD),
new THREE.Vector3(halfW, referenceY, halfD),
new THREE.Vector3(-halfW, referenceY, halfD),
new THREE.Vector3(-halfW, referenceY, -halfD)
];
const perimeter = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(perimeterPoints),
referenceLineMaterial
);
perimeter.computeLineDistances();
perimeter.renderOrder = 5;
scene.add(perimeter);
const originalSection = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-halfW, referenceY, 0),
new THREE.Vector3(halfW, referenceY, 0)
]),
referenceLineMaterial
);
originalSection.computeLineDistances();
originalSection.renderOrder = 5;
scene.add(originalSection);
// -------------------------------------------------------------------------
// One fixed world-space wind arrow
// -------------------------------------------------------------------------
const windArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-3.72, 0.25, 2.84),
1.9,
0x147f86,
0.34,
0.24
);
windArrow.line.material.transparent = true;
windArrow.line.material.opacity = 0.95;
windArrow.cone.material.transparent = true;
windArrow.cone.material.opacity = 0.95;
scene.add(windArrow);
// -------------------------------------------------------------------------
// UI and authored stage playback
// -------------------------------------------------------------------------
const stages = [
{
name: "Starting profile",
short: "Even sand bed",
description:
"A nearly level sand layer establishes the original reference condition."
},
{
name: "Pore trapping",
short: "First contact",
description:
"A shallow upstream bank begins to form against the slats while the overall profile remains low and smooth."
},
{
name: "Wake building",
short: "Broad ridges",
description:
"Sand passing through the open spaces produces a low downstream wake with broad, repeated ridges."
},
{
name: "Leeward drift",
short: "Accumulated form",
description:
"The downstream deposit becomes wider and taller, while the windward bank and the open fence gaps remain legible."
}
];
const stageList = document.getElementById("stageList");
const stageCount = document.getElementById("stageCount");
const stageDescription = document.getElementById("stageDescription");
const playButton = document.getElementById("play");
const progressFill = document.getElementById("progressFill");
const stageButtons = stages.map((stage, index) => {
const button = document.createElement("button");
button.type = "button";
button.className = "stage-step";
button.innerHTML = `
<span class="step-number">0${index + 1}</span>
<span class="step-copy">
<strong>${stage.name}</strong>
<small>${stage.short}</small>
</span>
`;
button.addEventListener("click", () => {
beginTransition(index, true);
});
stageList.appendChild(button);
return button;
});
let activeStage = 0;
let fromStage = 0;
let toStage = 0;
let transitionAmount = 0;
let transitionStart = 0;
let transitioning = false;
let playing = false;
let holdUntil = 0;
const TRANSITION_DURATION = 1750;
const STAGE_HOLD = 1250;
function setActiveStage(index) {
stageButtons.forEach((button, buttonIndex) => {
button.classList.toggle("active", buttonIndex === index);
button.classList.toggle("done", buttonIndex < index);
});
stageCount.textContent = `Stage ${index + 1} / 4`;
stageDescription.textContent = stages[index].description;
}
function updatePlayButton() {
if (playing) {
playButton.textContent = "Ⅱ Pause";
} else if (activeStage === stages.length - 1) {
playButton.textContent = "↻ Replay";
} else {
playButton.textContent = "▶ Continue";
}
}
function beginTransition(index, manual = false) {
if (index === activeStage && !transitioning) return;
if (manual) {
playing = false;
// Finish an existing transition before starting a new one.
if (transitioning) {
activeStage = toStage;
updateSand(activeStage, activeStage, 0);
transitioning = false;
}
}
fromStage = activeStage;
toStage = index;
transitionAmount = 0;
transitionStart = performance.now();
transitioning = true;
setActiveStage(index);
updatePlayButton();
}
playButton.addEventListener("click", () => {
if (playing) {
playing = false;
updatePlayButton();
return;
}
if (activeStage === stages.length - 1 && !transitioning) {
activeStage = 0;
updateSand(0, 0, 0);
setActiveStage(0);
}
playing = true;
holdUntil = performance.now() + 900;
updatePlayButton();
});
setActiveStage(0);
updatePlayButton();
updateSand(0, 0, 0);
// -------------------------------------------------------------------------
// Screen-space label for the fixed wind arrow
// -------------------------------------------------------------------------
const windTag = document.getElementById("windTag");
const windLabelPosition = new THREE.Vector3(-2.75, 0.57, 2.84);
const projected = new THREE.Vector3();
function updateScreenLabels() {
projected.copy(windLabelPosition).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
windTag.style.left = `${x}px`;
windTag.style.top = `${y}px`;
windTag.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
// -------------------------------------------------------------------------
// Resize and animation loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function easeInOut(t) {
return t * t * (3 - 2 * t);
}
function animate(now) {
requestAnimationFrame(animate);
if (transitioning) {
const linear = THREE.MathUtils.clamp(
(now - transitionStart) / TRANSITION_DURATION,
0,
1
);
transitionAmount = easeInOut(linear);
updateSand(fromStage, toStage, transitionAmount);
if (linear >= 1) {
activeStage = toStage;
transitioning = false;
transitionAmount = 0;
updateSand(activeStage, activeStage, 0);
holdUntil = now + STAGE_HOLD;
if (activeStage === stages.length - 1) {
playing = false;
updatePlayButton();
}
}
} else if (
playing &&
activeStage < stages.length - 1 &&
now >= holdUntil
) {
beginTransition(activeStage + 1);
}
const progress =
(activeStage + (transitioning ? transitionAmount : 0)) /
(stages.length - 1);
progressFill.style.width = `${progress * 100}%`;
updateCamera();
updateScreenLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
document.getElementById("loading").remove();
</script>
</body>
</html>
```