Prompt specification
Build or customize the Three.js 3D scene "Bouquet arrangement by height" 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: Bouquet arrangement by height
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/bouquet-arrangement-by-height-x0498
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: bouquet arrangement by height. Move stems vertically inside a clear vase and compare flat versus tiered bloom arrangements; keep stem paths continuous and prevent them from passing through the vase wall.
Working Three.js Starter (bouquet-arrangement-by-height-x0498):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Height-Study Bouquet</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(17, 28, 37, 0.88);
--border: rgba(255, 255, 255, 0.12);
--muted: #9eb1bd;
--accent: #7de6cf;
--accent-2: #ffc875;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101820;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas.grabbing {
cursor: grabbing;
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(310px, calc(100vw - 36px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
color: #eef7fb;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 22px;
line-height: 1.15;
letter-spacing: -0.03em;
}
.description {
margin: 9px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.section-label {
margin: 15px 0 8px;
color: #c7d5dd;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
min-height: 36px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
color: #eaf3f7;
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.24);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(125, 230, 207, 0.6);
background: rgba(125, 230, 207, 0.14);
color: #baf8e9;
}
.control {
transition: opacity 150ms ease;
}
.control.disabled {
opacity: 0.38;
pointer-events: none;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: #dbe8ee;
font-size: 12px;
}
output {
min-width: 42px;
color: var(--accent);
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
accent-color: var(--accent);
cursor: pointer;
}
.selection-card {
margin: 15px 0 10px;
padding: 11px 12px;
border: 1px solid rgba(125, 230, 207, 0.2);
border-radius: 12px;
background: rgba(125, 230, 207, 0.055);
}
#selection-title {
color: var(--accent);
font-size: 11px;
font-weight: 750;
}
#selection-value {
margin-top: 4px;
color: #dce9ef;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.check-row {
display: flex;
align-items: center;
gap: 8px;
margin: 12px 0;
color: #d7e4ea;
font-size: 12px;
}
.check-row input {
accent-color: var(--accent);
}
.safe-note {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--border);
color: #9eb1bd;
font-size: 10px;
line-height: 1.4;
}
.safe-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px rgba(125, 230, 207, 0.75);
}
#hint {
position: fixed;
z-index: 9;
left: 50%;
bottom: 18px;
max-width: calc(100vw - 36px);
padding: 9px 13px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(17, 28, 37, 0.78);
color: #c7d7df;
font-size: 11px;
text-align: center;
transform: translateX(-50%);
pointer-events: none;
backdrop-filter: blur(10px);
}
@media (max-width: 620px) {
#panel {
top: 10px;
left: 10px;
padding: 14px;
}
.description {
display: none;
}
#hint {
bottom: 10px;
}
}
</style>
</head>
<body>
<aside id="panel">
<div class="eyebrow">Three.js height study</div>
<h1>Bouquet by Height</h1>
<p class="description">
Compare a level floral canopy with a stepped, tiered bouquet.
Drag a bloom upward or downward to move its stem.
</p>
<div class="section-label">Arrangement</div>
<div class="segmented">
<button id="flat-button" type="button">Flat</button>
<button id="tiered-button" class="active" type="button">Tiered</button>
</div>
<div id="tier-control" class="control" style="margin-top: 14px">
<div class="control-row">
<label for="tier-spacing">Tier spacing</label>
<output id="tier-output">0.55</output>
</div>
<input
id="tier-spacing"
type="range"
min="0.2"
max="0.95"
step="0.05"
value="0.55"
/>
</div>
<div class="control" style="margin-top: 12px">
<div class="control-row">
<label for="height-offset">Overall height</label>
<output id="offset-output">+0.00</output>
</div>
<input
id="height-offset"
type="range"
min="-0.25"
max="1"
step="0.05"
value="0"
/>
</div>
<div class="selection-card">
<div id="selection-title">No bloom selected</div>
<div id="selection-value">Select a flower to inspect its height.</div>
</div>
<div class="button-row">
<button id="lower-button" type="button">Lower 0.08</button>
<button id="raise-button" type="button">Raise 0.08</button>
</div>
<label class="check-row">
<input id="show-stems" type="checkbox" checked />
Show stems and leaves
</label>
<div class="button-row">
<button id="reset-button" type="button">Reset arrangement</button>
<button id="camera-button" type="button">Reset view</button>
</div>
<div class="safe-note">
<span class="safe-dot"></span>
Continuous Bézier stems remain inside a conservative vase-safe radius.
</div>
</aside>
<div id="hint">
Drag a bloom vertically · Drag background to orbit · Scroll to zoom · Arrow keys to nudge
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly bouquet in a transparent vase"
);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101820);
scene.fog = new THREE.Fog(0x101820, 13, 25);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraTarget = new THREE.Vector3(0, 2.65, 0);
const cameraState = {
radius: 10.5,
phi: 1.1,
theta: 0.58
};
function updateCamera() {
const { radius, phi, theta } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xdff7ff, 0x25333d, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.2);
keyLight.position.set(5, 8, 4);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x75d9ff, 24, 15, 2);
rimLight.position.set(-4, 4.8, -3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb879, 18, 12, 2);
warmLight.position.set(3.5, 3.2, -2.5);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x17232c,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.005;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Transparent faceted vase
//
// The inner wall has a minimum radius of approximately 0.61.
// Stem curve control points are clamped to 0.48, and the tube radius is
// 0.022. Therefore the outside of every stem remains below radius 0.502,
// leaving at least roughly 0.108 units of clearance from the glass wall.
// -------------------------------------------------------------------------
const VASE_RIM_Y = 3.12;
const SAFE_CENTER_RADIUS = 0.48;
const STEM_RADIUS = 0.022;
const vaseProfile = [
new THREE.Vector2(0.02, 0.02),
new THREE.Vector2(0.55, 0.02),
new THREE.Vector2(0.68, 0.08),
new THREE.Vector2(0.78, 0.28),
new THREE.Vector2(0.87, 0.72),
new THREE.Vector2(0.90, 1.55),
new THREE.Vector2(0.86, 2.20),
new THREE.Vector2(0.77, 2.68),
new THREE.Vector2(0.73, 2.95),
new THREE.Vector2(0.74, VASE_RIM_Y),
new THREE.Vector2(0.63, VASE_RIM_Y),
new THREE.Vector2(0.61, 2.95),
new THREE.Vector2(0.65, 2.67),
new THREE.Vector2(0.74, 2.18),
new THREE.Vector2(0.78, 1.55),
new THREE.Vector2(0.75, 0.74),
new THREE.Vector2(0.66, 0.31),
new THREE.Vector2(0.54, 0.21),
new THREE.Vector2(0.02, 0.21)
];
const vaseMaterial = new THREE.MeshPhysicalMaterial({
color: 0xbce9ef,
roughness: 0.16,
metalness: 0,
transparent: true,
opacity: 0.22,
transmission: 0.25,
thickness: 0.08,
ior: 1.45,
side: THREE.DoubleSide,
depthWrite: false
});
const vase = new THREE.Mesh(
new THREE.LatheGeometry(vaseProfile, 32),
vaseMaterial
);
vase.renderOrder = 3;
scene.add(vase);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.685, 0.018, 4, 32),
new THREE.MeshPhysicalMaterial({
color: 0xd8f8ff,
roughness: 0.12,
transparent: true,
opacity: 0.42,
transmission: 0.3,
depthWrite: false
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = VASE_RIM_Y;
rim.renderOrder = 4;
scene.add(rim);
// A subtle water volume and surface.
const waterMaterial = new THREE.MeshPhysicalMaterial({
color: 0x69c8d8,
roughness: 0.12,
transparent: true,
opacity: 0.075,
transmission: 0.1,
side: THREE.DoubleSide,
depthWrite: false
});
const waterBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.77, 0.62, 1.32, 24, 1, true),
waterMaterial
);
waterBody.position.y = 0.96;
waterBody.renderOrder = 2;
scene.add(waterBody);
const waterSurface = new THREE.Mesh(
new THREE.CircleGeometry(0.77, 32),
new THREE.MeshBasicMaterial({
color: 0xa4eff6,
transparent: true,
opacity: 0.09,
side: THREE.DoubleSide,
depthWrite: false
})
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = 1.62;
waterSurface.renderOrder = 2;
scene.add(waterSurface);
// -------------------------------------------------------------------------
// Subtle height ruler
// -------------------------------------------------------------------------
const rulerPoints = [
new THREE.Vector3(1.32, VASE_RIM_Y, 0.1),
new THREE.Vector3(1.32, 5.95, 0.1)
];
for (let y = VASE_RIM_Y; y <= 5.95; y += 0.25) {
const length = Math.abs(y - VASE_RIM_Y) < 0.01 ? 0.18 : 0.11;
rulerPoints.push(
new THREE.Vector3(1.32 - length, y, 0.1),
new THREE.Vector3(1.32 + length, y, 0.1)
);
}
const ruler = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(rulerPoints),
new THREE.LineBasicMaterial({
color: 0x9ac2cf,
transparent: true,
opacity: 0.22
})
);
scene.add(ruler);
// -------------------------------------------------------------------------
// Shared low-poly geometry
// -------------------------------------------------------------------------
function createPetalGeometry() {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
0.055, 0.000, 0.000,
0.145, 0.015, 0.085,
0.380, 0.040, 0.000,
0.145, 0.015, -0.085,
0.190, 0.075, 0.000
],
3
)
);
geometry.setIndex([
0, 4, 1,
1, 4, 2,
2, 4, 3,
3, 4, 0
]);
geometry.computeVertexNormals();
return geometry;
}
function createLeafGeometry() {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
0.00, 0.00, 0.00,
0.15, 0.015, 0.085,
0.42, 0.025, 0.00,
0.15, 0.015, -0.085,
0.18, 0.065, 0.00
],
3
)
);
geometry.setIndex([
0, 4, 1,
1, 4, 2,
2, 4, 3,
3, 4, 0
]);
geometry.computeVertexNormals();
return geometry;
}
const petalGeometry = createPetalGeometry();
const leafGeometry = createLeafGeometry();
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x3f925e,
roughness: 0.85,
flatShading: true,
side: THREE.DoubleSide
});
const centerMaterial = new THREE.MeshStandardMaterial({
color: 0xffc85a,
roughness: 0.72,
flatShading: true
});
const petalPalette = [
0xf58c9d,
0xffb36b,
0xd79ae8,
0xf5d47d,
0xef8d72,
0xb8a1ef
];
// -------------------------------------------------------------------------
// Arrangement definitions
// -------------------------------------------------------------------------
const descriptors = [];
const ringDefinitions = [
{ count: 5, angleOffset: 0.0 },
{ count: 3, angleOffset: Math.PI / 3 },
{ count: 1, angleOffset: 0.35 }
];
let descriptorIndex = 0;
ringDefinitions.forEach((ring, ringIndex) => {
for (let i = 0; i < ring.count; i++) {
descriptors.push({
ring: ringIndex,
angle:
ringIndex === 2
? 0.35
: (i / ring.count) * Math.PI * 2 + ring.angleOffset,
colorIndex: descriptorIndex % petalPalette.length
});
descriptorIndex++;
}
});
const stems = [];
const hitTargets = [];
function createFlower(index, descriptor) {
const group = new THREE.Group();
const petalMaterial = new THREE.MeshStandardMaterial({
color: petalPalette[descriptor.colorIndex],
roughness: 0.72,
flatShading: true,
side: THREE.DoubleSide
});
const petalCount = 7 + (index % 3);
for (let p = 0; p < petalCount; p++) {
const petal = new THREE.Mesh(petalGeometry, petalMaterial);
petal.rotation.y = (p / petalCount) * Math.PI * 2;
petal.rotation.z = (p % 2 === 0 ? 1 : -1) * 0.035;
petal.castShadow = true;
petal.receiveShadow = true;
group.add(petal);
}
const center = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.125, 0),
centerMaterial
);
center.position.y = 0.055;
center.scale.y = 0.72;
center.castShadow = true;
group.add(center);
const sepal = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.17, 6),
new THREE.MeshStandardMaterial({
color: 0x3f8753,
roughness: 0.9,
flatShading: true
})
);
sepal.position.y = -0.085;
sepal.rotation.x = Math.PI;
group.add(sepal);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.012, 4, 32),
new THREE.MeshBasicMaterial({
color: 0xbaf8e9,
transparent: true,
opacity: 0.8,
depthTest: false
})
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 0.025;
halo.visible = false;
halo.renderOrder = 10;
group.add(halo);
const hit = new THREE.Mesh(
new THREE.SphereGeometry(0.43, 10, 8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
hit.userData.stemIndex = index;
group.add(hit);
hitTargets.push(hit);
group.userData.halo = halo;
group.scale.setScalar(
descriptor.ring === 0 ? 0.92 : descriptor.ring === 1 ? 1.0 : 1.05
);
scene.add(group);
return group;
}
function createStem(index, descriptor) {
const base = new THREE.Vector3(
Math.cos(descriptor.angle) * 0.12,
0.32,
Math.sin(descriptor.angle) * 0.12
);
const endpoint = base.clone();
const stemMaterial = new THREE.MeshStandardMaterial({
color: index % 2 === 0 ? 0x397e4b : 0x438d52,
roughness: 0.88,
flatShading: true
});
const mesh = new THREE.Mesh(
new THREE.BufferGeometry(),
stemMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
const leaves = [];
for (let i = 0; i < 2; i++) {
const leaf = new THREE.Mesh(leafGeometry, leafMaterial);
leaf.castShadow = true;
scene.add(leaf);
leaves.push(leaf);
}
const flower = createFlower(index, descriptor);
const stem = {
index,
descriptor,
base,
endpoint,
mesh,
leaves,
flower,
curve: null
};
stems.push(stem);
return stem;
}
descriptors.forEach((descriptor, index) => {
createStem(index, descriptor);
});
// A selected stem is traced in cyan to emphasize path continuity.
const selectionLine = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0x8cf4e1,
transparent: true,
opacity: 0.72,
depthTest: false
})
);
selectionLine.renderOrder = 9;
selectionLine.frustumCulled = false;
selectionLine.visible = false;
scene.add(selectionLine);
// -------------------------------------------------------------------------
// Continuous, wall-safe stem construction
// -------------------------------------------------------------------------
const X_AXIS = new THREE.Vector3(1, 0, 0);
function clampToSafeRadius(vector) {
const radius = Math.hypot(vector.x, vector.z);
if (radius > SAFE_CENTER_RADIUS) {
const factor = SAFE_CENTER_RADIUS / radius;
vector.x *= factor;
vector.z *= factor;
}
return vector;
}
function updateStem(index) {
const stem = stems[index];
const { base, endpoint, descriptor } = stem;
const height = endpoint.y - base.y;
const perpendicular = new THREE.Vector3(
-Math.sin(descriptor.angle),
0,
Math.cos(descriptor.angle)
);
const p0 = base.clone();
const p1 = new THREE.Vector3(
base.x * 0.72 + endpoint.x * 0.28,
base.y + height * 0.34,
base.z * 0.72 + endpoint.z * 0.28
);
p1.addScaledVector(perpendicular, 0.055);
const p2 = new THREE.Vector3(
base.x * 0.25 + endpoint.x * 0.75,
base.y + height * 0.72,
base.z * 0.25 + endpoint.z * 0.75
);
p2.addScaledVector(perpendicular, -0.025);
const p3 = endpoint.clone();
// Every control point is inside the safe disk. A cubic Bézier curve
// remains inside the convex hull of its control points, so the whole
// curve cannot leave that safe disk.
clampToSafeRadius(p0);
clampToSafeRadius(p1);
clampToSafeRadius(p2);
clampToSafeRadius(p3);
const curve = new THREE.CubicBezierCurve3(p0, p1, p2, p3);
stem.curve = curve;
const oldGeometry = stem.mesh.geometry;
stem.mesh.geometry = new THREE.TubeGeometry(
curve,
28,
STEM_RADIUS,
5,
false
);
oldGeometry.dispose();
// Leaves are placed only after the stem exits the vase opening.
stem.leaves.forEach((leaf, leafIndex) => {
const t = leafIndex === 0 ? 0.88 : 0.96;
const point = curve.getPoint(t);
const tangent = curve.getTangent(t).normalize();
const radial = new THREE.Vector3(point.x, 0, point.z);
if (radial.lengthSq() < 0.0001) {
radial.set(Math.cos(descriptor.angle), 0, Math.sin(descriptor.angle));
} else {
radial.normalize();
}
const direction = radial
.clone()
.multiplyScalar(0.9)
.addScaledVector(tangent, 0.15)
.add(new THREE.Vector3(0, 0.12, 0))
.normalize();
leaf.position.copy(point);
leaf.quaternion.setFromUnitVectors(X_AXIS, direction);
leaf.scale.setScalar(leafIndex === 0 ? 0.9 : 0.72);
leaf.visible = showStems && point.y > VASE_RIM_Y + 0.03;
});
stem.flower.position.copy(endpoint);
if (selectedIndex === index) {
updateSelectionLine();
}
}
function updateSelectionLine() {
if (selectedIndex < 0 || !stems[selectedIndex]?.curve) {
selectionLine.visible = false;
return;
}
const points = stems[selectedIndex].curve.getPoints(48);
selectionLine.geometry.dispose();
selectionLine.geometry = new THREE.BufferGeometry().setFromPoints(points);
selectionLine.visible = true;
}
// -------------------------------------------------------------------------
// UI state and layout
// -------------------------------------------------------------------------
let layout = "tiered";
let tierSpacing = 0.55;
let heightOffset = 0;
let selectedIndex = -1;
let showStems = true;
const flatButton = document.querySelector("#flat-button");
const tieredButton = document.querySelector("#tiered-button");
const tierControl = document.querySelector("#tier-control");
const tierSlider = document.querySelector("#tier-spacing");
const tierOutput = document.querySelector("#tier-output");
const offsetSlider = document.querySelector("#height-offset");
const offsetOutput = document.querySelector("#offset-output");
const showStemsInput = document.querySelector("#show-stems");
const selectionTitle = document.querySelector("#selection-title");
const selectionValue = document.querySelector("#selection-value");
function applyLayout() {
const ringRadii = [0.47, 0.36, 0.14];
stems.forEach((stem) => {
const { ring, angle } = stem.descriptor;
const radius = ringRadii[ring];
let y;
if (layout === "flat") {
y = VASE_RIM_Y + 0.66 + heightOffset;
} else {
y = VASE_RIM_Y + 0.50 + ring * tierSpacing + heightOffset;
}
stem.endpoint.set(
Math.cos(angle) * radius,
y,
Math.sin(angle) * radius
);
updateStem(stem.index);
});
tierControl.classList.toggle("disabled", layout === "flat");
flatButton.classList.toggle("active", layout === "flat");
tieredButton.classList.toggle("active", layout === "tiered");
flatButton.setAttribute("aria-pressed", String(layout === "flat"));
tieredButton.setAttribute("aria-pressed", String(layout === "tiered"));
updateHUD();
}
function updateHUD() {
if (selectedIndex < 0) {
selectionTitle.textContent = "No bloom selected";
selectionValue.textContent = "Select a flower to inspect its height.";
return;
}
const stem = stems[selectedIndex];
const aboveRim = stem.endpoint.y - VASE_RIM_Y;
selectionTitle.textContent =
`Bloom ${String(selectedIndex + 1).padStart(2, "0")} · ` +
`height band ${stem.descriptor.ring + 1}`;
selectionValue.textContent =
`Y ${stem.endpoint.y.toFixed(2)} · ` +
`${aboveRim >= 0 ? "+" : ""}${aboveRim.toFixed(2)} above rim`;
}
function selectStem(index) {
selectedIndex = index;
stems.forEach((stem, stemIndex) => {
stem.flower.userData.halo.visible = stemIndex === index;
});
updateSelectionLine();
updateHUD();
}
function nudgeSelected(delta) {
if (selectedIndex < 0) return;
const stem = stems[selectedIndex];
stem.endpoint.y = THREE.MathUtils.clamp(
stem.endpoint.y + delta,
VASE_RIM_Y + 0.20,
6.4
);
updateStem(selectedIndex);
updateHUD();
}
flatButton.addEventListener("click", () => {
layout = "flat";
applyLayout();
});
tieredButton.addEventListener("click", () => {
layout = "tiered";
applyLayout();
});
tierSlider.addEventListener("input", () => {
tierSpacing = Number(tierSlider.value);
tierOutput.textContent = tierSpacing.toFixed(2);
applyLayout();
});
offsetSlider.addEventListener("input", () => {
heightOffset = Number(offsetSlider.value);
offsetOutput.textContent =
`${heightOffset >= 0 ? "+" : ""}${heightOffset.toFixed(2)}`;
applyLayout();
});
showStemsInput.addEventListener("change", () => {
showStems = showStemsInput.checked;
stems.forEach((stem) => {
stem.mesh.visible = showStems;
stem.leaves.forEach((leaf) => {
if (showStems && leaf.position.y > VASE_RIM_Y + 0.03) {
leaf.visible = true;
} else {
leaf.visible = false;
}
});
});
});
document.querySelector("#raise-button").addEventListener("click", () => {
nudgeSelected(0.08);
});
document.querySelector("#lower-button").addEventListener("click", () => {
nudgeSelected(-0.08);
});
document.querySelector("#reset-button").addEventListener("click", () => {
layout = "tiered";
tierSpacing = 0.55;
heightOffset = 0;
tierSlider.value = "0.55";
tierOutput.textContent = "0.55";
offsetSlider.value = "0";
offsetOutput.textContent = "+0.00";
applyLayout();
});
document.querySelector("#camera-button").addEventListener("click", () => {
cameraState.radius = 10.5;
cameraState.phi = 1.1;
cameraState.theta = 0.58;
updateCamera();
});
// -------------------------------------------------------------------------
// Direct camera and bloom controls, implemented without addons
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane();
const dragNormal = new THREE.Vector3();
const cameraDirection = new THREE.Vector3();
const dragIntersection = new THREE.Vector3();
let orbiting = false;
let draggingBloom = false;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointer(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;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(hitTargets, false);
if (hits.length > 0) {
const index = hits[0].object.userData.stemIndex;
selectStem(index);
const stem = stems[index];
camera.getWorldDirection(cameraDirection);
dragNormal.set(cameraDirection.x, 0, cameraDirection.z);
if (dragNormal.lengthSq() < 0.0001) {
dragNormal.set(0, 0, 1);
} else {
dragNormal.normalize();
}
// A vertical plane through the selected stem. Intersecting this plane
// converts pointer movement into world-space vertical movement.
dragPlane.setFromNormalAndCoplanarPoint(
dragNormal,
stem.endpoint
);
draggingBloom = true;
renderer.domElement.classList.add("grabbing");
renderer.domElement.setPointerCapture(event.pointerId);
event.preventDefault();
} else {
orbiting = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.classList.add("grabbing");
renderer.domElement.setPointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (draggingBloom) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragIntersection)) {
const stem = stems[selectedIndex];
stem.endpoint.y = THREE.MathUtils.clamp(
dragIntersection.y,
VASE_RIM_Y + 0.20,
6.4
);
updateStem(selectedIndex);
updateHUD();
}
event.preventDefault();
return;
}
if (orbiting) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.006,
0.5,
1.46
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
event.preventDefault();
}
});
function endPointer(event) {
orbiting = false;
draggingBloom = false;
renderer.domElement.classList.remove("grabbing");
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
6.5,
16
);
updateCamera();
event.preventDefault();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (selectedIndex < 0) return;
if (event.key === "ArrowUp") {
nudgeSelected(0.04);
event.preventDefault();
} else if (event.key === "ArrowDown") {
nudgeSelected(-0.04);
event.preventDefault();
}
});
// -------------------------------------------------------------------------
// Resize and render
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
applyLayout();
function animate() {
requestAnimationFrame(animate);
// Gentle selection pulse without moving the flower or stem.
if (selectedIndex >= 0) {
const halo = stems[selectedIndex].flower.userData.halo;
const pulse = 1 + Math.sin(performance.now() * 0.004) * 0.035;
halo.scale.setScalar(pulse);
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```