Prompt specification
Build or customize the Three.js 3D scene "Gas bubbles in a jar" 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: Gas bubbles in a jar
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/gas-bubbles-in-a-jar-x0106
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring vegetable fermentation. Animate symbolic bubbles rising between vegetable pieces toward the headspace; a caption identifies microbial gas production while a separate counter tracks elapsed illustrative time. Low-poly.
Working Three.js Starter (gas-bubbles-in-a-jar-x0106):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Vegetable Fermentation — Three.js</title>
<style>
:root {
--ink: #24372c;
--muted: #667267;
--paper: #f3f0e5;
--green: #547757;
--lime: #d9e4ad;
--orange: #d8753f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5d8;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.interface {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.title-block {
position: absolute;
top: 28px;
left: 30px;
max-width: 430px;
}
.eyebrow {
margin: 0 0 10px;
color: var(--green);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4vw, 54px);
font-weight: 500;
letter-spacing: -0.045em;
line-height: 0.96;
}
.subtitle {
max-width: 340px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.panel {
border: 1px solid rgba(65, 79, 65, 0.16);
border-radius: 16px;
background: rgba(248, 246, 237, 0.82);
box-shadow:
0 18px 50px rgba(42, 53, 43, 0.08),
inset 0 1px 0 rgba(255, 255, 255, 0.75);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.timer {
position: absolute;
top: 28px;
right: 30px;
width: 218px;
padding: 15px 17px 14px;
}
.timer-label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.timer-value {
display: block;
font-variant-numeric: tabular-nums;
font-size: 30px;
font-weight: 620;
letter-spacing: -0.04em;
line-height: 1;
}
.time-track {
height: 3px;
margin-top: 12px;
overflow: hidden;
border-radius: 99px;
background: rgba(65, 79, 65, 0.12);
}
#time-progress {
width: 0%;
height: 100%;
border-radius: inherit;
background: var(--orange);
}
.caption {
position: absolute;
bottom: 28px;
left: 30px;
display: grid;
grid-template-columns: 38px 1fr;
gap: 13px;
width: min(430px, calc(100vw - 60px));
padding: 17px 19px 18px;
}
.bubble-symbol {
position: relative;
width: 35px;
height: 35px;
margin-top: 1px;
}
.bubble-symbol span {
position: absolute;
display: block;
border: 2px solid var(--green);
border-radius: 50%;
background: rgba(255, 255, 255, 0.35);
}
.bubble-symbol span:nth-child(1) {
width: 19px;
height: 19px;
left: 1px;
bottom: 1px;
}
.bubble-symbol span:nth-child(2) {
width: 11px;
height: 11px;
right: 0;
top: 1px;
}
.bubble-symbol span:nth-child(3) {
width: 7px;
height: 7px;
right: 7px;
bottom: 7px;
}
.caption h2 {
margin: 0 0 5px;
font-size: 14px;
font-weight: 760;
letter-spacing: -0.01em;
}
.caption p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.caption strong {
color: var(--ink);
font-weight: 700;
}
.controls-hint {
position: absolute;
right: 30px;
bottom: 30px;
padding: 9px 12px;
border: 1px solid rgba(65, 79, 65, 0.13);
border-radius: 999px;
color: var(--muted);
background: rgba(248, 246, 237, 0.68);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
}
.headspace-label {
position: absolute;
display: flex;
align-items: center;
gap: 8px;
transform: translate(14px, -50%);
color: #566653;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
white-space: nowrap;
}
.headspace-label::before {
content: "";
width: 28px;
height: 1px;
background: currentColor;
opacity: 0.55;
}
@media (max-width: 700px) {
.title-block {
top: 18px;
left: 18px;
max-width: 55%;
}
.eyebrow {
margin-bottom: 7px;
font-size: 8px;
}
.subtitle {
display: none;
}
.timer {
top: 18px;
right: 18px;
width: 154px;
padding: 12px 13px;
}
.timer-value {
font-size: 24px;
}
.caption {
bottom: 18px;
left: 18px;
width: calc(100vw - 36px);
padding: 14px 15px;
}
.controls-hint {
display: none;
}
.headspace-label {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Animated low-poly vegetable fermentation scene"></main>
<div class="interface">
<header class="title-block">
<p class="eyebrow">Living preservation · Study 04</p>
<h1>Vegetable<br />fermentation</h1>
<p class="subtitle">
A cutaway view of salt-preserved produce and the invisible life transforming it.
</p>
</header>
<section class="timer panel" aria-label="Elapsed illustrative time">
<span class="timer-label">Illustrative elapsed time</span>
<output id="timer-value" class="timer-value">00:00.0</output>
<div class="time-track" aria-hidden="true">
<div id="time-progress"></div>
</div>
</section>
<section class="caption panel" aria-label="Microbial gas production caption">
<div class="bubble-symbol" aria-hidden="true">
<span></span><span></span><span></span>
</div>
<div>
<h2>Microbial gas production</h2>
<p>
Fermenting microbes release <strong>carbon dioxide (CO₂)</strong>.
Symbolic bubbles rise between the vegetables and collect in the jar’s
<strong>headspace</strong>.
</p>
</div>
</section>
<div id="headspace-label" class="headspace-label">Headspace · CO₂</div>
<div class="controls-hint">Drag to orbit · Scroll to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.querySelector("#scene");
const timerValue = document.querySelector("#timer-value");
const timeProgress = document.querySelector("#time-progress");
const headspaceLabel = document.querySelector("#headspace-label");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5d8);
scene.fog = new THREE.Fog(0xe9e5d8, 13, 23);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Low-poly fermentation jar with rising carbon dioxide bubbles"
);
container.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
50
);
// Camera controls are implemented directly, without Three.js addons.
const cameraTarget = new THREE.Vector3(0, 2.15, 0);
const spherical = new THREE.Spherical(9.6, 1.18, 0.72);
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
spherical.theta -= deltaX * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - deltaY * 0.005,
0.68,
1.43
);
});
function stopDragging(event) {
dragging = false;
if (
event?.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius + event.deltaY * 0.007,
6.7,
13
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
spherical.radius = 9.6;
spherical.phi = 1.18;
spherical.theta = 0.72;
});
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0xfff8e8, 0x63735d, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.6);
keyLight.position.set(5, 9, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xcbe3ca, 1.8);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xf6b878, 1.1, 12);
warmLight.position.set(4, 3, 3);
scene.add(warmLight);
// Ground and display plinth
const ground = new THREE.Mesh(
new THREE.CircleGeometry(12, 64),
new THREE.MeshStandardMaterial({
color: 0xe3dece,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.02;
ground.receiveShadow = true;
scene.add(ground);
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(3.15, 3.3, 0.2, 32),
new THREE.MeshStandardMaterial({
color: 0xd5cfbd,
roughness: 0.9,
flatShading: true
})
);
plinth.position.y = 0.08;
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
const jar = new THREE.Group();
scene.add(jar);
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0xb8d4c7,
transparent: true,
opacity: 0.16,
shininess: 85,
side: THREE.DoubleSide,
depthWrite: false
});
const glass = new THREE.Mesh(
new THREE.CylinderGeometry(2.25, 2.25, 4.65, 24, 1, true),
glassMaterial
);
glass.position.y = 2.58;
glass.renderOrder = 3;
jar.add(glass);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xa9c7b9,
roughness: 0.34,
metalness: 0.04,
transparent: true,
opacity: 0.65
});
const topRim = new THREE.Mesh(
new THREE.TorusGeometry(2.25, 0.075, 6, 24),
rimMaterial
);
topRim.rotation.x = Math.PI / 2;
topRim.position.y = 4.9;
topRim.castShadow = true;
jar.add(topRim);
const bottomRim = new THREE.Mesh(
new THREE.TorusGeometry(2.25, 0.09, 6, 24),
rimMaterial
);
bottomRim.rotation.x = Math.PI / 2;
bottomRim.position.y = 0.29;
bottomRim.castShadow = true;
jar.add(bottomRim);
const jarBottom = new THREE.Mesh(
new THREE.CylinderGeometry(2.23, 2.23, 0.13, 24),
new THREE.MeshStandardMaterial({
color: 0x8aa998,
roughness: 0.32,
transparent: true,
opacity: 0.4,
depthWrite: false
})
);
jarBottom.position.y = 0.32;
jarBottom.renderOrder = 2;
jar.add(jarBottom);
// Brine
const brine = new THREE.Mesh(
new THREE.CylinderGeometry(2.19, 2.19, 2.17, 24),
new THREE.MeshPhongMaterial({
color: 0x8fa763,
transparent: true,
opacity: 0.43,
shininess: 45,
depthWrite: false
})
);
brine.position.y = 1.44;
brine.renderOrder = 1;
jar.add(brine);
const brineSurface = new THREE.Mesh(
new THREE.CircleGeometry(2.19, 24),
new THREE.MeshStandardMaterial({
color: 0xa7b978,
roughness: 0.45,
transparent: true,
opacity: 0.82,
side: THREE.DoubleSide,
depthWrite: false
})
);
brineSurface.rotation.x = -Math.PI / 2;
brineSurface.position.y = 2.55;
brineSurface.renderOrder = 2;
jar.add(brineSurface);
// Low-poly vegetable materials
const materials = {
cabbage: new THREE.MeshStandardMaterial({
color: 0xa9c674,
roughness: 0.93,
flatShading: true
}),
cabbageLight: new THREE.MeshStandardMaterial({
color: 0xd5dc95,
roughness: 0.95,
flatShading: true
}),
carrot: new THREE.MeshStandardMaterial({
color: 0xd9793e,
roughness: 0.9,
flatShading: true
}),
carrotCore: new THREE.MeshStandardMaterial({
color: 0xeea25c,
roughness: 0.95,
flatShading: true
}),
cucumber: new THREE.MeshStandardMaterial({
color: 0x52794c,
roughness: 0.92,
flatShading: true
}),
cucumberLight: new THREE.MeshStandardMaterial({
color: 0x88a35b,
roughness: 0.92,
flatShading: true
}),
onion: new THREE.MeshStandardMaterial({
color: 0xd5c5a0,
roughness: 0.96,
flatShading: true
}),
pepper: new THREE.MeshStandardMaterial({
color: 0xba4937,
roughness: 0.88,
flatShading: true
}),
stem: new THREE.MeshStandardMaterial({
color: 0x547647,
roughness: 0.95,
flatShading: true
})
};
const vegetableGroup = new THREE.Group();
jar.add(vegetableGroup);
const floatingPieces = [];
function shadowize(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
}
function makeCabbage(scale = 1) {
const group = new THREE.Group();
const core = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.48 * scale, 0),
materials.cabbageLight
);
core.scale.set(1, 0.83, 0.95);
group.add(core);
for (let i = 0; i < 3; i++) {
const leaf = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.43 * scale, 0),
materials.cabbage
);
const angle = (i / 3) * Math.PI * 2;
leaf.position.set(
Math.cos(angle) * 0.27 * scale,
-0.05 * scale,
Math.sin(angle) * 0.27 * scale
);
leaf.scale.set(0.9, 0.62, 1.1);
leaf.rotation.set(i * 0.5, angle, i * 0.35);
group.add(leaf);
}
return group;
}
function makeCarrot(length = 1.05, radius = 0.38) {
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.ConeGeometry(radius, length, 6),
materials.carrot
);
group.add(body);
const cut = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 0.08, 10),
materials.carrotCore
);
cut.position.y = -length * 0.5;
group.add(cut);
return group;
}
function makeCucumber(length = 1, radius = 0.34) {
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius * 0.96, length, 8),
materials.cucumber
);
group.add(body);
const cut = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 0.88, radius * 0.88, 0.05, 8),
materials.cucumberLight
);
cut.position.y = length * 0.5;
group.add(cut);
return group;
}
function makePepper(scale = 1) {
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.5 * scale, 1),
materials.pepper
);
body.scale.set(1, 0.82, 0.9);
group.add(body);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.075, 0.28, 6),
materials.stem
);
stem.position.y = 0.47 * scale;
stem.rotation.z = -0.25;
group.add(stem);
return group;
}
function makeOnion(scale = 1) {
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.46 * scale, 1),
materials.onion
);
body.scale.set(0.95, 1.05, 0.95);
group.add(body);
const root = new THREE.Mesh(
new THREE.ConeGeometry(0.09, 0.18, 5),
materials.cabbageLight
);
root.position.y = -0.48 * scale;
root.rotation.z = Math.PI;
group.add(root);
return group;
}
function addVegetable(object, position, rotation, scale = 1) {
object.position.set(...position);
object.rotation.set(...rotation);
object.scale.setScalar(scale);
shadowize(object);
vegetableGroup.add(object);
floatingPieces.push({
object,
baseY: position[1],
phase: Math.random() * Math.PI * 2
});
return object;
}
addVegetable(
makeCabbage(1.05),
[-1.13, 1.28, -0.28],
[0.2, 0.5, -0.15]
);
addVegetable(
makeCabbage(0.82),
[0.28, 1.42, 0.72],
[-0.1, 1.1, 0.35]
);
addVegetable(
makeCarrot(1.2, 0.4),
[-0.48, 0.96, 0.12],
[0.12, 0.35, -1.32]
);
addVegetable(
makeCarrot(1.02, 0.34),
[0.95, 1.02, -0.57],
[0.35, -0.4, -1.48],
0.92
);
addVegetable(
makeCucumber(1.1, 0.35),
[-0.03, 0.81, -0.87],
[0.15, 0.2, 1.43]
);
addVegetable(
makeCucumber(0.82, 0.3),
[1.22, 1.64, -0.02],
[0.4, 0.7, 1.58],
0.9
);
addVegetable(
makeOnion(0.92),
[0.57, 0.73, 0.76],
[0.2, 0.3, 0.1]
);
addVegetable(
makePepper(0.9),
[-1.22, 0.82, 0.66],
[0.2, 0.5, -0.15],
0.9
);
// A few angular salt crystals near the vegetables.
const saltMaterial = new THREE.MeshStandardMaterial({
color: 0xf4efd8,
roughness: 0.85,
flatShading: true
});
for (let i = 0; i < 16; i++) {
const angle = (i / 16) * Math.PI * 2 + i * 0.21;
const radius = 0.65 + (i % 4) * 0.28;
const crystal = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.065 + (i % 3) * 0.012),
saltMaterial
);
crystal.position.set(
Math.cos(angle) * radius,
0.48 + (i % 5) * 0.055,
Math.sin(angle) * radius
);
crystal.rotation.set(i * 0.7, i * 0.3, i * 0.5);
crystal.castShadow = true;
vegetableGroup.add(crystal);
}
// Symbolic CO₂ bubbles. They originate in gaps between the vegetables.
const bubbleSources = [
[-0.47, 0.79, 0.02],
[0.55, 0.92, 0.22],
[-0.02, 1.15, -0.48],
[-1.02, 1.34, 0.12],
[0.98, 1.4, -0.15],
[0.05, 0.68, 0.83],
[-0.55, 1.12, -0.7]
];
const bubbleGeometry = new THREE.IcosahedronGeometry(1, 1);
const bubbles = [];
function seededRandom(index) {
const value = Math.sin(index * 91.733 + 17.11) * 43758.5453;
return value - Math.floor(value);
}
for (let i = 0; i < 34; i++) {
const source = bubbleSources[i % bubbleSources.length];
const randomX = (seededRandom(i + 1) - 0.5) * 0.35;
const randomZ = (seededRandom(i + 17) - 0.5) * 0.35;
const material = new THREE.MeshStandardMaterial({
color: i % 4 === 0 ? 0xf1d997 : 0xf7f4dc,
emissive: 0xc9b86a,
emissiveIntensity: 0.17,
roughness: 0.18,
metalness: 0,
transparent: true,
opacity: 0,
depthWrite: false,
flatShading: true
});
const bubble = new THREE.Mesh(bubbleGeometry, material);
bubble.renderOrder = 4;
bubble.position.set(
source[0] + randomX,
source[1],
source[2] + randomZ
);
jar.add(bubble);
bubbles.push({
mesh: bubble,
source,
offset: i / bubbles.length,
speed: 0.075 + seededRandom(i + 80) * 0.055,
size: 0.09 + seededRandom(i + 120) * 0.095,
driftX: (seededRandom(i + 200) - 0.5) * 0.55,
driftZ: (seededRandom(i + 300) - 0.5) * 0.42,
seed: seededRandom(i + 400) * Math.PI * 2
});
}
// Small translucent bubbles in the headspace suggest accumulating gas.
const headspaceBubbles = [];
for (let i = 0; i < 9; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xf6e9ae,
transparent: true,
opacity: 0.08,
depthWrite: false
});
const mesh = new THREE.Mesh(bubbleGeometry, material);
mesh.scale.setScalar(0.2 + seededRandom(i + 700) * 0.14);
mesh.position.set(
(seededRandom(i + 710) - 0.5) * 2.4,
3.65 + seededRandom(i + 720) * 0.75,
(seededRandom(i + 730) - 0.5) * 1.5
);
mesh.renderOrder = 4;
jar.add(mesh);
headspaceBubbles.push({
mesh,
phase: seededRandom(i + 740) * Math.PI * 2
});
}
const labelAnchor = new THREE.Vector3(2.08, 4.12, 0);
const projectedLabel = new THREE.Vector3();
const clock = new THREE.Clock();
const simulationStart = performance.now();
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function animate() {
const elapsed = clock.getElapsedTime();
const motionTime = reducedMotion ? elapsed * 0.3 : elapsed;
// Gentle movement of the submerged pieces.
floatingPieces.forEach((piece, index) => {
piece.object.position.y =
piece.baseY + Math.sin(motionTime * 0.8 + piece.phase) * 0.025;
piece.object.rotation.y +=
Math.sin(motionTime * 0.35 + index) * 0.00025;
});
// A restrained ripple on the polygonal brine surface.
brineSurface.position.y =
2.55 + Math.sin(motionTime * 1.15) * 0.012;
brineSurface.rotation.z = Math.sin(motionTime * 0.45) * 0.006;
// Rising bubbles travel from vegetable gaps, cross the brine surface,
// and disperse in the headspace.
bubbles.forEach((bubble) => {
const progress =
(motionTime * bubble.speed + bubble.offset) % 1;
const liquidProgress = THREE.MathUtils.clamp(progress / 0.69, 0, 1);
const headspaceProgress = THREE.MathUtils.clamp(
(progress - 0.69) / 0.31,
0,
1
);
const x =
bubble.source[0] +
bubble.driftX * liquidProgress +
Math.sin(progress * 8 + bubble.seed) * 0.12 * (1 - progress);
const z =
bubble.source[2] +
bubble.driftZ * liquidProgress +
Math.cos(progress * 7 + bubble.seed) * 0.11 * (1 - progress);
let y;
if (progress < 0.69) {
y = THREE.MathUtils.lerp(
bubble.source[1],
2.62,
Math.pow(liquidProgress, 0.86)
);
} else {
y =
2.62 +
Math.pow(headspaceProgress, 0.72) * 1.73 +
Math.sin(headspaceProgress * Math.PI) * 0.12;
}
bubble.mesh.position.set(x, y, z);
const appear = THREE.MathUtils.smoothstep(progress, 0, 0.055);
const disappear = 1 -
THREE.MathUtils.smoothstep(progress, 0.83, 1);
const scale =
bubble.size *
appear *
(1 - headspaceProgress * 0.25) *
(0.95 + Math.sin(progress * 19 + bubble.seed) * 0.05);
bubble.mesh.scale.setScalar(scale);
bubble.mesh.material.opacity = 0.82 * appear * disappear;
bubble.mesh.rotation.x += 0.006;
bubble.mesh.rotation.y += 0.008;
});
headspaceBubbles.forEach((bubble, index) => {
bubble.mesh.position.y +=
Math.sin(motionTime * 0.65 + bubble.phase) * 0.0008;
bubble.mesh.position.x +=
Math.sin(motionTime * 0.25 + index) * 0.00035;
bubble.mesh.material.opacity =
0.055 + Math.sin(motionTime * 0.7 + bubble.phase) * 0.025;
});
// Camera is recomputed so direct mouse controls remain smooth.
updateCamera();
// Position the DOM headspace annotation from its 3D anchor.
projectedLabel.copy(labelAnchor).project(camera);
const labelX = (projectedLabel.x * 0.5 + 0.5) * window.innerWidth;
const labelY = (-projectedLabel.y * 0.5 + 0.5) * window.innerHeight;
headspaceLabel.style.left = `${labelX}px`;
headspaceLabel.style.top = `${labelY}px`;
// Separate elapsed illustrative-time counter.
const totalSeconds = (performance.now() - simulationStart) / 1000;
const minutes = Math.floor(totalSeconds / 60);
const seconds = Math.floor(totalSeconds % 60);
const tenths = Math.floor((totalSeconds * 10) % 10);
timerValue.textContent =
`${String(minutes).padStart(2, "0")}:` +
`${String(seconds).padStart(2, "0")}.${tenths}`;
timeProgress.style.width = `${(totalSeconds % 60) * (100 / 60)}%`;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```