Prompt specification
Build or customize the Three.js 3D scene "Biofilm patch on a submerged pebble" 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: Biofilm patch on a submerged pebble
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/biofilm-patch-on-a-submerged-pebble-x0805
Scene Description Prompt:
A low-poly submerged pebble with a translucent biofilm section separating microbes, matrix, and surrounding water. Three.js from cdnjs, one HTML file.
Working Three.js Starter (biofilm-patch-on-a-submerged-pebble-x0805):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Submerged Biofilm Pebble</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #062a33;
}
#app {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 50% 43%,
transparent 35%,
rgba(0, 12, 18, 0.16) 72%,
rgba(0, 8, 12, 0.48) 100%
);
}
.hud,
.legend {
position: fixed;
z-index: 2;
pointer-events: none;
color: #d6fff7;
text-shadow: 0 2px 14px rgba(0, 0, 0, 0.55);
}
.hud {
top: clamp(20px, 4vw, 46px);
left: clamp(20px, 4vw, 48px);
max-width: 390px;
}
.eyebrow {
margin-bottom: 9px;
color: #73dfcc;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 4vw, 44px);
font-weight: 500;
line-height: 1.04;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 320px;
margin: 13px 0 0;
color: rgba(216, 255, 248, 0.68);
font-size: 12px;
line-height: 1.6;
}
.legend {
right: clamp(18px, 3vw, 40px);
bottom: clamp(18px, 3vw, 36px);
display: grid;
gap: 9px;
padding: 15px 17px;
border: 1px solid rgba(154, 255, 233, 0.14);
border-radius: 14px;
background: rgba(3, 25, 31, 0.42);
backdrop-filter: blur(12px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: rgba(221, 255, 248, 0.76);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.membrane {
color: #85ffe2;
background: currentColor;
}
.matrix {
color: #43bda8;
background: currentColor;
}
.microbe {
color: #ffb47b;
background: currentColor;
}
.controls {
margin-top: 3px;
color: rgba(208, 255, 246, 0.42);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 650px) {
.subtitle {
max-width: 250px;
}
.legend {
padding: 12px 14px;
}
}
</style>
</head>
<body>
<main id="app"></main>
<div class="vignette"></div>
<section class="hud">
<div class="eyebrow">Submerged microbiology / specimen 01</div>
<h1>Life inside a stone-bound biofilm</h1>
<p class="subtitle">
A translucent membrane encloses an extracellular matrix and a microscopic
microbial community on a low-poly submerged pebble.
</p>
</section>
<aside class="legend">
<div class="legend-row">
<span class="swatch membrane"></span>
Biofilm membrane
</div>
<div class="legend-row">
<span class="swatch matrix"></span>
Extracellular matrix
</div>
<div class="legend-row">
<span class="swatch microbe"></span>
Microbial cells
</div>
<div class="controls">Drag to orbit · Scroll or pinch to zoom</div>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const app = document.querySelector("#app");
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.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional submerged pebble with a translucent biofilm"
);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x062a33, 0.047);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const target = new THREE.Vector3(0, -0.18, 0);
const desired = {
radius: window.innerWidth < 700 ? 6.9 : 5.8,
phi: 1.12,
theta: 0.57
};
const current = {
radius: desired.radius,
phi: desired.phi,
theta: desired.theta
};
// ------------------------------------------------------------
// Deterministic random helper
// ------------------------------------------------------------
let seed = 0x8f31a72b;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function randomRange(min, max) {
return min + (max - min) * random();
}
// ------------------------------------------------------------
// Underwater background gradient
// ------------------------------------------------------------
const backgroundMaterial = new THREE.ShaderMaterial({
side: THREE.BackSide,
depthWrite: false,
uniforms: {
time: { value: 0 }
},
vertexShader: `
varying vec3 vLocalPosition;
void main() {
vLocalPosition = position;
gl_Position =
projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float time;
varying vec3 vLocalPosition;
void main() {
vec3 direction = normalize(vLocalPosition);
float height = direction.y * 0.5 + 0.5;
vec3 bottomColor = vec3(0.008, 0.055, 0.075);
vec3 middleColor = vec3(0.018, 0.145, 0.175);
vec3 topColor = vec3(0.045, 0.255, 0.275);
vec3 color = mix(bottomColor, middleColor, smoothstep(0.0, 0.55, height));
color = mix(color, topColor, smoothstep(0.48, 1.0, height));
float glow =
pow(max(0.0, 1.0 - length(direction.xz)), 5.0) *
smoothstep(0.05, 0.85, height);
float shimmer =
sin(direction.x * 19.0 + time * 0.12) *
sin(direction.z * 17.0 - time * 0.09);
color += vec3(0.02, 0.065, 0.06) * glow;
color += shimmer * 0.003;
gl_FragColor = vec4(color, 1.0);
}
`
});
const background = new THREE.Mesh(
new THREE.SphereGeometry(30, 32, 18),
backgroundMaterial
);
background.frustumCulled = false;
scene.add(background);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0x9ce5df,
0x071b20,
1.75
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xc7fff2, 2.65);
keyLight.position.set(-4.5, 7.0, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x53f5d2, 24, 9, 2);
rimLight.position.set(-3.0, 1.6, 3.2);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffbd84, 11, 8, 2);
warmLight.position.set(3.5, -0.2, 2.4);
scene.add(warmLight);
const lowerLight = new THREE.DirectionalLight(0x2e8c8c, 0.7);
lowerLight.position.set(2, -3, -2);
scene.add(lowerLight);
// ------------------------------------------------------------
// Seabed
// ------------------------------------------------------------
const floorGeometry = new THREE.PlaneGeometry(32, 32, 24, 24);
floorGeometry.rotateX(-Math.PI / 2);
const floorPositions = floorGeometry.attributes.position;
const floorColors = [];
for (let i = 0; i < floorPositions.count; i++) {
const x = floorPositions.getX(i);
const z = floorPositions.getZ(i);
const y =
-1.58 +
Math.sin(x * 0.62) * Math.cos(z * 0.47) * 0.07 +
Math.sin((x + z) * 1.1) * 0.025;
floorPositions.setY(i, y);
const floorColor = new THREE.Color();
const variation = (Math.sin(x * 1.7 + z * 0.9) + 1) * 0.5;
floorColor.setHSL(
0.47 + variation * 0.025,
0.18 + variation * 0.08,
0.19 + variation * 0.055
);
floorColors.push(
floorColor.r,
floorColor.g,
floorColor.b
);
}
floorGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(floorColors, 3)
);
floorGeometry.computeVertexNormals();
const floor = new THREE.Mesh(
floorGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0
})
);
floor.receiveShadow = true;
scene.add(floor);
// Animated caustic light projected over the seabed.
const causticMaterial = new THREE.ShaderMaterial({
transparent: true,
depthWrite: false,
blending: THREE.AdditiveBlending,
uniforms: {
time: { value: 0 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position =
projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float time;
varying vec2 vUv;
void main() {
vec2 p = (vUv - 0.5) * 34.0;
float a = sin(p.x + sin(p.y * 0.73 + time * 0.42));
float b = sin(p.y + sin(p.x * 0.81 - time * 0.31));
float c = 1.0 - abs((a + b) * 0.5);
float caustic = pow(max(0.0, c), 7.0);
float secondary = pow(
max(0.0, 1.0 - abs(sin(p.y * 0.62 - time * 0.25))),
18.0
);
float alpha = caustic * 0.16 + secondary * 0.045;
gl_FragColor = vec4(0.18, 0.95, 0.77, alpha);
}
`
});
const caustics = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
causticMaterial
);
caustics.rotation.x = -Math.PI / 2;
caustics.position.y = -1.405;
scene.add(caustics);
// Small low-poly stones scattered across the seabed.
const stoneGeometry = new THREE.IcosahedronGeometry(1, 1);
for (let i = 0; i < 15; i++) {
const stoneColor = new THREE.Color();
stoneColor.setHSL(
randomRange(0.42, 0.49),
randomRange(0.10, 0.20),
randomRange(0.18, 0.29)
);
const stone = new THREE.Mesh(
stoneGeometry,
new THREE.MeshStandardMaterial({
color: stoneColor,
flatShading: true,
roughness: 0.96
})
);
const side = random() > 0.5 ? 1 : -1;
stone.position.set(
side * randomRange(2.0, 7.5),
randomRange(-1.62, -1.48),
randomRange(-5.0, 4.0)
);
stone.scale.set(
randomRange(0.12, 0.42),
randomRange(0.08, 0.23),
randomRange(0.14, 0.5)
);
stone.rotation.set(
randomRange(-0.4, 0.4),
randomRange(0, Math.PI),
randomRange(-0.4, 0.4)
);
stone.castShadow = true;
stone.receiveShadow = true;
scene.add(stone);
}
// ------------------------------------------------------------
// Main low-poly pebble
// ------------------------------------------------------------
let pebbleGeometry = new THREE.IcosahedronGeometry(1, 2);
if (pebbleGeometry.index) {
pebbleGeometry = pebbleGeometry.toNonIndexed();
}
const pebblePositions = pebbleGeometry.attributes.position;
for (let i = 0; i < pebblePositions.count; i++) {
const x = pebblePositions.getX(i);
const y = pebblePositions.getY(i);
const z = pebblePositions.getZ(i);
const noise =
Math.sin(x * 12.9898 + y * 78.233 + z * 37.719) * 43758.5453;
const randomNoise = noise - Math.floor(noise);
const radius =
1 +
(randomNoise - 0.5) * 0.085 +
Math.sin(x * 4.1 + z * 2.7) * 0.026 +
Math.cos(y * 5.3 - x * 1.8) * 0.02;
pebblePositions.setXYZ(
i,
x * radius,
y * radius,
z * radius
);
}
pebbleGeometry.computeVertexNormals();
const pebbleColors = [];
for (let i = 0; i < pebblePositions.count; i += 3) {
const faceColor = new THREE.Color();
const shade = randomRange(-0.035, 0.045);
faceColor.setHSL(
randomRange(0.37, 0.42),
randomRange(0.10, 0.17),
0.31 + shade
);
for (let vertex = 0; vertex < 3; vertex++) {
pebbleColors.push(
faceColor.r,
faceColor.g,
faceColor.b
);
}
}
pebbleGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(pebbleColors, 3)
);
const pebbleRoot = new THREE.Group();
pebbleRoot.position.y = -0.3;
pebbleRoot.rotation.y = -0.12;
pebbleRoot.rotation.z = -0.055;
scene.add(pebbleRoot);
const pebble = new THREE.Mesh(
pebbleGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.94,
metalness: 0.015
})
);
pebble.scale.set(1.66, 1.02, 1.18);
pebble.castShadow = true;
pebble.receiveShadow = true;
pebbleRoot.add(pebble);
const pebbleEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(pebbleGeometry, 24),
new THREE.LineBasicMaterial({
color: 0xb1c9b8,
transparent: true,
opacity: 0.075,
depthWrite: false
})
);
pebbleEdges.scale.copy(pebble.scale);
pebbleRoot.add(pebbleEdges);
// ------------------------------------------------------------
// Translucent biofilm section
//
// Layer order:
// 1. Dark cutaway aperture
// 2. Microbial cells
// 3. Translucent EPS matrix
// 4. Outer biofilm membrane
// ------------------------------------------------------------
const biofilm = new THREE.Group();
biofilm.position.set(-0.22, 0.13, 1.065);
biofilm.rotation.set(-0.025, -0.11, 0.045);
pebbleRoot.add(biofilm);
const aperture = new THREE.Mesh(
new THREE.CircleGeometry(1, 64),
new THREE.MeshStandardMaterial({
color: 0x103c3c,
emissive: 0x061a1c,
emissiveIntensity: 0.45,
roughness: 0.74,
metalness: 0.02,
side: THREE.DoubleSide
})
);
aperture.scale.set(0.655, 0.425, 1);
aperture.position.z = -0.055;
biofilm.add(aperture);
// Fine strands suspended in the extracellular polymeric matrix.
const fiberPoints = [];
for (let i = 0; i < 32; i++) {
const angle = randomRange(0, Math.PI * 2);
const radius = Math.sqrt(random()) * 0.91;
const x = Math.cos(angle) * radius * 0.57;
const y = Math.sin(angle) * radius * 0.34;
const fiber = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, y, randomRange(-0.015, 0.065)),
new THREE.Vector3(
x + randomRange(-0.12, 0.12),
y + randomRange(-0.08, 0.08),
randomRange(-0.015, 0.07)
)
]),
new THREE.LineBasicMaterial({
color: 0x9cffe4,
transparent: true,
opacity: randomRange(0.10, 0.28),
depthWrite: false
})
);
biofilm.add(fiber);
}
const microbeGeometry = new THREE.IcosahedronGeometry(1, 1);
const microbeColors = [
0xffb66f,
0xff8eaa,
0xe5ed72,
0x76e4da,
0xffd18c
];
const microbes = [];
for (let i = 0; i < 36; i++) {
const angle = randomRange(0, Math.PI * 2);
const radial = Math.sqrt(random()) * 0.93;
const x = Math.cos(angle) * radial * 0.58;
const y = Math.sin(angle) * radial * 0.36;
const z = randomRange(0.005, 0.085);
const color = new THREE.Color(
microbeColors[Math.floor(random() * microbeColors.length)]
);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.25,
roughness: 0.34,
metalness: 0.06,
flatShading: true
});
const microbe = new THREE.Mesh(microbeGeometry, material);
if (i % 4 === 0) {
microbe.scale.set(
randomRange(0.042, 0.062),
randomRange(0.042, 0.062),
randomRange(0.042, 0.062)
);
} else {
microbe.scale.set(
randomRange(0.027, 0.043),
randomRange(0.07, 0.14),
randomRange(0.027, 0.043)
);
}
microbe.position.set(x, y, z);
microbe.rotation.set(
randomRange(-0.8, 0.8),
randomRange(-0.8, 0.8),
randomRange(0, Math.PI)
);
microbe.userData.basePosition = microbe.position.clone();
microbe.userData.baseRotation = microbe.rotation.clone();
microbe.userData.phase = randomRange(0, Math.PI * 2);
microbes.push(microbe);
biofilm.add(microbe);
}
const matrix = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 3),
new THREE.MeshStandardMaterial({
color: 0x43c4ad,
emissive: 0x0b4c45,
emissiveIntensity: 0.32,
transparent: true,
opacity: 0.27,
roughness: 0.42,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
matrix.scale.set(0.61, 0.395, 0.135);
matrix.position.z = 0.025;
matrix.renderOrder = 3;
biofilm.add(matrix);
const membrane = new THREE.Mesh(
new THREE.SphereGeometry(1, 40, 24),
new THREE.MeshPhysicalMaterial({
color: 0x8fffe3,
emissive: 0x0b5149,
emissiveIntensity: 0.22,
transparent: true,
opacity: 0.22,
roughness: 0.18,
metalness: 0,
clearcoat: 0.7,
clearcoatRoughness: 0.25,
side: THREE.DoubleSide,
depthWrite: false
})
);
membrane.scale.set(0.72, 0.49, 0.18);
membrane.renderOrder = 5;
biofilm.add(membrane);
function addEllipseLine(radiusX, radiusY, z, color, opacity) {
const curve = new THREE.EllipseCurve(
0,
0,
radiusX,
radiusY,
0,
Math.PI * 2,
false,
0
);
const points = curve
.getPoints(80)
.map((point) => new THREE.Vector3(point.x, point.y, z));
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.LineLoop(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
})
);
line.renderOrder = 6;
biofilm.add(line);
}
addEllipseLine(0.685, 0.455, 0.172, 0xb6ffe9, 0.78);
addEllipseLine(0.598, 0.375, 0.143, 0x79e9d4, 0.31);
const biofilmGlow = new THREE.PointLight(0x62ffd7, 2.5, 2.8, 2);
biofilmGlow.position.set(-0.2, 0.1, 0.4);
biofilm.add(biofilmGlow);
// ------------------------------------------------------------
// Suspended particles
// ------------------------------------------------------------
const particleCount = 1250;
const particlePositions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
particlePositions[i * 3] = randomRange(-11, 11);
particlePositions[i * 3 + 1] = randomRange(-1.6, 8.5);
particlePositions[i * 3 + 2] = randomRange(-10, 9);
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0xb8fff3,
size: 0.031,
transparent: true,
opacity: 0.47,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(particles);
// ------------------------------------------------------------
// Light shafts and distant water surface
// ------------------------------------------------------------
const shafts = [];
function addLightShaft(x, z, radius, height, opacity) {
const material = new THREE.MeshBasicMaterial({
color: 0x73ead6,
transparent: true,
opacity,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
});
const shaft = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 18, 1, true),
material
);
shaft.position.set(x, 2.6, z);
shaft.rotation.z = randomRange(-0.13, 0.13);
shaft.rotation.x = randomRange(-0.08, 0.08);
shaft.userData.baseOpacity = opacity;
shaft.userData.phase = randomRange(0, Math.PI * 2);
shafts.push(shaft);
scene.add(shaft);
}
addLightShaft(-4.2, -2.6, 1.9, 9.0, 0.026);
addLightShaft(-1.4, -4.5, 1.25, 8.0, 0.02);
addLightShaft(3.7, -3.0, 1.55, 9.5, 0.018);
const waterSurface = new THREE.Mesh(
new THREE.CircleGeometry(18, 64),
new THREE.MeshBasicMaterial({
color: 0x5ac8bd,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
})
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = 4.8;
scene.add(waterSurface);
// ------------------------------------------------------------
// Rising bubbles
// ------------------------------------------------------------
const bubbleGeometry = new THREE.SphereGeometry(1, 8, 6);
const bubbleMaterial = new THREE.MeshBasicMaterial({
color: 0xb8fff6,
wireframe: true,
transparent: true,
opacity: 0.15,
depthWrite: false
});
const bubbleHighlightGeometry = new THREE.SphereGeometry(1, 6, 4);
const bubbleHighlightMaterial = new THREE.MeshBasicMaterial({
color: 0xe5fffb,
transparent: true,
opacity: 0.38,
depthWrite: false
});
const bubbles = [];
for (let i = 0; i < 18; i++) {
const bubble = new THREE.Group();
const shell = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
bubble.add(shell);
const highlight = new THREE.Mesh(
bubbleHighlightGeometry,
bubbleHighlightMaterial
);
highlight.position.set(0.25, 0.28, 0.72);
highlight.scale.setScalar(0.13);
bubble.add(highlight);
const size = randomRange(0.035, 0.13);
bubble.scale.setScalar(size);
bubble.position.set(
randomRange(-4.5, 4.5),
randomRange(-1.2, 4.8),
randomRange(-3.4, 3.6)
);
bubble.userData.baseX = bubble.position.x;
bubble.userData.speed = randomRange(0.12, 0.34);
bubble.userData.offset = randomRange(0, 7);
bubble.userData.phase = randomRange(0, Math.PI * 2);
bubbles.push(bubble);
scene.add(bubble);
}
// ------------------------------------------------------------
// Direct camera controls: orbit, zoom, pinch, keyboard
// ------------------------------------------------------------
const pointers = new Map();
let dragPointer = null;
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
renderer.domElement.setPointerCapture(event.pointerId);
if (pointers.size === 1) {
dragPointer = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = pointerDistance();
dragPointer = null;
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const nextDistance = pointerDistance();
if (pinchDistance > 0 && nextDistance > 0) {
desired.radius *= pinchDistance / nextDistance;
desired.radius = THREE.MathUtils.clamp(
desired.radius,
3.4,
10.5
);
}
pinchDistance = nextDistance;
return;
}
if (event.pointerId !== dragPointer) return;
const deltaX = event.clientX - lastX;
const deltaY = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
desired.theta -= deltaX * 0.0053;
desired.phi = THREE.MathUtils.clamp(
desired.phi + deltaY * 0.0045,
0.38,
1.48
);
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (event.pointerId === dragPointer) {
dragPointer = pointers.size ? pointers.keys().next().value : null;
}
if (pointers.size < 2) {
pinchDistance = 0;
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius *= Math.exp(event.deltaY * 0.001);
desired.radius = THREE.MathUtils.clamp(
desired.radius,
3.4,
10.5
);
},
{ passive: false }
);
function resetCamera() {
desired.radius = window.innerWidth < 700 ? 6.9 : 5.8;
desired.phi = 1.12;
desired.theta = 0.57;
}
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") desired.theta += 0.12;
if (event.key === "ArrowRight") desired.theta -= 0.12;
if (event.key === "ArrowUp") {
desired.phi = Math.max(0.38, desired.phi - 0.1);
}
if (event.key === "ArrowDown") {
desired.phi = Math.min(1.48, desired.phi + 0.1);
}
if (event.key === "+" || event.key === "=") {
desired.radius = Math.max(3.4, desired.radius - 0.35);
}
if (event.key === "-" || event.key === "_") {
desired.radius = Math.min(10.5, desired.radius + 0.35);
}
if (event.key.toLowerCase() === "r") resetCamera();
});
// ------------------------------------------------------------
// Resize
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
// Damped camera movement.
current.radius += (desired.radius - current.radius) * 0.085;
current.phi += (desired.phi - current.phi) * 0.085;
current.theta += (desired.theta - current.theta) * 0.085;
camera.position.setFromSphericalCoords(
current.radius,
current.phi,
current.theta
);
camera.position.add(target);
camera.lookAt(target);
// Gentle specimen motion.
pebbleRoot.position.y = -0.3 + Math.sin(time * 0.48) * 0.014;
pebbleRoot.rotation.y =
-0.12 + Math.sin(time * 0.13) * 0.035;
const membranePulse = 1 + Math.sin(time * 1.15) * 0.008;
biofilm.scale.setScalar(membranePulse);
// Microbial drift.
for (const microbe of microbes) {
const data = microbe.userData;
const phase = data.phase;
microbe.position.y =
data.basePosition.y + Math.sin(time * 0.72 + phase) * 0.006;
microbe.position.z =
data.basePosition.z + Math.sin(time * 0.48 + phase) * 0.008;
microbe.rotation.z =
data.baseRotation.z + Math.sin(time * 0.34 + phase) * 0.08;
}
// Bubbles rise and gently bend.
for (const bubble of bubbles) {
const data = bubble.userData;
bubble.position.y =
-1.25 +
((time * data.speed + data.offset) % 7.2);
bubble.position.x =
data.baseX +
Math.sin(time * 0.55 + data.phase) * 0.09;
bubble.rotation.y = time * 0.12 + data.phase;
}
// Water movement.
particles.rotation.y = time * 0.004;
particles.position.y = Math.sin(time * 0.18) * 0.05;
for (const shaft of shafts) {
shaft.material.opacity =
shaft.userData.baseOpacity *
(0.82 + Math.sin(time * 0.32 + shaft.userData.phase) * 0.18);
}
backgroundMaterial.uniforms.time.value = time;
causticMaterial.uniforms.time.value = time;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```