Prompt specification
Build or customize the Three.js 3D scene "Additive trait-token balance" 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: Additive trait-token balance
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/additive-trait-token-balance-x0949
Scene Description Prompt:
A low-poly fictional plant-height model stacking separate hereditary contribution blocks; label the additive inheritance model simplified. Three.js from cdnjs, one HTML file.
Working Three.js Starter (additive-trait-token-balance-x0949):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hereditary Height — Additive Model</title>
<style>
:root {
--ink: #183128;
--muted: #6e7d70;
--paper: #f4f4eb;
--panel: #fbfbf5;
--line: #dce1d6;
--green: #315e4d;
--orange: #d8784f;
--gold: #dfa83f;
--violet: #746ca6;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: var(--paper);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
display: flex;
flex-direction: column;
height: 100dvh;
min-height: 620px;
}
.topbar {
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 76px;
padding: 14px 24px;
border-bottom: 1px solid var(--line);
background: rgba(251, 251, 245, 0.94);
backdrop-filter: blur(12px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 31px;
height: 35px;
}
.brand-mark span {
position: absolute;
left: 0;
width: 31px;
height: 8px;
border-radius: 2px;
transform: skewY(-9deg);
}
.brand-mark span:nth-child(1) {
top: 1px;
background: var(--green);
}
.brand-mark span:nth-child(2) {
top: 12px;
background: var(--orange);
}
.brand-mark span:nth-child(3) {
top: 23px;
background: var(--gold);
}
.brand-title {
margin: 0;
font-size: 15px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.topbar-right {
display: flex;
align-items: center;
gap: 14px;
}
.model-badge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid #cbd6c9;
border-radius: 999px;
color: #355444;
background: #edf3e9;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #4b876b;
box-shadow: 0 0 0 4px rgba(75, 135, 107, 0.12);
content: "";
}
.reset-button {
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 9px;
background: transparent;
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition: background 160ms ease, border-color 160ms ease;
}
.reset-button:hover {
border-color: #b9c5b8;
background: #f0f3eb;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
flex: 1;
min-height: 0;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 60% 34%, rgba(255, 255, 255, 0.86), transparent 40%),
#e9eee0;
}
#scene {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.stage-intro {
position: absolute;
z-index: 2;
top: 31px;
left: 32px;
max-width: 340px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 13px;
color: #607364;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.stage-intro h1 {
margin: 0;
font-size: clamp(34px, 4vw, 58px);
font-weight: 760;
line-height: 0.98;
letter-spacing: -0.055em;
}
.stage-intro h1 span {
color: #718272;
font-weight: 520;
}
.stage-intro p {
max-width: 300px;
margin: 18px 0 0;
color: #637164;
font-size: 13px;
line-height: 1.65;
}
.simplified-label {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 19px;
padding: 7px 10px;
border: 1px solid rgba(83, 108, 87, 0.25);
border-radius: 7px;
color: #526859;
background: rgba(249, 251, 244, 0.62);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.simplified-label::before {
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--orange);
content: "";
}
.stage-footer {
position: absolute;
z-index: 3;
right: 22px;
bottom: 20px;
display: flex;
align-items: center;
gap: 8px;
}
.stage-button {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 36px;
padding: 8px 11px;
border: 1px solid rgba(91, 111, 94, 0.25);
border-radius: 9px;
color: #536457;
background: rgba(250, 251, 245, 0.78);
cursor: pointer;
font-size: 11px;
font-weight: 700;
backdrop-filter: blur(9px);
transition: background 150ms ease, color 150ms ease;
}
.stage-button:hover,
.stage-button.active {
color: #fff;
background: var(--green);
}
.orbit-hint {
position: absolute;
z-index: 2;
bottom: 22px;
left: 32px;
color: #788578;
font-size: 11px;
pointer-events: none;
}
.orbit-hint strong {
color: #526456;
}
.panel {
z-index: 5;
min-height: 0;
overflow-y: auto;
padding: 29px 27px 24px;
border-left: 1px solid var(--line);
background: var(--panel);
scrollbar-width: thin;
scrollbar-color: #cdd5c9 transparent;
}
.panel-kicker {
margin: 0 0 9px;
color: #829080;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: 28px;
font-weight: 770;
line-height: 1.05;
letter-spacing: -0.04em;
}
.panel-description {
margin: 13px 0 23px;
color: var(--muted);
font-size: 12px;
line-height: 1.62;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 11px;
color: #718071;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-heading span:last-child {
color: #9aa598;
font-size: 9px;
letter-spacing: 0.08em;
}
.contributions {
display: grid;
gap: 8px;
}
.contribution {
padding: 12px 13px 11px;
border: 1px solid #e0e5da;
border-radius: 11px;
background: #fff;
transition: border-color 150ms ease, box-shadow 150ms ease;
}
.contribution:focus-within {
border-color: var(--accent);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent);
}
.contribution-row {
display: grid;
grid-template-columns: 12px 1fr auto;
align-items: center;
gap: 9px;
}
.swatch {
width: 10px;
height: 27px;
border-radius: 3px;
background: var(--accent);
}
.contribution-name {
display: block;
font-size: 12px;
font-weight: 750;
}
.contribution-source {
display: block;
margin-top: 3px;
color: #899488;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.contribution-value {
color: var(--accent);
font-size: 15px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 12px 0 1px;
border-radius: 999px;
appearance: none;
background:
linear-gradient(
to right,
var(--accent) 0%,
var(--accent) var(--pct),
#e3e8df var(--pct),
#e3e8df 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #fff;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 1px 5px rgba(24, 49, 40, 0.24);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 1px 5px rgba(24, 49, 40, 0.24);
}
.total-card {
display: flex;
align-items: flex-end;
justify-content: space-between;
margin-top: 20px;
padding: 17px 18px;
border-radius: 13px;
color: #f8fbf4;
background:
radial-gradient(circle at 92% 20%, rgba(117, 164, 127, 0.28), transparent 35%),
var(--green);
box-shadow: 0 9px 22px rgba(37, 75, 57, 0.14);
}
.total-label {
display: block;
margin-bottom: 5px;
color: #c6d8c9;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.total-value {
font-size: 34px;
font-weight: 800;
line-height: 1;
letter-spacing: -0.05em;
font-variant-numeric: tabular-nums;
}
.total-unit {
margin-left: 4px;
color: #c6d8c9;
font-size: 13px;
font-weight: 600;
}
.model-chip {
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 999px;
color: #d8e5d9;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.formula {
display: flex;
align-items: center;
justify-content: center;
gap: 7px;
margin-top: 11px;
color: #8a9688;
font-size: 11px;
}
.formula b {
color: #526456;
}
.formula .term {
display: inline-grid;
width: 22px;
height: 22px;
place-items: center;
border-radius: 6px;
color: #fff;
font-size: 10px;
}
.profiles {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 11px;
}
.profile-button {
padding: 8px 5px;
border: 1px solid #dfe5da;
border-radius: 8px;
color: #687667;
background: #fff;
cursor: pointer;
font-size: 10px;
font-weight: 700;
transition: border-color 150ms ease, color 150ms ease, background 150ms ease;
}
.profile-button:hover,
.profile-button.active {
border-color: #9eaf9d;
color: var(--green);
background: #f0f5ec;
}
.disclaimer {
margin: 22px 0 0;
padding-top: 17px;
border-top: 1px solid var(--line);
color: #899487;
font-size: 10px;
line-height: 1.6;
}
.disclaimer strong {
color: #637160;
}
@media (max-width: 1000px) {
.workspace {
grid-template-columns: minmax(0, 1fr) 330px;
}
.stage-intro {
left: 24px;
}
.stage-intro h1 {
font-size: 39px;
}
.stage-intro p {
max-width: 240px;
}
}
@media (max-width: 760px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100dvh;
}
.topbar {
min-height: 68px;
padding: 12px 16px;
}
.brand-subtitle,
.model-badge {
display: none;
}
.workspace {
display: flex;
flex-direction: column;
}
.stage {
height: 61dvh;
min-height: 470px;
}
.stage-intro {
top: 22px;
left: 20px;
max-width: 240px;
}
.stage-intro h1 {
font-size: 34px;
}
.stage-intro p {
display: none;
}
.simplified-label {
margin-top: 12px;
font-size: 8px;
}
.orbit-hint {
display: none;
}
.stage-footer {
right: 13px;
bottom: 13px;
}
.stage-button {
padding: 7px 9px;
font-size: 9px;
}
.panel {
overflow: visible;
padding: 25px 20px 30px;
border-top: 1px solid var(--line);
border-left: 0;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<span></span>
<span></span>
<span></span>
</div>
<div>
<p class="brand-title">Trait Stack</p>
<div class="brand-subtitle">Fictional heredity laboratory</div>
</div>
</div>
<div class="topbar-right">
<div class="model-badge">Additive model · simplified</div>
<button class="reset-button" id="resetModel" type="button">
Reset model
</button>
</div>
</header>
<main class="workspace">
<section class="stage" id="stage" aria-label="Interactive three-dimensional plant model">
<canvas
id="scene"
tabindex="0"
aria-label="Drag to orbit the low-poly plant. Use arrow keys to rotate and plus or minus to zoom."
></canvas>
<div class="stage-intro">
<div class="eyebrow">Heritable height / Concept 01</div>
<h1>One plant.<br><span>Four inherited lifts.</span></h1>
<p>
Every colored block represents a separate inherited contribution.
Their heights add together to form the fictional plant's total stature.
</p>
<div class="simplified-label">Simplified additive inheritance</div>
</div>
<div class="orbit-hint">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom
</div>
<div class="stage-footer">
<button class="stage-button" id="toggleRotate" type="button" aria-pressed="false">
Auto rotate
</button>
<button class="stage-button" id="resetView" type="button">
Reset view
</button>
</div>
</section>
<aside class="panel">
<p class="panel-kicker">Contribution stack</p>
<h2>Height is a sum.</h2>
<p class="panel-description">
Adjust the four inherited segments. The model rebuilds the stacked
blocks and the low-poly plant above them.
</p>
<div class="section-heading">
<span>Inherited segments</span>
<span>0–60 cm each</span>
</div>
<div class="contributions" id="contributions"></div>
<div class="total-card">
<div>
<span class="total-label">Predicted height</span>
<span class="total-value" id="totalValue">96</span>
<span class="total-unit">cm</span>
</div>
<span class="model-chip">H = Σ blocks</span>
</div>
<div class="formula" aria-label="Height equals the sum of all four contributions">
<span class="term" style="background:#315e4d">A</span>
<b>+</b>
<span class="term" style="background:#d8784f">B</span>
<b>+</b>
<span class="term" style="background:#dfa83f">C</span>
<b>+</b>
<span class="term" style="background:#746ca6">D</span>
<b>=</b>
<strong id="formulaTotal">96 cm</strong>
</div>
<div class="section-heading" style="margin-top:22px">
<span>Trait profiles</span>
<span>Presets</span>
</div>
<div class="profiles">
<button class="profile-button active" type="button" data-profile="balanced">
Balanced
</button>
<button class="profile-button" type="button" data-profile="tower">
Tower
</button>
<button class="profile-button" type="button" data-profile="compact">
Compact
</button>
</div>
<p class="disclaimer">
<strong>Conceptual model only.</strong> Real height is usually polygenic,
environment-dependent, and influenced by non-additive effects. These
values are fictional and exist only to demonstrate additive inheritance.
</p>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model definition
// -------------------------------------------------------------------------
const WORLD_SCALE = 0.022;
const MODEL_X = 0.36;
const BASE_Y = 0.13;
const contributions = [
{
key: "A",
name: "Core stature",
source: "Parent 01",
color: "#315e4d",
value: 38
},
{
key: "B",
name: "Mid-stem gain",
source: "Parent 01",
color: "#d8784f",
value: 26
},
{
key: "C",
name: "Upper reach",
source: "Parent 02",
color: "#dfa83f",
value: 18
},
{
key: "D",
name: "Terminal lift",
source: "Parent 02",
color: "#746ca6",
value: 14
}
];
const profiles = {
balanced: [38, 26, 18, 14],
tower: [52, 38, 26, 18],
compact: [24, 16, 11, 8]
};
const stage = document.getElementById("stage");
const canvas = document.getElementById("scene");
const contributionsElement = document.getElementById("contributions");
const totalValueElement = document.getElementById("totalValue");
const formulaTotalElement = document.getElementById("formulaTotal");
const toggleRotateButton = document.getElementById("toggleRotate");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e9eee0");
scene.fog = new THREE.Fog("#e9eee0", 8, 17);
const camera = new THREE.PerspectiveCamera(34, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(
"#fffdf3",
"#77836f",
2.2
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight("#fff7df", 3.1);
keyLight.position.set(-3.5, 6.5, 4.8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 15;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight("#b9d8d2", 1.3);
rimLight.position.set(4, 3.5, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight("#ffd39b", 8, 8, 2);
warmLight.position.set(-1.5, 2.5, 2.5);
scene.add(warmLight);
const modelRoot = new THREE.Group();
modelRoot.position.x = MODEL_X;
scene.add(modelRoot);
const blocksGroup = new THREE.Group();
const plantGroup = new THREE.Group();
const labelsGroup = new THREE.Group();
const rulerGroup = new THREE.Group();
modelRoot.add(
blocksGroup,
plantGroup,
labelsGroup,
rulerGroup
);
// Ground and low-poly soil plinth.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: "#dfe6d7",
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.105;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(18, 36, "#c2cebd", "#d2dccb");
grid.position.y = -0.098;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(1.48, 1.58, 0.18, 12),
new THREE.MeshStandardMaterial({
color: "#a68d70",
roughness: 0.95,
flatShading: true
})
);
plinth.position.set(MODEL_X, -0.005, 0);
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
const plinthTop = new THREE.Mesh(
new THREE.CylinderGeometry(1.39, 1.39, 0.025, 12),
new THREE.MeshStandardMaterial({
color: "#796a53",
roughness: 1,
flatShading: true
})
);
plinthTop.position.set(MODEL_X, 0.097, 0);
plinthTop.receiveShadow = true;
scene.add(plinthTop);
const soilRing = new THREE.Mesh(
new THREE.TorusGeometry(1.36, 0.025, 5, 24),
new THREE.MeshStandardMaterial({
color: "#b7a184",
roughness: 0.9
})
);
soilRing.rotation.x = Math.PI / 2;
soilRing.position.set(MODEL_X, 0.116, 0);
scene.add(soilRing);
// Decorative low-poly pollen.
const pollenGroup = new THREE.Group();
pollenGroup.position.x = MODEL_X;
scene.add(pollenGroup);
const pollenGeometry = new THREE.OctahedronGeometry(0.035, 0);
const pollenMaterials = [
new THREE.MeshStandardMaterial({
color: "#dfb54a",
roughness: 0.8,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: "#d98859",
roughness: 0.8,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: "#89a986",
roughness: 0.8,
flatShading: true
})
];
for (let i = 0; i < 13; i++) {
const pollen = new THREE.Mesh(
pollenGeometry,
pollenMaterials[i % pollenMaterials.length]
);
const angle = i * 2.399;
const radius = 0.8 + (i % 4) * 0.18;
pollen.position.set(
Math.cos(angle) * radius,
0.8 + (i % 6) * 0.33,
Math.sin(angle) * radius * 0.65
);
pollen.rotation.set(i * 0.31, i * 0.51, i * 0.17);
pollen.userData.baseY = pollen.position.y;
pollen.userData.phase = i * 0.73;
pollenGroup.add(pollen);
}
// -------------------------------------------------------------------------
// Direct camera controls: no external controls addon
// -------------------------------------------------------------------------
const controls = {
theta: 0.48,
phi: 0.27,
radius: 6.45,
target: new THREE.Vector3(MODEL_X, 1.05, 0)
};
let dragging = false;
let dragX = 0;
let dragY = 0;
let autoRotate = false;
function updateCamera() {
const horizontal = controls.radius * Math.cos(controls.phi);
camera.position.set(
controls.target.x + horizontal * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.sin(controls.phi),
controls.target.z + horizontal * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function resetView() {
controls.theta = 0.48;
controls.phi = 0.27;
controls.radius = desiredRadius();
controls.target.set(MODEL_X, desiredTargetY(), 0);
updateCamera();
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - dragX;
const deltaY = event.clientY - dragY;
controls.theta -= deltaX * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi + deltaY * 0.0045,
0.08,
0.72
);
dragX = event.clientX;
dragY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
3.6,
10.5
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
controls.theta += 0.08;
break;
case "ArrowRight":
controls.theta -= 0.08;
break;
case "ArrowUp":
controls.phi = THREE.MathUtils.clamp(
controls.phi - 0.06,
0.08,
0.72
);
break;
case "ArrowDown":
controls.phi = THREE.MathUtils.clamp(
controls.phi + 0.06,
0.08,
0.72
);
break;
case "+":
case "=":
controls.radius = Math.max(3.6, controls.radius - 0.3);
break;
case "-":
case "_":
controls.radius = Math.min(10.5, controls.radius + 0.3);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
document.getElementById("resetView").addEventListener("click", resetView);
toggleRotateButton.addEventListener("click", () => {
autoRotate = !autoRotate;
toggleRotateButton.classList.toggle("active", autoRotate);
toggleRotateButton.setAttribute("aria-pressed", String(autoRotate));
toggleRotateButton.textContent = autoRotate ? "Rotate: on" : "Auto rotate";
});
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function disposeObject(object) {
const materials = new Set();
const textures = new Set();
object.traverse((child) => {
if (child.geometry) child.geometry.dispose();
const childMaterials = Array.isArray(child.material)
? child.material
: child.material
? [child.material]
: [];
for (const material of childMaterials) {
materials.add(material);
for (const value of Object.values(material)) {
if (value && value.isTexture) textures.add(value);
}
}
});
textures.forEach((texture) => texture.dispose());
materials.forEach((material) => material.dispose());
}
function clearGroup(group) {
while (group.children.length) {
const child = group.children[0];
group.remove(child);
disposeObject(child);
}
}
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeLabelSprite(title, value, accent, width = 1.48, height = 0.43) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 150;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 512, 150);
context.shadowColor = "rgba(24, 49, 40, 0.18)";
context.shadowBlur = 18;
context.shadowOffsetY = 6;
roundedRect(context, 8, 8, 496, 130, 22);
context.fillStyle = "rgba(19, 37, 29, 0.92)";
context.fill();
context.shadowColor = "transparent";
roundedRect(context, 24, 26, 12, 94, 6);
context.fillStyle = accent;
context.fill();
context.fillStyle = "#eef5eb";
context.font = "700 39px system-ui, sans-serif";
context.textBaseline = "middle";
context.fillText(title, 56, 55);
context.fillStyle = accent;
context.font = "800 47px system-ui, sans-serif";
context.fillText(value, 56, 108);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
function createLeafGeometry(length, width) {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array([
0, 0, 0,
length, 0, 0,
length * 0.43, width * 0.5, 0,
length * 0.43, -width * 0.5, 0,
length * 0.46, 0, width * 0.075
]);
const indices = [
0, 2, 4,
2, 1, 4,
1, 3, 4,
3, 0, 4
];
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function addLeaf(parent, y, angle, length, width, color, tilt = 0.25) {
const leaf = new THREE.Mesh(
createLeafGeometry(length, width),
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
})
);
leaf.position.set(0, y, 0.06);
leaf.rotation.order = "YXZ";
leaf.rotation.y = angle;
leaf.rotation.z = tilt;
leaf.castShadow = true;
parent.add(leaf);
}
function addBoundaryNode(parent, y, color) {
const node = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 0),
new THREE.MeshStandardMaterial({
color,
roughness: 0.85,
flatShading: true
})
);
node.position.set(0, y, 0.06);
node.castShadow = true;
parent.add(node);
}
// -------------------------------------------------------------------------
// Build stacked blocks, plant, ruler, and labels
// -------------------------------------------------------------------------
function totalContribution() {
return contributions.reduce((sum, item) => sum + item.value, 0);
}
function desiredTargetY() {
return BASE_Y + totalContribution() * WORLD_SCALE * 0.47;
}
function desiredRadius() {
const totalHeight = totalContribution() * WORLD_SCALE;
return THREE.MathUtils.clamp(5.0 + totalHeight * 0.62, 5.1, 8.9);
}
function buildModel() {
clearGroup(blocksGroup);
clearGroup(plantGroup);
clearGroup(labelsGroup);
clearGroup(rulerGroup);
let cursorY = BASE_Y;
const total = totalContribution();
const totalTop = BASE_Y + total * WORLD_SCALE;
contributions.forEach((item, index) => {
const height = item.value * WORLD_SCALE;
const width = 1.12 + index * 0.025;
const depth = 1.02;
if (height > 0.004) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const color = new THREE.Color(item.color);
const block = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color,
roughness: 0.7,
metalness: 0,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
block.position.set(0, cursorY + height / 2, -0.08);
block.castShadow = true;
block.renderOrder = index;
blocksGroup.add(block);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: color.clone().multiplyScalar(0.62),
transparent: true,
opacity: 0.72
})
);
edges.position.copy(block.position);
edges.renderOrder = 8;
blocksGroup.add(edges);
// A thin separator visually confirms that every block is separate.
const separator = new THREE.Mesh(
new THREE.BoxGeometry(width + 0.035, 0.014, depth + 0.035),
new THREE.MeshStandardMaterial({
color: color.clone().multiplyScalar(0.82),
roughness: 0.75
})
);
separator.position.set(0, cursorY + height, -0.08);
separator.castShadow = true;
blocksGroup.add(separator);
const label = makeLabelSprite(
`${item.key} · ${item.name.toUpperCase()}`,
`${item.value} cm`,
item.color
);
label.position.set(
width / 2 + 0.55,
cursorY + height / 2,
0.16
);
labelsGroup.add(label);
}
// Stem segment mirrors the contribution block.
if (height > 0.004) {
const stemColor = new THREE.Color(item.color).lerp(
new THREE.Color("#376c47"),
0.62
);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(
0.038 + index * 0.003,
0.068 - index * 0.005,
height,
5,
1,
false
),
new THREE.MeshStandardMaterial({
color: stemColor,
roughness: 0.9,
flatShading: true
})
);
stem.position.set(0, cursorY + height / 2, 0.06);
stem.castShadow = true;
plantGroup.add(stem);
addBoundaryNode(
plantGroup,
cursorY + height,
stemColor.clone().offsetHSL(0, 0, 0.08)
);
const leafColor = stemColor.clone().offsetHSL(
index % 2 === 0 ? 0.025 : -0.015,
0.05,
index % 2 === 0 ? 0.05 : -0.025
);
const leafLength = 0.37 + index * 0.025;
const leafWidth = 0.19 + index * 0.008;
addLeaf(
plantGroup,
cursorY + height - 0.035,
index * 1.52,
leafLength,
leafWidth,
leafColor,
0.26
);
addLeaf(
plantGroup,
cursorY + height - 0.045,
index * 1.52 + Math.PI * 0.78,
leafLength * 0.9,
leafWidth * 0.9,
leafColor.clone().offsetHSL(0.015, 0, 0.035),
0.2
);
}
cursorY += height;
});
// Low-poly terminal bloom, kept within the modeled total height.
const bloom = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.11, 0),
new THREE.MeshStandardMaterial({
color: "#e9b94f",
roughness: 0.7,
flatShading: true
})
);
bloom.position.set(0, totalTop - 0.105, 0.06);
bloom.castShadow = true;
plantGroup.add(bloom);
for (let i = 0; i < 5; i++) {
const petal = new THREE.Mesh(
new THREE.OctahedronGeometry(0.075, 0),
new THREE.MeshStandardMaterial({
color: i % 2 === 0 ? "#f1cb62" : "#e99d55",
roughness: 0.75,
flatShading: true
})
);
const angle = (i / 5) * Math.PI * 2;
petal.scale.set(0.65, 1, 0.45);
petal.position.set(
Math.cos(angle) * 0.09,
totalTop - 0.105 + Math.sin(angle) * 0.035,
0.06 + Math.sin(angle) * 0.075
);
petal.rotation.z = angle;
petal.castShadow = true;
plantGroup.add(petal);
}
// Simple measurement ruler.
const rulerPoints = [];
const rulerX = -1.18;
rulerPoints.push(
rulerX, BASE_Y, 0.12,
rulerX, totalTop, 0.12
);
const tickStep = total > 150 ? 20 : 10;
for (let cm = 0; cm <= total; cm += tickStep) {
const y = BASE_Y + cm * WORLD_SCALE;
const major = cm % (tickStep * 2) === 0;
const tickLength = major ? 0.16 : 0.09;
rulerPoints.push(
rulerX, y, 0.12,
rulerX - tickLength, y, 0.12
);
}
const rulerGeometry = new THREE.BufferGeometry();
rulerGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(rulerPoints, 3)
);
const ruler = new THREE.LineSegments(
rulerGeometry,
new THREE.LineBasicMaterial({
color: "#6e7e6b",
transparent: true,
opacity: 0.72
})
);
rulerGroup.add(ruler);
const totalLabel = makeLabelSprite(
"MODELED HEIGHT",
`${total} cm`,
"#315e4d",
1.42,
0.42
);
totalLabel.position.set(-1.25, totalTop + 0.18, 0.18);
labelsGroup.add(totalLabel);
// Keep the model comfortably framed after large changes.
controls.target.y = THREE.MathUtils.lerp(
controls.target.y,
desiredTargetY(),
0.55
);
controls.radius = THREE.MathUtils.lerp(
controls.radius,
desiredRadius(),
0.45
);
updateCamera();
updateUI();
}
// -------------------------------------------------------------------------
// Interface
// -------------------------------------------------------------------------
function createInterface() {
contributions.forEach((item, index) => {
const row = document.createElement("div");
row.className = "contribution";
row.style.setProperty("--accent", item.color);
row.innerHTML = `
<div class="contribution-row">
<span class="swatch" aria-hidden="true"></span>
<div>
<span class="contribution-name">${item.key} · ${item.name}</span>
<span class="contribution-source">${item.source}</span>
</div>
<strong class="contribution-value">${item.value} cm</strong>
</div>
<input
type="range"
min="0"
max="60"
step="1"
value="${item.value}"
aria-label="${item.name} contribution in centimeters"
>
`;
const slider = row.querySelector("input");
const value = row.querySelector(".contribution-value");
slider.addEventListener("input", () => {
item.value = Number(slider.value);
value.textContent = `${item.value} cm`;
const percentage = (item.value / 60) * 100;
slider.style.setProperty("--pct", `${percentage}%`);
clearActiveProfile();
buildModel();
});
contributionsElement.appendChild(row);
item.ui = { row, slider, value };
});
}
function updateUI() {
const total = totalContribution();
totalValueElement.textContent = total;
formulaTotalElement.textContent = `${total} cm`;
contributions.forEach((item) => {
const percentage = (item.value / 60) * 100;
item.ui.slider.value = item.value;
item.ui.slider.style.setProperty("--pct", `${percentage}%`);
item.ui.value.textContent = `${item.value} cm`;
});
}
function clearActiveProfile() {
document.querySelectorAll(".profile-button").forEach((button) => {
button.classList.remove("active");
});
}
function applyProfile(name) {
const values = profiles[name];
if (!values) return;
contributions.forEach((item, index) => {
item.value = values[index];
});
clearActiveProfile();
document
.querySelector(`[data-profile="${name}"]`)
?.classList.add("active");
buildModel();
}
document.querySelectorAll(".profile-button").forEach((button) => {
button.addEventListener("click", () => {
applyProfile(button.dataset.profile);
});
});
document.getElementById("resetModel").addEventListener("click", () => {
applyProfile("balanced");
resetView();
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (autoRotate && !dragging) {
controls.theta += delta * 0.18;
updateCamera();
}
pollenGroup.children.forEach((pollen, index) => {
pollen.rotation.x += delta * (0.18 + index * 0.004);
pollen.rotation.y += delta * (0.23 + index * 0.003);
pollen.position.y =
pollen.userData.baseY +
Math.sin(elapsed * 0.8 + pollen.userData.phase) * 0.055;
});
renderer.render(scene, camera);
}
createInterface();
resize();
resetView();
buildModel();
animate();
</script>
</body>
</html>
```