Prompt specification
Build or customize the Three.js 3D scene "Mechanical flower play wall" 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: Mechanical flower play wall
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mechanical-flower-play-wall-x0339
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: mechanical flower play wall. Build a toy wall where turning a crank drives gear-centered flowers and simple flags; color-coded shafts reveal which visible decorations share connected motion paths. Return one self-contained HTML file.
Working Three.js Starter (mechanical-flower-play-wall-x0339):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mechanical Flower Wall</title>
<style>
:root {
--ink: #20343d;
--cream: #fff8e9;
--coral: #ef6a5b;
--teal: #22b8b0;
--gold: #f2bd3d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce7e3;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 45%, rgba(31, 52, 61, 0.12) 100%),
linear-gradient(to bottom, rgba(255, 255, 255, 0.08), transparent 22%);
z-index: 2;
}
.ui {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.header {
position: absolute;
top: 24px;
left: 26px;
max-width: min(440px, calc(100vw - 52px));
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 7px 11px;
border-radius: 999px;
background: rgba(255, 248, 233, 0.86);
border: 1px solid rgba(32, 52, 61, 0.12);
box-shadow: 0 8px 24px rgba(31, 52, 61, 0.09);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 0 0 4px rgba(239, 106, 91, 0.16);
}
h1 {
margin: 11px 0 5px;
color: var(--ink);
font-size: clamp(27px, 4vw, 49px);
line-height: 0.95;
letter-spacing: -0.055em;
text-transform: uppercase;
text-shadow: 0 2px 0 rgba(255, 255, 255, 0.5);
}
.subtitle {
margin: 0;
max-width: 390px;
color: rgba(32, 52, 61, 0.76);
font-size: 13px;
font-weight: 650;
line-height: 1.45;
}
.tools {
position: absolute;
top: 24px;
right: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
pointer-events: auto;
}
button {
border: 0;
color: var(--ink);
font: inherit;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.tool {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 42px;
padding: 0 15px;
border-radius: 14px;
background: rgba(255, 248, 233, 0.9);
border: 1px solid rgba(32, 52, 61, 0.13);
box-shadow: 0 10px 25px rgba(31, 52, 61, 0.1);
font-size: 12px;
font-weight: 850;
backdrop-filter: blur(12px);
transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.tool:hover {
transform: translateY(-2px);
box-shadow: 0 13px 28px rgba(31, 52, 61, 0.14);
}
.tool.active {
color: #fff;
background: var(--ink);
}
.tool .dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--teal);
}
.metric {
position: absolute;
top: 82px;
right: 24px;
min-width: 162px;
padding: 13px 15px;
border-radius: 17px;
background: rgba(255, 248, 233, 0.88);
border: 1px solid rgba(32, 52, 61, 0.12);
box-shadow: 0 10px 28px rgba(31, 52, 61, 0.09);
backdrop-filter: blur(12px);
}
.metric-label {
display: flex;
align-items: center;
justify-content: space-between;
color: rgba(32, 52, 61, 0.56);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.status {
display: inline-flex;
align-items: center;
gap: 5px;
color: var(--teal);
}
.status::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
}
.turns {
margin-top: 4px;
font-size: 24px;
font-weight: 900;
letter-spacing: -0.05em;
font-variant-numeric: tabular-nums;
}
.legend {
position: absolute;
left: 24px;
bottom: 24px;
width: min(330px, calc(100vw - 48px));
padding: 16px;
border-radius: 22px;
background: rgba(255, 248, 233, 0.9);
border: 1px solid rgba(32, 52, 61, 0.12);
box-shadow: 0 16px 40px rgba(31, 52, 61, 0.13);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.legend-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
}
.legend-title {
font-size: 11px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.all-button {
padding: 5px 9px;
border-radius: 9px;
background: rgba(32, 52, 61, 0.07);
color: rgba(32, 52, 61, 0.67);
font-size: 10px;
font-weight: 850;
}
.legend-row {
display: grid;
grid-template-columns: 34px 1fr auto;
align-items: center;
width: 100%;
margin-top: 6px;
padding: 9px;
border-radius: 14px;
background: transparent;
text-align: left;
transition: background 150ms ease, transform 150ms ease;
}
.legend-row:hover {
background: rgba(32, 52, 61, 0.055);
transform: translateX(2px);
}
.legend-row.active {
background: rgba(32, 52, 61, 0.08);
}
.swatch {
position: relative;
width: 24px;
height: 24px;
border: 5px solid var(--swatch);
border-radius: 50%;
background: var(--cream);
box-shadow: inset 0 0 0 2px rgba(32, 52, 61, 0.13);
}
.legend-name {
font-size: 12px;
font-weight: 900;
letter-spacing: 0.02em;
}
.legend-detail {
margin-top: 2px;
color: rgba(32, 52, 61, 0.56);
font-size: 10px;
font-weight: 650;
}
.legend-arrow {
color: rgba(32, 52, 61, 0.32);
font-size: 18px;
}
.hint {
position: absolute;
left: 50%;
bottom: 26px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 10px;
padding: 10px 15px;
border-radius: 999px;
background: rgba(32, 52, 61, 0.84);
color: rgba(255, 248, 233, 0.92);
box-shadow: 0 10px 30px rgba(31, 52, 61, 0.15);
font-size: 11px;
font-weight: 750;
white-space: nowrap;
backdrop-filter: blur(10px);
}
.hint b {
color: var(--coral);
}
@media (max-width: 760px) {
.header {
top: 16px;
left: 16px;
}
h1 {
max-width: 250px;
font-size: 29px;
}
.subtitle {
display: none;
}
.tools {
top: 15px;
right: 15px;
max-width: 180px;
}
.tool {
min-height: 38px;
padding: 0 11px;
border-radius: 12px;
font-size: 10px;
}
.metric {
top: 112px;
right: 15px;
min-width: 142px;
}
.legend {
left: 14px;
bottom: 14px;
width: 225px;
padding: 11px;
border-radius: 18px;
}
.legend-row {
padding: 7px;
}
.legend-detail {
display: none;
}
.hint {
right: 14px;
bottom: 18px;
left: auto;
transform: none;
max-width: calc(100vw - 260px);
white-space: normal;
line-height: 1.4;
}
}
@media (max-width: 520px) {
.eyebrow {
display: none;
}
h1 {
margin-top: 0;
max-width: 180px;
font-size: 25px;
}
.tools {
max-width: 142px;
}
.tool {
padding: 0 9px;
}
.tool span.long {
display: none;
}
.metric {
top: 61px;
}
.legend {
width: 195px;
}
.legend-title {
font-size: 9px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div class="ui">
<header class="header">
<div class="eyebrow">Interactive kinetic toy</div>
<h1>Mechanical<br>Flower Wall</h1>
<p class="subtitle">
Turn the coral crank. Watch each colored shaft reveal the flower and pennant
sharing its motion path.
</p>
</header>
<div class="tools">
<button class="tool active" id="pathToggle" aria-pressed="true">
<span class="dot"></span>
<span class="long">Drive paths</span>
<span>On</span>
</button>
<button class="tool" id="autoSpin" aria-pressed="false">
<span>▶</span>
<span class="long">Auto </span>
<span>Spin</span>
</button>
<button class="tool" id="reset">
<span>↺</span>
<span>Reset</span>
</button>
</div>
<div class="metric">
<div class="metric-label">
<span>Crank</span>
<span class="status" id="status">Ready</span>
</div>
<div class="turns" id="turns">0.00 turns</div>
</div>
<section class="legend" aria-label="Connected motion paths">
<div class="legend-head">
<div class="legend-title">Connected motion paths</div>
<button class="all-button active" data-focus="all">Show all</button>
</div>
<button class="legend-row active" data-focus="coral">
<span class="swatch" style="--swatch: var(--coral)"></span>
<span>
<span class="legend-name">Coral circuit</span>
<span class="legend-detail">Blossom + pennant</span>
</span>
<span class="legend-arrow">›</span>
</button>
<button class="legend-row active" data-focus="teal">
<span class="swatch" style="--swatch: var(--teal)"></span>
<span>
<span class="legend-name">Teal circuit</span>
<span class="legend-detail">Blossom + pennant</span>
</span>
<span class="legend-arrow">›</span>
</button>
<button class="legend-row active" data-focus="gold">
<span class="swatch" style="--swatch: var(--gold)"></span>
<span>
<span class="legend-name">Gold circuit</span>
<span class="legend-detail">Blossom + pennant</span>
</span>
<span class="legend-arrow">›</span>
</button>
</section>
<div class="hint">
<b>Drag</b> the coral handle · <b>Drag</b> the wall to look · <b>Scroll</b> to zoom
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce7e3);
scene.fog = new THREE.Fog(0xdce7e3, 25, 48);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.12;
renderer.domElement.setAttribute("aria-label", "Interactive mechanical flower wall");
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
const cameraState = {
yaw: 0,
pitch: 0.025,
radius: 18,
desiredRadius: 18,
baseRadius: 18,
userZoomed: false
};
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x74898a, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4d7, 3.2);
keyLight.position.set(-7, 11, 13);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9de5df, 1.15);
fillLight.position.set(8, 4, 9);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xff9a82, 18, 20, 2);
rimLight.position.set(6, -2, 5);
scene.add(rimLight);
const colors = {
ink: 0x20343d,
cream: 0xfff4dc,
panel: 0xf2e6ce,
coral: 0xef6a5b,
coralLight: 0xf5a094,
teal: 0x22b8b0,
tealLight: 0x79d8cf,
gold: 0xf2bd3d,
goldLight: 0xffda68,
wood: 0xc98b62,
metal: 0xb7c7c5,
peg: 0xa9a89e
};
const materials = {
ink: new THREE.MeshStandardMaterial({
color: colors.ink,
roughness: 0.5,
metalness: 0.18
}),
cream: new THREE.MeshStandardMaterial({
color: colors.cream,
roughness: 0.58,
metalness: 0.02
}),
panel: new THREE.MeshStandardMaterial({
color: colors.panel,
roughness: 0.72,
metalness: 0.01
}),
coral: new THREE.MeshStandardMaterial({
color: colors.coral,
roughness: 0.42,
metalness: 0.08
}),
coralLight: new THREE.MeshStandardMaterial({
color: colors.coralLight,
roughness: 0.46
}),
teal: new THREE.MeshStandardMaterial({
color: colors.teal,
roughness: 0.4,
metalness: 0.1
}),
tealLight: new THREE.MeshStandardMaterial({
color: colors.tealLight,
roughness: 0.45
}),
gold: new THREE.MeshStandardMaterial({
color: colors.gold,
roughness: 0.4,
metalness: 0.1
}),
goldLight: new THREE.MeshStandardMaterial({
color: colors.goldLight,
roughness: 0.43
}),
wood: new THREE.MeshStandardMaterial({
color: colors.wood,
roughness: 0.7
}),
metal: new THREE.MeshStandardMaterial({
color: colors.metal,
roughness: 0.3,
metalness: 0.55
}),
peg: new THREE.MeshBasicMaterial({ color: colors.peg })
};
function makeRoundedShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function makeRoundedPanel(width, height, radius, depth, material, z) {
const geometry = new THREE.ExtrudeGeometry(makeRoundedShape(width, height, radius), {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 4,
bevelSize: 0.08,
bevelThickness: 0.07,
curveSegments: 10
});
geometry.translate(0, 0, -depth / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = z;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// A rounded toy-wall frame and cream pegboard face.
makeRoundedPanel(13.75, 7.85, 0.38, 0.5, materials.ink, -0.78);
makeRoundedPanel(13.2, 7.3, 0.31, 0.38, materials.panel, -0.48);
// Floor and toy feet.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({ color: 0xcbdad5, roughness: 0.88 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.76;
floor.receiveShadow = true;
scene.add(floor);
for (const x of [-4.85, 4.85]) {
const foot = new THREE.Mesh(
new THREE.BoxGeometry(1.25, 0.34, 1.0),
materials.ink
);
foot.position.set(x, -3.56, -0.18);
foot.castShadow = true;
foot.receiveShadow = true;
scene.add(foot);
}
// Pegboard holes.
const holeGeometry = new THREE.CircleGeometry(0.035, 10);
const holeCountX = 25;
const holeCountY = 12;
const holes = new THREE.InstancedMesh(
holeGeometry,
materials.peg,
holeCountX * holeCountY
);
const holeMatrix = new THREE.Matrix4();
let holeIndex = 0;
for (let row = 0; row < holeCountY; row++) {
for (let col = 0; col < holeCountX; col++) {
holeMatrix.makeTranslation(
-5.76 + col * 0.48,
-3.05 + row * 0.55,
-0.272
);
holes.setMatrixAt(holeIndex++, holeMatrix);
}
}
holes.instanceMatrix.needsUpdate = true;
scene.add(holes);
// Frame screws.
const screwGeometry = new THREE.CylinderGeometry(0.085, 0.085, 0.07, 20);
for (const x of [-6.15, 6.15]) {
for (const y of [-3.12, 3.12]) {
const screw = new THREE.Mesh(screwGeometry, materials.metal);
screw.rotation.x = Math.PI / 2;
screw.position.set(x, y, -0.22);
screw.castShadow = true;
scene.add(screw);
}
}
function makeGear(radius, material, teeth = 16, holeRadius = 0.12) {
const group = new THREE.Group();
const rootRadius = radius * 0.82;
const shape = new THREE.Shape();
for (let tooth = 0; tooth < teeth; tooth++) {
const a0 = (tooth / teeth) * Math.PI * 2;
const step = (Math.PI * 2) / teeth;
const points = [
[a0, rootRadius],
[a0 + step * 0.18, radius],
[a0 + step * 0.58, radius],
[a0 + step * 0.76, rootRadius]
];
for (let i = 0; i < points.length; i++) {
const [angle, r] = points[i];
const x = Math.cos(angle) * r;
const y = Math.sin(angle) * r;
if (tooth === 0 && i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
}
shape.closePath();
const hole = new THREE.Path();
hole.absarc(0, 0, holeRadius, 0, Math.PI * 2, true);
shape.holes.push(hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.18,
steps: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.025,
bevelThickness: 0.025,
curveSegments: 12
});
geometry.translate(0, 0, -0.09);
const gear = new THREE.Mesh(geometry, material);
gear.castShadow = true;
gear.receiveShadow = true;
group.add(gear);
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(holeRadius * 0.83, holeRadius * 0.83, 0.34, 24),
materials.ink
);
axle.rotation.x = Math.PI / 2;
axle.position.z = 0.04;
axle.castShadow = true;
group.add(axle);
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(holeRadius * 0.52, holeRadius * 0.52, 0.37, 24),
materials.cream
);
cap.rotation.x = Math.PI / 2;
cap.position.z = 0.07;
cap.castShadow = true;
group.add(cap);
return group;
}
const centralGears = [];
const gearSpecs = [
{ key: "coral", x: 2.08, material: materials.coral },
{ key: "teal", x: 3.13, material: materials.teal },
{ key: "gold", x: 4.18, material: materials.gold }
];
for (const spec of gearSpecs) {
const gear = makeGear(0.58, spec.material, 16, 0.12);
gear.position.set(spec.x, 0.15, -0.08);
scene.add(gear);
centralGears.push({ ...spec, gear });
}
const inputGear = makeGear(0.58, materials.ink, 16, 0.12);
inputGear.position.set(5.25, 0.15, -0.08);
scene.add(inputGear);
// Static guide rings around the crank.
const crankGuide = new THREE.Mesh(
new THREE.TorusGeometry(0.79, 0.085, 12, 64),
materials.cream
);
crankGuide.position.set(5.25, 0.15, 0.28);
crankGuide.castShadow = true;
scene.add(crankGuide);
const crankGuideEdge = new THREE.Mesh(
new THREE.TorusGeometry(0.84, 0.025, 8, 64),
materials.ink
);
crankGuideEdge.position.set(5.25, 0.15, 0.28);
scene.add(crankGuideEdge);
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.95, 24),
materials.metal
);
axle.rotation.x = Math.PI / 2;
axle.position.set(5.25, 0.15, 0.1);
axle.castShadow = true;
scene.add(axle);
const crank = new THREE.Group();
crank.position.set(5.25, 0.15, 0.48);
scene.add(crank);
const crankArm = new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.17, 0.18),
materials.coral
);
crankArm.position.x = 0.35;
crankArm.castShadow = true;
crankArm.userData.isCrank = true;
crank.add(crankArm);
const crankBoss = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.22, 0.23, 28),
materials.coralLight
);
crankBoss.rotation.x = Math.PI / 2;
crankBoss.castShadow = true;
crankBoss.userData.isCrank = true;
crank.add(crankBoss);
const crankKnob = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 24, 16),
materials.coral
);
crankKnob.position.set(0.78, 0, 0.17);
crankKnob.castShadow = true;
crankKnob.userData.isCrank = true;
crank.add(crankKnob);
function makePetalGeometry() {
const shape = new THREE.Shape();
shape.moveTo(-0.18, -0.31);
shape.quadraticCurveTo(-0.34, -0.05, -0.21, 0.36);
shape.quadraticCurveTo(-0.13, 0.57, 0, 0.66);
shape.quadraticCurveTo(0.13, 0.57, 0.21, 0.36);
shape.quadraticCurveTo(0.34, -0.05, 0.18, -0.31);
shape.quadraticCurveTo(0, -0.43, -0.18, -0.31);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.12,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.055,
bevelThickness: 0.045,
curveSegments: 10
});
geometry.translate(0, 0, -0.06);
return geometry;
}
const petalGeometry = makePetalGeometry();
function createFlower(key, x, y, scale, petalMaterial) {
const group = new THREE.Group();
group.position.set(x, y, 0.08);
group.scale.setScalar(scale);
scene.add(group);
const rotor = new THREE.Group();
group.add(rotor);
const backingGear = makeGear(0.72, materials.ink, 18, 0.17);
backingGear.position.z = -0.02;
rotor.add(backingGear);
const innerGear = new THREE.Mesh(
new THREE.TorusGeometry(0.47, 0.065, 10, 48),
petalMaterial
);
innerGear.position.z = 0.12;
innerGear.castShadow = true;
rotor.add(innerGear);
const petalCount = 7;
for (let i = 0; i < petalCount; i++) {
const angle = (i / petalCount) * Math.PI * 2;
const petal = new THREE.Mesh(petalGeometry, petalMaterial);
petal.position.set(
Math.sin(angle) * 0.68,
Math.cos(angle) * 0.68,
0.18
);
petal.rotation.z = -angle;
petal.castShadow = true;
petal.receiveShadow = true;
rotor.add(petal);
}
const center = new THREE.Mesh(
new THREE.CylinderGeometry(0.27, 0.29, 0.2, 32),
materials.cream
);
center.rotation.x = Math.PI / 2;
center.position.z = 0.29;
center.castShadow = true;
rotor.add(center);
const centerDot = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.22, 24),
materials.ink
);
centerDot.rotation.x = Math.PI / 2;
centerDot.position.z = 0.32;
centerDot.castShadow = true;
rotor.add(centerDot);
return { key, group, rotor };
}
const flowers = [
createFlower("coral", -4.45, 2.35, 0.92, materials.coral),
createFlower("teal", -1.35, 2.55, 0.88, materials.teal),
createFlower("gold", 1.65, 2.35, 0.98, materials.gold)
];
function createFlag(key, x, y, clothMaterial) {
const group = new THREE.Group();
group.position.set(x, y, 0.06);
scene.add(group);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.32, 0.13, 28),
materials.ink
);
base.rotation.x = Math.PI / 2;
base.position.z = 0.015;
base.castShadow = true;
group.add(base);
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.065, 1.82, 16),
materials.wood
);
pole.position.y = 0.88;
pole.castShadow = true;
group.add(pole);
const finial = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
materials.gold
);
finial.position.y = 1.82;
finial.castShadow = true;
group.add(finial);
const width = 1.38;
const height = 0.68;
const geometry = new THREE.PlaneGeometry(width, height, 14, 6);
geometry.translate(width / 2, 0, 0);
const cloth = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: clothMaterial.color,
roughness: 0.78,
metalness: 0,
side: THREE.DoubleSide
})
);
cloth.position.set(0, 1.38, 0.065);
cloth.castShadow = true;
group.add(cloth);
const basePositions = geometry.attributes.position.array.slice();
return {
key,
group,
cloth,
basePositions,
width,
phase: Math.random() * Math.PI * 2
};
}
const flags = [
createFlag("coral", -3.8, -2.25, materials.coral),
createFlag("teal", -0.45, -2.25, materials.teal),
createFlag("gold", 2.9, -2.25, materials.gold)
];
function cylinderBetween(a, b, radius, material, parent, radialSegments = 12) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function pathCurve(points) {
const curve = new THREE.CurvePath();
for (let i = 0; i < points.length - 1; i++) {
curve.add(new THREE.LineCurve3(points[i], points[i + 1]));
}
return curve;
}
const pathsRoot = new THREE.Group();
scene.add(pathsRoot);
const pathDefinitions = [
{
key: "coral",
color: colors.coral,
ratio: 0.72,
main: [
new THREE.Vector3(2.08, 0.15, -0.015),
new THREE.Vector3(1.45, 0.15, -0.015),
new THREE.Vector3(-4.45, -0.45, -0.015),
new THREE.Vector3(-4.45, 2.35, 0.015)
],
branch: [
new THREE.Vector3(-4.45, 2.35, 0.015),
new THREE.Vector3(-3.8, 1.58, 0.015),
new THREE.Vector3(-3.8, -2.25, 0.015)
]
},
{
key: "teal",
color: colors.teal,
ratio: 0.88,
main: [
new THREE.Vector3(3.13, 0.15, 0.005),
new THREE.Vector3(2.62, 0.15, 0.005),
new THREE.Vector3(-1.35, -0.25, 0.005),
new THREE.Vector3(-1.35, 2.55, 0.025)
],
branch: [
new THREE.Vector3(-1.35, 2.55, 0.025),
new THREE.Vector3(-0.45, 1.58, 0.025),
new THREE.Vector3(-0.45, -2.25, 0.025)
]
},
{
key: "gold",
color: colors.gold,
ratio: 1.05,
main: [
new THREE.Vector3(4.18, 0.15, 0.025),
new THREE.Vector3(3.72, 0.15, 0.025),
new THREE.Vector3(1.65, -0.05, 0.025),
new THREE.Vector3(1.65, 2.35, 0.035)
],
branch: [
new THREE.Vector3(1.65, 2.35, 0.035),
new THREE.Vector3(2.9, 1.58, 0.035),
new THREE.Vector3(2.9, -2.25, 0.035)
]
}
];
const paths = [];
for (const definition of pathDefinitions) {
const pathGroup = new THREE.Group();
pathsRoot.add(pathGroup);
const pathMaterial = new THREE.MeshStandardMaterial({
color: definition.color,
roughness: 0.34,
metalness: 0.18,
transparent: true,
opacity: 0.82,
emissive: definition.color,
emissiveIntensity: 0.08
});
for (const points of [definition.main, definition.branch]) {
for (let i = 0; i < points.length - 1; i++) {
cylinderBetween(points[i], points[i + 1], 0.065, pathMaterial, pathGroup);
}
for (let i = 1; i < points.length - 1; i++) {
const joint = new THREE.Mesh(
new THREE.SphereGeometry(0.092, 14, 10),
pathMaterial
);
joint.position.copy(points[i]);
joint.castShadow = true;
pathGroup.add(joint);
}
}
// Colored collars visually mark the decoration endpoints.
for (const endpoint of [definition.main.at(-1), definition.branch.at(-1)]) {
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.045, 8, 28),
pathMaterial
);
collar.position.copy(endpoint);
collar.position.z += 0.09;
pathGroup.add(collar);
}
const mainCurve = pathCurve(definition.main);
const branchCurve = pathCurve(definition.branch);
const beads = [];
for (let i = 0; i < 2; i++) {
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 16, 12),
pathMaterial
);
bead.castShadow = true;
pathGroup.add(bead);
beads.push(bead);
}
paths.push({
...definition,
group: pathGroup,
material: pathMaterial,
mainCurve,
branchCurve,
beads
});
}
function makePlaque(text, subtitle, color, x, y, width = 1.8) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 240;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#fff4dc";
ctx.beginPath();
ctx.roundRect(10, 10, 748, 220, 38);
ctx.fill();
ctx.strokeStyle = "#20343d";
ctx.lineWidth = 14;
ctx.stroke();
ctx.fillStyle = `#${new THREE.Color(color).getHexString()}`;
ctx.beginPath();
ctx.arc(88, 120, 43, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#20343d";
ctx.font = "900 64px sans-serif";
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillText(text, 158, 96);
ctx.fillStyle = "rgba(32,52,61,.64)";
ctx.font = "700 29px sans-serif";
ctx.fillText(subtitle, 158, 161);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const plaque = new THREE.Mesh(
new THREE.PlaneGeometry(width, width * 240 / 768),
new THREE.MeshStandardMaterial({
map: texture,
roughness: 0.75,
transparent: true
})
);
plaque.position.set(x, y, 0.015);
scene.add(plaque);
return plaque;
}
makePlaque("TURN", "one handle, three paths", colors.coral, 5.05, -1.28, 1.85);
makePlaque("GEAR GARDEN", "turn • watch • connect", colors.teal, 0, -3.03, 2.65);
const state = {
targetAngle: 0,
actualAngle: 0,
previousAngle: 0,
speed: 0,
driveEnergy: 0,
autoSpin: false,
draggingCrank: false,
draggingCamera: false,
lastPointerAngle: 0,
lastPointerX: 0,
lastPointerY: 0
};
const pathToggle = document.getElementById("pathToggle");
const autoSpinButton = document.getElementById("autoSpin");
const resetButton = document.getElementById("reset");
const turnsDisplay = document.getElementById("turns");
const statusDisplay = document.getElementById("status");
const focusButtons = [...document.querySelectorAll("[data-focus]")];
let pathsVisible = true;
let focus = "all";
function applyPathFocus() {
for (const path of paths) {
const selected = focus === "all" || focus === path.key;
path.material.opacity = selected ? 0.88 : 0.1;
path.material.emissiveIntensity = selected ? 0.11 : 0.015;
}
for (const button of focusButtons) {
button.classList.toggle("active", button.dataset.focus === focus);
}
}
for (const button of focusButtons) {
button.addEventListener("click", () => {
focus = button.dataset.focus;
applyPathFocus();
});
}
pathToggle.addEventListener("click", () => {
pathsVisible = !pathsVisible;
pathsRoot.visible = pathsVisible;
pathToggle.classList.toggle("active", pathsVisible);
pathToggle.setAttribute("aria-pressed", String(pathsVisible));
pathToggle.querySelector("span:last-child").textContent = pathsVisible ? "On" : "Off";
});
autoSpinButton.addEventListener("click", () => {
state.autoSpin = !state.autoSpin;
autoSpinButton.classList.toggle("active", state.autoSpin);
autoSpinButton.setAttribute("aria-pressed", String(state.autoSpin));
});
resetButton.addEventListener("click", () => {
state.targetAngle = 0;
state.actualAngle = 0;
state.previousAngle = 0;
state.speed = 0;
state.driveEnergy = 0;
state.autoSpin = false;
autoSpinButton.classList.remove("active");
autoSpinButton.setAttribute("aria-pressed", "false");
});
applyPathFocus();
// Pointer interaction: crank dragging and direct camera orbit controls.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const interactionPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
const intersection = new THREE.Vector3();
const crankCenter = new THREE.Vector3(5.25, 0.15, 0);
const crankPickables = [crankArm, crankBoss, crankKnob];
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function pointerOnCrankPlane(event) {
updatePointer(event);
const knobWorld = new THREE.Vector3();
crankKnob.getWorldPosition(knobWorld);
interactionPlane.constant = -knobWorld.z;
return raycaster.ray.intersectPlane(interactionPlane, intersection);
}
renderer.domElement.addEventListener("pointerdown", event => {
updatePointer(event);
scene.updateMatrixWorld(true);
const hits = raycaster.intersectObjects(crankPickables, false);
if (hits.length) {
state.draggingCrank = true;
state.lastPointerX = event.clientX;
state.lastPointerY = event.clientY;
if (pointerOnCrankPlane(event)) {
state.lastPointerAngle = Math.atan2(
intersection.y - crankCenter.y,
intersection.x - crankCenter.x
);
}
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
event.preventDefault();
} else {
state.draggingCamera = true;
state.lastPointerX = event.clientX;
state.lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (state.draggingCrank) {
if (pointerOnCrankPlane(event)) {
const angle = Math.atan2(
intersection.y - crankCenter.y,
intersection.x - crankCenter.x
);
let delta = angle - state.lastPointerAngle;
while (delta > Math.PI) delta -= Math.PI * 2;
while (delta < -Math.PI) delta += Math.PI * 2;
state.targetAngle += delta;
state.lastPointerAngle = angle;
}
return;
}
if (state.draggingCamera) {
const dx = event.clientX - state.lastPointerX;
const dy = event.clientY - state.lastPointerY;
cameraState.yaw = THREE.MathUtils.clamp(
cameraState.yaw - dx * 0.0045,
-0.48,
0.48
);
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0035,
-0.26,
0.3
);
state.lastPointerX = event.clientX;
state.lastPointerY = event.clientY;
return;
}
updatePointer(event);
scene.updateMatrixWorld(true);
const hoveringCrank = raycaster.intersectObjects(crankPickables, false).length > 0;
renderer.domElement.style.cursor = hoveringCrank ? "grab" : "default";
});
function endPointer(event) {
state.draggingCrank = false;
state.draggingCamera = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
renderer.domElement.style.cursor = "default";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.userZoomed = true;
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius + event.deltaY * 0.012,
cameraState.baseRadius * 0.72,
cameraState.baseRadius * 1.35
);
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (event.key === "ArrowRight") {
state.targetAngle += 0.16;
event.preventDefault();
}
if (event.key === "ArrowLeft") {
state.targetAngle -= 0.16;
event.preventDefault();
}
if (event.code === "Space") {
state.autoSpin = !state.autoSpin;
autoSpinButton.classList.toggle("active", state.autoSpin);
autoSpinButton.setAttribute("aria-pressed", String(state.autoSpin));
event.preventDefault();
}
});
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);
cameraState.baseRadius = Math.max(18, 17.4 / camera.aspect);
if (!cameraState.userZoomed) {
cameraState.desiredRadius = cameraState.baseRadius;
cameraState.radius = cameraState.baseRadius;
}
}
window.addEventListener("resize", resize);
resize();
function updateCamera() {
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.desiredRadius,
0.11
);
const cosPitch = Math.cos(cameraState.pitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraState.yaw) * cosPitch * cameraState.radius,
cameraTarget.y + Math.sin(cameraState.pitch) * cameraState.radius,
cameraTarget.z + Math.cos(cameraState.yaw) * cosPitch * cameraState.radius
);
camera.lookAt(cameraTarget);
}
function fract(value) {
return ((value % 1) + 1) % 1;
}
const clock = new THREE.Clock();
let hudTimer = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (state.autoSpin) {
state.targetAngle += dt * 1.15;
}
const easing = 1 - Math.exp(-dt * 18);
state.actualAngle = THREE.MathUtils.lerp(
state.actualAngle,
state.targetAngle,
easing
);
const angularDelta = state.actualAngle - state.previousAngle;
state.previousAngle = state.actualAngle;
const measuredSpeed = angularDelta / Math.max(dt, 0.001);
state.speed = THREE.MathUtils.lerp(state.speed, measuredSpeed, 0.2);
const energyTarget = THREE.MathUtils.clamp(Math.abs(state.speed) * 1.6, 0, 1);
state.driveEnergy = THREE.MathUtils.lerp(
state.driveEnergy,
energyTarget,
1 - Math.exp(-dt * 5)
);
crank.rotation.z = state.actualAngle;
inputGear.rotation.z = state.actualAngle;
const goldGear = centralGears.find(g => g.key === "gold").gear;
const tealGear = centralGears.find(g => g.key === "teal").gear;
const coralGear = centralGears.find(g => g.key === "coral").gear;
goldGear.rotation.z = -state.actualAngle * 1.02;
tealGear.rotation.z = state.actualAngle * 1.02;
coralGear.rotation.z = -state.actualAngle * 1.02;
for (const flower of flowers) {
const ratio = flower.key === "coral" ? 0.72 : flower.key === "teal" ? 0.88 : 1.05;
flower.rotor.rotation.z = state.actualAngle * ratio;
}
for (const flag of flags) {
const ratio = flag.key === "coral" ? 0.72 : flag.key === "teal" ? 0.88 : 1.05;
flag.phase += dt * (0.7 + state.driveEnergy * 9) * ratio;
const position = flag.cloth.geometry.attributes.position;
const amplitude = 0.025 + state.driveEnergy * 0.15;
for (let i = 0; i < position.count; i++) {
const x = flag.basePositions[i * 3];
const y = flag.basePositions[i * 3 + 1];
const u = THREE.MathUtils.clamp(x / flag.width, 0, 1);
const wave =
Math.sin(u * 8.5 - flag.phase * 2.2) * amplitude * u +
Math.sin(u * 14 + flag.phase) * amplitude * 0.18 * u;
position.setXYZ(i, x, y + wave * 0.08, wave);
}
position.needsUpdate = true;
flag.cloth.geometry.computeVertexNormals();
}
for (const path of paths) {
const mainProgressA = fract(state.actualAngle * path.ratio * 0.075 + 0.08);
const mainProgressB = fract(state.actualAngle * path.ratio * 0.075 + 0.58);
const branchProgressA = fract(state.actualAngle * path.ratio * 0.075 + 0.28);
const branchProgressB = fract(state.actualAngle * path.ratio * 0.075 + 0.82);
path.beads[0].position.copy(path.mainCurve.getPoint(mainProgressA));
path.beads[1].position.copy(path.branchCurve.getPoint(branchProgressA));
const pulse = 1 + state.driveEnergy * 0.35;
path.beads[0].scale.setScalar(pulse);
path.beads[1].scale.setScalar(pulse);
}
updateCamera();
hudTimer += dt;
if (hudTimer > 0.08) {
hudTimer = 0;
const turns = state.actualAngle / (Math.PI * 2);
turnsDisplay.textContent = `${turns.toFixed(2)} turns`;
const moving = Math.abs(state.speed) > 0.08;
statusDisplay.textContent = moving ? "Driving" : "Ready";
statusDisplay.style.color = moving ? "#ef6a5b" : "#22b8b0";
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```