Prompt specification
Build or customize the Three.js 3D scene "Metachronal wave along a cilia row" 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: Metachronal wave along a cilia row
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/metachronal-wave-along-a-cilia-row-x0826
Scene Description Prompt:
Animate stylized cilia with staggered phases and floating particles, distinguishing the coordination wave from individual motion. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (metachronal-wave-along-a-cilia-row-x0826):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ciliary Field — Coordination and Individual Motion</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, #071b25 0%, #030a10 45%, #010407 100%);
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 2;
top: 28px;
left: 30px;
width: min(360px, calc(100vw - 60px));
color: #d9fbff;
pointer-events: none;
user-select: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
.eyebrow {
margin-bottom: 8px;
color: #72aeb8;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.26em;
text-transform: uppercase;
}
h1 {
margin: 0 0 18px;
font-size: clamp(26px, 4vw, 46px);
font-weight: 300;
letter-spacing: 0.09em;
line-height: 1;
}
.legend {
display: grid;
gap: 9px;
padding: 13px 15px;
border: 1px solid rgba(139, 226, 236, 0.14);
border-radius: 12px;
background: rgba(4, 15, 22, 0.48);
backdrop-filter: blur(12px);
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
font-size: 11px;
letter-spacing: 0.08em;
}
.swatch {
width: 22px;
height: 3px;
flex: 0 0 auto;
border-radius: 999px;
box-shadow: 0 0 12px currentColor;
}
.gold {
color: #ffad42;
background: #ffad42;
}
.cyan {
color: #4df8e6;
background: #4df8e6;
}
.legend small {
margin-left: auto;
color: #75949c;
font-size: 9px;
letter-spacing: 0.04em;
}
.controls {
position: fixed;
z-index: 2;
right: 26px;
bottom: 22px;
color: #628b95;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
pointer-events: none;
user-select: none;
}
@media (max-width: 600px) {
.hud {
top: 18px;
left: 18px;
}
.legend small {
display: none;
}
.controls {
right: 18px;
bottom: 16px;
}
}
</style>
</head>
<body>
<div class="hud">
<div class="eyebrow">Collective Motion Study</div>
<h1>CILIARY FIELD</h1>
<div class="legend">
<div class="legend-row">
<span class="swatch gold"></span>
<span>GOLD PULSE</span>
<small>phase-locked sweep</small>
</div>
<div class="legend-row">
<span class="swatch cyan"></span>
<span>CYAN FLUTTER</span>
<small>individual motion</small>
</div>
</div>
</div>
<div class="controls">Drag to orbit · Scroll to zoom</div>
<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 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.08;
renderer.setClearColor(0x02070b, 1);
renderer.domElement.setAttribute(
"aria-label",
"Interactive animated field of microscopic cilia"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x02070b);
const camera = new THREE.PerspectiveCamera(
44,
window.innerWidth / window.innerHeight,
0.1,
80
);
// ------------------------------------------------------------
// Small deterministic random generator
// ------------------------------------------------------------
function mulberry32(seed) {
return function () {
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(87421);
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
// This height function is shared by the membrane, cilia and nodes.
function heightAt(x, z, time) {
return (
0.10 * Math.sin(x * 0.45 + time * 0.55) +
0.07 * Math.sin(z * 0.72 - time * 0.42) +
0.035 * Math.sin((x + z) * 1.15 + time * 0.33)
);
}
// ------------------------------------------------------------
// Organic membrane
// ------------------------------------------------------------
const surfaceUniforms = {
uTime: { value: 0 }
};
const surfaceMaterial = new THREE.ShaderMaterial({
uniforms: surfaceUniforms,
vertexShader: `
uniform float uTime;
varying vec2 vUv;
varying vec3 vWorldPosition;
float heightAt(vec2 p) {
return
0.10 * sin(p.x * 0.45 + uTime * 0.55) +
0.07 * sin(p.y * 0.72 - uTime * 0.42) +
0.035 * sin((p.x + p.y) * 1.15 + uTime * 0.33);
}
void main() {
vec3 transformed = position;
transformed.y += heightAt(position.xz);
vec4 worldPosition = modelMatrix * vec4(transformed, 1.0);
vUv = uv;
vWorldPosition = worldPosition.xyz;
gl_Position = projectionMatrix * viewMatrix * worldPosition;
}
`,
fragmentShader: `
uniform float uTime;
varying vec2 vUv;
varying vec3 vWorldPosition;
float hash21(vec2 p) {
p = fract(p * vec2(123.34, 456.21));
p += dot(p, p + 45.32);
return fract(p.x * p.y);
}
float noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(
mix(hash21(i), hash21(i + vec2(1.0, 0.0)), f.x),
mix(hash21(i + vec2(0.0, 1.0)),
hash21(i + vec2(1.0, 1.0)), f.x),
f.y
);
}
float fbm(vec2 p) {
float value = 0.0;
float amplitude = 0.5;
for (int i = 0; i < 4; i++) {
value += amplitude * noise(p);
p = p * 2.03 + 17.1;
amplitude *= 0.5;
}
return value;
}
void main() {
vec2 q = (vUv - 0.5) * 2.0;
float irregularity =
(noise(q * 8.0 + uTime * 0.025) - 0.5) * 0.045;
float radiusSquared = dot(q, q) + irregularity;
if (radiusSquared > 1.0) {
discard;
}
float n = fbm(vWorldPosition.xz * 0.55);
float cells = noise(vWorldPosition.xz * 7.0);
// This phase exactly matches the coordinated cilium motion.
float phase = sin(
uTime * 2.05 -
vWorldPosition.x * 0.60 +
vWorldPosition.z * 0.16
);
float waveBand = smoothstep(0.42, 0.94, phase);
float edge = smoothstep(0.70, 1.0, radiusSquared);
vec3 deep = vec3(0.006, 0.026, 0.043);
vec3 teal = vec3(0.015, 0.155, 0.175);
vec3 gold = vec3(1.0, 0.56, 0.12);
vec3 color = mix(deep, teal, n * 0.75 + cells * 0.08);
color = mix(color, gold, waveBand * 0.63);
color += edge * vec3(0.02, 0.34, 0.39);
gl_FragColor = vec4(color, 1.0);
}
`,
side: THREE.DoubleSide
});
const surfaceGeometry = new THREE.PlaneGeometry(
18,
13,
110,
82
);
surfaceGeometry.rotateX(-Math.PI / 2);
const surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial);
scene.add(surface);
// ------------------------------------------------------------
// Cilia: dynamically updated camera-facing ribbon geometry
// ------------------------------------------------------------
const cilia = [];
const columns = 33;
const rows = 23;
for (let iz = 0; iz < rows; iz++) {
for (let ix = 0; ix < columns; ix++) {
const x = -7.8 + (ix / (columns - 1)) * 15.6;
const z = -5.2 + (iz / (rows - 1)) * 10.4;
const ellipse =
(x * x) / (8.1 * 8.1) +
(z * z) / (5.5 * 5.5);
if (ellipse > 0.95) {
continue;
}
cilia.push({
x: x + (random() - 0.5) * 0.16,
z: z + (random() - 0.5) * 0.14,
length: 0.86 + random() * 0.76,
width: 0.036 + random() * 0.025,
phase: random() * Math.PI * 2,
frequency: 1.45 + random() * 1.35,
localAngle: random() * Math.PI * 2,
leanX: (random() - 0.5) * 0.045,
leanZ: (random() - 0.5) * 0.045
});
}
}
const ciliaCount = cilia.length;
const samples = 11;
const verticesPerCilium = samples * 2;
const ribbonPositions = new Float32Array(
ciliaCount * verticesPerCilium * 3
);
const ribbonColors = new Float32Array(
ciliaCount * verticesPerCilium * 3
);
const centers = new Float32Array(ciliaCount * samples * 3);
const ribbonIndices = new Uint16Array(
ciliaCount * (samples - 1) * 6
);
let indexOffset = 0;
for (let c = 0; c < ciliaCount; c++) {
const vertexBase = c * verticesPerCilium;
for (let j = 0; j < samples - 1; j++) {
const a = vertexBase + j * 2;
const b = a + 1;
const d = vertexBase + (j + 1) * 2;
const e = d + 1;
ribbonIndices[indexOffset++] = a;
ribbonIndices[indexOffset++] = b;
ribbonIndices[indexOffset++] = d;
ribbonIndices[indexOffset++] = b;
ribbonIndices[indexOffset++] = e;
ribbonIndices[indexOffset++] = d;
}
}
const ribbonGeometry = new THREE.BufferGeometry();
ribbonGeometry.setAttribute(
"position",
new THREE.BufferAttribute(ribbonPositions, 3)
.setUsage(THREE.DynamicDrawUsage)
);
ribbonGeometry.setAttribute(
"color",
new THREE.BufferAttribute(ribbonColors, 3)
.setUsage(THREE.DynamicDrawUsage)
);
ribbonGeometry.setIndex(new THREE.BufferAttribute(ribbonIndices, 1));
const ribbonMaterial = new THREE.MeshBasicMaterial({
vertexColors: true,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.94,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false
});
const ribbonMesh = new THREE.Mesh(ribbonGeometry, ribbonMaterial);
ribbonMesh.frustumCulled = false;
ribbonMesh.renderOrder = 2;
scene.add(ribbonMesh);
// Thin luminous center lines add definition without external line addons.
const corePositions = new Float32Array(
ciliaCount * (samples - 1) * 2 * 3
);
const coreColors = new Float32Array(
ciliaCount * (samples - 1) * 2 * 3
);
const coreGeometry = new THREE.BufferGeometry();
coreGeometry.setAttribute(
"position",
new THREE.BufferAttribute(corePositions, 3)
.setUsage(THREE.DynamicDrawUsage)
);
coreGeometry.setAttribute(
"color",
new THREE.BufferAttribute(coreColors, 3)
.setUsage(THREE.DynamicDrawUsage)
);
const coreMaterial = new THREE.LineBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.38,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false
});
const coreLines = new THREE.LineSegments(coreGeometry, coreMaterial);
coreLines.frustumCulled = false;
coreLines.renderOrder = 3;
scene.add(coreLines);
const baseColor = new THREE.Color(0.025, 0.20, 0.29);
const tipColor = new THREE.Color(0.20, 1.0, 0.84);
const waveColor = new THREE.Color(1.0, 0.57, 0.12);
function writeCiliumColor(
array,
offset,
coordinated,
individual,
micro,
s
) {
const shimmer = 0.76 + 0.24 * Math.abs(individual);
const goldMix = smoothstep(0.52, 0.96, coordinated) *
(0.20 + 0.70 * s);
let r = THREE.MathUtils.lerp(baseColor.r, tipColor.r, s) * shimmer;
let g = THREE.MathUtils.lerp(baseColor.g, tipColor.g, s) * shimmer;
let b = THREE.MathUtils.lerp(baseColor.b, tipColor.b, s) * shimmer;
r = THREE.MathUtils.lerp(r, waveColor.r, goldMix);
g = THREE.MathUtils.lerp(g, waveColor.g, goldMix);
b = THREE.MathUtils.lerp(b, waveColor.b, goldMix);
const whiteFlash = Math.max(0, micro) * 0.10 * s;
r += whiteFlash;
g += whiteFlash;
b += whiteFlash;
array[offset] = r;
array[offset + 1] = g;
array[offset + 2] = b;
}
function updateCilia(time) {
let centerOffset = 0;
let ribbonOffset = 0;
let coreOffset = 0;
for (let c = 0; c < ciliaCount; c++) {
const cilium = cilia[c];
// Traveling coordination wave. Nearby cilia are phase-aligned.
const globalPhase =
time * 2.05 -
cilium.x * 0.60 +
cilium.z * 0.16;
const coordinated = Math.sin(globalPhase);
const coordinatedDepth = Math.cos(globalPhase);
// Each cilium also has its own frequency, phase and direction.
const individual = Math.sin(
time * cilium.frequency + cilium.phase
);
const micro = Math.sin(
time * (4.3 + cilium.frequency * 0.7) +
cilium.phase * 1.7
);
const localX = Math.cos(cilium.localAngle);
const localZ = Math.sin(cilium.localAngle);
const baseY = heightAt(cilium.x, cilium.z, time);
for (let j = 0; j < samples; j++) {
const s = j / (samples - 1);
const shaped = Math.pow(s, 1.58);
const deepShape = Math.pow(s, 1.72);
const coordinatedBendX = coordinated * 0.40;
const coordinatedBendZ = coordinatedDepth * 0.17;
const individualBendX =
individual * localX * 0.13 +
micro * localZ * 0.025;
const individualBendZ =
individual * localZ * 0.13 -
micro * localX * 0.025;
const centerX =
cilium.x +
cilium.length *
(coordinatedBendX + individualBendX + cilium.leanX) *
shaped;
const centerZ =
cilium.z +
cilium.length *
(coordinatedBendZ + individualBendZ + cilium.leanZ) *
deepShape;
const centerY =
baseY +
cilium.length * s -
0.12 * s * s;
centers[centerOffset] = centerX;
centers[centerOffset + 1] = centerY;
centers[centerOffset + 2] = centerZ;
centerOffset += 3;
}
for (let j = 0; j < samples; j++) {
const s = j / (samples - 1);
const centerIndex = (c * samples + j) * 3;
const previousIndex = centerIndex -
(j === 0 ? 0 : 3);
const nextIndex = centerIndex +
(j === samples - 1 ? 0 : 3);
const tx = centers[nextIndex] - centers[previousIndex];
const ty = centers[nextIndex + 1] - centers[previousIndex + 1];
const tz = centers[nextIndex + 2] - centers[previousIndex + 2];
const vx = camera.position.x - centers[centerIndex];
const vy = camera.position.y - centers[centerIndex + 1];
const vz = camera.position.z - centers[centerIndex + 2];
// Cross product gives a width vector facing the camera.
let sx = ty * vz - tz * vy;
let sy = tz * vx - tx * vz;
let sz = tx * vy - ty * vx;
const sideLength = Math.hypot(sx, sy, sz) || 1;
sx /= sideLength;
sy /= sideLength;
sz /= sideLength;
const halfWidth = cilium.width * (1 - 0.82 * s);
const vertexOffset = (c * samples + j) * 6;
ribbonPositions[ribbonOffset] =
centers[centerIndex] - sx * halfWidth;
ribbonPositions[ribbonOffset + 1] =
centers[centerIndex + 1] - sy * halfWidth;
ribbonPositions[ribbonOffset + 2] =
centers[centerIndex + 2] - sz * halfWidth;
ribbonPositions[ribbonOffset + 3] =
centers[centerIndex] + sx * halfWidth;
ribbonPositions[ribbonOffset + 4] =
centers[centerIndex + 1] + sy * halfWidth;
ribbonPositions[ribbonOffset + 5] =
centers[centerIndex + 2] + sz * halfWidth;
writeCiliumColor(
ribbonColors,
ribbonOffset,
coordinated,
individual,
micro,
s
);
writeCiliumColor(
ribbonColors,
ribbonOffset + 3,
coordinated,
individual,
micro,
s
);
ribbonOffset += 6;
}
for (let j = 0; j < samples - 1; j++) {
const a = (c * samples + j) * 3;
const b = a + 3;
corePositions[coreOffset] = centers[a];
corePositions[coreOffset + 1] = centers[a + 1];
corePositions[coreOffset + 2] = centers[a + 2];
corePositions[coreOffset + 3] = centers[b];
corePositions[coreOffset + 4] = centers[b + 1];
corePositions[coreOffset + 5] = centers[b + 2];
writeCiliumColor(
coreColors,
coreOffset,
coordinated,
individual,
micro,
j / (samples - 1)
);
writeCiliumColor(
coreColors,
coreOffset + 3,
coordinated,
individual,
micro,
(j + 1) / (samples - 1)
);
coreOffset += 6;
}
}
ribbonGeometry.attributes.position.needsUpdate = true;
ribbonGeometry.attributes.color.needsUpdate = true;
coreGeometry.attributes.position.needsUpdate = true;
coreGeometry.attributes.color.needsUpdate = true;
}
// ------------------------------------------------------------
// Glowing nodes at the cilia roots
// ------------------------------------------------------------
const nodePositions = new Float32Array(ciliaCount * 3);
for (let c = 0; c < ciliaCount; c++) {
nodePositions[c * 3] = cilia[c].x;
nodePositions[c * 3 + 1] = 0;
nodePositions[c * 3 + 2] = cilia[c].z;
}
const nodeUniforms = {
uTime: { value: 0 },
uPixelRatio: { value: renderer.getPixelRatio() }
};
const nodeGeometry = new THREE.BufferGeometry();
nodeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(nodePositions, 3)
);
const nodeMaterial = new THREE.ShaderMaterial({
uniforms: nodeUniforms,
vertexShader: `
uniform float uTime;
uniform float uPixelRatio;
varying float vWave;
float heightAt(vec2 p) {
return
0.10 * sin(p.x * 0.45 + uTime * 0.55) +
0.07 * sin(p.y * 0.72 - uTime * 0.42) +
0.035 * sin((p.x + p.y) * 1.15 + uTime * 0.33);
}
void main() {
vec3 p = position;
p.y += heightAt(p.xz) + 0.025;
float phase = sin(
uTime * 2.05 -
p.x * 0.60 +
p.z * 0.16
);
vWave = smoothstep(0.42, 0.94, phase);
vec4 mvPosition = modelViewMatrix * vec4(p, 1.0);
gl_Position = projectionMatrix * mvPosition;
gl_PointSize =
(3.2 + 3.0 * vWave) *
uPixelRatio *
(20.0 / max(1.0, -mvPosition.z));
}
`,
fragmentShader: `
varying float vWave;
void main() {
float d = length(gl_PointCoord - 0.5);
float core = 1.0 - smoothstep(0.0, 0.16, d);
float halo = 1.0 - smoothstep(0.08, 0.5, d);
vec3 teal = vec3(0.08, 0.88, 0.80);
vec3 gold = vec3(1.0, 0.58, 0.13);
vec3 color = mix(teal, gold, vWave);
float alpha = (core * 0.7 + halo * 0.24) * 0.8;
gl_FragColor = vec4(color, alpha);
}
`,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false
});
const nodes = new THREE.Points(nodeGeometry, nodeMaterial);
nodes.renderOrder = 1;
scene.add(nodes);
// ------------------------------------------------------------
// Floating particles
// ------------------------------------------------------------
const particleCount = 1250;
const particlePositions = new Float32Array(particleCount * 3);
const particleSizes = new Float32Array(particleCount);
const particleSpeeds = new Float32Array(particleCount);
const particleSeeds = new Float32Array(particleCount);
for (let i = 0; i < particleCount; i++) {
particlePositions[i * 3] = (random() - 0.5) * 22;
particlePositions[i * 3 + 1] = random() * 9;
particlePositions[i * 3 + 2] = -7 + random() * 15;
particleSizes[i] = 0.45 + random() * 2.15;
particleSpeeds[i] = 0.04 + random() * 0.22;
particleSeeds[i] = random();
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
particleGeometry.setAttribute(
"aSize",
new THREE.BufferAttribute(particleSizes, 1)
);
particleGeometry.setAttribute(
"aSpeed",
new THREE.BufferAttribute(particleSpeeds, 1)
);
particleGeometry.setAttribute(
"aSeed",
new THREE.BufferAttribute(particleSeeds, 1)
);
const particleUniforms = {
uTime: { value: 0 },
uPixelRatio: { value: renderer.getPixelRatio() }
};
const particleMaterial = new THREE.ShaderMaterial({
uniforms: particleUniforms,
vertexShader: `
uniform float uTime;
uniform float uPixelRatio;
attribute float aSize;
attribute float aSpeed;
attribute float aSeed;
varying float vWave;
varying float vFade;
void main() {
vec3 p = position;
p.y = mod(
p.y + uTime * aSpeed + 1.0,
9.0
) - 1.0;
p.x += sin(
uTime * 0.25 +
aSeed * 6.283 +
p.y * 0.7
) * 0.18;
p.z += cos(
uTime * 0.19 +
aSeed * 8.0 +
p.y * 0.5
) * 0.14;
float phase = sin(
uTime * 2.05 -
p.x * 0.60 +
p.z * 0.16
);
vWave = smoothstep(0.64, 0.96, phase);
vFade = 0.45 + aSeed * 0.55;
vec4 mvPosition = modelViewMatrix * vec4(p, 1.0);
gl_Position = projectionMatrix * mvPosition;
gl_PointSize =
aSize *
uPixelRatio *
(45.0 / max(1.0, -mvPosition.z));
}
`,
fragmentShader: `
varying float vWave;
varying float vFade;
void main() {
float d = length(gl_PointCoord - 0.5);
float glow = 1.0 - smoothstep(0.06, 0.5, d);
float core = 1.0 - smoothstep(0.0, 0.12, d);
vec3 cyan = vec3(0.16, 0.80, 0.92);
vec3 gold = vec3(1.0, 0.60, 0.16);
vec3 color = mix(cyan, gold, vWave * 0.72);
float alpha = (glow * 0.42 + core * 0.45) * vFade;
gl_FragColor = vec4(color, alpha);
}
`,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false
});
const particles = new THREE.Points(
particleGeometry,
particleMaterial
);
particles.frustumCulled = false;
particles.renderOrder = 4;
scene.add(particles);
// ------------------------------------------------------------
// Custom camera controls — no Three.js addons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.2, 0);
let cameraTheta = 0.30;
let cameraPhi = 1.02;
let cameraRadius = 14.5;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
const cosPhi = Math.cos(cameraPhi);
camera.position.set(
cameraTarget.x +
cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y +
cameraRadius * cosPhi,
cameraTarget.z +
cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = clamp(cameraPhi - dy * 0.005, 0.43, 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();
cameraRadius = clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
9,
22
);
},
{ passive: false }
);
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = window.innerWidth < 650 ? 54 : 44;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
const pixelRatio = renderer.getPixelRatio();
particleUniforms.uPixelRatio.value = pixelRatio;
nodeUniforms.uPixelRatio.value = pixelRatio;
});
camera.fov = window.innerWidth < 650 ? 54 : 44;
camera.updateProjectionMatrix();
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
surfaceUniforms.uTime.value = elapsed;
particleUniforms.uTime.value = elapsed;
nodeUniforms.uTime.value = elapsed;
updateCamera();
updateCilia(elapsed);
renderer.render(scene, camera);
}
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```