Prompt specification
Build or customize the Three.js 3D scene "Water-loss tradeoff classroom model" 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: Water-loss tradeoff classroom model
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-loss-tradeoff-classroom-model-x0792
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows leaf stomata. Create a simplified leaf model balancing symbolic gas uptake and water loss; users vary aperture under declared assumptions and inspect the tradeoff rather than receiving plant-care prescriptions. Low-poly.
Working Three.js Starter (water-loss-tradeoff-classroom-model-x0792):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<title>Stomata — Aperture Trade-off Lab</title>
<style>
:root {
--bg: #07110f;
--panel: #0c1715;
--panel-2: #11211d;
--line: rgba(190, 229, 211, 0.13);
--text: #e8f3ee;
--muted: #91a89f;
--green: #72dfaa;
--cyan: #68e6c0;
--blue: #8fb8ff;
--amber: #e6b975;
--purple: #bd9cff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
width: 100vw;
height: 100dvh;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, rgba(32, 90, 69, 0.18), transparent 42%),
#07110f;
}
#viewport,
#viewport canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
#viewport canvas {
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#viewport canvas:active {
cursor: grabbing;
}
.scene-heading {
position: absolute;
top: 24px;
left: 26px;
z-index: 3;
pointer-events: none;
}
.kicker {
margin: 0 0 7px;
color: var(--green);
font-size: 0.68rem;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.scene-heading h1 {
margin: 0;
font-size: clamp(1.35rem, 2.2vw, 2.2rem);
font-weight: 620;
letter-spacing: -0.035em;
}
.scene-heading p {
max-width: 390px;
margin: 7px 0 0;
color: var(--muted);
font-size: 0.82rem;
line-height: 1.5;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(114, 223, 170, 0.24);
border-radius: 999px;
background: rgba(9, 25, 20, 0.7);
color: #b9d9ca;
font-size: 0.65rem;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px var(--green);
content: "";
}
.scene-tools {
position: absolute;
right: 20px;
bottom: 22px;
z-index: 4;
display: flex;
gap: 8px;
}
.tool-button {
border: 1px solid var(--line);
border-radius: 10px;
padding: 9px 12px;
background: rgba(10, 24, 20, 0.82);
color: #c8d9d1;
font-size: 0.73rem;
font-weight: 650;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
border-color 160ms ease,
background 160ms ease,
color 160ms ease;
}
.tool-button:hover,
.tool-button[aria-pressed="true"] {
border-color: rgba(114, 223, 170, 0.45);
background: rgba(19, 48, 38, 0.92);
color: #effff7;
}
.scene-legend {
position: absolute;
left: 26px;
bottom: 23px;
z-index: 3;
display: flex;
flex-wrap: wrap;
gap: 13px;
color: #a8bbb3;
font-size: 0.7rem;
pointer-events: none;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.legend-dot.co2 {
color: var(--cyan);
background: var(--cyan);
}
.legend-dot.water {
color: var(--blue);
background: var(--blue);
}
.legend-dot.pore {
width: 9px;
height: 4px;
border-radius: 50%;
color: #06100d;
background: #06100d;
box-shadow: 0 0 0 1px #82ce9e;
}
.camera-hint {
position: absolute;
right: 22px;
top: 22px;
z-index: 3;
color: rgba(192, 214, 204, 0.58);
font-size: 0.66rem;
pointer-events: none;
}
#panel {
position: relative;
z-index: 5;
height: 100dvh;
overflow-y: auto;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(18, 37, 31, 0.64), transparent 220px),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #29473c transparent;
}
.panel-inner {
padding: 25px 24px 34px;
}
.panel-kicker {
margin: 0 0 8px;
color: var(--green);
font-size: 0.67rem;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#panel h2 {
margin: 0;
font-size: 1.35rem;
font-weight: 650;
letter-spacing: -0.025em;
}
.intro {
margin: 9px 0 18px;
color: var(--muted);
font-size: 0.79rem;
line-height: 1.55;
}
.boundary-note {
margin-bottom: 23px;
padding: 11px 12px;
border-left: 2px solid var(--amber);
background: rgba(230, 185, 117, 0.07);
color: #c8bda9;
font-size: 0.7rem;
line-height: 1.5;
}
.section-label {
margin: 0 0 13px;
color: #c8d8d0;
font-size: 0.68rem;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.control {
margin-bottom: 19px;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 14px;
margin-bottom: 9px;
}
.control label {
color: #d7e4de;
font-size: 0.77rem;
font-weight: 600;
}
.control output {
color: var(--green);
font-size: 0.78rem;
font-variant-numeric: tabular-nums;
font-weight: 700;
}
.control small {
display: block;
margin-top: 7px;
color: #748b81;
font-size: 0.65rem;
line-height: 1.4;
}
input[type="range"] {
--fill: 50%;
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
appearance: none;
background: linear-gradient(
to right,
var(--green) 0%,
var(--green) var(--fill),
#253c34 var(--fill),
#253c34 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #10251d;
border-radius: 50%;
appearance: none;
background: var(--green);
box-shadow: 0 0 0 1px rgba(114, 223, 170, 0.55);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #10251d;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 1px rgba(114, 223, 170, 0.55);
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #637a70;
font-size: 0.59rem;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 5px 0 22px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.015);
color: #b6c9c0;
font-size: 0.72rem;
}
.toggle-row input {
accent-color: var(--green);
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 9px;
}
.metric {
min-width: 0;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.018);
}
.metric-label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 0.65rem;
line-height: 1.35;
}
.metric-value {
display: block;
font-size: 1.55rem;
font-weight: 650;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.metric.gas .metric-value {
color: var(--cyan);
}
.metric.water .metric-value {
color: var(--blue);
}
.metric.balance {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 1fr auto;
align-items: end;
}
.metric.balance .metric-value {
color: var(--purple);
}
.metric-note {
display: block;
margin-top: 5px;
color: #70877d;
font-size: 0.61rem;
line-height: 1.4;
}
.meter {
height: 3px;
margin-top: 10px;
overflow: hidden;
border-radius: 999px;
background: #20342c;
}
.meter > span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 100ms linear;
}
.meter.gas > span {
background: var(--cyan);
}
.meter.water > span {
background: var(--blue);
}
.model-status {
min-height: 48px;
margin: 0 0 22px;
padding: 10px 12px;
border: 1px solid rgba(114, 223, 170, 0.13);
border-radius: 10px;
background: rgba(114, 223, 170, 0.045);
color: #9fb7ac;
font-size: 0.68rem;
line-height: 1.5;
}
.chart-card {
margin-bottom: 21px;
padding: 13px 12px 9px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(3, 12, 9, 0.23);
}
.chart-title {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
}
.chart-title strong {
color: #dce9e3;
font-size: 0.73rem;
font-weight: 650;
}
.chart-title span {
color: #6e857a;
font-size: 0.61rem;
}
.chart-legend {
display: flex;
gap: 13px;
margin-bottom: 3px;
color: #81988e;
font-size: 0.61rem;
}
.chart-legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.chart-legend i {
width: 13px;
height: 2px;
background: var(--cyan);
}
.chart-legend i.water {
background: var(--blue);
}
#tradeoffChart {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
#tradeoffChart text {
fill: #667e73;
font-family: inherit;
font-size: 8px;
}
#tradeoffChart .axis-label {
fill: #81998e;
font-size: 8.5px;
}
details {
border-top: 1px solid var(--line);
padding-top: 16px;
}
summary {
color: #cbdad3;
font-size: 0.72rem;
font-weight: 650;
cursor: pointer;
user-select: none;
}
.assumptions {
margin: 12px 0 0;
padding-left: 18px;
color: #80968c;
font-size: 0.68rem;
line-height: 1.55;
}
.assumptions li {
margin-bottom: 8px;
}
.assumptions code {
color: #b7d0c3;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.65rem;
}
.closing-note {
margin: 17px 0 0;
padding-top: 14px;
border-top: 1px solid var(--line);
color: #71887e;
font-size: 0.66rem;
line-height: 1.5;
}
@media (max-width: 860px) {
body {
overflow: auto;
}
#app {
grid-template-columns: 1fr;
grid-template-rows: 58dvh auto;
height: auto;
min-height: 100dvh;
}
#panel {
height: auto;
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.scene-heading {
top: 18px;
left: 18px;
}
.scene-heading h1 {
font-size: 1.45rem;
}
.scene-heading p {
max-width: 290px;
font-size: 0.73rem;
}
.camera-hint {
display: none;
}
.scene-legend {
left: 18px;
bottom: 18px;
}
.scene-tools {
right: 14px;
bottom: 14px;
}
.panel-inner {
padding: 24px 18px 40px;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<main id="app">
<section id="stage" aria-label="Interactive low-poly leaf model">
<div id="viewport"></div>
<div class="scene-heading">
<p class="kicker">Symbolic transport study</p>
<h1>Stomata / Aperture Lab</h1>
<p>
Open a conceptual pore and watch the gas-uptake and water-loss
responses separate.
</p>
<div class="badge">Schematic leaf underside</div>
</div>
<div class="camera-hint">drag to orbit · wheel or pinch to zoom</div>
<div class="scene-legend" aria-hidden="true">
<span class="legend-item">
<span class="legend-dot co2"></span>
CO₂ in
</span>
<span class="legend-item">
<span class="legend-dot water"></span>
H₂O out
</span>
<span class="legend-item">
<span class="legend-dot pore"></span>
open area
</span>
</div>
<div class="scene-tools">
<button
id="overviewButton"
class="tool-button"
type="button"
aria-pressed="true"
>
Overview
</button>
<button
id="macroButton"
class="tool-button"
type="button"
aria-pressed="false"
>
Pore close-up
</button>
</div>
</section>
<aside id="panel" aria-label="Symbolic stomatal model controls">
<div class="panel-inner">
<p class="panel-kicker">Aperture / response</p>
<h2>Inspect the trade-off</h2>
<p class="intro">
Change the assumed opening and relative transport conditions. The
numbers are dimensionless comparison indices, not physiological
measurements.
</p>
<div class="boundary-note">
This is a teaching abstraction. It does not diagnose plant health or
prescribe an aperture, watering schedule, species, or environment.
</div>
<p class="section-label">Model inputs</p>
<div class="control">
<div class="control-head">
<label for="aperture">Relative aperture</label>
<output id="apertureValue" for="aperture">45%</output>
</div>
<input
id="aperture"
type="range"
min="0"
max="100"
step="1"
value="45"
>
<div class="range-ends">
<span>Closed</span>
<span>Wide</span>
</div>
<small>Relative pore width in this simplified representation.</small>
</div>
<div class="control">
<div class="control-head">
<label for="demand">Relative evaporative demand</label>
<output id="demandValue" for="demand">100%</output>
</div>
<input
id="demand"
type="range"
min="50"
max="150"
step="5"
value="100"
>
<div class="range-ends">
<span>Lower</span>
<span>Higher</span>
</div>
<small>Scales the assumed water-loss response.</small>
</div>
<div class="control">
<div class="control-head">
<label for="carbon">Relative gas-uptake value</label>
<output id="carbonValue" for="carbon">100%</output>
</div>
<input
id="carbon"
type="range"
min="50"
max="150"
step="5"
value="100"
>
<div class="range-ends">
<span>Lower value</span>
<span>Higher value</span>
</div>
<small>Changes the weight of uptake in the symbolic balance.</small>
</div>
<label class="toggle-row" for="flowToggle">
<span>Show conceptual flow particles</span>
<input id="flowToggle" type="checkbox" checked>
</label>
<p class="section-label">Current symbolic response</p>
<div class="metric-grid">
<div class="metric gas">
<span class="metric-label">Gas-uptake index · U</span>
<strong id="gasValue" class="metric-value">—</strong>
<div class="meter gas"><span id="gasBar"></span></div>
<span id="gasNote" class="metric-note"></span>
</div>
<div class="metric water">
<span class="metric-label">Water-loss index · W</span>
<strong id="waterValue" class="metric-value">—</strong>
<div class="meter water"><span id="waterBar"></span></div>
<span id="waterNote" class="metric-note"></span>
</div>
<div class="metric balance">
<div>
<span class="metric-label">Weighted balance · C U − W</span>
<span class="metric-note">A comparison score under these assumptions</span>
</div>
<strong id="balanceValue" class="metric-value">—</strong>
</div>
</div>
<p id="modelStatus" class="model-status" aria-live="polite"></p>
<div class="chart-card">
<div class="chart-title">
<strong>Response curves</strong>
<span>current assumptions</span>
</div>
<div class="chart-legend">
<span><i></i>Gas uptake U</span>
<span><i class="water"></i>Water loss W</span>
</div>
<svg
id="tradeoffChart"
viewBox="0 0 320 170"
role="img"
aria-label="Chart of symbolic gas uptake and water loss against relative aperture"
>
<g id="chartGrid"></g>
<path
id="uptakePath"
fill="none"
stroke="#68e6c0"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
id="waterPath"
fill="none"
stroke="#8fb8ff"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<line
id="currentLine"
x1="0"
x2="0"
y1="14"
y2="136"
stroke="rgba(225,241,234,.25)"
stroke-dasharray="3 3"
></line>
<circle
id="gasMarker"
r="4"
fill="#68e6c0"
stroke="#07110f"
stroke-width="2"
></circle>
<circle
id="waterMarker"
r="4"
fill="#8fb8ff"
stroke="#07110f"
stroke-width="2"
></circle>
<text id="chartMaxLabel" x="4" y="11"></text>
<text x="34" y="158" class="axis-label">relative aperture →</text>
<text
x="0"
y="76"
class="axis-label"
transform="rotate(-90 0 76)"
>symbolic index →</text>
</svg>
</div>
<details open>
<summary>Declared assumptions</summary>
<ol class="assumptions">
<li>
Let <code>r</code> be relative aperture from 0 to 1. Effective
open-area proxy: <code>A = r²</code>.
</li>
<li>
Diminishing gas-uptake proxy:
<code>U = 100 · A^0.72</code>.
The exponent is an explicit simplifying assumption.
</li>
<li>
Let <code>D</code> be relative evaporative demand. Water-loss
proxy: <code>W = 100 · D · A</code>.
</li>
<li>
Let <code>C</code> be the chosen gas-uptake value. Weighted
balance: <code>S = C · U − W</code>.
</li>
<li>
The indices have no units. Species anatomy, diffusion,
photosynthesis, hydraulics, feedback, and real environmental
variation are omitted.
</li>
<li>
Guard-cell spacing, pore size, particle count, and particle
speed are exaggerated for legibility and do not encode measured
physiology.
</li>
</ol>
</details>
<p class="closing-note">
Interpretation boundary: use the curves to compare directions and
trade-offs within the stated model. They do not identify a
“correct” opening for a real plant.
</p>
</div>
</aside>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model state
// -------------------------------------------------------------------------
const state = {
aperture: 45,
demand: 100,
carbon: 100,
showFlow: true
};
const apertureInput = document.querySelector("#aperture");
const demandInput = document.querySelector("#demand");
const carbonInput = document.querySelector("#carbon");
const flowToggle = document.querySelector("#flowToggle");
const apertureValue = document.querySelector("#apertureValue");
const demandValue = document.querySelector("#demandValue");
const carbonValue = document.querySelector("#carbonValue");
const gasValue = document.querySelector("#gasValue");
const waterValue = document.querySelector("#waterValue");
const balanceValue = document.querySelector("#balanceValue");
const gasBar = document.querySelector("#gasBar");
const waterBar = document.querySelector("#waterBar");
const gasNote = document.querySelector("#gasNote");
const waterNote = document.querySelector("#waterNote");
const modelStatus = document.querySelector("#modelStatus");
const uptakePath = document.querySelector("#uptakePath");
const waterPath = document.querySelector("#waterPath");
const currentLine = document.querySelector("#currentLine");
const gasMarker = document.querySelector("#gasMarker");
const waterMarker = document.querySelector("#waterMarker");
const chartMaxLabel = document.querySelector("#chartMaxLabel");
function evaluateModel(aperturePercent, demandPercent, carbonPercent) {
const r = aperturePercent / 100;
const d = demandPercent / 100;
const c = carbonPercent / 100;
const area = r * r;
const uptake = 100 * Math.pow(area, 0.72);
const water = 100 * d * area;
const weightedUptake = c * uptake;
const balance = weightedUptake - water;
const ratio = water > 0.000001 ? uptake / water : null;
return {
r,
d,
c,
area,
uptake,
water,
weightedUptake,
balance,
ratio
};
}
const chart = {
left: 34,
top: 14,
width: 272,
height: 122,
max: 100
};
function chartX(aperturePercent) {
return chart.left + (aperturePercent / 100) * chart.width;
}
function chartY(value) {
return chart.top + (1 - value / chart.max) * chart.height;
}
function buildChartPath(kind) {
const points = [];
for (let i = 0; i <= 100; i++) {
const result = evaluateModel(i, state.demand, state.carbon);
const value = kind === "uptake" ? result.uptake : result.water;
points.push(`${chartX(i).toFixed(2)},${chartY(value).toFixed(2)}`);
}
return `M ${points.join(" L ")}`;
}
function drawChartGrid() {
const grid = document.querySelector("#chartGrid");
const fragments = [];
for (let i = 0; i <= 4; i++) {
const y = chart.top + (i / 4) * chart.height;
const value = chart.max * (1 - i / 4);
fragments.push(`
<line
x1="${chart.left}"
x2="${chart.left + chart.width}"
y1="${y}"
y2="${y}"
stroke="rgba(190,229,211,.09)"
/>
`);
fragments.push(`
<text x="29" y="${y + 3}" text-anchor="end">${Math.round(value)}</text>
`);
}
for (let i = 0; i <= 4; i++) {
const x = chart.left + (i / 4) * chart.width;
fragments.push(`
<line
x1="${x}"
x2="${x}"
y1="${chart.top}"
y2="${chart.top + chart.height}"
stroke="rgba(190,229,211,.045)"
/>
`);
fragments.push(`
<text x="${x}" y="150" text-anchor="middle">${i * 25}</text>
`);
}
grid.innerHTML = fragments.join("");
}
function setRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function updateModelUI() {
const result = evaluateModel(
state.aperture,
state.demand,
state.carbon
);
apertureValue.textContent = `${state.aperture}%`;
demandValue.textContent = `${state.demand}%`;
carbonValue.textContent = `${state.carbon}%`;
gasValue.textContent = result.uptake.toFixed(1);
waterValue.textContent = result.water.toFixed(1);
balanceValue.textContent =
`${result.balance >= 0 ? "+" : ""}${result.balance.toFixed(1)}`;
gasBar.style.width = `${Math.min(100, result.uptake)}%`;
waterBar.style.width = `${Math.min(100, result.water)}%`;
gasNote.textContent =
`Area A = ${result.area.toFixed(3)} · weighted ${result.weightedUptake.toFixed(1)} pts`;
waterNote.textContent =
`Demand D = ${result.d.toFixed(2)} · ratio U/W = ${
result.ratio === null ? "—" : `${result.ratio.toFixed(2)}×`
}`;
modelStatus.textContent =
`At ${state.aperture}% aperture, the chosen uptake weighting gives ` +
`${result.weightedUptake.toFixed(1)} symbolic uptake points against ` +
`${result.water.toFixed(1)} water-loss points.`;
chart.max = Math.ceil(Math.max(100, state.demand) / 50) * 50;
chartMaxLabel.textContent = chart.max;
uptakePath.setAttribute("d", buildChartPath("uptake"));
waterPath.setAttribute("d", buildChartPath("water"));
const x = chartX(state.aperture);
currentLine.setAttribute("x1", x);
currentLine.setAttribute("x2", x);
gasMarker.setAttribute("cx", x);
gasMarker.setAttribute("cy", chartY(result.uptake));
waterMarker.setAttribute("cx", x);
waterMarker.setAttribute("cy", chartY(result.water));
drawChartGrid();
[apertureInput, demandInput, carbonInput].forEach(setRangeFill);
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07110f);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.domElement.setAttribute(
"aria-label",
"A rotatable low-poly leaf underside with conceptual stomatal pores"
);
renderer.domElement.tabIndex = 0;
viewport.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(42, 1, 0.03, 80);
const hemisphere = new THREE.HemisphereLight(
0xa7dfca,
0x06100d,
2.2
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xf1fff7, 3.1);
keyLight.position.set(4, 6, 8);
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x4ed9ac, 22, 24, 2);
rimLight.position.set(-5, -2, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0x9fbaff, 12, 20, 2);
warmLight.position.set(5, 4, -1);
scene.add(warmLight);
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshBasicMaterial({ color: 0x071512 })
);
backdrop.position.z = -1.5;
scene.add(backdrop);
for (const radius of [6.2, 9.8, 13.5]) {
const ring = new THREE.Mesh(
new THREE.RingGeometry(radius, radius + 0.018, 48),
new THREE.MeshBasicMaterial({
color: 0x315f4c,
transparent: true,
opacity: 0.1,
side: THREE.DoubleSide
})
);
ring.position.z = -1.35;
scene.add(ring);
}
// Deterministic pseudo-random values keep the leaf layout stable.
let seed = 81723;
function random() {
seed = (1664525 * seed + 1013904223) >>> 0;
return seed / 4294967296;
}
const dustPositions = [];
for (let i = 0; i < 100; i++) {
dustPositions.push(
(random() - 0.5) * 24,
(random() - 0.5) * 17,
-0.5 - random() * 7
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x8bbca4,
size: 0.025,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
scene.add(dust);
// Leaf surface functions.
function leafHalfWidth(y) {
const t = (y + 3.4) / 6.8;
const sine = Math.max(0, Math.sin(Math.PI * t));
return 4.65 * Math.pow(sine, 0.7);
}
function surfaceZ(x, y) {
const nx = x / 4.7;
return (
0.20 * nx * nx +
0.065 * Math.cos(y * 1.05) -
0.12 * Math.pow(y / 3.4, 2)
);
}
// Faceted leaf mesh.
const rows = 15;
const columns = 10;
const grid = [];
for (let row = 0; row < rows; row++) {
const t = row / (rows - 1);
const y = -3.4 + t * 6.8;
const width = leafHalfWidth(y);
const rowPoints = [];
for (let column = 0; column < columns; column++) {
const u = column / (columns - 1);
const x = (u * 2 - 1) * width;
rowPoints.push(new THREE.Vector3(x, y, surfaceZ(x, y)));
}
grid.push(rowPoints);
}
const leafPositions = [];
const leafColors = [];
const leafBase = new THREE.Color();
function pushTriangle(a, b, c) {
const lightness = 0.155 + random() * 0.055;
const hue = 0.365 + random() * 0.018;
const saturation = 0.34 + random() * 0.08;
leafBase.setHSL(hue, saturation, lightness);
for (const point of [a, b, c]) {
leafPositions.push(point.x, point.y, point.z);
leafColors.push(leafBase.r, leafBase.g, leafBase.b);
}
}
for (let row = 0; row < rows - 1; row++) {
for (let column = 0; column < columns - 1; column++) {
const a = grid[row][column];
const b = grid[row][column + 1];
const c = grid[row + 1][column];
const d = grid[row + 1][column + 1];
pushTriangle(a, b, d);
pushTriangle(a, d, c);
}
}
const leafGeometry = new THREE.BufferGeometry();
leafGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(leafPositions, 3)
);
leafGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(leafColors, 3)
);
leafGeometry.computeVertexNormals();
const leaf = new THREE.Mesh(
leafGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.94,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
})
);
scene.add(leaf);
// Leaf edge.
const edgePoints = [];
for (let row = 0; row < rows; row++) {
const point = grid[row][0].clone();
point.z += 0.012;
edgePoints.push(point);
}
for (let row = rows - 1; row >= 0; row--) {
const point = grid[row][columns - 1].clone();
point.z += 0.012;
edgePoints.push(point);
}
const edge = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(edgePoints),
new THREE.LineBasicMaterial({
color: 0x6ba784,
transparent: true,
opacity: 0.52
})
);
scene.add(edge);
// Central midrib.
const midribPoints = [];
for (let i = 0; i <= 16; i++) {
const y = -3.25 + (i / 16) * 6.5;
const x = Math.sin(y * 0.8) * 0.045;
midribPoints.push(new THREE.Vector3(x, y, surfaceZ(x, y) + 0.045));
}
const midribCurve = new THREE.CatmullRomCurve3(midribPoints);
const midrib = new THREE.Mesh(
new THREE.TubeGeometry(midribCurve, 18, 0.035, 4, false),
new THREE.MeshStandardMaterial({
color: 0x79a765,
roughness: 0.9,
flatShading: true
})
);
scene.add(midrib);
// Branching veins.
const veinPositions = [];
const branchBases = [-2.65, -2.0, -1.35, -0.7, 0, 0.7, 1.35, 2.0, 2.55];
for (const baseY of branchBases) {
for (const side of [-1, 1]) {
const startX = 0.025 * side;
const endX = side * leafHalfWidth(baseY) * 0.76;
const endY = baseY + 0.28;
veinPositions.push(
startX,
baseY,
surfaceZ(startX, baseY) + 0.032,
endX,
endY,
surfaceZ(endX, endY) + 0.025
);
}
}
const veinGeometry = new THREE.BufferGeometry();
veinGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(veinPositions, 3)
);
const veins = new THREE.LineSegments(
veinGeometry,
new THREE.LineBasicMaterial({
color: 0x88ad71,
transparent: true,
opacity: 0.42
})
);
scene.add(veins);
// -------------------------------------------------------------------------
// Stomata: dark pores between paired low-poly guard cells.
// -------------------------------------------------------------------------
const poreGeometry = new THREE.CircleGeometry(1, 10);
const poreMaterial = new THREE.MeshStandardMaterial({
color: 0x06100d,
roughness: 1,
metalness: 0,
side: THREE.DoubleSide
});
const guardGeometry = new THREE.IcosahedronGeometry(1, 0);
const guardMaterial = new THREE.MeshStandardMaterial({
color: 0x83c978,
roughness: 0.82,
metalness: 0,
flatShading: true
});
const stomata = [];
for (let row = 0; row < 8; row++) {
for (let column = 0; column < 6; column++) {
const baseY = -2.65 + (row / 7) * 5.3;
const y = baseY + (random() - 0.5) * 0.17;
const halfWidth = leafHalfWidth(y) * 0.79;
let x =
-halfWidth +
((column + 0.5) / 6) * halfWidth * 2 +
(random() - 0.5) * 0.2;
if (Math.abs(x) < 0.22) {
x += column % 2 === 0 ? 0.29 : -0.29;
}
const group = new THREE.Group();
group.position.set(x, y, surfaceZ(x, y) + 0.045);
group.rotation.z = (random() - 0.5) * 0.8;
const baseScale = 0.78 + random() * 0.42;
group.scale.setScalar(baseScale);
const pore = new THREE.Mesh(poreGeometry, poreMaterial);
pore.position.z = 0.004;
group.add(pore);
const leftGuard = new THREE.Mesh(guardGeometry, guardMaterial);
leftGuard.scale.set(0.055, 0.145, 0.035);
leftGuard.position.z = 0.018;
group.add(leftGuard);
const rightGuard = new THREE.Mesh(guardGeometry, guardMaterial);
rightGuard.scale.set(0.055, 0.145, 0.035);
rightGuard.position.z = 0.018;
group.add(rightGuard);
scene.add(group);
stomata.push({
group,
pore,
leftGuard,
rightGuard,
x,
y,
z: group.position.z,
phase: random(),
drift: (random() - 0.5) * 0.12
});
}
}
// Select a visually useful stomata for the close-up.
let hero = stomata[0];
let heroDistance = Infinity;
for (const stoma of stomata) {
const distance =
Math.abs(stoma.x - 1.6) * 1.25 + Math.abs(stoma.y - 0.45);
if (distance < heroDistance) {
heroDistance = distance;
hero = stoma;
}
}
hero.group.scale.multiplyScalar(1.45);
function updateStomataVisual(aperturePercent) {
const r = aperturePercent / 100;
for (const stoma of stomata) {
stoma.pore.scale.set(0.012 + 0.082 * r, 0.125, 1);
const guardOffset = 0.052 + 0.039 * r;
stoma.leftGuard.position.x = -guardOffset;
stoma.rightGuard.position.x = guardOffset;
}
}
// -------------------------------------------------------------------------
// Conceptual flow particles
// -------------------------------------------------------------------------
const co2SlotsPerStoma = 3;
const waterSlotsPerStoma = 2;
const particleCount = stomata.length * co2SlotsPerStoma;
const co2Positions = new Float32Array(
stomata.length * co2SlotsPerStoma * 3
);
const waterPositions = new Float32Array(
stomata.length * waterSlotsPerStoma * 3
);
const co2Geometry = new THREE.BufferGeometry();
co2Geometry.setAttribute(
"position",
new THREE.BufferAttribute(co2Positions, 3)
);
const waterGeometry = new THREE.BufferGeometry();
waterGeometry.setAttribute(
"position",
new THREE.BufferAttribute(waterPositions, 3)
);
const co2Particles = new THREE.Points(
co2Geometry,
new THREE.PointsMaterial({
color: 0x68e6c0,
size: 0.052,
transparent: true,
opacity: 0.82,
depthWrite: false,
sizeAttenuation: true
})
);
const waterParticles = new THREE.Points(
waterGeometry,
new THREE.PointsMaterial({
color: 0x8fb8ff,
size: 0.044,
transparent: true,
opacity: 0.72,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(co2Particles, waterParticles);
function updateFlowParticles(time) {
const r = state.aperture / 100;
const demandScale = 0.82 + state.demand / 500;
const co2ActiveSlots = Math.ceil(co2SlotsPerStoma * r);
const waterActiveSlots = Math.ceil(waterSlotsPerStoma * r);
for (let i = 0; i < stomata.length; i++) {
const stoma = stomata[i];
for (let slot = 0; slot < co2SlotsPerStoma; slot++) {
const index = (i * co2SlotsPerStoma + slot) * 3;
if (slot >= co2ActiveSlots) {
co2Positions[index] = 999;
co2Positions[index + 1] = 999;
co2Positions[index + 2] = 999;
continue;
}
const q =
(time * (0.14 + r * 0.08) +
slot / co2SlotsPerStoma +
stoma.phase) %
1;
co2Positions[index] =
stoma.x + stoma.drift * (q - 0.5) * 0.55;
co2Positions[index + 1] = stoma.y;
co2Positions[index + 2] = stoma.z + 1.12 - q * 1.03;
}
for (let slot = 0; slot < waterSlotsPerStoma; slot++) {
const index = (i * waterSlotsPerStoma + slot) * 3;
if (slot >= waterActiveSlots) {
waterPositions[index] = 999;
waterPositions[index + 1] = 999;
waterPositions[index + 2] = 999;
continue;
}
const q =
(time *
(0.16 + r * 0.10) *
demandScale +
slot / waterSlotsPerStoma +
stoma.phase) %
1;
waterPositions[index] =
stoma.x + stoma.drift * q * 0.7;
waterPositions[index + 1] = stoma.y;
waterPositions[index + 2] = stoma.z + 0.04 + q * 1.25;
}
}
co2Geometry.attributes.position.needsUpdate = true;
waterGeometry.attributes.position.needsUpdate = true;
co2Particles.visible = state.showFlow;
waterParticles.visible = state.showFlow;
}
// -------------------------------------------------------------------------
// Direct camera controls: no Three.js addons.
// -------------------------------------------------------------------------
const overviewTarget = new THREE.Vector3(0, 0, 0);
const heroTarget = hero.group.position.clone();
function overviewDistance() {
const verticalHalfSize = 4.45;
const horizontalHalfSize = 5.6;
const tangent = Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5));
return Math.max(
verticalHalfSize / tangent,
horizontalHalfSize / (tangent * camera.aspect)
);
}
const cameraGoal = {
target: overviewTarget.clone(),
distance: 13,
theta: 0,
phi: 0.11
};
const cameraCurrent = {
target: overviewTarget.clone(),
distance: 13,
theta: 0,
phi: 0.11
};
function setOverview() {
cameraGoal.target.copy(overviewTarget);
cameraGoal.distance = overviewDistance();
cameraGoal.theta = 0;
cameraGoal.phi = 0.11;
document.querySelector("#overviewButton").setAttribute(
"aria-pressed",
"true"
);
document.querySelector("#macroButton").setAttribute(
"aria-pressed",
"false"
);
}
function setMacro() {
cameraGoal.target.copy(heroTarget);
cameraGoal.distance = 1.12;
cameraGoal.theta = -0.18;
cameraGoal.phi = 0.06;
document.querySelector("#overviewButton").setAttribute(
"aria-pressed",
"false"
);
document.querySelector("#macroButton").setAttribute(
"aria-pressed",
"true"
);
}
document
.querySelector("#overviewButton")
.addEventListener("click", setOverview);
document
.querySelector("#macroButton")
.addEventListener("click", setMacro);
const pointers = new Map();
let lastPinchDistance = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
lastPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
cameraGoal.theta -= dx * 0.006;
cameraGoal.phi = THREE.MathUtils.clamp(
cameraGoal.phi + dy * 0.006,
0.035,
1.48
);
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (lastPinchDistance && distance > 0) {
cameraGoal.distance = THREE.MathUtils.clamp(
cameraGoal.distance * (lastPinchDistance / distance),
0.65,
28
);
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = null;
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("lostpointercapture", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal.distance = THREE.MathUtils.clamp(
cameraGoal.distance * Math.exp(event.deltaY * 0.001),
0.65,
28
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
if (
document.querySelector("#macroButton").getAttribute("aria-pressed") ===
"true"
) {
setOverview();
} else {
setMacro();
}
});
function updateCamera(deltaTime) {
const blend = 1 - Math.exp(-deltaTime * 8);
cameraCurrent.target.lerp(cameraGoal.target, blend);
cameraCurrent.distance = THREE.MathUtils.lerp(
cameraCurrent.distance,
cameraGoal.distance,
blend
);
cameraCurrent.theta = THREE.MathUtils.lerp(
cameraCurrent.theta,
cameraGoal.theta,
blend
);
cameraCurrent.phi = THREE.MathUtils.lerp(
cameraCurrent.phi,
cameraGoal.phi,
blend
);
const { target, distance, theta, phi } = cameraCurrent;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + distance * sinPhi * Math.sin(theta),
target.y + distance * sinPhi * Math.cos(theta),
target.z + distance * Math.cos(phi)
);
camera.lookAt(target);
}
// -------------------------------------------------------------------------
// Input handling and resize
// -------------------------------------------------------------------------
apertureInput.addEventListener("input", () => {
state.aperture = Number(apertureInput.value);
updateStomataVisual(state.aperture);
updateModelUI();
});
demandInput.addEventListener("input", () => {
state.demand = Number(demandInput.value);
updateModelUI();
});
carbonInput.addEventListener("input", () => {
state.carbon = Number(carbonInput.value);
updateModelUI();
});
flowToggle.addEventListener("change", () => {
state.showFlow = flowToggle.checked;
});
let firstResize = true;
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(1, height);
camera.updateProjectionMatrix();
if (firstResize) {
cameraGoal.distance = overviewDistance();
cameraCurrent.distance = cameraGoal.distance;
firstResize = false;
}
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
// -------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------
updateStomataVisual(state.aperture);
updateModelUI();
const clock = new THREE.Clock();
function animate() {
const deltaTime = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera(deltaTime);
updateFlowParticles(elapsed);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```