Prompt specification
Build or customize the Three.js 3D scene "Dry and hydrated moss cushions" 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: Dry and hydrated moss cushions
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/dry-and-hydrated-moss-cushions-x0753
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Display matched moss cushions with curled versus expanded leaf forms; include a droplet scale reference and label the examples as selected hydration states of a simplified moss.
Working Three.js Starter (dry-and-hydrated-moss-cushions-x0753):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Moss — Selected Hydration States</title>
<style>
:root {
--ink: #26332b;
--muted: #6f776f;
--paper: #eeeae1;
--line: rgba(38, 51, 43, 0.18);
--dry: #8b8056;
--wet: #438557;
--drop: #4d9298;
}
* {
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;
}
body {
overscroll-behavior: none;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas.dragging {
cursor: grabbing;
}
header {
position: absolute;
z-index: 5;
top: 30px;
left: 34px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 11px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 5vw, 68px);
font-weight: 400;
line-height: 0.94;
letter-spacing: -0.045em;
}
.intro {
max-width: 365px;
margin: 17px 0 0;
color: #526057;
font-size: 13px;
line-height: 1.55;
}
.metadata {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 18px;
}
.metadata span {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(248, 246, 240, 0.56);
color: #59645c;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.state-label {
position: absolute;
z-index: 4;
width: 216px;
padding: 13px 15px 14px;
border: 1px solid rgba(38, 51, 43, 0.17);
border-radius: 4px;
background: rgba(248, 246, 240, 0.87);
box-shadow: 0 12px 36px rgba(42, 51, 43, 0.09);
backdrop-filter: blur(12px);
pointer-events: none;
will-change: transform, opacity;
transition: opacity 180ms ease;
}
.state-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 25px;
background: rgba(38, 51, 43, 0.35);
}
.state-label .label-kicker {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 7px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.state-label .dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--dry);
box-shadow: 0 0 0 3px rgba(139, 128, 86, 0.12);
}
.state-label.wet .dot {
background: var(--wet);
box-shadow: 0 0 0 3px rgba(67, 133, 87, 0.12);
}
.state-label h2 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
font-weight: 400;
line-height: 1;
letter-spacing: -0.025em;
}
.state-label p {
margin: 7px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.drop-label {
position: absolute;
z-index: 4;
width: 124px;
padding: 8px 10px;
border: 1px solid rgba(54, 117, 123, 0.25);
border-radius: 3px;
background: rgba(237, 247, 244, 0.86);
color: #376e72;
font-size: 8px;
font-weight: 800;
line-height: 1.4;
letter-spacing: 0.1em;
text-align: center;
text-transform: uppercase;
pointer-events: none;
backdrop-filter: blur(10px);
will-change: transform, opacity;
transition: opacity 180ms ease;
}
.drop-label strong {
display: block;
margin-top: 2px;
font-size: 13px;
letter-spacing: 0.02em;
text-transform: none;
}
footer {
position: absolute;
z-index: 6;
right: 30px;
bottom: 25px;
display: flex;
align-items: flex-end;
gap: 20px;
pointer-events: none;
}
.note {
max-width: 365px;
color: #717870;
font-size: 9px;
line-height: 1.55;
text-align: right;
}
.controls {
display: flex;
gap: 7px;
pointer-events: auto;
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid rgba(38, 51, 43, 0.2);
border-radius: 3px;
background: rgba(248, 246, 240, 0.86);
color: var(--ink);
font: inherit;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(10px);
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(255, 253, 248, 0.98);
transform: translateY(-1px);
}
button[aria-pressed="true"] {
border-color: rgba(38, 51, 43, 0.42);
background: #34463b;
color: #f6f2e9;
}
.hint {
position: absolute;
z-index: 5;
left: 34px;
bottom: 28px;
color: rgba(38, 51, 43, 0.55);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
header {
top: 20px;
left: 20px;
max-width: 320px;
}
h1 {
font-size: 40px;
}
.intro {
max-width: 290px;
margin-top: 12px;
font-size: 11px;
}
.metadata {
margin-top: 12px;
}
.metadata span {
padding: 5px 8px;
font-size: 7px;
}
.state-label {
width: 164px;
padding: 10px 11px;
}
.state-label h2 {
font-size: 20px;
}
.state-label p {
font-size: 8px;
}
.drop-label {
width: 102px;
font-size: 7px;
}
.drop-label strong {
font-size: 11px;
}
footer {
right: 18px;
bottom: 18px;
left: 18px;
justify-content: space-between;
align-items: flex-end;
}
.note {
display: none;
}
.hint {
left: 20px;
bottom: 70px;
max-width: 150px;
line-height: 1.5;
}
}
</style>
</head>
<body>
<main id="app">
<header>
<p class="eyebrow">Botanical micro-scene / 01</p>
<h1>Moss, held<br />in two states.</h1>
<p class="intro">
Selected hydration states of a simplified moss. Matched cushions reveal
how compact, curled leaves become broad and open when water is available.
</p>
<div class="metadata">
<span>Matched cushions</span>
<span>56 leaves each</span>
<span>Equal base</span>
</div>
</header>
<div id="dryLabel" class="state-label" aria-label="Curled selected hydration state">
<div class="label-kicker"><span class="dot"></span>Selected hydration state · 01</div>
<h2>Curled</h2>
<p>Low-water selection<br />Compact, folded leaf posture</p>
</div>
<div id="wetLabel" class="state-label wet" aria-label="Expanded selected hydration state">
<div class="label-kicker"><span class="dot"></span>Selected hydration state · 02</div>
<h2>Expanded</h2>
<p>High-water selection<br />Open, broadened leaf posture</p>
</div>
<div id="dropLabel" class="drop-label" aria-label="Droplet scale reference">
Droplet scale cue
<strong>≈ 2 mm</strong>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<footer>
<div class="note">
Representative morphology only · simplified procedural model ·
Droplet shown as an illustrative scale cue, not a measurement.
</div>
<div class="controls">
<button id="orbitButton" type="button" aria-pressed="false">Auto orbit</button>
<button id="resetButton" type="button">Reset view</button>
</div>
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
// -------------------------------------------------------------------------
// Renderer, camera and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive Three.js comparison of curled and expanded moss cushions."
);
app.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeeeae1);
scene.fog = new THREE.Fog(0xeeeae1, 11, 24);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x6a766c, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.4);
keyLight.position.set(-4.5, 7.5, 4.2);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.00025;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.PointLight(0xcbe1db, 1.15, 14, 2);
fillLight.position.set(4.5, 3.5, -3.5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xd7e6d9, 1.1);
rimLight.position.set(3, 4, -5);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Shared display platform and floor
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xe9e5dc,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.315;
floor.receiveShadow = true;
scene.add(floor);
function roundedRectangleShape(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
shape.closePath();
return shape;
}
const platformGeometry = new THREE.ExtrudeGeometry(
roundedRectangleShape(8.65, 3.85, 0.32),
{
depth: 0.22,
bevelEnabled: true,
bevelSegments: 5,
bevelSize: 0.045,
bevelThickness: 0.035,
curveSegments: 16,
steps: 1
}
);
platformGeometry.computeVertexNormals();
const platform = new THREE.Mesh(
platformGeometry,
new THREE.MeshStandardMaterial({
color: 0xd8d2c5,
roughness: 0.92,
metalness: 0
})
);
platform.rotation.x = -Math.PI / 2;
platform.position.y = -0.265;
platform.castShadow = true;
platform.receiveShadow = true;
scene.add(platform);
// Subtle scientific scale marks along the front of the plinth.
const tickPoints = [];
const tickStart = -3.9;
const tickEnd = 3.9;
for (let x = tickStart; x <= tickEnd + 0.001; x += 0.2) {
const major = Math.abs(Math.round(x * 5) - x * 5) < 0.001;
tickPoints.push(
new THREE.Vector3(x, 0.012, 1.79),
new THREE.Vector3(x, 0.012, major ? 1.91 : 1.86)
);
}
const tickGeometry = new THREE.BufferGeometry().setFromPoints(tickPoints);
const ticks = new THREE.LineSegments(
tickGeometry,
new THREE.LineBasicMaterial({
color: 0x777b72,
transparent: true,
opacity: 0.19
})
);
scene.add(ticks);
const dividerGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0.013, -1.65),
new THREE.Vector3(0, 0.013, 1.63)
]);
const divider = new THREE.Line(
dividerGeometry,
new THREE.LineDashedMaterial({
color: 0x6f786f,
transparent: true,
opacity: 0.24,
dashSize: 0.065,
gapSize: 0.065
})
);
divider.computeLineDistances();
scene.add(divider);
// -------------------------------------------------------------------------
// Deterministic layout shared by both cushions
// -------------------------------------------------------------------------
function mulberry32(seed) {
return function random() {
let t = (seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(41027);
const LEAF_COUNT = 56;
const sharedLayout = [];
for (let i = 0; i < LEAF_COUNT; i++) {
const angle =
i * 2.399963229728653 + (random() - 0.5) * 0.27;
const radius = Math.sqrt(random()) * 0.82;
sharedLayout.push({
angle,
radius,
length: 0.69 + random() * 0.48,
wetWidth: 0.15 + random() * 0.075,
dryWidth: 0.052 + random() * 0.026,
roll: (random() - 0.5) * 0.52,
turns: 1.52 + random() * 0.56,
tint: random()
});
}
// -------------------------------------------------------------------------
// Procedural leaf geometry
// -------------------------------------------------------------------------
const tempCenter = new THREE.Vector3();
const tempTangent = new THREE.Vector3();
const tempNormal = new THREE.Vector3();
const tempWidth = new THREE.Vector3();
const tempVector = new THREE.Vector3();
function leafCenter(layout, mode, t, target) {
const L = layout.length;
if (mode === "expanded") {
target.set(
0,
L * (0.115 * Math.sin(Math.PI * t) + 0.025 * t),
L * t
);
} else {
const initialRadius = L * 0.27;
const radius = initialRadius * (1 - 0.84 * t);
const angle = -Math.PI / 2 + t * layout.turns * Math.PI;
target.set(
0,
initialRadius + radius * Math.sin(angle),
radius * Math.cos(angle)
);
}
return target;
}
function buildLeafField(layout, mode, palette) {
const positions = [];
const colors = [];
const indices = [];
const segments = mode === "expanded" ? 17 : 20;
const across = 4;
const yAxis = new THREE.Vector3(0, 1, 0);
const zAxis = new THREE.Vector3(0, 0, 1);
const qRadial = new THREE.Quaternion();
const qRoll = new THREE.Quaternion();
const qFinal = new THREE.Quaternion();
layout.forEach((leaf, leafIndex) => {
const radialAngle = Math.PI / 2 - leaf.angle;
qRadial.setFromAxisAngle(yAxis, radialAngle);
qRoll.setFromAxisAngle(zAxis, leaf.roll);
qFinal.copy(qRadial).multiply(qRoll);
const root = new THREE.Vector3(
Math.cos(leaf.angle) * leaf.radius,
0.395 + (1 - leaf.radius / 0.9) * 0.085,
Math.sin(leaf.angle) * leaf.radius
);
const baseColor = new THREE.Color(
palette[Math.floor(leaf.tint * palette.length) % palette.length]
);
const vertexOffset = positions.length / 3;
for (let row = 0; row <= segments; row++) {
const t = row / segments;
leafCenter(leaf, mode, t, tempCenter);
leafCenter(leaf, mode, Math.max(0, t - 0.004), tempVector);
const after = tempVector.clone();
leafCenter(leaf, mode, Math.min(1, t + 0.004), tempVector);
tempTangent.copy(tempVector).sub(after).normalize();
// Width follows the local tangent, giving each blade a readable face.
tempWidth.set(1, 0, 0);
tempNormal.copy(tempTangent).cross(tempWidth).normalize();
const twist = Math.sin(t * Math.PI) * 0.12;
tempWidth.applyAxisAngle(tempTangent, twist).normalize();
const profile = Math.pow(Math.sin(Math.PI * Math.min(t, 0.999)), 0.66);
const fullWidth =
(mode === "expanded" ? leaf.wetWidth : leaf.dryWidth) * profile;
for (let col = 0; col <= across; col++) {
const s = (col / across) * 2 - 1;
const halfWidth = fullWidth * 0.5;
const cup =
mode === "expanded"
? leaf.length * 0.038 * Math.sin(Math.PI * t)
: leaf.length * 0.018 * Math.sin(Math.PI * t);
const ridge =
mode === "expanded"
? Math.sin(t * 10 + leafIndex) * 0.007 * leaf.length
: 0;
const position = tempCenter
.clone()
.addScaledVector(tempWidth, s * halfWidth)
.addScaledVector(
tempNormal,
cup * (1 - s * s) + ridge * (1 - s * s)
)
.applyQuaternion(qFinal)
.add(root);
positions.push(position.x, position.y, position.z);
const shade =
0.87 +
(1 - Math.abs(s)) * 0.13 +
(1 - t) * 0.035 +
(leaf.tint - 0.5) * 0.055;
const color = baseColor.clone().multiplyScalar(shade);
colors.push(color.r, color.g, color.b);
}
}
const rowSize = across + 1;
for (let row = 0; row < segments; row++) {
for (let col = 0; col < across; col++) {
const a = vertexOffset + row * rowSize + col;
const b = a + 1;
const c = a + rowSize;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
});
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: mode === "expanded" ? 0.72 : 0.9,
metalness: 0,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// -------------------------------------------------------------------------
// Cushion construction
// -------------------------------------------------------------------------
function createIrregularCushionGeometry() {
const geometry = new THREE.SphereGeometry(1, 48, 28);
const position = geometry.attributes.position;
for (let i = 0; i < position.count; i++) {
const x = position.getX(i);
const y = position.getY(i);
const z = position.getZ(i);
const variation =
1 +
Math.sin(x * 11.4 + y * 7.1) *
Math.cos(z * 9.7 - x * 4.2) *
0.028;
position.setXYZ(i, x * variation, y * variation, z * variation);
}
position.needsUpdate = true;
geometry.computeVertexNormals();
return geometry;
}
const cushionGeometry = createIrregularCushionGeometry();
const bumpGeometry = new THREE.SphereGeometry(1, 12, 8);
function createMossCushion({
x,
mode,
bodyColor,
bumpColor,
palette,
accentColor
}) {
const group = new THREE.Group();
group.position.set(x, 0, 0.05);
const soil = new THREE.Mesh(
new THREE.CylinderGeometry(1.14, 1.18, 0.13, 64),
new THREE.MeshStandardMaterial({
color: 0x554c3f,
roughness: 1
})
);
soil.position.y = 0.065;
soil.castShadow = true;
soil.receiveShadow = true;
group.add(soil);
const soilTop = new THREE.Mesh(
new THREE.CylinderGeometry(1.105, 1.105, 0.025, 64),
new THREE.MeshStandardMaterial({
color: mode === "expanded" ? 0x3e4935 : 0x4b4438,
roughness: 1
})
);
soilTop.position.y = 0.142;
soilTop.receiveShadow = true;
group.add(soilTop);
const body = new THREE.Mesh(
cushionGeometry,
new THREE.MeshStandardMaterial({
color: bodyColor,
roughness: 0.94
})
);
body.position.y = 0.225;
body.scale.set(1.08, 0.255, 0.95);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.185, 0.014, 8, 96),
new THREE.MeshStandardMaterial({
color: accentColor,
roughness: 0.72
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.14;
group.add(ring);
const bumpMaterial = new THREE.MeshStandardMaterial({
color: bumpColor,
roughness: 0.95
});
const bumpCount = mode === "expanded" ? 58 : 64;
const bumps = new THREE.InstancedMesh(
bumpGeometry,
bumpMaterial,
bumpCount
);
const dummy = new THREE.Object3D();
const bumpRandom = mulberry32(mode === "expanded" ? 817 : 319);
for (let i = 0; i < bumpCount; i++) {
const angle = bumpRandom() * Math.PI * 2;
const radial = Math.sqrt(bumpRandom()) * 0.96;
const px = Math.cos(angle) * radial * 1.015;
const pz = Math.sin(angle) * radial * 0.88;
const normalized =
(px / 1.08) ** 2 + (pz / 0.95) ** 2;
const py =
0.225 + 0.252 * Math.sqrt(Math.max(0, 1 - normalized));
const size = 0.045 + bumpRandom() * 0.078;
dummy.position.set(px, py, pz);
dummy.scale.set(size, size * (0.42 + bumpRandom() * 0.26), size);
dummy.rotation.set(
bumpRandom() * 0.25,
bumpRandom() * Math.PI,
bumpRandom() * 0.25
);
dummy.updateMatrix();
bumps.setMatrixAt(i, dummy.matrix);
}
bumps.instanceMatrix.needsUpdate = true;
bumps.castShadow = true;
bumps.receiveShadow = true;
group.add(bumps);
const leaves = buildLeafField(sharedLayout, mode, palette);
group.add(leaves);
scene.add(group);
return group;
}
const dryCushion = createMossCushion({
x: -1.55,
mode: "curled",
bodyColor: 0x747954,
bumpColor: 0x8a895d,
accentColor: 0x9a8956,
palette: [0x74784f, 0x8d8758, 0x646d49, 0x9a9063]
});
const wetCushion = createMossCushion({
x: 1.55,
mode: "expanded",
bodyColor: 0x397346,
bumpColor: 0x4c8b50,
accentColor: 0x4d925c,
palette: [0x4e9652, 0x64a95a, 0x347d48, 0x75ad5d]
});
// -------------------------------------------------------------------------
// Droplet scale reference
// -------------------------------------------------------------------------
const dropletGroup = new THREE.Group();
dropletGroup.position.set(0, 0.035, 1.3);
dropletGroup.scale.setScalar(0.6);
const dropletProfile = [
new THREE.Vector2(0, 0),
new THREE.Vector2(0.085, 0.025),
new THREE.Vector2(0.145, 0.105),
new THREE.Vector2(0.18, 0.225),
new THREE.Vector2(0.155, 0.37),
new THREE.Vector2(0.09, 0.53),
new THREE.Vector2(0.035, 0.61),
new THREE.Vector2(0, 0.65)
];
const droplet = new THREE.Mesh(
new THREE.LatheGeometry(dropletProfile, 48),
new THREE.MeshPhysicalMaterial({
color: 0x8bcbd0,
roughness: 0.08,
metalness: 0,
transmission: 0.68,
thickness: 0.28,
ior: 1.33,
transparent: true,
opacity: 0.84,
clearcoat: 1,
clearcoatRoughness: 0.04,
side: THREE.DoubleSide
})
);
droplet.renderOrder = 3;
dropletGroup.add(droplet);
const dropletHighlight = new THREE.Mesh(
new THREE.SphereGeometry(0.026, 16, 12),
new THREE.MeshBasicMaterial({
color: 0xf8ffff,
transparent: true,
opacity: 0.76
})
);
dropletHighlight.position.set(-0.055, 0.32, 0.075);
dropletGroup.add(dropletHighlight);
scene.add(dropletGroup);
const dropletRing = new THREE.Mesh(
new THREE.RingGeometry(0.205, 0.225, 64),
new THREE.MeshBasicMaterial({
color: 0x4f9298,
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide
})
);
dropletRing.rotation.x = -Math.PI / 2;
dropletRing.position.set(0, 0.018, 1.3);
scene.add(dropletRing);
const measurementPoints = [
new THREE.Vector3(0.27, 0.035, 1.3),
new THREE.Vector3(0.27, 0.43, 1.3),
new THREE.Vector3(0.235, 0.035, 1.3),
new THREE.Vector3(0.305, 0.035, 1.3),
new THREE.Vector3(0.235, 0.43, 1.3),
new THREE.Vector3(0.305, 0.43, 1.3)
];
const measurementGeometry = new THREE.BufferGeometry().setFromPoints(
measurementPoints
);
const measurement = new THREE.LineSegments(
measurementGeometry,
new THREE.LineBasicMaterial({
color: 0x4f9298,
transparent: true,
opacity: 0.62
})
);
scene.add(measurement);
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const dryLabelElement = document.querySelector("#dryLabel");
const wetLabelElement = document.querySelector("#wetLabel");
const dropLabelElement = document.querySelector("#dropLabel");
const labelData = [
{
element: dryLabelElement,
anchor: new THREE.Vector3(-1.55, 0.98, 0.05)
},
{
element: wetLabelElement,
anchor: new THREE.Vector3(1.55, 0.98, 0.05)
},
{
element: dropLabelElement,
anchor: new THREE.Vector3(0.24, 0.58, 1.3)
}
];
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labelData.forEach((item) => {
const projected = item.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
item.element.style.opacity = visible ? "1" : "0";
});
}
// -------------------------------------------------------------------------
// Camera controls implemented directly, without addons
// -------------------------------------------------------------------------
const controls = {
theta: 0.1,
phi: 1.03,
radius: 8.4,
targetTheta: 0.1,
targetPhi: 1.03,
targetRadius: 8.4,
target: new THREE.Vector3(0, 0.28, 0),
dragging: false,
pointerX: 0,
pointerY: 0,
userZoomed: false,
autoOrbit: false
};
function fittedRadius() {
const verticalFov = THREE.MathUtils.degToRad(camera.fov);
const horizontalNeeded = 7.35;
const horizontalDistance =
horizontalNeeded /
(2 * Math.tan(verticalFov / 2) * camera.aspect);
return Math.max(8.4, horizontalDistance);
}
function resetView() {
controls.targetTheta = 0.1;
controls.targetPhi = 1.03;
controls.targetRadius = fittedRadius();
controls.userZoomed = false;
}
resetView();
controls.radius = controls.targetRadius;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.pointerX;
const dy = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.targetTheta -= dx * 0.006;
controls.targetPhi = THREE.MathUtils.clamp(
controls.targetPhi - dy * 0.005,
0.38,
1.38
);
});
function endPointer(event) {
controls.dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.targetRadius = THREE.MathUtils.clamp(
controls.targetRadius * Math.exp(event.deltaY * 0.001),
5.4,
22
);
controls.userZoomed = true;
},
{ passive: false }
);
const orbitButton = document.querySelector("#orbitButton");
const resetButton = document.querySelector("#resetButton");
orbitButton.addEventListener("click", () => {
controls.autoOrbit = !controls.autoOrbit;
orbitButton.setAttribute("aria-pressed", String(controls.autoOrbit));
orbitButton.textContent = controls.autoOrbit ? "Pause orbit" : "Auto orbit";
});
resetButton.addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
if (!controls.userZoomed) {
controls.targetRadius = fittedRadius();
}
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (controls.autoOrbit && !controls.dragging) {
controls.targetTheta += delta * 0.075;
}
const damping = 1 - Math.pow(0.0008, delta);
controls.theta +=
(controls.targetTheta - controls.theta) * damping;
controls.phi +=
(controls.targetPhi - controls.phi) * damping;
controls.radius +=
(controls.targetRadius - controls.radius) * damping;
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.radius * sinPhi * Math.sin(controls.theta),
controls.radius * Math.cos(controls.phi) + controls.target.y,
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
// A restrained hover makes the scale cue feel like a suspended specimen.
dropletGroup.position.y = 0.035 + Math.sin(elapsed * 1.7) * 0.022;
droplet.rotation.y = Math.sin(elapsed * 0.55) * 0.12;
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```