Prompt specification
Build or customize the Three.js 3D scene "Rotating head and fluid-lag analogy" 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: Rotating head and fluid-lag analogy
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rotating-head-and-fluid-lag-analogy-x1030
Scene Description Prompt:
Please use Three.js from cdnjs for a cutaway scene based on this idea: rotating head and fluid-lag analogy. Animate a transparent loop rotating while illustrative fluid markers lag briefly; state the simplified dynamics and separate this analogy from the full vestibular response. Return one self-contained HTML file.
Working Three.js Starter (rotating-head-and-fluid-lag-analogy-x1030):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Turning and the Inner Ear — Fluid-Lag Analogy</title>
<style>
:root {
color-scheme: dark;
--bg: #06101b;
--panel: rgba(8, 22, 35, 0.82);
--panel-border: rgba(154, 220, 232, 0.18);
--text: #e9f7f8;
--muted: #9bb5be;
--cyan: #6de7ef;
--cyan-soft: rgba(109, 231, 239, 0.14);
--amber: #ffc65a;
--amber-soft: rgba(255, 198, 90, 0.13);
--white: #f4fbfc;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 30%, rgba(23, 79, 96, 0.28), transparent 36%),
linear-gradient(145deg, #071622, #040b13 72%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
header {
position: absolute;
top: 24px;
left: 28px;
max-width: 540px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 1px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
h1 {
margin: 9px 0 8px;
max-width: 520px;
font-size: clamp(29px, 4vw, 52px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 720;
}
.subtitle {
margin: 0;
max-width: 470px;
color: #b7cbd1;
font-size: 14px;
line-height: 1.55;
}
.model-badge {
position: absolute;
top: 25px;
right: 26px;
padding: 9px 12px;
border: 1px solid rgba(255, 198, 90, 0.35);
border-radius: 999px;
background: rgba(28, 25, 16, 0.66);
color: #ffe2a7;
font-size: 10px;
font-weight: 760;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.card {
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 22px 65px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(18px) saturate(120%);
}
.dynamics {
position: absolute;
left: 24px;
bottom: 24px;
width: min(440px, calc(100vw - 48px));
padding: 19px 20px 17px;
pointer-events: auto;
}
.card-kicker {
color: var(--cyan);
font-size: 10px;
font-weight: 780;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.dynamics h2 {
margin: 7px 0 11px;
font-size: 19px;
line-height: 1.2;
letter-spacing: -0.025em;
}
.equation {
margin: 9px 0 10px;
padding: 11px 12px;
border: 1px solid rgba(109, 231, 239, 0.16);
border-radius: 11px;
background: var(--cyan-soft);
color: #d9fdff;
font-family: "Cambria Math", "Times New Roman", serif;
font-size: 20px;
text-align: center;
}
.dynamics p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.48;
}
.dynamics b {
color: #e8f7f8;
font-weight: 670;
}
.parameter-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 9px;
color: #bdd2d7;
font-size: 11px;
}
.parameter-row code {
padding: 3px 6px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.065);
color: var(--amber);
font-family: inherit;
}
.trace {
width: 100%;
height: 64px;
margin: 12px 0 5px;
overflow: visible;
}
.trace-grid {
stroke: rgba(255, 255, 255, 0.08);
stroke-width: 1;
}
#thetaPath {
fill: none;
stroke: var(--white);
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
#phiPath {
fill: none;
stroke: var(--cyan);
stroke-width: 2.2;
stroke-linecap: round;
stroke-linejoin: round;
}
.trace-legend {
display: flex;
gap: 15px;
margin-top: -3px;
color: var(--muted);
font-size: 10px;
}
.trace-legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.trace-legend i {
width: 14px;
height: 2px;
border-radius: 2px;
background: currentColor;
}
.trace-legend .head-key {
color: var(--white);
}
.trace-legend .fluid-key {
color: var(--cyan);
}
.distinction {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.distinction > div {
padding: 10px;
border-radius: 11px;
}
.distinction .analogy {
background: var(--cyan-soft);
}
.distinction .physiology {
background: var(--amber-soft);
}
.tag {
display: inline-block;
margin-bottom: 6px;
font-size: 9px;
font-weight: 780;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.analogy .tag {
color: var(--cyan);
}
.physiology .tag {
color: var(--amber);
}
.distinction p {
font-size: 10.5px;
line-height: 1.45;
}
.controls {
position: absolute;
right: 25px;
bottom: 25px;
width: 250px;
padding: 15px;
pointer-events: auto;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
#motionStatus {
color: var(--amber);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.lag-meter {
position: relative;
height: 4px;
margin: 10px 0 14px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.09);
}
#lagFill {
position: absolute;
inset: 0 auto 0 0;
width: 0;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), var(--amber));
box-shadow: 0 0 12px rgba(109, 231, 239, 0.5);
}
.readouts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-bottom: 13px;
}
.readout {
padding: 8px 5px;
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
text-align: center;
}
.readout span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 720;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.readout b {
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 35px;
border: 1px solid rgba(109, 231, 239, 0.23);
border-radius: 9px;
background: rgba(109, 231, 239, 0.09);
color: #e9fbfc;
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 700;
transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}
button:hover {
border-color: rgba(109, 231, 239, 0.5);
background: rgba(109, 231, 239, 0.16);
}
button:active {
transform: translateY(1px);
}
.speed-control {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 9px;
margin-top: 12px;
color: var(--muted);
font-size: 10px;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
}
.camera-help {
margin-top: 11px;
color: #78939d;
font-size: 9.5px;
text-align: center;
}
.scene-label {
position: absolute;
z-index: 4;
padding: 7px 10px;
border: 1px solid rgba(109, 231, 239, 0.25);
border-radius: 999px;
background: rgba(5, 18, 29, 0.82);
color: #cceff2;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.025em;
white-space: nowrap;
transform: translate(-50%, -125%);
opacity: 0;
transition: opacity 180ms ease;
backdrop-filter: blur(10px);
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 17px;
background: linear-gradient(var(--cyan), transparent);
}
#tracerLabel {
border-color: rgba(255, 198, 90, 0.32);
color: #ffe3aa;
}
#tracerLabel::after {
background: linear-gradient(var(--amber), transparent);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #06101b;
color: var(--cyan);
font-size: 12px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
header {
top: 17px;
left: 18px;
max-width: calc(100vw - 36px);
}
h1 {
font-size: 31px;
}
.subtitle {
max-width: 310px;
font-size: 12px;
}
.model-badge {
display: none;
}
.dynamics {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-height: 48vh;
padding: 14px;
overflow-y: auto;
}
.dynamics h2 {
font-size: 16px;
}
.distinction {
grid-template-columns: 1fr;
}
.trace {
height: 46px;
}
.controls {
right: 12px;
bottom: calc(48vh + 23px);
width: 205px;
padding: 11px;
}
.readouts {
margin-bottom: 8px;
}
.scene-label {
display: none;
}
}
@media (max-height: 690px) and (min-width: 761px) {
.dynamics {
bottom: 15px;
width: 410px;
transform: scale(0.9);
transform-origin: left bottom;
}
.controls {
bottom: 15px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="hud">
<header>
<div class="eyebrow">Rotational motion · cutaway schematic</div>
<h1>Turning, with a brief lag.</h1>
<p class="subtitle">
A transparent inner-ear loop turns with the head while illustrative fluid
markers briefly trail, then catch up.
</p>
</header>
<div class="model-badge">Simplified analogy · not a physiological simulation</div>
<section class="card dynamics" aria-label="Model explanation">
<div class="card-kicker">Simplified dynamics</div>
<h2>One delayed phase—not a fluid solver.</h2>
<div class="equation">φ̈ + 2ζωφ̇ + ω²(φ − θ) = 0</div>
<p>
<b>θ</b> is head yaw and <b>φ</b> is the illustrative bulk-fluid phase.
The second-order lag makes φ chase θ, overshoot slightly less as it settles,
and show a transient difference during turning and stopping.
</p>
<div class="parameter-row">
<span>Display parameters</span>
<code>ζ = 0.88</code>
<code>ω = 6.2 s⁻¹</code>
</div>
<svg class="trace" viewBox="0 0 260 64" role="img" aria-label="Head and fluid phase trace">
<path class="trace-grid" d="M4 10H256 M4 32H256 M4 54H256" />
<path id="thetaPath"></path>
<path id="phiPath"></path>
</svg>
<div class="trace-legend">
<span class="head-key"><i></i> head / loop θ</span>
<span class="fluid-key"><i></i> fluid phase φ</span>
</div>
<div class="distinction">
<div class="analogy">
<span class="tag">The analogy</span>
<p>
The dots are phase tracers. Their lag is deliberately exaggerated to
make relative motion visible; they do not represent literal fluid
parcels circulating around the canal.
</p>
</div>
<div class="physiology">
<span class="tag">Full vestibular response</span>
<p>
A real canal senses rotation through endolymph flow relative to the
canal wall, cupula deflection, hair-cell transduction, nerve firing,
and central processing across multiple canals and otolith organs.
</p>
</div>
</div>
</section>
<section class="card controls" aria-label="Animation controls">
<div class="status-row">
<span id="motionStatus">Hold · baseline</span>
<span style="color: var(--muted); font-size: 10px;">relative lag</span>
</div>
<div class="lag-meter"><div id="lagFill"></div></div>
<div class="readouts">
<div class="readout">
<span>Head θ</span>
<b id="thetaValue">0.0°</b>
</div>
<div class="readout">
<span>Fluid φ</span>
<b id="phiValue" style="color: var(--cyan);">0.0°</b>
</div>
<div class="readout">
<span>Lag</span>
<b id="lagValue" style="color: var(--amber);">0.0°</b>
</div>
</div>
<div class="button-row">
<button id="pauseButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay turn</button>
</div>
<div class="speed-control">
<span>Speed</span>
<input id="speedInput" type="range" min="0.45" max="1.5" step="0.05" value="1" />
<span id="speedValue">1.00×</span>
</div>
<div class="button-row" style="margin-top: 8px;">
<button id="cameraButton" type="button">Reset camera</button>
<button id="viewButton" type="button">Side view</button>
</div>
<div class="camera-help">Drag to orbit · wheel/pinch to zoom</div>
</section>
<div id="canalLabel" class="scene-label">Transparent canal schematic</div>
<div id="tracerLabel" class="scene-label">Illustrative fluid tracer</div>
</div>
<div id="loading">Building the cutaway…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const loading = document.getElementById("loading");
const ui = {
status: document.getElementById("motionStatus"),
theta: document.getElementById("thetaValue"),
phi: document.getElementById("phiValue"),
lag: document.getElementById("lagValue"),
lagFill: document.getElementById("lagFill"),
pause: document.getElementById("pauseButton"),
replay: document.getElementById("replayButton"),
camera: document.getElementById("cameraButton"),
view: document.getElementById("viewButton"),
speed: document.getElementById("speedInput"),
speedValue: document.getElementById("speedValue"),
thetaPath: document.getElementById("thetaPath"),
phiPath: document.getElementById("phiPath"),
canalLabel: document.getElementById("canalLabel"),
tracerLabel: document.getElementById("tracerLabel")
};
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
loading.textContent = "WebGL could not start in this browser.";
throw error;
}
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;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06101b);
scene.fog = new THREE.FogExp2(0x06101b, 0.035);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
yaw: 0.28,
pitch: 0.13,
distance: 6.35,
target: new THREE.Vector3(0.05, -0.05, 0)
};
const ambient = new THREE.HemisphereLight(0xb9f4ff, 0x102333, 2.15);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xd9faff, 3.2);
keyLight.position.set(4, 6, 6);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3ac9e0, 2.4);
rimLight.position.set(-4, 2, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffc25c, 12, 7, 2);
warmLight.position.set(-0.2, -0.1, 2.2);
scene.add(warmLight);
const floor = new THREE.GridHelper(18, 36, 0x285362, 0x142c39);
floor.position.y = -1.72;
floor.material.transparent = true;
floor.material.opacity = 0.26;
scene.add(floor);
const platform = new THREE.Mesh(
new THREE.RingGeometry(1.82, 1.86, 96),
new THREE.MeshBasicMaterial({
color: 0x6de7ef,
transparent: true,
opacity: 0.22,
side: THREE.DoubleSide
})
);
platform.rotation.x = -Math.PI / 2;
platform.position.y = -1.705;
scene.add(platform);
const head = new THREE.Group();
head.rotation.order = "YXZ";
scene.add(head);
// Stylized profile-shaped cutaway shell.
const profile = new THREE.Shape();
profile.moveTo(-1.18, -1.31);
profile.bezierCurveTo(-0.86, -1.55, 0.38, -1.54, 0.98, -1.24);
profile.bezierCurveTo(1.27, -1.03, 1.34, -0.62, 1.22, -0.25);
profile.bezierCurveTo(1.55, -0.13, 1.62, 0.03, 1.48, 0.20);
profile.bezierCurveTo(1.39, 0.31, 1.30, 0.38, 1.18, 0.42);
profile.bezierCurveTo(1.28, 0.64, 1.13, 0.96, 0.83, 1.14);
profile.bezierCurveTo(0.50, 1.38, 0.20, 1.52, -0.20, 1.50);
profile.bezierCurveTo(-0.69, 1.48, -1.05, 1.18, -1.17, 0.72);
profile.bezierCurveTo(-1.29, 0.29, -1.30, -0.15, -1.25, -0.55);
profile.bezierCurveTo(-1.22, -0.84, -1.23, -1.10, -1.18, -1.31);
const shellGeometry = new THREE.ExtrudeGeometry(profile, {
depth: 0.72,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: 0.045,
bevelThickness: 0.045,
curveSegments: 36
});
shellGeometry.translate(0, 0, -0.36);
const shell = new THREE.Mesh(
shellGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x9ad4df,
transparent: true,
opacity: 0.115,
roughness: 0.42,
metalness: 0,
clearcoat: 0.45,
side: THREE.DoubleSide,
depthWrite: false
})
);
shell.renderOrder = 2;
head.add(shell);
const shellEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(shellGeometry, 24),
new THREE.LineBasicMaterial({
color: 0xb5e8ef,
transparent: true,
opacity: 0.33
})
);
shellEdges.renderOrder = 3;
head.add(shellEdges);
// Semi-transparent interior forms make the scene read as a cutaway.
function addBrainLobe(position, scale, color, opacity) {
const lobe = new THREE.Mesh(
new THREE.SphereGeometry(1, 36, 24),
new THREE.MeshStandardMaterial({
color,
transparent: true,
opacity,
roughness: 0.9,
depthWrite: false
})
);
lobe.position.copy(position);
lobe.scale.copy(scale);
lobe.renderOrder = 0;
head.add(lobe);
return lobe;
}
addBrainLobe(
new THREE.Vector3(-0.22, 0.26, -0.18),
new THREE.Vector3(0.72, 0.77, 0.37),
0xd98698,
0.11
);
addBrainLobe(
new THREE.Vector3(-0.50, 0.10, -0.20),
new THREE.Vector3(0.42, 0.55, 0.33),
0xe899a8,
0.095
);
addBrainLobe(
new THREE.Vector3(0.18, 0.34, -0.21),
new THREE.Vector3(0.42, 0.56, 0.31),
0xc9798d,
0.09
);
addBrainLobe(
new THREE.Vector3(-0.72, -0.46, -0.17),
new THREE.Vector3(0.34, 0.30, 0.28),
0xb96983,
0.105
);
// A small eye helps orient the profile.
const eye = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 24, 16),
new THREE.MeshStandardMaterial({
color: 0x102832,
roughness: 0.22,
metalness: 0.08
})
);
eye.position.set(0.79, 0.61, 0.43);
eye.scale.z = 0.45;
head.add(eye);
const eyeRing = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.012, 8, 48),
new THREE.MeshBasicMaterial({
color: 0xc0edf0,
transparent: true,
opacity: 0.34
})
);
eyeRing.position.set(0.79, 0.61, 0.435);
eyeRing.scale.y = 0.72;
head.add(eyeRing);
// Schematic external ear.
const earPoints = [];
for (let i = 0; i <= 64; i++) {
const a = -Math.PI * 0.72 + (i / 64) * Math.PI * 1.55;
earPoints.push(
new THREE.Vector3(
-0.88 + Math.cos(a) * 0.12,
0.02 + Math.sin(a) * 0.27,
0.365
)
);
}
const earCurve = new THREE.CatmullRomCurve3(earPoints);
const ear = new THREE.Mesh(
new THREE.TubeGeometry(earCurve, 64, 0.019, 8, false),
new THREE.MeshBasicMaterial({
color: 0x94dce5,
transparent: true,
opacity: 0.38
})
);
head.add(ear);
// Inner-ear assembly.
const inner = new THREE.Group();
inner.position.set(-0.23, 0.02, 0.35);
head.add(inner);
const canalRadius = 0.67;
const canal = new THREE.Mesh(
new THREE.TorusGeometry(canalRadius, 0.092, 28, 144),
new THREE.MeshPhysicalMaterial({
color: 0x6de7ef,
transparent: true,
opacity: 0.31,
roughness: 0.12,
metalness: 0.02,
clearcoat: 1,
clearcoatRoughness: 0.16,
side: THREE.DoubleSide,
depthWrite: false
})
);
canal.renderOrder = 5;
inner.add(canal);
const canalCore = new THREE.Mesh(
new THREE.TorusGeometry(canalRadius, 0.021, 10, 144),
new THREE.MeshBasicMaterial({
color: 0xa7f7fa,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
canalCore.renderOrder = 6;
inner.add(canalCore);
// A schematic cochlea and nerve provide anatomical orientation.
const cochleaPoints = [];
for (let i = 0; i <= 120; i++) {
const u = i / 120;
const a = u * Math.PI * 4.5;
const r = 0.29 * (1 - 0.73 * u);
cochleaPoints.push(
new THREE.Vector3(
0.77 + Math.cos(a) * r,
-0.43 + Math.sin(a) * r,
-0.015
)
);
}
const cochlea = new THREE.Mesh(
new THREE.TubeGeometry(
new THREE.CatmullRomCurve3(cochleaPoints),
160,
0.025,
8,
false
),
new THREE.MeshStandardMaterial({
color: 0xffc65a,
emissive: 0x8b4c08,
emissiveIntensity: 0.28,
roughness: 0.44,
transparent: true,
opacity: 0.88
})
);
inner.add(cochlea);
const nerve = new THREE.Mesh(
new THREE.TubeGeometry(
new THREE.CatmullRomCurve3([
new THREE.Vector3(0.02, -0.57, -0.015),
new THREE.Vector3(-0.02, -0.83, -0.035),
new THREE.Vector3(0.00, -1.18, -0.06),
new THREE.Vector3(-0.10, -1.43, -0.08)
]),
60,
0.035,
10,
false
),
new THREE.MeshStandardMaterial({
color: 0xffd16c,
emissive: 0x7d4d08,
emissiveIntensity: 0.35,
roughness: 0.48
})
);
inner.add(nerve);
// Unevenly spaced markers make phase rotation visible.
const markerDefinitions = [
{ s: 0.10, size: 0.108, color: 0xffc65a, main: true },
{ s: 0.38, size: 0.052, color: 0x72edf2 },
{ s: 0.83, size: 0.064, color: 0x72edf2 },
{ s: 1.22, size: 0.046, color: 0x9cf7fa },
{ s: 1.71, size: 0.058, color: 0x72edf2 },
{ s: 2.08, size: 0.048, color: 0x9cf7fa },
{ s: 2.53, size: 0.066, color: 0x72edf2 },
{ s: 2.91, size: 0.043, color: 0x9cf7fa },
{ s: 3.38, size: 0.055, color: 0x72edf2 },
{ s: 3.75, size: 0.047, color: 0x9cf7fa },
{ s: 4.16, size: 0.062, color: 0x72edf2 },
{ s: 4.58, size: 0.045, color: 0x9cf7fa },
{ s: 5.06, size: 0.057, color: 0x72edf2 },
{ s: 5.46, size: 0.049, color: 0x9cf7fa },
{ s: 5.83, size: 0.061, color: 0x72edf2 }
];
const markers = markerDefinitions.map((definition) => {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(1, 22, 16),
new THREE.MeshStandardMaterial({
color: definition.color,
emissive: definition.color,
emissiveIntensity: definition.main ? 1.35 : 0.78,
roughness: 0.25,
metalness: 0.02
})
);
mesh.scale.setScalar(definition.size);
mesh.renderOrder = 7;
inner.add(mesh);
return { ...definition, mesh };
});
const mainMarker = markers.find((marker) => marker.main);
// Amber arc shows slip between the tracer and its head-attached reference.
const slipGeometry = new THREE.BufferGeometry();
slipGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(41 * 3), 3)
);
const slipLine = new THREE.Line(
slipGeometry,
new THREE.LineBasicMaterial({
color: 0xffc65a,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
slipLine.renderOrder = 9;
inner.add(slipLine);
const slipArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.042, 0.13, 14),
new THREE.MeshBasicMaterial({
color: 0xffc65a,
depthTest: false
})
);
slipArrow.renderOrder = 9;
inner.add(slipArrow);
const canalAnchor = new THREE.Object3D();
canalAnchor.position.set(0, canalRadius + 0.28, 0);
inner.add(canalAnchor);
const tracerAnchor = new THREE.Object3D();
inner.add(tracerAnchor);
// Vertical yaw axis and an overhead rotation cue.
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -1.65, 0),
new THREE.Vector3(0, 2.03, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x75dce7,
dashSize: 0.075,
gapSize: 0.055,
transparent: true,
opacity: 0.35
})
);
axis.computeLineDistances();
scene.add(axis);
const arcPoints = [];
const arcRadius = 1.88;
const arcEnd = 1.28;
for (let i = 0; i <= 80; i++) {
const a = (i / 80) * arcEnd;
arcPoints.push(
new THREE.Vector3(
Math.sin(a) * arcRadius,
1.73,
Math.cos(a) * arcRadius
)
);
}
const rotationArc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(arcPoints),
new THREE.LineBasicMaterial({
color: 0xffc65a,
transparent: true,
opacity: 0.46
})
);
scene.add(rotationArc);
const arcArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.24, 16),
new THREE.MeshBasicMaterial({ color: 0xffc65a })
);
const arcTangent = new THREE.Vector3(
Math.cos(arcEnd),
0,
-Math.sin(arcEnd)
).normalize();
arcArrow.position.copy(arcPoints[arcPoints.length - 1]);
arcArrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
arcTangent
);
scene.add(arcArrow);
const axisLabelAnchor = new THREE.Vector3(0, 2.08, 0);
// Model state.
let paused = false;
let speed = 1;
let simulationTime = 0;
let theta = 0;
let phi = 0;
let phiVelocity = 0;
let currentStatus = "Hold · baseline";
const omega = 6.2;
const dampingRatio = 0.88;
const maximumAngle = THREE.MathUtils.degToRad(48);
const cycleDuration = 10;
const history = [];
function smoothstep(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function motionAt(time) {
const phase = ((time % cycleDuration) + cycleDuration) % cycleDuration;
if (phase < 1.2) {
return { angle: 0, status: "Hold · baseline" };
}
if (phase < 3.2) {
const u = smoothstep((phase - 1.2) / 2);
return { angle: maximumAngle * u, status: "Turn right" };
}
if (phase < 4.8) {
return { angle: maximumAngle, status: "Hold · catch-up" };
}
if (phase < 6.8) {
const u = smoothstep((phase - 4.8) / 2);
return {
angle: maximumAngle * (1 - u),
status: "Return left"
};
}
return { angle: 0, status: "Hold · settling" };
}
function resetAnimation() {
simulationTime = 0;
theta = 0;
phi = 0;
phiVelocity = 0;
history.length = 0;
currentStatus = "Hold · baseline";
}
function updateSimulation(realDelta) {
const scaledDelta = realDelta * speed;
simulationTime += scaledDelta;
const motion = motionAt(simulationTime);
theta = motion.angle;
currentStatus = motion.status;
// Stable small substeps for the illustrative second-order phase model.
const steps = Math.max(1, Math.ceil(scaledDelta / 0.007));
const h = scaledDelta / steps;
for (let i = 0; i < steps; i++) {
const phiAcceleration =
omega * omega * (theta - phi) -
2 * dampingRatio * omega * phiVelocity;
phiVelocity += phiAcceleration * h;
phi += phiVelocity * h;
}
history.push([theta, phi]);
if (history.length > 180) history.shift();
}
function updateMarkers() {
head.rotation.y = theta;
// In head-local coordinates, a fluid tracer's angle is
// s + (phi - theta). The yaw of the head then gives world phase s + phi.
const relativeLag = phi - theta;
for (const marker of markers) {
const localAngle = marker.s + relativeLag;
marker.mesh.position.set(
Math.cos(localAngle) * canalRadius,
Math.sin(localAngle) * canalRadius,
0.015
);
if (marker.main) {
const pulse = 1 + 0.045 * Math.sin(simulationTime * 5.0);
marker.mesh.scale.setScalar(marker.size * pulse);
}
}
const marker = mainMarker;
const actualAngle = marker.s + relativeLag;
const nominalAngle = marker.s;
const positions = slipGeometry.attributes.position;
for (let i = 0; i <= 40; i++) {
const u = i / 40;
const a = THREE.MathUtils.lerp(actualAngle, nominalAngle, u);
positions.setXYZ(
i,
Math.cos(a) * canalRadius,
Math.sin(a) * canalRadius,
0.055
);
}
positions.needsUpdate = true;
slipGeometry.computeBoundingSphere();
slipArrow.position.set(
Math.cos(nominalAngle) * canalRadius,
Math.sin(nominalAngle) * canalRadius,
0.055
);
const tangent = new THREE.Vector3(
-Math.sin(nominalAngle),
Math.cos(nominalAngle),
0
).normalize();
slipArrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
tracerAnchor.position.copy(mainMarker.mesh.position);
}
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
camera.position.set(
target.x + Math.sin(yaw) * Math.cos(pitch) * distance,
target.y + Math.sin(pitch) * distance,
target.z + Math.cos(yaw) * Math.cos(pitch) * distance
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
const tempWorld = new THREE.Vector3();
function updateLabels() {
canalAnchor.getWorldPosition(tempWorld);
placeLabel(ui.canalLabel, tempWorld);
tracerAnchor.getWorldPosition(tempWorld);
placeLabel(ui.tracerLabel, tempWorld);
}
function makeTracePath(index) {
if (history.length < 2) return "";
const maximum = THREE.MathUtils.degToRad(60);
return history
.map((sample, i) => {
const x = 4 + (i / (history.length - 1)) * 252;
const y = 32 - (sample[index] / maximum) * 25;
return `${i === 0 ? "M" : "L"}${x.toFixed(1)},${y.toFixed(1)}`;
})
.join(" ");
}
function updateReadouts() {
const thetaDegrees = THREE.MathUtils.radToDeg(theta);
const phiDegrees = THREE.MathUtils.radToDeg(phi);
const lagDegrees = Math.abs(thetaDegrees - phiDegrees);
ui.status.textContent = currentStatus;
ui.theta.textContent = `${thetaDegrees.toFixed(1)}°`;
ui.phi.textContent = `${phiDegrees.toFixed(1)}°`;
ui.lag.textContent = `${lagDegrees.toFixed(1)}°`;
ui.lagFill.style.width = `${Math.min(100, (lagDegrees / 18) * 100)}%`;
ui.thetaPath.setAttribute("d", makeTracePath(0));
ui.phiPath.setAttribute("d", makeTracePath(1));
}
// Direct camera controls; no examples/addons are imported.
let dragging = false;
let previousX = 0;
let previousY = 0;
let pointerDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
pointerDistance = 0;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
pointerDistance += Math.abs(dx) + Math.abs(dy);
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
-0.22,
0.78
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.0045,
4.2,
9.2
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
cameraState.yaw = 0.28;
cameraState.pitch = 0.13;
cameraState.distance = 6.35;
});
ui.pause.addEventListener("click", () => {
paused = !paused;
ui.pause.textContent = paused ? "Resume" : "Pause";
});
ui.replay.addEventListener("click", resetAnimation);
ui.camera.addEventListener("click", () => {
cameraState.yaw = 0.28;
cameraState.pitch = 0.13;
cameraState.distance = 6.35;
});
ui.view.addEventListener("click", () => {
cameraState.yaw = 0.02;
cameraState.pitch = 0.08;
cameraState.distance = 6.15;
});
ui.speed.addEventListener("input", () => {
speed = Number(ui.speed.value);
ui.speedValue.textContent = `${speed.toFixed(2)}×`;
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) updateSimulation(delta);
updateMarkers();
updateCamera();
scene.updateMatrixWorld(true);
updateLabels();
updateReadouts();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
window.setTimeout(() => loading.remove(), 500);
}
}
resetAnimation();
updateMarkers();
updateCamera();
animate();
</script>
</body>
</html>
```