Prompt specification
Build or customize the Three.js 3D scene "Diffusion and convection tea exhibit" 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: Diffusion and convection tea exhibit
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/diffusion-and-convection-tea-exhibit-x0160
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing two enlarged low-poly glass cups on a tea demonstration table. Both begin with the same small cluster of colored tracer particles near a submerged tea sachet. In the left cup, use a simplified diffusion process; in the right, add an explicitly prescribed circulating flow; make the second cup's circulation visible through particle motion rather than an unrelated spinning arrow; provide a shared time control and matching initial tracer counts. Label the displays as simplified transport models, with no claim that their particle speeds or temperature fields reproduce real tea brewing. Keep the cup walls subtle enough to inspect the spreading patterns. Keep both sachets stationary so circulation of the tracer cloud, rather than movement of its source, produces the visible difference.
Working Three.js Starter (diffusion-and-convection-tea-exhibit-x0160):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tea Transport: Diffusion vs. Prescribed Circulation</title>
<style>
:root {
color-scheme: light;
--ink: #26312f;
--muted: #66716d;
--paper: rgba(250, 248, 242, 0.92);
--line: rgba(38, 49, 47, 0.16);
--coral: #e56555;
--teal: #168f88;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e2d7;
color: var(--ink);
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;
}
.title-card {
position: fixed;
z-index: 5;
top: 18px;
left: 20px;
max-width: 440px;
padding: 14px 17px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--paper);
box-shadow: 0 10px 30px rgba(55, 48, 38, 0.08);
pointer-events: none;
}
.eyebrow {
margin-bottom: 4px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(19px, 2.2vw, 28px);
font-weight: 760;
letter-spacing: -0.025em;
}
.subtitle {
margin: 6px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.model-note {
position: fixed;
z-index: 5;
top: 20px;
right: 20px;
width: min(315px, calc(100vw - 40px));
padding: 11px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(250, 248, 242, 0.86);
color: #53605c;
font-size: 11px;
line-height: 1.45;
pointer-events: none;
}
.model-note strong {
color: var(--ink);
}
#controls {
position: fixed;
z-index: 10;
left: 50%;
bottom: 14px;
width: min(900px, calc(100vw - 28px));
transform: translateX(-50%);
padding: 12px 14px 10px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(250, 248, 242, 0.94);
box-shadow: 0 14px 40px rgba(55, 48, 38, 0.13);
backdrop-filter: blur(10px);
}
.control-row {
display: grid;
grid-template-columns: auto auto minmax(130px, 1fr) auto auto;
align-items: center;
gap: 10px;
}
button,
select {
min-height: 34px;
border: 1px solid rgba(38, 49, 47, 0.19);
border-radius: 8px;
background: #fffdf8;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
}
button {
padding: 0 13px;
cursor: pointer;
}
button:hover,
select:hover {
border-color: rgba(22, 143, 136, 0.55);
}
#play {
min-width: 75px;
border-color: var(--teal);
background: var(--teal);
color: white;
}
select {
padding: 0 28px 0 9px;
}
input[type="range"] {
width: 100%;
accent-color: var(--teal);
cursor: pointer;
}
.time-readout,
#status {
white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.time-readout {
min-width: 70px;
color: var(--muted);
font-size: 12px;
font-weight: 700;
}
#status {
color: var(--muted);
font-size: 11px;
}
.disclaimer {
margin: 8px 2px 0;
color: #6c746f;
font-size: 10.5px;
line-height: 1.4;
}
.camera-hint {
color: #78807c;
font-size: 10px;
font-weight: 500;
}
@media (max-width: 720px) {
.title-card {
top: 10px;
left: 10px;
max-width: calc(100vw - 20px);
padding: 10px 12px;
}
.model-note {
display: none;
}
#controls {
bottom: 8px;
width: calc(100vw - 16px);
}
.control-row {
grid-template-columns: auto auto 1fr auto;
}
.camera-hint,
#status {
display: none;
}
.subtitle {
font-size: 11px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="title-card">
<div class="eyebrow">Tea transport demonstration</div>
<h1>One release, two transport treatments</h1>
<p class="subtitle">
Identical colored tracer clusters, identical counts, stationary sachets,
and one shared simulation clock.
</p>
</header>
<aside class="model-note">
<strong>Flow is shown only by tracer motion.</strong>
The right-hand model adds an explicitly prescribed toroidal velocity field;
there is no decorative or unrelated spinning arrow.
</aside>
<section id="controls" aria-label="Shared simulation controls">
<div class="control-row">
<button id="play" type="button">Pause</button>
<button id="restart" type="button">Restart</button>
<input
id="timeline"
type="range"
min="0"
max="24"
step="0.05"
value="0"
aria-label="Shared simulation time"
>
<output id="time" class="time-readout">0.00 s</output>
<select id="speed" aria-label="Shared playback speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="1.5">1.5×</option>
<option value="2">2×</option>
</select>
</div>
<div class="control-row" style="grid-template-columns: 1fr auto; margin-top: 6px">
<div id="status">320 tracers per cup · identical initial cluster</div>
<div class="camera-hint">Drag to orbit · wheel to zoom · double-click to reset view</div>
</div>
<p class="disclaimer">
<strong>Simplified transport models only.</strong>
Particle speeds are illustrative rather than physical predictions.
No temperature field is shown or claimed to reproduce real tea brewing.
</p>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera, and direct camera controls
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e2d7);
scene.fog = new THREE.Fog(0xe9e2d7, 14, 25);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraView = {
yaw: 0,
pitch: 0.36,
distance: 11.4,
target: new THREE.Vector3(0, 1.32, 0)
};
function updateCamera() {
const portraitScale = camera.aspect < 1 ? 1.38 : 1;
const distance = cameraView.distance * portraitScale;
const horizontalDistance =
distance * Math.cos(cameraView.pitch);
camera.position.set(
cameraView.target.x +
Math.sin(cameraView.yaw) * horizontalDistance,
cameraView.target.y +
Math.sin(cameraView.pitch) * distance,
cameraView.target.z +
Math.cos(cameraView.yaw) * horizontalDistance
);
camera.lookAt(cameraView.target);
}
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraView.yaw = THREE.MathUtils.clamp(
cameraView.yaw - dx * 0.005,
-0.7,
0.7
);
cameraView.pitch = THREE.MathUtils.clamp(
cameraView.pitch + dy * 0.004,
0.12,
0.92
);
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraView.distance = THREE.MathUtils.clamp(
cameraView.distance + event.deltaY * 0.009,
8.2,
16.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraView.yaw = 0;
cameraView.pitch = 0.36;
cameraView.distance = 11.4;
});
// -------------------------------------------------------------------------
// Lighting and demonstration table
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfff8e8, 0x766f66, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.1);
keyLight.position.set(-4.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xd8eeff, 1.1);
fillLight.position.set(6, 4, -4);
scene.add(fillLight);
const tableMaterial = new THREE.MeshStandardMaterial({
color: 0x9a6845,
roughness: 0.83,
metalness: 0.02
});
const table = new THREE.Mesh(
new THREE.BoxGeometry(11.6, 0.30, 6.0),
tableMaterial
);
table.position.y = -0.08;
table.receiveShadow = true;
scene.add(table);
const tableEdge = new THREE.Mesh(
new THREE.BoxGeometry(11.65, 0.12, 6.05),
new THREE.MeshStandardMaterial({
color: 0x70472f,
roughness: 0.9
})
);
tableEdge.position.y = -0.24;
scene.add(tableEdge);
const runner = new THREE.Mesh(
new THREE.BoxGeometry(10.25, 0.025, 3.55),
new THREE.MeshStandardMaterial({
color: 0x526d67,
roughness: 0.95
})
);
runner.position.set(0, 0.087, 0.05);
runner.receiveShadow = true;
scene.add(runner);
// Small center placard reinforces the demonstration-table setting.
function makePlacardTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 220;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#eee5d3";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#887b65";
ctx.lineWidth = 5;
ctx.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
ctx.fillStyle = "#354a45";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "700 48px sans-serif";
ctx.fillText("TEA TRANSPORT DEMONSTRATION", 512, 83);
ctx.fillStyle = "#796f60";
ctx.font = "28px sans-serif";
ctx.fillText("stationary source · shared tracer count · shared clock", 512, 145);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const placard = new THREE.Mesh(
new THREE.PlaneGeometry(3.65, 0.78),
new THREE.MeshStandardMaterial({
map: makePlacardTexture(),
roughness: 0.95,
side: THREE.DoubleSide
})
);
placard.rotation.x = -Math.PI / 2;
placard.position.set(0, 0.112, 2.05);
scene.add(placard);
// -------------------------------------------------------------------------
// Shared simulation constants
// -------------------------------------------------------------------------
const PARTICLE_COUNT = 320;
const DT = 1 / 30;
const MAX_TIME = 24;
const MAX_STEPS = Math.round(MAX_TIME / DT);
const WATER_MIN_Y = 0.44;
const WATER_MAX_Y = 2.10;
const WATER_MAX_RADIUS = 1.18;
const SACHET_CENTER = new THREE.Vector3(-0.55, 0.68, -0.045);
const INITIAL_CENTER = new THREE.Vector3(-0.55, 0.72, 0.18);
const DIFFUSION_SCALE = 0.25;
const INITIAL_SEED = 0x51a7c0de;
const STEP_SEED = 0x2f61a4d3;
function mulberry32(seed) {
let state = seed >>> 0;
return function random() {
state += 0x6d2b79f5;
let value = state;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const initialPositions = new Float32Array(PARTICLE_COUNT * 3);
const initialScales = new Float32Array(PARTICLE_COUNT);
const leftPositions = new Float32Array(PARTICLE_COUNT * 3);
const rightPositions = new Float32Array(PARTICLE_COUNT * 3);
const tracerPalette = [
0xe56555,
0x18a69d,
0xf1bd3f,
0x7868d7
];
(() => {
const random = mulberry32(INITIAL_SEED);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const index = i * 3;
// A small spherical cluster immediately in front of the sachet.
const radius = 0.135 * Math.cbrt(random());
const theta = random() * Math.PI * 2;
const phi = Math.acos(2 * random() - 1);
const dx =
radius * Math.sin(phi) * Math.cos(theta);
const dy =
radius * Math.cos(phi) * 0.82;
const dz =
radius * Math.sin(phi) * Math.sin(theta);
initialPositions[index] = INITIAL_CENTER.x + dx;
initialPositions[index + 1] = INITIAL_CENTER.y + dy;
initialPositions[index + 2] = INITIAL_CENTER.z + dz;
initialScales[i] = 0.72 + random() * 0.60;
}
})();
// -------------------------------------------------------------------------
// Labels and cup construction
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function makeLabelSprite(title, subtitle, accent) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 260;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 1008, 244, 24);
ctx.fillStyle = "rgba(250,248,242,0.94)";
ctx.fill();
ctx.strokeStyle = "rgba(38,49,47,0.18)";
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = accent;
roundedRect(ctx, 34, 34, 10, 192, 5);
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#26312f";
ctx.font = "800 45px sans-serif";
ctx.fillText(title, 520, 83);
ctx.fillStyle = "#68736e";
ctx.font = "27px sans-serif";
ctx.fillText(subtitle, 520, 151);
ctx.fillStyle = accent;
ctx.font = "700 22px sans-serif";
ctx.fillText("SIMPLIFIED TRANSPORT MODEL", 520, 205);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(4.25, 4.25 * 260 / 1024, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeTeaLabelTexture() {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#d9bc7d";
ctx.fillRect(0, 0, 256, 128);
ctx.strokeStyle = "#6c542e";
ctx.lineWidth = 6;
ctx.strokeRect(8, 8, 240, 112);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#4f3b1f";
ctx.font = "800 39px sans-serif";
ctx.fillText("TEA", 128, 54);
ctx.font = "19px sans-serif";
ctx.fillText("SACHET", 128, 91);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const teaLabelTexture = makeTeaLabelTexture();
const cupGlassMaterial = new THREE.MeshStandardMaterial({
color: 0xc9e4df,
roughness: 0.24,
metalness: 0.02,
transparent: true,
opacity: 0.17,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
});
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xd7a95d,
roughness: 0.5,
transparent: true,
opacity: 0.055,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
});
const waterSurfaceMaterial = new THREE.MeshStandardMaterial({
color: 0xe5b96f,
roughness: 0.38,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xb8d6d0,
roughness: 0.3,
transparent: true,
opacity: 0.4,
depthWrite: false
});
const sachetMaterial = new THREE.MeshStandardMaterial({
color: 0xc7a46a,
roughness: 0.93,
flatShading: true
});
const particleGeometry = new THREE.IcosahedronGeometry(0.042, 0);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.48,
metalness: 0.02
});
function makeCup(x, accent, labelTitle, labelSubtitle) {
const group = new THREE.Group();
group.position.set(x, 0.14, 0);
scene.add(group);
const saucer = new THREE.Mesh(
new THREE.CylinderGeometry(1.73, 1.62, 0.13, 24),
new THREE.MeshStandardMaterial({
color: 0xe9e1cf,
roughness: 0.72,
flatShading: true
})
);
saucer.position.y = 0.02;
saucer.receiveShadow = true;
group.add(saucer);
const saucerEdge = new THREE.Mesh(
new THREE.TorusGeometry(1.70, 0.035, 5, 24),
new THREE.MeshStandardMaterial({
color: 0xcabca3,
roughness: 0.8,
flatShading: true
})
);
saucerEdge.rotation.x = Math.PI / 2;
saucerEdge.position.y = 0.075;
group.add(saucerEdge);
const cupBottom = new THREE.Mesh(
new THREE.CylinderGeometry(1.32, 1.32, 0.08, 20),
cupGlassMaterial.clone()
);
cupBottom.material.opacity = 0.2;
cupBottom.position.y = 0.09;
group.add(cupBottom);
// Low-poly, open glass wall.
const wallGeometry = new THREE.CylinderGeometry(
1.48,
1.31,
2.50,
20,
1,
true
);
const wall = new THREE.Mesh(wallGeometry, cupGlassMaterial);
wall.position.y = 1.36;
wall.renderOrder = 3;
group.add(wall);
const wallEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(wallGeometry, 12),
new THREE.LineBasicMaterial({
color: 0x8fb8b1,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
wallEdges.position.copy(wall.position);
wallEdges.renderOrder = 4;
group.add(wallEdges);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(1.48, 0.025, 5, 24),
rimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 2.61;
rim.renderOrder = 4;
group.add(rim);
// A faint water volume keeps the glass readable without hiding tracers.
const water = new THREE.Mesh(
new THREE.CylinderGeometry(1.37, 1.27, 1.72, 20, 1, true),
waterMaterial
);
water.position.y = 1.30;
water.renderOrder = 1;
group.add(water);
const waterSurface = new THREE.Mesh(
new THREE.CircleGeometry(1.37, 20),
waterSurfaceMaterial
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = 2.16;
waterSurface.renderOrder = 2;
group.add(waterSurface);
const waterline = new THREE.Mesh(
new THREE.TorusGeometry(1.37, 0.013, 4, 24),
new THREE.MeshBasicMaterial({
color: 0xc79345,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
waterline.rotation.x = Math.PI / 2;
waterline.position.y = 2.16;
waterline.renderOrder = 2;
group.add(waterline);
// Stationary tea sachet. It is intentionally never animated or advected.
const sachet = new THREE.Group();
sachet.position.copy(SACHET_CENTER);
sachet.rotation.y = 0.16;
sachet.rotation.z = -0.08;
group.add(sachet);
const packet = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.36, 0.14),
sachetMaterial
);
packet.castShadow = true;
sachet.add(packet);
const packetEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(packet.geometry),
new THREE.LineBasicMaterial({
color: 0x72542c,
transparent: true,
opacity: 0.58
})
);
sachet.add(packetEdges);
const teaLabel = new THREE.Mesh(
new THREE.PlaneGeometry(0.31, 0.155),
new THREE.MeshBasicMaterial({
map: teaLabelTexture,
side: THREE.DoubleSide
})
);
teaLabel.position.z = 0.071;
sachet.add(teaLabel);
// A stationary tether also makes the source's lack of motion explicit.
const stringGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.53, 0.84, -0.035),
new THREE.Vector3(-0.46, 1.45, -0.03),
new THREE.Vector3(-0.39, 2.60, -0.025)
]);
const string = new THREE.Line(
stringGeometry,
new THREE.LineBasicMaterial({
color: 0xd9cbaa,
transparent: true,
opacity: 0.72
})
);
group.add(string);
const particles = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
PARTICLE_COUNT
);
particles.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particles.frustumCulled = false;
for (let i = 0; i < PARTICLE_COUNT; i++) {
particles.setColorAt(
i,
new THREE.Color(tracerPalette[i % tracerPalette.length])
);
}
particles.instanceColor.needsUpdate = true;
group.add(particles);
const label = makeLabelSprite(
labelTitle,
labelSubtitle,
accent
);
label.position.set(0, 3.08, 0);
group.add(label);
return { group, particles };
}
const leftCup = makeCup(
-3.08,
"#e56555",
"LEFT · SIMPLIFIED DIFFUSION",
"random-walk spreading · stationary sachet"
);
const rightCup = makeCup(
3.08,
"#168f88",
"RIGHT · DIFFUSION + CIRCULATION",
"prescribed toroidal flow · stationary sachet"
);
// -------------------------------------------------------------------------
// Transport models
//
// Left: random-walk diffusion only.
// Right: exactly the same random-walk increments PLUS a prescribed
// toroidal/axisymmetric velocity field.
//
// The source sachet is not part of either particle update.
// -------------------------------------------------------------------------
let simulationStep = 0;
let stepRandom = mulberry32(STEP_SEED);
let accumulator = 0;
const flowResult = new THREE.Vector3();
function confineToWater(array, index) {
let x = array[index];
let y = array[index + 1];
let z = array[index + 2];
if (y < WATER_MIN_Y) {
y = WATER_MIN_Y + (WATER_MIN_Y - y);
} else if (y > WATER_MAX_Y) {
y = WATER_MAX_Y - (y - WATER_MAX_Y);
}
const radius = Math.hypot(x, z);
if (radius > WATER_MAX_RADIUS) {
const scale = WATER_MAX_RADIUS / radius;
x *= scale;
z *= scale;
}
array[index] = x;
array[index + 1] = y;
array[index + 2] = z;
}
/*
Explicitly prescribed right-cup velocity field.
It combines:
1. A toroidal recirculation cell in the radial/vertical plane.
2. An azimuthal component that bends the flow into a visibly
three-dimensional circulating trajectory.
There is no arrow or unrelated rotating graphic; this field acts
directly on the colored tracer particles.
*/
function prescribedFlow(x, y, z, output) {
const radius = Math.max(Math.hypot(x, z), 0.0001);
const nx = x / radius;
const nz = z / radius;
const majorRadius = 0.62;
const centerHeight = 1.05;
const minorScale = 0.66;
const radialOffset = radius - majorRadius;
const verticalOffset = y - centerHeight;
const toroidalWeight = Math.exp(
-1.25 *
(radialOffset * radialOffset + verticalOffset * verticalOffset) /
(minorScale * minorScale)
);
const radialVelocity = -1.15 * verticalOffset * toroidalWeight;
const verticalVelocity = 1.15 * radialOffset * toroidalWeight;
const azimuthalVelocity =
1.05 *
radius *
Math.exp(
-(radialOffset * radialOffset) / 0.75 -
(verticalOffset * verticalOffset) / 1.25
);
output.set(
radialVelocity * nx - azimuthalVelocity * nz,
verticalVelocity,
radialVelocity * nz + azimuthalVelocity * nx
);
}
function stepParticles() {
const noiseScale = DIFFUSION_SCALE * Math.sqrt(DT);
for (let i = 0; i < PARTICLE_COUNT; i++) {
const index = i * 3;
// The same noise sequence is applied to both cups.
const noiseX =
(stepRandom() - 0.5) * 2 * noiseScale;
const noiseY =
(stepRandom() - 0.5) * 2 * noiseScale;
const noiseZ =
(stepRandom() - 0.5) * 2 * noiseScale;
// Left: diffusion only.
leftPositions[index] = leftPositions[index] + noiseX;
leftPositions[index + 1] = leftPositions[index + 1] + noiseY;
leftPositions[index + 2] = leftPositions[index + 2] + noiseZ;
// Right: the same diffusion plus prescribed circulation.
prescribedFlow(
rightPositions[index],
rightPositions[index + 1],
rightPositions[index + 2],
flowResult
);
rightPositions[index] =
rightPositions[index] + noiseX + flowResult.x * DT;
rightPositions[index + 1] =
rightPositions[index + 1] + noiseY + flowResult.y * DT;
rightPositions[index + 2] =
rightPositions[index + 2] + noiseZ + flowResult.z * DT;
confineToWater(leftPositions, index);
confineToWater(rightPositions, index);
}
simulationStep++;
}
function resetSimulation() {
leftPositions.set(initialPositions);
rightPositions.set(initialPositions);
simulationStep = 0;
accumulator = 0;
stepRandom = mulberry32(STEP_SEED);
}
function simulateTo(time) {
const targetStep = THREE.MathUtils.clamp(
Math.round(time / DT),
0,
MAX_STEPS
);
if (targetStep < simulationStep) {
resetSimulation();
}
while (simulationStep < targetStep) {
stepParticles();
}
}
const dummy = new THREE.Object3D();
function updateParticleInstances() {
for (let i = 0; i < PARTICLE_COUNT; i++) {
const index = i * 3;
dummy.position.set(
leftPositions[index],
leftPositions[index + 1],
leftPositions[index + 2]
);
dummy.scale.setScalar(initialScales[i]);
dummy.updateMatrix();
leftCup.particles.setMatrixAt(i, dummy.matrix);
dummy.position.set(
rightPositions[index],
rightPositions[index + 1],
rightPositions[index + 2]
);
dummy.scale.setScalar(initialScales[i]);
dummy.updateMatrix();
rightCup.particles.setMatrixAt(i, dummy.matrix);
}
leftCup.particles.instanceMatrix.needsUpdate = true;
rightCup.particles.instanceMatrix.needsUpdate = true;
}
resetSimulation();
updateParticleInstances();
// -------------------------------------------------------------------------
// Shared controls
// -------------------------------------------------------------------------
const playButton = document.getElementById("play");
const restartButton = document.getElementById("restart");
const timeline = document.getElementById("timeline");
const timeOutput = document.getElementById("time");
const speedSelect = document.getElementById("speed");
const statusOutput = document.getElementById("status");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let playing = !reducedMotion;
function updateUI() {
const time = simulationStep * DT;
timeline.value = time.toFixed(2);
timeOutput.textContent = `${time.toFixed(2)} s`;
playButton.textContent = playing
? "Pause"
: simulationStep >= MAX_STEPS
? "Replay"
: "Play";
statusOutput.textContent =
`${PARTICLE_COUNT} tracers per cup · ` +
(simulationStep === 0
? "identical initial cluster"
: "shared simulated time");
}
playButton.addEventListener("click", () => {
if (simulationStep >= MAX_STEPS) {
resetSimulation();
playing = true;
} else {
playing = !playing;
}
accumulator = 0;
updateParticleInstances();
updateUI();
});
restartButton.addEventListener("click", () => {
resetSimulation();
playing = false;
updateParticleInstances();
updateUI();
});
timeline.addEventListener("input", () => {
playing = false;
simulateTo(Number(timeline.value));
updateParticleInstances();
updateUI();
});
speedSelect.addEventListener("change", () => {
accumulator = 0;
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const realDelta = Math.min((now - previousTime) / 1000, 0.1);
previousTime = now;
if (playing) {
accumulator +=
realDelta * Number(speedSelect.value);
while (accumulator >= DT) {
if (simulationStep >= MAX_STEPS) {
playing = false;
accumulator = 0;
break;
}
stepParticles();
accumulator -= DT;
}
updateParticleInstances();
}
updateUI();
updateCamera();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = camera.aspect < 1 ? 52 : 38;
camera.updateProjectionMatrix();
});
updateUI();
updateCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```