Prompt specification
Build or customize the Three.js 3D scene "Ciliated surface patch" 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: Ciliated surface patch
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ciliated-surface-patch-x0825
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Create rows of short flexible cilia on a cell-surface tile, with an asymmetric beat cycle; keep the cilia attached and label their movement schematic.
Working Three.js Starter (ciliated-surface-patch-x0825):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Asymmetric Cilia — Cell-Surface Tile</title>
<style>
:root {
--ink: #e7fbf5;
--muted: #8fb8b4;
--cyan: #72e3d2;
--amber: #ffc45d;
--panel: rgba(7, 22, 27, 0.78);
--line: rgba(133, 225, 211, 0.2);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071116;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 5;
pointer-events: none;
}
.title-card {
top: 26px;
left: 28px;
max-width: 540px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4.3vw, 58px);
font-weight: 720;
letter-spacing: -0.055em;
line-height: 0.95;
text-shadow: 0 3px 28px rgba(0, 0, 0, 0.35);
}
.subtitle {
max-width: 480px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.chips {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 15px;
}
.chip {
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 27, 32, 0.58);
color: #bde4dc;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.schematic-card {
top: 22px;
right: 22px;
width: min(370px, calc(100vw - 44px));
padding: 16px 17px 14px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
.schematic-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 7px;
}
.schematic-head strong {
font-size: 11px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.phase-readout {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#phase-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 12px var(--amber);
}
.schematic-card svg {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
.schematic-caption {
margin: -2px 2px 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.timeline {
display: flex;
height: 29px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 8px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.timeline span {
display: flex;
align-items: center;
justify-content: center;
}
.power {
width: 30%;
color: #241a08;
background: var(--amber);
}
.recovery {
width: 70%;
color: #08201e;
background: var(--cyan);
}
.legend {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
color: var(--muted);
font-size: 10px;
}
.legend b {
color: #d9f5ee;
font-weight: 650;
}
.controls {
left: 28px;
bottom: 24px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(7, 22, 27, 0.62);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
}
.controls b {
color: var(--ink);
font-weight: 650;
}
.corner-mark {
right: 25px;
bottom: 23px;
color: rgba(159, 207, 199, 0.55);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.title-card {
top: 18px;
left: 18px;
right: 18px;
}
.subtitle {
max-width: 340px;
font-size: 11px;
}
.chips {
display: none;
}
.schematic-card {
top: auto;
right: 12px;
bottom: 12px;
width: min(350px, calc(100vw - 24px));
padding: 12px 13px 11px;
}
.schematic-caption,
.legend {
display: none;
}
.controls,
.corner-mark {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive low-poly cell-surface tile with rows of flexible, anchored cilia"
></canvas>
<header class="hud title-card">
<div class="eyebrow">Cell-surface study / 01</div>
<h1>ASYMMETRIC<br />CILIA</h1>
<p class="subtitle">
Seven rows of short, flexible cilia beat across a low-poly membrane tile.
Every base remains locked to the surface while the tips sweep, extend, and
curl through a deliberately uneven cycle.
</p>
<div class="chips">
<span class="chip">63 anchored cilia</span>
<span class="chip">30% power / 70% recovery</span>
<span class="chip">Live metachronal wave</span>
</div>
</header>
<aside class="hud schematic-card" aria-label="Movement schematic">
<div class="schematic-head">
<strong>Movement schematic</strong>
<div class="phase-readout">
<span id="phase-dot"></span>
<span id="phase-label">Power stroke</span>
</div>
</div>
<svg viewBox="0 0 360 170" role="img" aria-label="Cilia power stroke and recovery diagram">
<defs>
<marker
id="amber-arrow"
markerWidth="7"
markerHeight="7"
refX="5.5"
refY="3.5"
orient="auto"
>
<path d="M0,0 L7,3.5 L0,7 Z" fill="#ffc45d" />
</marker>
<marker
id="cyan-arrow"
markerWidth="7"
markerHeight="7"
refX="5.5"
refY="3.5"
orient="auto"
>
<path d="M0,0 L7,3.5 L0,7 Z" fill="#72e3d2" />
</marker>
</defs>
<!-- membrane -->
<path
d="M16 134 L344 134"
fill="none"
stroke="#75c6bd"
stroke-width="3"
stroke-linecap="round"
opacity=".55"
/>
<path
d="M22 143 L338 143"
fill="none"
stroke="#28585b"
stroke-width="8"
stroke-linecap="round"
opacity=".28"
/>
<!-- fixed base -->
<ellipse cx="92" cy="133" rx="10" ry="3.5" fill="#f26f55" />
<circle cx="92" cy="132" r="3" fill="#ffbf61" />
<!-- power stroke -->
<path
d="M92 130 C119 115 139 88 159 53"
fill="none"
stroke="#ffc45d"
stroke-width="7"
stroke-linecap="round"
marker-end="url(#amber-arrow)"
/>
<path
d="M92 130 C119 115 139 88 159 53"
fill="none"
stroke="#ffe0a2"
stroke-width="1.5"
stroke-linecap="round"
opacity=".65"
/>
<!-- recovery curl -->
<ellipse cx="224" cy="133" rx="10" ry="3.5" fill="#f26f55" />
<circle cx="224" cy="132" r="3" fill="#72e3d2" />
<path
d="M224 130 C258 125 281 107 269 83 C260 65 239 62 231 77 C225 89 238 97 248 91"
fill="none"
stroke="#72e3d2"
stroke-width="7"
stroke-linecap="round"
/>
<path
d="M269 82 C278 103 257 123 226 130"
fill="none"
stroke="#b2fff0"
stroke-width="2"
stroke-linecap="round"
marker-end="url(#cyan-arrow)"
opacity=".8"
/>
<text x="52" y="27" fill="#ffc45d" font-size="11" font-weight="800">
POWER STROKE
</text>
<text x="52" y="43" fill="#92aaa8" font-size="9">
fast / extended
</text>
<text x="219" y="27" fill="#72e3d2" font-size="11" font-weight="800">
RECOVERY
</text>
<text x="219" y="43" fill="#92aaa8" font-size="9">
slow / curled return
</text>
<text x="14" y="163" fill="#71938f" font-size="9">
PINNED BASE
</text>
<path
d="M82 159 L92 137"
fill="none"
stroke="#71938f"
stroke-width="1"
marker-end="url(#cyan-arrow)"
/>
</svg>
<p class="schematic-caption">
The quick effective stroke straightens and sweeps the cilia. The longer
recovery folds the shaft before returning to its starting pose.
</p>
<div class="timeline" aria-label="Beat cycle timing">
<span class="power">POWER 30%</span>
<span class="recovery">RECOVERY 70%</span>
</div>
<div class="legend">
<span><b>Amber</b> effective stroke</span>
<span><b>Cyan</b> recovery</span>
<span><b>Coral</b> fixed base</span>
</div>
</aside>
<div class="hud controls">
<b>Drag</b> orbit · <b>Wheel / pinch</b> zoom ·
<b>Double-click</b> reset
</div>
<div class="hud corner-mark">Procedural geometry / Three.js</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const phaseLabel = document.querySelector("#phase-label");
const phaseDot = document.querySelector("#phase-dot");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071116);
scene.fog = new THREE.FogExp2(0x071116, 0.024);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
theta: 0.64,
phi: 0.94,
radius: 13.4,
target: new THREE.Vector3(0, 0.65, 0)
};
let userAdjustedCamera = false;
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
// Lighting ---------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa8f5e7, 0x142126, 1.65));
const keyLight = new THREE.DirectionalLight(0xffedc5, 3.15);
keyLight.position.set(5.5, 10, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0005;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x65d9ff, 1.35);
fillLight.position.set(-6, 4.5, -4);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xff8c62, 0.75);
rimLight.position.set(2, 3, -8);
scene.add(rimLight);
// Ground -----------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x071116,
roughness: 0.95,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.31;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x285b5c, 0x17363b);
grid.position.y = -0.302;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// Low-poly chamfered prism ----------------------------------------------
function chamferedPrism(width, depth, height, chamfer) {
const x = width * 0.5;
const z = depth * 0.5;
const c = chamfer;
const points = [
[-x + c, -z],
[x - c, -z],
[x, -z + c],
[x, z - c],
[x - c, z],
[-x + c, z],
[-x, z - c],
[-x, -z + c]
];
const top = [];
const sides = [];
const bottom = [];
for (let i = 0; i < points.length; i++) {
const current = points[i];
const next = points[(i + 1) % points.length];
// Up-facing top fan.
top.push(
0, 0, 0,
next[0], 0, next[1],
current[0], 0, current[1]
);
// Down-facing bottom fan.
bottom.push(
0, -height, 0,
current[0], -height, current[1],
next[0], -height, next[1]
);
// Two triangles for each vertical side.
sides.push(
current[0], 0, current[1],
next[0], 0, next[1],
current[0], -height, current[1],
next[0], 0, next[1],
next[0], -height, next[1],
current[0], -height, current[1]
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(top, 3)
);
geometry.addGroup(0, top.length / 3, 0);
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sides, 3)
);
geometry.addGroup(top.length / 3, sides.length / 3, 1);
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(bottom, 3)
);
geometry.addGroup(
top.length / 3 + sides.length / 3,
bottom.length / 3,
2
);
geometry.computeVertexNormals();
return geometry;
}
function outlineLoop(width, depth, chamfer, y, color, opacity) {
const x = width * 0.5;
const z = depth * 0.5;
const c = chamfer;
const points = [
[-x + c, y, -z],
[x - c, y, -z],
[x, y, -z + c],
[x, y, z - c],
[x - c, y, z],
[-x + c, y, z],
[-x, y, z - c],
[-x, y, -z + c]
].map((p) => new THREE.Vector3(...p));
return new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
}
const tile = new THREE.Mesh(
chamferedPrism(9.6, 7.2, 0.66, 0.72),
[
new THREE.MeshStandardMaterial({
color: 0x1b4a50,
roughness: 0.86
}),
new THREE.MeshStandardMaterial({
color: 0x102d35,
roughness: 0.8
}),
new THREE.MeshStandardMaterial({
color: 0x0b242b,
roughness: 0.9
})
]
);
tile.position.y = 0.36;
tile.castShadow = true;
tile.receiveShadow = true;
scene.add(tile);
const surface = new THREE.Mesh(
chamferedPrism(9.1, 6.7, 0.13, 0.55),
[
new THREE.MeshStandardMaterial({
color: 0x26716e,
roughness: 0.93,
metalness: 0.01
}),
new THREE.MeshStandardMaterial({
color: 0x185054,
roughness: 0.9
}),
new THREE.MeshStandardMaterial({
color: 0x123d42,
roughness: 0.95
})
]
);
surface.position.y = 0.49;
surface.castShadow = true;
surface.receiveShadow = true;
scene.add(surface);
scene.add(outlineLoop(9.1, 6.7, 0.55, 0.496, 0x8ce4d6, 0.42));
// Triangulated membrane facets -----------------------------------------
const facetPositions = [];
const facetColors = [];
const facetPalette = [
0x2d7774,
0x34827d,
0x276d6b,
0x3a8a83,
0x2b716f,
0x418d85
];
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6d2b79f5;
let t = value;
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 = seededRandom(14821);
const facetColumns = 10;
const facetRows = 7;
const facetX0 = -4.12;
const facetX1 = 4.12;
const facetZ0 = -2.82;
const facetZ1 = 2.82;
const facetY = 0.495;
function pushFacetTriangle(a, b, c) {
facetPositions.push(
a[0], facetY, a[1],
b[0], facetY, b[1],
c[0], facetY, c[1]
);
const color = new THREE.Color(
facetPalette[Math.floor(random() * facetPalette.length)]
);
for (let i = 0; i < 3; i++) {
facetColors.push(color.r, color.g, color.b);
}
}
for (let row = 0; row < facetRows; row++) {
for (let col = 0; col < facetColumns; col++) {
const x0 =
THREE.MathUtils.lerp(facetX0, facetX1, col / facetColumns);
const x1 =
THREE.MathUtils.lerp(facetX0, facetX1, (col + 1) / facetColumns);
const z0 =
THREE.MathUtils.lerp(facetZ0, facetZ1, row / facetRows);
const z1 =
THREE.MathUtils.lerp(facetZ0, facetZ1, (row + 1) / facetRows);
const a = [x0, z0];
const b = [x1, z0];
const c = [x1, z1];
const d = [x0, z1];
if ((row + col) % 2 === 0) {
pushFacetTriangle(a, c, b);
pushFacetTriangle(a, d, c);
} else {
pushFacetTriangle(a, d, b);
pushFacetTriangle(b, d, c);
}
}
}
const facetGeometry = new THREE.BufferGeometry();
facetGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(facetPositions, 3)
);
facetGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(facetColors, 3)
);
facetGeometry.computeVertexNormals();
const facets = new THREE.Mesh(
facetGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.98,
metalness: 0,
flatShading: true,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
})
);
facets.receiveShadow = true;
scene.add(facets);
const facetEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(facetGeometry, 18),
new THREE.LineBasicMaterial({
color: 0x9ce5d7,
transparent: true,
opacity: 0.1
})
);
facetEdges.position.y = 0.002;
scene.add(facetEdges);
// Cilia ------------------------------------------------------------------
const SIDES = 5;
const SEGMENTS = 5;
const SEGMENT_LENGTH = 0.265;
const BASE_RADIUS = 0.075;
const TIP_RADIUS = 0.022;
const CILIA_BASE_Y = 0.58;
const baseColor = new THREE.Color(0xd95648);
const tipColor = new THREE.Color(0xffc15b);
const ciliaMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.56,
metalness: 0.04,
flatShading: true,
side: THREE.DoubleSide
});
function createCiliaGeometry() {
const ringCount = SEGMENTS + 1;
const vertexCount = ringCount * SIDES + 2;
const positions = new Float32Array(vertexCount * 3);
const colors = new Float32Array(vertexCount * 3);
const indices = [];
for (let ring = 0; ring < ringCount; ring++) {
const t = ring / SEGMENTS;
const color = baseColor.clone().lerp(tipColor, t * t);
for (let side = 0; side < SIDES; side++) {
const index = ring * SIDES + side;
colors[index * 3] = color.r;
colors[index * 3 + 1] = color.g;
colors[index * 3 + 2] = color.b;
}
}
for (let segment = 0; segment < SEGMENTS; segment++) {
for (let side = 0; side < SIDES; side++) {
const nextSide = (side + 1) % SIDES;
const a = segment * SIDES + side;
const b = segment * SIDES + nextSide;
const c = (segment + 1) * SIDES + side;
const d = (segment + 1) * SIDES + nextSide;
indices.push(a, c, b, b, c, d);
}
}
const baseCenter = ringCount * SIDES;
const tipCenter = baseCenter + 1;
const lastRing = SEGMENTS * SIDES;
for (let side = 0; side < SIDES; side++) {
const nextSide = (side + 1) % SIDES;
indices.push(baseCenter, side, nextSide);
indices.push(
tipCenter,
lastRing + side,
lastRing + nextSide
);
}
colors[baseCenter * 3] = baseColor.r;
colors[baseCenter * 3 + 1] = baseColor.g;
colors[baseCenter * 3 + 2] = baseColor.b;
colors[tipCenter * 3] = tipColor.r;
colors[tipCenter * 3 + 1] = tipColor.g;
colors[tipCenter * 3 + 2] = tipColor.b;
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const ciliaStates = [];
const rowZ = [-2.52, -1.68, -0.84, 0, 0.84, 1.68, 2.52];
for (let row = 0; row < rowZ.length; row++) {
for (let col = 0; col < 9; col++) {
const x =
-3.6 +
col * 0.9 +
(row % 2 === 0 ? -0.1 : 0.1);
const z = rowZ[row];
const geometry = createCiliaGeometry();
const mesh = new THREE.Mesh(geometry, ciliaMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
ciliaStates.push({
mesh,
baseX: x,
baseY: CILIA_BASE_Y,
baseZ: z,
phaseOffset:
col * 0.055 +
row * 0.07 +
random() * 0.018,
lean: 0.15 + random() * 0.08,
lengthScale: 0.91 + random() * 0.16,
nodes: new Float32Array((SEGMENTS + 1) * 3),
tangents: new Float32Array(SEGMENTS * 3)
});
}
}
// Fixed low-poly bases ---------------------------------------------------
const baseGeometry = new THREE.CylinderGeometry(
0.115,
0.145,
0.09,
6,
1,
false
);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0xca5145,
roughness: 0.72,
metalness: 0.02,
flatShading: true
});
const baseInstances = new THREE.InstancedMesh(
baseGeometry,
baseMaterial,
ciliaStates.length
);
baseInstances.castShadow = true;
baseInstances.receiveShadow = true;
const instanceMatrix = new THREE.Matrix4();
ciliaStates.forEach((state, index) => {
instanceMatrix.makeTranslation(
state.baseX,
0.535,
state.baseZ
);
baseInstances.setMatrixAt(index, instanceMatrix);
});
baseInstances.instanceMatrix.needsUpdate = true;
scene.add(baseInstances);
// Asymmetric beat and flexible mesh deformation --------------------------
function smoothStep(value) {
return value * value * (3 - 2 * value);
}
function updateCilia(state, elapsed) {
const globalPhase = (elapsed / 4.8) % 1;
const phase = (globalPhase + state.phaseOffset) % 1;
let sweep;
let curl;
if (phase < 0.3) {
// Fast, relatively straight effective stroke.
const u = phase / 0.3;
const eased = 1 - Math.pow(1 - u, 2);
sweep = THREE.MathUtils.lerp(-0.42, 0.68, eased);
curl = 0;
} else {
// Slower, visibly curled recovery.
const u = (phase - 0.3) / 0.7;
const eased = smoothStep(u);
sweep = THREE.MathUtils.lerp(0.68, -0.42, eased);
curl = Math.pow(Math.sin(Math.PI * u), 0.72);
}
const nodes = state.nodes;
const tangents = state.tangents;
const sweepSin = Math.sin(sweep);
const sweepCos = Math.cos(sweep);
nodes[0] = state.baseX;
nodes[1] = state.baseY;
nodes[2] = state.baseZ;
for (let segment = 0; segment < SEGMENTS; segment++) {
const weight = segment / (SEGMENTS - 1);
// Progressive bending plus a subtle hooked tip during recovery.
const bend =
curl *
(
2.0 * Math.pow(weight, 1.15) -
1.1 * Math.pow(weight, 4.0)
);
const theta = state.lean + bend;
const sinTheta = Math.sin(theta);
const cosTheta = Math.cos(theta);
const tx = sweepSin * sinTheta;
const ty = cosTheta;
const tz = sweepCos * sinTheta;
tangents[segment * 3] = tx;
tangents[segment * 3 + 1] = ty;
tangents[segment * 3 + 2] = tz;
const next = (segment + 1) * 3;
nodes[next] =
nodes[segment * 3] +
tx * SEGMENT_LENGTH * state.lengthScale;
nodes[next + 1] =
nodes[segment * 3 + 1] +
ty * SEGMENT_LENGTH * state.lengthScale;
nodes[next + 2] =
nodes[segment * 3 + 2] +
tz * SEGMENT_LENGTH * state.lengthScale;
}
const positionAttribute = state.mesh.geometry.getAttribute("position");
const positions = positionAttribute.array;
let offset = 0;
for (let ring = 0; ring <= SEGMENTS; ring++) {
const weight = ring / SEGMENTS;
const nodeIndex = ring * 3;
// Average adjoining segment tangents for a continuous low-poly joint.
let tx;
let ty;
let tz;
if (ring === 0) {
tx = tangents[0];
ty = tangents[1];
tz = tangents[2];
} else if (ring === SEGMENTS) {
const tangentIndex = (SEGMENTS - 1) * 3;
tx = tangents[tangentIndex];
ty = tangents[tangentIndex + 1];
tz = tangents[tangentIndex + 2];
} else {
const previous = (ring - 1) * 3;
const next = ring * 3;
tx = (tangents[previous] + tangents[next]) * 0.5;
ty = (tangents[previous + 1] + tangents[next + 1]) * 0.5;
tz = (tangents[previous + 2] + tangents[next + 2]) * 0.5;
}
// Project the world X axis onto the ring's perpendicular plane.
let sideX = 1 - tx * tx;
let sideY = -ty * tx;
let sideZ = -tz * tx;
const sideLength = Math.hypot(sideX, sideY, sideZ) || 1;
sideX /= sideLength;
sideY /= sideLength;
sideZ /= sideLength;
// normal = tangent × side
const normalX = ty * sideZ - tz * sideY;
const normalY = tz * sideX - tx * sideZ;
const normalZ = tx * sideY - ty * sideX;
const radius =
THREE.MathUtils.lerp(BASE_RADIUS, TIP_RADIUS, weight) *
state.lengthScale;
for (let side = 0; side < SIDES; side++) {
const angle = (side / SIDES) * Math.PI * 2;
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
positions[offset++] =
nodes[nodeIndex] +
radius * (sideX * cosine + normalX * sine);
positions[offset++] =
nodes[nodeIndex + 1] +
radius * (sideY * cosine + normalY * sine);
positions[offset++] =
nodes[nodeIndex + 2] +
radius * (sideZ * cosine + normalZ * sine);
}
}
// Base and tip cap centers.
positions[offset++] = state.baseX;
positions[offset++] = state.baseY;
positions[offset++] = state.baseZ;
const tipNode = SEGMENTS * 3;
positions[offset++] = nodes[tipNode];
positions[offset++] = nodes[tipNode + 1];
positions[offset++] = nodes[tipNode + 2];
positionAttribute.needsUpdate = true;
state.mesh.geometry.computeVertexNormals();
}
// Direct camera controls: no addons --------------------------------------
const pointers = new Map();
let lastPinchDistance = 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
);
}
canvas.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
if (pointers.size === 2) {
lastPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.32,
1.43
);
userAdjustedCamera = true;
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (lastPinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (lastPinchDistance / distance),
8.5,
23
);
userAdjustedCamera = true;
}
lastPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8.5,
23
);
userAdjustedCamera = true;
updateCamera();
},
{ passive: false }
);
function resetCamera() {
orbit.theta = 0.64;
orbit.phi = 0.94;
orbit.radius = window.innerWidth < 760 ? 18 : 13.4;
userAdjustedCamera = false;
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.fov = aspect < 0.8 ? 49 : 42;
camera.updateProjectionMatrix();
if (!userAdjustedCamera) {
orbit.radius = aspect < 0.8 ? 18 : 13.4;
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// Render loop ------------------------------------------------------------
const clock = new THREE.Clock();
let previousPhaseLabel = "";
function animate() {
const elapsed = clock.getElapsedTime();
for (const state of ciliaStates) {
updateCilia(state, elapsed);
}
const phase = (elapsed / 4.8) % 1;
const isPower = phase < 0.3;
const currentLabel = isPower ? "Power stroke" : "Recovery";
if (currentLabel !== previousPhaseLabel) {
phaseLabel.textContent = currentLabel;
phaseDot.style.background = isPower ? "#ffc45d" : "#72e3d2";
phaseDot.style.boxShadow = isPower
? "0 0 12px #ffc45d"
: "0 0 12px #72e3d2";
previousPhaseLabel = currentLabel;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```