Prompt specification
Build or customize the Three.js 3D scene "Plant cell turgor teaching station" 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: Plant cell turgor teaching station
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/plant-cell-turgor-teaching-station-x0863
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows osmosis. Compose a rigid cell wall around a flexible membrane and vacuole; compare supported and reduced-turgor states with clear geometry rather than a generic deflating sphere. Voxel style.
Working Three.js Starter (plant-cell-turgor-teaching-station-x0863):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Osmosis & Turgor — Voxel Plant Cell</title>
<style>
:root {
--ink: #17352d;
--muted: #64756e;
--paper: #f4f1e8;
--wall: #c8a46a;
--membrane: #32c4cf;
--vacuole: #58b875;
--cytoplasm: #e5b64d;
--water: #168ed1;
--panel: rgba(255, 255, 255, 0.82);
--line: rgba(23, 53, 45, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
min-height: 92px;
padding: 20px 26px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
pointer-events: none;
background: linear-gradient(
to bottom,
rgba(244, 241, 232, 0.96),
rgba(244, 241, 232, 0)
);
}
.brand {
min-width: 260px;
}
.eyebrow {
display: block;
margin-bottom: 4px;
color: #658077;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 820;
}
.intro {
max-width: 480px;
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.intro strong {
color: var(--ink);
}
.controls {
display: flex;
gap: 8px;
pointer-events: auto;
}
.control-button {
appearance: none;
border: 1px solid rgba(23, 53, 45, 0.18);
border-radius: 999px;
padding: 9px 14px;
color: var(--ink);
background: rgba(255, 255, 255, 0.76);
box-shadow: 0 6px 20px rgba(30, 48, 40, 0.06);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition:
transform 150ms ease,
background 150ms ease;
}
.control-button:hover {
transform: translateY(-1px);
background: #fff;
}
.control-button:focus-visible {
outline: 3px solid rgba(50, 196, 207, 0.35);
outline-offset: 2px;
}
.legend {
position: fixed;
z-index: 8;
top: 99px;
right: 26px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: 390px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.72);
backdrop-filter: blur(8px);
color: #52665e;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.02em;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.swatch.wall {
background: var(--wall);
}
.swatch.membrane {
background: var(--membrane);
}
.swatch.vacuole {
background: var(--vacuole);
}
.swatch.cytoplasm {
background: var(--cytoplasm);
}
.swatch.water {
background: var(--water);
}
.model-tag,
.flow-tag {
position: fixed;
z-index: 9;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.model-tag {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 12px 8px 8px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 10px 30px rgba(29, 47, 39, 0.09);
backdrop-filter: blur(10px);
}
.model-tag .letter {
display: grid;
place-items: center;
width: 28px;
height: 28px;
border-radius: 8px;
color: white;
background: var(--ink);
font-size: 12px;
font-weight: 850;
}
.model-tag b {
display: block;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model-tag small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.flow-tag {
padding: 6px 9px;
border-radius: 999px;
color: #fff;
background: #168ed1;
box-shadow: 0 7px 18px rgba(22, 142, 209, 0.24);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.flow-tag.out {
background: #b95c42;
box-shadow: 0 7px 18px rgba(185, 92, 66, 0.22);
}
.comparison {
position: fixed;
z-index: 8;
left: 26px;
right: 26px;
bottom: 22px;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
pointer-events: none;
}
.card {
position: relative;
overflow: hidden;
padding: 16px 18px 15px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(33, 48, 41, 0.08);
backdrop-filter: blur(14px);
}
.card::before {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 3px;
content: "";
background: #3a9b62;
}
.card.reduced::before {
background: #c36c4e;
}
.card-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.state {
color: #527061;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.flow-badge {
padding: 5px 8px;
border-radius: 999px;
color: #126f2e;
background: #dff2d8;
font-size: 9px;
font-weight: 850;
white-space: nowrap;
}
.flow-badge.out {
color: #97472f;
background: #f4dfd4;
}
.card h2 {
margin: 7px 0 5px;
font-size: 17px;
letter-spacing: -0.025em;
}
.card p {
max-width: 570px;
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.48;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
margin-top: 12px;
}
.metric {
padding-top: 8px;
border-top: 1px solid var(--line);
}
.metric b {
display: block;
margin-bottom: 2px;
font-size: 9px;
}
.metric span {
color: var(--muted);
font-size: 9px;
line-height: 1.3;
}
.orbit-hint {
position: fixed;
z-index: 7;
left: 50%;
bottom: 213px;
padding: 6px 10px;
border: 1px solid var(--line);
border-radius: 999px;
color: #6d7d76;
background: rgba(255, 255, 255, 0.62);
font-size: 9px;
font-weight: 700;
transform: translateX(-50%);
pointer-events: none;
}
@media (max-width: 760px) {
.topbar {
padding: 15px;
gap: 10px;
}
.brand {
min-width: 0;
}
.intro {
display: none;
}
.control-button {
padding: 8px 10px;
font-size: 9px;
}
.legend {
top: 76px;
right: 12px;
left: 12px;
justify-content: center;
max-width: none;
}
.legend-item {
padding: 5px 7px;
font-size: 8px;
}
.comparison {
left: 10px;
right: 10px;
bottom: 10px;
gap: 8px;
}
.card {
padding: 12px;
border-radius: 12px;
}
.card h2 {
font-size: 14px;
}
.card p {
font-size: 9px;
}
.metrics {
grid-template-columns: 1fr;
gap: 4px;
margin-top: 8px;
}
.metric:nth-child(3) {
display: none;
}
.metric {
padding-top: 4px;
}
.model-tag {
padding: 6px 8px 6px 6px;
}
.model-tag .letter {
width: 22px;
height: 22px;
font-size: 9px;
}
.model-tag b {
font-size: 8px;
}
.model-tag small {
display: none;
}
.orbit-hint {
bottom: 255px;
}
}
@media (max-height: 650px) {
.legend {
display: none;
}
.card p {
display: none;
}
.orbit-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive voxel plant-cell comparison"></main>
<header class="topbar">
<div class="brand">
<span class="eyebrow">Biology Lab · Osmosis</span>
<h1>Turgor, under pressure.</h1>
</div>
<p class="intro">
<strong>The cell wall stays rigid.</strong>
Only the membrane and vacuole change shape as water moves.
Drag to orbit; scroll or pinch to zoom.
</p>
<div class="controls">
<button class="control-button" id="flowButton" type="button">
Pause water flow
</button>
<button class="control-button" id="resetButton" type="button">
Reset view
</button>
</div>
</header>
<aside class="legend" aria-label="Color legend">
<div class="legend-item"><span class="swatch wall"></span>Cell wall</div>
<div class="legend-item"><span class="swatch membrane"></span>Membrane</div>
<div class="legend-item"><span class="swatch vacuole"></span>Vacuole</div>
<div class="legend-item"><span class="swatch cytoplasm"></span>Cytoplasm</div>
<div class="legend-item"><span class="swatch water"></span>Water</div>
</aside>
<div class="model-tag" id="supportedTag">
<span class="letter">A</span>
<div>
<b>Supported turgor</b>
<small>Expanded membrane · wall loaded</small>
</div>
</div>
<div class="model-tag" id="reducedTag">
<span class="letter">B</span>
<div>
<b>Reduced turgor</b>
<small>Contracted contents · wall unchanged</small>
</div>
</div>
<div class="flow-tag" id="inFlowTag">H<sub>2</sub>O in</div>
<div class="flow-tag out" id="outFlowTag">H<sub>2</sub>O out</div>
<div class="orbit-hint">Drag to orbit · Shift-drag to pan · Double-click to reset</div>
<section class="comparison" aria-label="Turgor state comparison">
<article class="card supported">
<div class="card-head">
<span class="state">A · Supported turgor</span>
<span class="flow-badge">Net flow → inward</span>
</div>
<h2>Water enters; pressure builds.</h2>
<p>
The solute-rich vacuole takes up water and expands. Its membrane presses
against the inside of the rigid wall, which resists further expansion.
</p>
<div class="metrics">
<div class="metric">
<b>Rigid wall</b>
<span>Same outline</span>
</div>
<div class="metric">
<b>Large vacuole</b>
<span>Presses outward</span>
</div>
<div class="metric">
<b>Turgor</b>
<span>High pressure</span>
</div>
</div>
</article>
<article class="card reduced">
<div class="card-head">
<span class="state">B · Reduced turgor</span>
<span class="flow-badge out">Net flow → outward</span>
</div>
<h2>Water leaves; contents pull inward.</h2>
<p>
The vacuole loses water and becomes smaller and irregular. The membrane
separates from the wall, while the wall itself keeps its original shape.
</p>
<div class="metrics">
<div class="metric">
<b>Rigid wall</b>
<span>Same outline</span>
</div>
<div class="metric">
<b>Smaller vacuole</b>
<span>Pulls inward</span>
</div>
<div class="metric">
<b>Turgor</b>
<span>Low pressure</span>
</div>
</div>
</article>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4f1e8);
scene.fog = new THREE.Fog(0xf4f1e8, 24, 58);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xffffff, 0xb8aa8c, 2.2);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff7df, 3.3);
keyLight.position.set(-6, 12, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcce9ff, 1.1);
fillLight.position.set(9, 3, 7);
scene.add(fillLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(70, 70),
new THREE.MeshStandardMaterial({
color: 0xf4f1e8,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.18;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(44, 44, 0xb9b6aa, 0xd9d4c7);
grid.position.y = -3.165;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
function hash(i, j, k = 0) {
const value = Math.sin(i * 127.1 + j * 311.7 + k * 74.7) * 43758.5453;
return value - Math.floor(value);
}
function pointInPolygon(x, y, polygon) {
let inside = false;
for (
let i = 0, j = polygon.length - 1;
i < polygon.length;
j = i++
) {
const xi = polygon[i][0];
const yi = polygon[i][1];
const xj = polygon[j][0];
const yj = polygon[j][1];
const crosses = yi > y !== yj > y;
if (
crosses &&
x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
) {
inside = !inside;
}
}
return inside;
}
function makeMask(polygon, bounds, step) {
const cells = [];
const minX = bounds[0];
const maxX = bounds[1];
const minY = bounds[2];
const maxY = bounds[3];
const columns = Math.floor((maxX - minX) / step) + 1;
const rows = Math.floor((maxY - minY) / step) + 1;
for (let i = 0; i < columns; i++) {
for (let j = 0; j < rows; j++) {
const x = minX + (i + 0.5) * step;
const y = minY + (j + 0.5) * step;
if (pointInPolygon(x, y, polygon)) {
cells.push({ i, j, x, y });
}
}
}
return cells;
}
function makeBoundary(mask) {
const lookup = new Map();
for (const cell of mask) {
lookup.set(`${cell.i},${cell.j}`, cell);
}
const boundary = [];
for (const cell of mask) {
const isBoundary =
!lookup.has(`${cell.i + 1},${cell.j}`) ||
!lookup.has(`${cell.i - 1},${cell.j}`) ||
!lookup.has(`${cell.i},${cell.j + 1}`) ||
!lookup.has(`${cell.i},${cell.j - 1}`);
if (isBoundary) {
boundary.push(cell);
}
}
return boundary;
}
function instancedBoxes(
positions,
size,
material,
parent,
colorFunction = null,
castShadow = true
) {
if (!positions.length) return null;
const geometry = new THREE.BoxGeometry(size, size, size);
if (material.vertexColors) {
const colors = new Float32Array(geometry.getAttribute("position").count * 3).fill(1);
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
}
const mesh = new THREE.InstancedMesh(geometry, material, positions.length);
positions.forEach((position, index) => {
dummy.position.set(position[0], position[1], position[2]);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (colorFunction) {
colorFunction(position, index, tempColor);
mesh.setColorAt(index, tempColor);
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.8,
metalness: 0
});
const membraneMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.36,
metalness: 0.03,
emissive: 0x073d43,
emissiveIntensity: 0.25
});
const vacuoleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.48,
metalness: 0,
emissive: 0x0b2818,
emissiveIntensity: 0.13
});
const cytoplasmMaterial = new THREE.MeshStandardMaterial({
color: 0xe5b64d,
roughness: 0.62,
metalness: 0,
emissive: 0x3b2500,
emissiveIntensity: 0.12
});
const soluteMaterial = new THREE.MeshStandardMaterial({
color: 0xd56845,
roughness: 0.5,
metalness: 0,
emissive: 0x4b1606,
emissiveIntensity: 0.18
});
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0xe4dfd3,
roughness: 0.88,
metalness: 0
});
function addPedestal(group) {
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(6.25, 0.22, 3.05),
pedestalMaterial
);
pedestal.position.set(0, -3.01, 0);
pedestal.receiveShadow = true;
pedestal.castShadow = true;
group.add(pedestal);
}
function addWall(group) {
const positions = [];
const halfWidth = 2.52;
const halfHeight = 2.85;
const size = 0.34;
const depths = [-0.78, 0, 0.78];
const columns = Math.round((halfWidth * 2) / size);
const rows = Math.round((halfHeight * 2) / size);
for (const z of depths) {
for (let i = 0; i < columns; i++) {
const x = -halfWidth + size / 2 + i * size;
positions.push([x, -halfHeight, z]);
positions.push([x, halfHeight, z]);
}
for (let j = 1; j < rows - 1; j++) {
const y = -halfHeight + size / 2 + j * size;
positions.push([-halfWidth, y, z]);
positions.push([halfWidth, y, z]);
}
}
instancedBoxes(
positions,
size * 0.91,
wallMaterial,
group,
(position, index, color) => {
const variation = hash(index, 4);
color.setHex(0xc6a066);
color.offsetHSL(
(variation - 0.5) * 0.012,
(variation - 0.5) * 0.08,
(variation - 0.5) * 0.1
);
}
);
}
function addMembrane(group, membranePolygon, bounds, step) {
const mask = makeMask(membranePolygon, bounds, step);
const boundary = makeBoundary(mask);
const lookup = new Set(mask.map((cell) => `${cell.i},${cell.j}`));
const positions = [];
for (const cell of boundary) {
positions.push([cell.x, cell.y, 0.76]);
positions.push([cell.x, cell.y, -0.76]);
}
const connectorStride = Math.max(1, Math.floor(boundary.length / 9));
boundary.forEach((cell, index) => {
if (index % connectorStride !== 0) return;
for (const z of [-0.56, -0.34, -0.12, 0.1, 0.32, 0.54]) {
positions.push([cell.x, cell.y, z]);
}
});
instancedBoxes(
positions,
step * 0.9,
membraneMaterial,
group,
(position, index, color) => {
const variation = hash(index, 9);
color.setHex(0x25bec8);
color.offsetHSL(0, 0, (variation - 0.5) * 0.13);
}
);
return lookup;
}
function addVacuole(group, vacuolePolygon, bounds, step) {
const mask = makeMask(vacuolePolygon, bounds, step);
const lookup = new Set(mask.map((cell) => `${cell.i},${cell.j}`));
const boundaryKeys = new Set(
makeBoundary(mask).map((cell) => `${cell.i},${cell.j}`)
);
const positions = [];
for (const cell of mask) {
positions.push([cell.x, cell.y, -0.22]);
positions.push([cell.x, cell.y, 0.27]);
}
instancedBoxes(
positions,
step * 0.93,
vacuoleMaterial,
group,
(position, index, color) => {
const cellIndex = Math.floor(index / 2);
const isBoundary = boundaryKeys.has(
`${Math.floor((position[0] - bounds[0]) / step)},${Math.floor(
(position[1] - bounds[2]) / step
)}`
);
const variation = hash(cellIndex, 17);
if (isBoundary) {
color.setHex(0x338f58);
} else {
color.setHex(0x70c67c);
}
color.offsetHSL(
(variation - 0.5) * 0.018,
(variation - 0.5) * 0.05,
(variation - 0.5) * 0.1
);
}
);
return lookup;
}
function addCytoplasm(
group,
membraneLookup,
vacuoleLookup,
membraneCells,
bounds,
step
) {
const positions = [];
for (const cell of membraneCells) {
const key = `${cell.i},${cell.j}`;
if (
!vacuoleLookup.has(key) &&
hash(cell.i, cell.j, 31) > 0.53
) {
positions.push([
cell.x + (hash(cell.i, cell.j, 32) - 0.5) * 0.06,
cell.y + (hash(cell.i, cell.j, 33) - 0.5) * 0.06,
0.47 + hash(cell.i, cell.j, 34) * 0.12
]);
}
}
instancedBoxes(
positions,
0.085,
cytoplasmMaterial,
group,
null,
false
);
}
function addSolute(group, vacuoleCells) {
const positions = [];
for (const cell of vacuoleCells) {
if (hash(cell.i, cell.j, 54) > 0.965) {
positions.push([
cell.x + (hash(cell.i, cell.j, 55) - 0.5) * 0.07,
cell.y + (hash(cell.i, cell.j, 56) - 0.5) * 0.07,
0.43
]);
}
}
instancedBoxes(
positions,
0.09,
soluteMaterial,
group,
null,
false
);
}
const supportedCell = new THREE.Group();
const reducedCell = new THREE.Group();
supportedCell.position.x = -3.7;
reducedCell.position.x = 3.7;
scene.add(supportedCell, reducedCell);
addPedestal(supportedCell);
addPedestal(reducedCell);
const supportedMembrane = [
[-2.1, -2.66],
[2.1, -2.66],
[2.28, -2.48],
[2.28, 2.48],
[2.1, 2.66],
[-2.1, 2.66],
[-2.28, 2.48],
[-2.28, -2.48]
];
const supportedVacuole = [
[-1.95, -2.45],
[1.95, -2.45],
[2.1, -2.28],
[2.1, 2.28],
[1.95, 2.45],
[-1.95, 2.45],
[-2.1, 2.28],
[-2.1, -2.28]
];
const reducedMembrane = [
[-1.72, -2.22],
[1.55, -2.34],
[1.95, -1.96],
[1.99, 1.75],
[1.68, 2.15],
[-1.55, 2.23],
[-1.98, 1.83],
[-1.98, -1.88]
];
const reducedVacuole = [
[-1.45, -1.52],
[-0.55, -1.88],
[0.72, -1.75],
[1.47, -1.21],
[1.37, 0.28],
[0.97, 1.38],
[0.08, 1.67],
[-1.22, 1.38],
[-1.63, 0.52],
[-1.58, -0.8]
];
const bounds = [-2.5, 2.5, -2.8, 2.8];
const step = 0.18;
for (const cell of [supportedCell, reducedCell]) {
addWall(cell);
}
const supportedMask = makeMask(supportedMembrane, bounds, step);
const supportedMembraneLookup = addMembrane(
supportedCell,
supportedMembrane,
bounds,
step
);
const supportedVacuoleMask = makeMask(supportedVacuole, bounds, step);
const supportedVacuoleLookup = addVacuole(
supportedCell,
supportedVacuole,
bounds,
step
);
addCytoplasm(
supportedCell,
supportedMembraneLookup,
supportedVacuoleLookup,
supportedMask,
bounds,
step
);
addSolute(supportedCell, supportedVacuoleMask);
const reducedMask = makeMask(reducedMembrane, bounds, step);
const reducedMembraneLookup = addMembrane(
reducedCell,
reducedMembrane,
bounds,
step
);
const reducedVacuoleMask = makeMask(reducedVacuole, bounds, step);
const reducedVacuoleLookup = addVacuole(
reducedCell,
reducedVacuole,
bounds,
step
);
addCytoplasm(
reducedCell,
reducedMembraneLookup,
reducedVacuoleLookup,
reducedMask,
bounds,
step
);
addSolute(reducedCell, reducedVacuoleMask);
function createWaterFlow(group, points, speed) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point)),
false,
"catmullrom",
0.35
);
const material = new THREE.MeshStandardMaterial({
color: 0x168ed1,
roughness: 0.22,
metalness: 0.02,
emissive: 0x07588d,
emissiveIntensity: 0.72
});
const particles = [];
const geometry = new THREE.BoxGeometry(0.115, 0.115, 0.115);
const count = 15;
for (let i = 0; i < count; i++) {
const particle = new THREE.Mesh(geometry, material);
particle.castShadow = true;
group.add(particle);
particles.push(particle);
}
return {
curve,
particles,
speed,
update(time) {
particles.forEach((particle, index) => {
const t = (time * speed + index / particles.length) % 1;
const point = curve.getPoint(t);
particle.position.copy(point);
const pulse = 0.82 + Math.sin(time * 5 + index) * 0.13;
particle.scale.setScalar(pulse);
});
}
};
}
const supportedFlow = createWaterFlow(
supportedCell,
[
[-2.83, 1.72, 1.2],
[-2.48, 1.65, 1.05],
[-2.08, 1.54, 0.82],
[-1.62, 1.43, 0.58],
[-1.06, 1.29, 0.43]
],
0.15
);
const reducedFlow = createWaterFlow(
reducedCell,
[
[0.35, 0.95, 0.43],
[0.9, 1.08, 0.61],
[1.48, 1.22, 0.8],
[2.12, 1.38, 1.02],
[2.83, 1.55, 1.2]
],
0.15
);
// Custom camera controls. No examples or addon modules are used.
const desired = {
theta: 0.18,
phi: 1.22,
radius: 17,
target: new THREE.Vector3(0, -0.12, 0)
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius,
target: desired.target.clone()
};
let dragging = false;
let panning = false;
let previousX = 0;
let previousY = 0;
let userMovedCamera = false;
const canvas = renderer.domElement;
function fitInitialCamera() {
const verticalFov = THREE.MathUtils.degToRad(camera.fov);
const aspect = window.innerWidth / window.innerHeight;
const horizontalHalfFov = Math.atan(Math.tan(verticalFov / 2) * aspect);
const requiredRadius = 6.8 / Math.tan(horizontalHalfFov);
desired.radius = THREE.MathUtils.clamp(
Math.max(17, requiredRadius),
17,
39
);
if (!userMovedCamera) {
current.radius = desired.radius;
}
}
function resetCamera() {
userMovedCamera = false;
desired.theta = 0.18;
desired.phi = 1.22;
desired.target.set(0, -0.12, 0);
fitInitialCamera();
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
previousX = event.clientX;
previousY = event.clientY;
userMovedCamera = true;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = panning ? "grabbing" : "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panning) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const scale =
(current.radius *
2 *
Math.tan(THREE.MathUtils.degToRad(camera.fov / 2))) /
window.innerHeight;
desired.target
.addScaledVector(right, -deltaX * scale)
.addScaledVector(up, deltaY * scale);
} else {
desired.theta -= deltaX * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi + deltaY * 0.005,
0.62,
2.12
);
}
});
function stopDragging(event) {
dragging = false;
panning = false;
canvas.style.cursor = "grab";
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
userMovedCamera = true;
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
9,
42
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetButton").addEventListener("click", resetCamera);
let flowPlaying = true;
let flowTime = 0;
const flowButton = document.getElementById("flowButton");
flowButton.addEventListener("click", () => {
flowPlaying = !flowPlaying;
flowButton.textContent = flowPlaying ? "Pause water flow" : "Play water flow";
});
const supportedTag = document.getElementById("supportedTag");
const reducedTag = document.getElementById("reducedTag");
const inFlowTag = document.getElementById("inFlowTag");
const outFlowTag = document.getElementById("outFlowTag");
function projectElement(element, group, localPosition) {
const world = group.localToWorld(
new THREE.Vector3(...localPosition)
);
const projected = world.project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = projected.z > 1 ? "0" : "1";
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
fitInitialCamera();
}
window.addEventListener("resize", onResize);
fitInitialCamera();
canvas.style.cursor = "grab";
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (flowPlaying) {
flowTime += delta;
}
current.theta = THREE.MathUtils.lerp(
current.theta,
desired.theta,
0.085
);
current.phi = THREE.MathUtils.lerp(
current.phi,
desired.phi,
0.085
);
current.radius = THREE.MathUtils.lerp(
current.radius,
desired.radius,
0.09
);
current.target.lerp(desired.target, 0.09);
const sinPhi = Math.sin(current.phi);
camera.position.set(
current.target.x +
current.radius * sinPhi * Math.sin(current.theta),
current.target.y + current.radius * Math.cos(current.phi),
current.target.z +
current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(current.target);
camera.updateMatrixWorld();
supportedFlow.update(flowTime);
reducedFlow.update(flowTime);
projectElement(supportedTag, supportedCell, [0, 3.33, 0.08]);
projectElement(reducedTag, reducedCell, [0, 3.33, 0.08]);
projectElement(inFlowTag, supportedCell, [-1.92, 2.03, 1.16]);
projectElement(outFlowTag, reducedCell, [2.16, 1.87, 1.18]);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```