Prompt specification
Build or customize the Three.js 3D scene "Fan-driven and buoyant transport comparison" 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: Fan-driven and buoyant transport comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/fan-driven-and-buoyant-transport-comparison-x1458
Scene Description Prompt:
Compare stylized chambers driven by a fan or bottom heating to distinguish forced and natural convection. Three.js from cdnjs, one HTML file.
Working Three.js Starter (fan-driven-and-buoyant-transport-comparison-x1458):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Convection Lab — Forced vs Natural</title>
<style>
:root {
color-scheme: dark;
--bg: #070c15;
--panel: rgba(12, 20, 34, 0.84);
--line: rgba(150, 190, 230, 0.17);
--text: #edf7ff;
--muted: #8da1b8;
--forced: #42dcff;
--natural: #ff7043;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 35%, rgba(2, 6, 13, 0.48) 100%),
linear-gradient(180deg, rgba(4, 9, 17, 0.16), transparent 32%);
}
.brand {
position: fixed;
top: 25px;
left: 28px;
z-index: 4;
pointer-events: none;
max-width: 490px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: #9fb3c9;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #55f3b4;
box-shadow: 0 0 12px rgba(85, 243, 180, 0.9);
}
h1 {
margin: 9px 0 5px;
font-size: clamp(25px, 3.3vw, 44px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 760;
}
.brand p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.panel {
border: 1px solid var(--line);
background: var(--panel);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.controls {
position: fixed;
z-index: 5;
top: 24px;
right: 24px;
width: 286px;
padding: 17px;
border-radius: 16px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.panel-heading strong {
font-size: 11px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.status {
display: flex;
align-items: center;
gap: 6px;
color: #7e94aa;
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: #55f3b4;
}
.control {
padding: 12px 0;
border-top: 1px solid rgba(150, 190, 230, 0.1);
}
.control:first-of-type {
border-top: 0;
padding-top: 2px;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 9px;
font-size: 11px;
}
.control-label span:first-child {
color: #c7d5e4;
font-weight: 650;
}
output {
min-width: 44px;
color: #8fa6bd;
font-variant-numeric: tabular-nums;
text-align: right;
font-size: 10px;
}
.forced-text {
color: var(--forced) !important;
}
.natural-text {
color: var(--natural) !important;
}
input[type="range"] {
display: block;
width: 100%;
height: 4px;
margin: 0;
border-radius: 10px;
appearance: none;
-webkit-appearance: none;
background: #1c2b3d;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 2px solid #0b1522;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: #dff7ff;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #0b1522;
border-radius: 50%;
background: #dff7ff;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.25);
}
#fanPower::-webkit-slider-thumb {
background: var(--forced);
}
#heatPower::-webkit-slider-thumb {
background: var(--natural);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
color: #a9bacd;
font-size: 11px;
}
.toggle-row input {
width: 15px;
height: 15px;
accent-color: #5edcff;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
button {
min-height: 36px;
border: 1px solid rgba(151, 192, 229, 0.18);
border-radius: 9px;
color: #d8e8f7;
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.085);
border-color: rgba(151, 192, 229, 0.34);
}
button:active {
transform: translateY(1px);
}
#pauseButton {
width: 100%;
color: #08121d;
border-color: transparent;
background: #dff8ff;
font-weight: 750;
}
.legend {
position: fixed;
z-index: 4;
left: 28px;
bottom: 24px;
width: min(470px, calc(100vw - 56px));
padding: 13px 15px;
border-radius: 13px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
pointer-events: none;
}
.legend-item {
display: grid;
grid-template-columns: 8px 1fr;
column-gap: 10px;
align-items: start;
}
.legend-line {
width: 4px;
height: 34px;
margin-top: 1px;
border-radius: 5px;
background: var(--forced);
box-shadow: 0 0 12px rgba(66, 220, 255, 0.45);
}
.legend-line.orange {
background: var(--natural);
box-shadow: 0 0 12px rgba(255, 112, 67, 0.45);
}
.legend strong {
display: block;
margin-bottom: 3px;
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend small {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.view-help {
position: fixed;
z-index: 4;
right: 28px;
bottom: 25px;
color: rgba(174, 196, 216, 0.65);
font-size: 10px;
letter-spacing: 0.06em;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #070c15;
color: #b8cadb;
font-size: 11px;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 17px;
left: 18px;
max-width: calc(100vw - 36px);
}
.brand p {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: min(286px, calc(100vw - 24px));
padding: 13px;
}
.legend {
display: none;
}
.view-help {
right: 15px;
bottom: auto;
top: 86px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<header class="brand">
<div class="eyebrow">
<span class="live-dot"></span>
Thermal dynamics / comparison 01
</div>
<h1>Convection Lab</h1>
<p>Compare an externally driven flow with one created by buoyancy.</p>
</header>
<aside class="controls panel">
<div class="panel-heading">
<strong>Lab controls</strong>
<div class="status">
<span class="status-dot"></span>
<span id="runStatus">Running</span>
</div>
</div>
<div class="control">
<div class="control-label">
<span class="forced-text">Fan drive</span>
<output id="fanValue">82%</output>
</div>
<input
id="fanPower"
type="range"
min="0"
max="100"
value="82"
aria-label="Fan power"
>
</div>
<div class="control">
<div class="control-label">
<span class="natural-text">Bottom heat</span>
<output id="heatValue">78%</output>
</div>
<input
id="heatPower"
type="range"
min="0"
max="100"
value="78"
aria-label="Bottom heater power"
>
</div>
<div class="control">
<div class="control-label">
<span>Simulation speed</span>
<output id="speedValue">1.0×</output>
</div>
<input
id="simSpeed"
type="range"
min="30"
max="180"
value="100"
aria-label="Simulation speed"
>
</div>
<div class="control toggle-row">
<span>Show velocity vectors</span>
<input id="showVectors" type="checkbox" checked>
</div>
<button id="pauseButton" type="button" aria-pressed="false">Pause</button>
<div class="button-row">
<button id="resetParticles" type="button">Reset tracers</button>
<button id="resetCamera" type="button">Reset view</button>
</div>
</aside>
<aside class="legend panel">
<div class="legend-item">
<div class="legend-line"></div>
<div>
<strong>Forced convection</strong>
<small>The fan supplies momentum, establishing a directed flow.</small>
</div>
</div>
<div class="legend-item">
<div class="legend-line orange"></div>
<div>
<strong>Natural convection</strong>
<small>The heated floor lowers density, creating a buoyant plume.</small>
</div>
</div>
</aside>
<div class="view-help">DRAG TO ORBIT · SCROLL TO ZOOM · SPACE TO PAUSE</div>
<div id="loading">Initializing thermal field…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const ui = {
fan: document.getElementById("fanPower"),
heat: document.getElementById("heatPower"),
speed: document.getElementById("simSpeed"),
fanValue: document.getElementById("fanValue"),
heatValue: document.getElementById("heatValue"),
speedValue: document.getElementById("speedValue"),
vectors: document.getElementById("showVectors"),
pause: document.getElementById("pauseButton"),
runStatus: document.getElementById("runStatus"),
resetParticles: document.getElementById("resetParticles"),
resetCamera: document.getElementById("resetCamera"),
loading: document.getElementById("loading")
};
const state = {
fan: 0.82,
heat: 0.78,
speed: 1,
paused: false,
vectors: true
};
const W = 3.15;
const H = 3.8;
const D = 2.25;
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070c15);
scene.fog = new THREE.FogExp2(0x070c15, 0.027);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraState = {
theta: -0.14,
phi: 1.26,
radius: 12.7
};
const cameraTarget = new THREE.Vector3(0, 1.85, 0);
function updateCamera() {
const aspectCompensation = Math.max(1, 1.18 / camera.aspect);
const radius = cameraState.radius * aspectCompensation;
camera.position.set(
cameraTarget.x + radius *
Math.sin(cameraState.phi) *
Math.sin(cameraState.theta),
cameraTarget.y + radius * Math.cos(cameraState.phi),
cameraTarget.z + radius *
Math.sin(cameraState.phi) *
Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xa8ceff, 0x111826, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xdcecff, 2.6);
keyLight.position.set(-4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5b8cff, 1.1);
rimLight.position.set(6, 3, -6);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 22),
new THREE.MeshStandardMaterial({
color: 0x09101b,
roughness: 0.88,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.15;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x294157, 0x142335);
grid.position.y = -0.135;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
function smoothstep(a, b, x) {
const t = THREE.MathUtils.clamp((x - a) / (b - a), 0, 1);
return t * t * (3 - 2 * t);
}
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + width, y, x + width, y + height, radius);
ctx.arcTo(x + width, y + height, x, y + height, radius);
ctx.arcTo(x, y + height, x, y, radius);
ctx.arcTo(x, y, x + width, y, radius);
ctx.closePath();
}
function makeLabel(title, subtitle, accent) {
const canvas = document.createElement("canvas");
canvas.width = 900;
canvas.height = 240;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 4, 4, 892, 232, 24);
ctx.fillStyle = "rgba(7, 14, 24, 0.90)";
ctx.fill();
ctx.strokeStyle = "rgba(165, 202, 235, 0.24)";
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = accent;
roundedRect(ctx, 30, 38, 7, 164, 4);
ctx.fill();
ctx.fillStyle = "#eef8ff";
ctx.font = "700 47px system-ui, sans-serif";
ctx.fillText(title, 61, 96);
ctx.fillStyle = accent;
ctx.font = "650 25px system-ui, sans-serif";
ctx.fillText(subtitle, 62, 151);
ctx.fillStyle = "rgba(169, 193, 216, 0.72)";
ctx.font = "500 19px system-ui, sans-serif";
ctx.fillText("CUTAWAY FLOW CHAMBER", 62, 194);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.6, 0.96, 1);
sprite.renderOrder = 20;
return sprite;
}
function addBackGrid(group) {
const points = [];
for (let i = 1; i < 6; i++) {
const x = -W / 2 + (W * i) / 6;
points.push(x, 0, -D / 2 + 0.012);
points.push(x, H, -D / 2 + 0.012);
}
for (let i = 1; i < 6; i++) {
const y = (H * i) / 6;
points.push(-W / 2, y, -D / 2 + 0.012);
points.push(W / 2, y, -D / 2 + 0.012);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
);
const lines = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0x7fa8ca,
transparent: true,
opacity: 0.11,
depthWrite: false
})
);
group.add(lines);
}
function createHeatRibbons(group) {
const ribbons = [];
const segments = 18;
for (let r = 0; r < 6; r++) {
const positions = new Float32Array((segments + 1) * 3);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0xff7b32,
transparent: true,
opacity: 0.15,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const line = new THREE.Line(geometry, material);
line.userData = {
baseX: (r - 2.5) * 0.18,
baseZ: (r % 2 ? 1 : -1) * 0.18,
phase: r * 0.87,
segments
};
group.add(line);
ribbons.push(line);
}
return ribbons;
}
function createFan(group) {
const fan = new THREE.Group();
fan.position.set(-W / 2 - 0.16, H * 0.64, 0);
group.add(fan);
const metal = new THREE.MeshStandardMaterial({
color: 0x5d7388,
metalness: 0.8,
roughness: 0.28,
transparent: true,
opacity: 0.86
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.055, 12, 48),
metal
);
ring.rotation.y = Math.PI / 2;
fan.add(ring);
const housing = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.43, 0.28, 40, 1, true),
new THREE.MeshStandardMaterial({
color: 0x26394d,
metalness: 0.75,
roughness: 0.32,
transparent: true,
opacity: 0.54,
side: THREE.DoubleSide
})
);
housing.rotation.z = Math.PI / 2;
fan.add(housing);
const rotor = new THREE.Group();
fan.add(rotor);
const bladeMaterial = new THREE.MeshStandardMaterial({
color: 0xbfefff,
emissive: 0x164c62,
emissiveIntensity: 0.6,
metalness: 0.35,
roughness: 0.35
});
const bladeGeometry = new THREE.BoxGeometry(0.045, 0.43, 0.12);
for (let i = 0; i < 5; i++) {
const pivot = new THREE.Group();
pivot.rotation.x = (i / 5) * Math.PI * 2;
const blade = new THREE.Mesh(bladeGeometry, bladeMaterial);
blade.position.y = 0.19;
blade.rotation.x = 0.48;
blade.castShadow = true;
pivot.add(blade);
rotor.add(pivot);
}
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.2, 24),
new THREE.MeshStandardMaterial({
color: 0x2c465c,
metalness: 0.85,
roughness: 0.2
})
);
hub.rotation.z = Math.PI / 2;
rotor.add(hub);
const jet = new THREE.Mesh(
new THREE.ConeGeometry(0.29, 1.45, 32, 1, true),
new THREE.MeshBasicMaterial({
color: 0x42dcff,
transparent: true,
opacity: 0.045,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
jet.rotation.z = Math.PI / 2;
jet.position.x = 0.58;
fan.add(jet);
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.08, 0.1),
metal
);
support.position.set(-0.15, -0.48, 0);
fan.add(support);
return { rotor, jet, bladeMaterial };
}
function createParticleSystem(type, count) {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(count * 3);
const colors = new Float32Array(count * 3);
const sizes = new Float32Array(count);
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
geometry.setAttribute(
"color",
new THREE.BufferAttribute(colors, 3).setUsage(
THREE.DynamicDrawUsage
)
);
geometry.setAttribute(
"aSize",
new THREE.BufferAttribute(sizes, 1).setUsage(
THREE.DynamicDrawUsage
)
);
const material = new THREE.ShaderMaterial({
uniforms: {
uPixelRatio: {
value: Math.min(window.devicePixelRatio, 2)
}
},
vertexShader: `
attribute vec3 color;
attribute float aSize;
varying vec3 vColor;
uniform float uPixelRatio;
void main() {
vColor = color;
vec4 viewPosition = modelViewMatrix *
vec4(position, 1.0);
gl_PointSize = aSize *
uPixelRatio *
(390.0 / max(1.0, -viewPosition.z));
gl_Position = projectionMatrix * viewPosition;
}
`,
fragmentShader: `
varying vec3 vColor;
void main() {
float distanceToCenter = length(
gl_PointCoord - vec2(0.5)
);
float alpha = 1.0 - smoothstep(
0.12,
0.50,
distanceToCenter
);
if (alpha < 0.015) discard;
gl_FragColor = vec4(vColor, alpha * 0.92);
}
`,
transparent: true,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const points = new THREE.Points(geometry, material);
points.frustumCulled = false;
const system = {
type,
points,
geometry,
material,
count,
positions,
colors,
sizes,
positionAttribute: geometry.attributes.position,
colorAttribute: geometry.attributes.color,
sizeAttribute: geometry.attributes.aSize
};
resetParticleSystem(system);
return system;
}
function resetParticleSystem(system) {
for (let i = 0; i < system.count; i++) {
const index = i * 3;
system.positions[index] =
THREE.MathUtils.randFloat(-W / 2 + 0.12, W / 2 - 0.12);
system.positions[index + 1] =
THREE.MathUtils.randFloat(0.16, H - 0.14);
system.positions[index + 2] =
THREE.MathUtils.randFloat(-D * 0.39, D * 0.39);
system.sizes[i] = 0.12;
}
system.positionAttribute.needsUpdate = true;
system.sizeAttribute.needsUpdate = true;
}
function forcedField(x, y, time) {
const nx = x / W + 0.5;
const ny = y / H;
const circulation = 0.035 + state.fan * 0.68;
let u =
-circulation *
Math.sin(Math.PI * nx) *
Math.cos(Math.PI * ny);
let v =
circulation *
Math.cos(Math.PI * nx) *
Math.sin(Math.PI * ny);
const distanceFromFan = Math.max(0, x + W / 2);
const fanHeight = H * 0.64;
const jet =
state.fan *
1.65 *
Math.exp(-distanceFromFan / 0.64) *
Math.exp(-Math.pow((y - fanHeight) / 0.39, 2));
u += jet;
v +=
state.fan *
0.085 *
Math.exp(-distanceFromFan / 0.72) *
Math.sin(time * 3.1 + y * 2.4);
return { u, v };
}
function naturalField(x, y) {
const nx = x / W + 0.5;
const ny = y / H;
const centeredX = nx - 0.5;
const absoluteX = Math.abs(centeredX);
const plumeWidth = 0.105 + ny * 0.13;
const plume =
Math.exp(
-(centeredX * centeredX) /
(plumeWidth * plumeWidth)
);
const upperTurn = smoothstep(0.58, 0.95, ny);
const edgeCooling =
Math.exp(-Math.pow((absoluteX - 0.36) / 0.13, 2));
const u =
state.heat *
0.25 *
Math.sin(Math.PI * ny) *
centeredX *
2;
const v =
state.heat *
(
0.68 * plume * (1 - 0.72 * upperTurn) -
0.2 * edgeCooling * upperTurn
);
return { u, v };
}
const forcedLow = new THREE.Color(0x185fff);
const forcedHigh = new THREE.Color(0x83f7ff);
const naturalLow = new THREE.Color(0xff3d1f);
const naturalHigh = new THREE.Color(0xffd36a);
const temporaryColor = new THREE.Color();
function updateParticles(system, dt, time) {
if (dt <= 0) return;
const positions = system.positions;
const colors = system.colors;
const sizes = system.sizes;
for (let i = 0; i < system.count; i++) {
const index = i * 3;
let x = positions[index];
let y = positions[index + 1];
let z = positions[index + 2];
const field =
system.type === "forced"
? forcedField(x, y, time)
: naturalField(x, y);
let u = field.u;
let v = field.v;
if (system.type === "forced") {
u +=
0.055 *
Math.sin(time * 1.25 + y * 2.1 + i * 0.17);
v +=
0.045 *
Math.cos(time * 1.05 + x * 1.8 + i * 0.11);
} else {
u +=
0.026 *
Math.sin(time * 1.4 + y * 2.4 + i * 0.13);
v +=
0.025 *
Math.cos(time * 1.25 + x * 2.1 + i * 0.09);
}
x += u * dt;
y += v * dt;
z += Math.sin(time * 0.72 + i * 0.19) * 0.055 * dt;
if (x < -W / 2 + 0.07) {
x = -W / 2 + 0.07;
u = Math.abs(u) * 0.25;
} else if (x > W / 2 - 0.07) {
x = W / 2 - 0.07;
u = -Math.abs(u) * 0.25;
}
if (y < 0.16) {
y = 0.16;
v = Math.abs(v) * 0.15;
} else if (y > H - 0.07) {
y = H - 0.07;
if (Math.abs(x) < 0.22) {
x += (x >= 0 ? 1 : -1) * 0.035;
}
v = -Math.abs(v) * 0.2;
}
z = THREE.MathUtils.clamp(z, -D * 0.42, D * 0.42);
positions[index] = x;
positions[index + 1] = y;
positions[index + 2] = z;
const speed = Math.hypot(u, v);
if (system.type === "forced") {
const t = THREE.MathUtils.clamp(speed / 1.65, 0, 1);
temporaryColor.copy(forcedLow).lerp(forcedHigh, t);
sizes[i] = 0.105 + t * 0.055;
} else {
const t = THREE.MathUtils.clamp(
y / H * 0.78 + speed * 0.13,
0,
1
);
temporaryColor.copy(naturalLow).lerp(naturalHigh, t);
sizes[i] = 0.11 + t * 0.06;
}
colors[index] = temporaryColor.r;
colors[index + 1] = temporaryColor.g;
colors[index + 2] = temporaryColor.b;
}
system.positionAttribute.needsUpdate = true;
system.colorAttribute.needsUpdate = true;
system.sizeAttribute.needsUpdate = true;
}
function createArrows(group, type) {
const arrows = [];
const xPositions = [-0.98, 0, 0.98];
const yPositions = [0.72, 1.68, 2.64, 3.25];
const color =
type === "forced"
? new THREE.Color(0x42dcff)
: new THREE.Color(0xff8454);
for (const y of yPositions) {
for (const x of xPositions) {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(x, y, 0.66),
0.16,
color,
0.065,
0.038
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.5;
arrow.line.material.depthWrite = false;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.62;
arrow.cone.material.depthWrite = false;
group.add(arrow);
arrows.push(arrow);
}
}
return arrows;
}
const arrowDirection = new THREE.Vector3();
function updateArrows(chamber, time) {
chamber.arrows.forEach((arrow) => {
const x = arrow.position.x;
const y = arrow.position.y;
const field =
chamber.type === "forced"
? forcedField(x, y, time)
: naturalField(x, y);
const speed = Math.hypot(field.u, field.v);
if (speed < 0.015) {
arrow.setDirection(arrowDirection.set(1, 0, 0));
arrow.setLength(0.06, 0.025, 0.018);
return;
}
arrow.setDirection(
arrowDirection.set(field.u, field.v, 0).normalize()
);
const length = THREE.MathUtils.clamp(
0.1 + speed * 0.18,
0.08,
0.36
);
arrow.setLength(
length,
Math.min(0.085, length * 0.34),
Math.min(0.045, length * 0.2)
);
});
}
function createChamber(type, xPosition) {
const group = new THREE.Group();
group.position.x = xPosition;
scene.add(group);
const accent =
type === "forced" ? 0x42dcff : 0xff7043;
const base = new THREE.Mesh(
new THREE.BoxGeometry(W + 0.24, 0.18, D + 0.24),
new THREE.MeshStandardMaterial({
color: 0x111c29,
metalness: 0.48,
roughness: 0.44
})
);
base.position.y = 0;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const accentStrip = new THREE.Mesh(
new THREE.BoxGeometry(W + 0.08, 0.035, 0.055),
new THREE.MeshBasicMaterial({
color: accent,
toneMapped: false
})
);
accentStrip.position.set(0, 0.105, D / 2 + 0.105);
group.add(accentStrip);
const glassMaterial = new THREE.MeshBasicMaterial({
color: type === "forced" ? 0x70caff : 0xffa783,
transparent: true,
opacity: 0.035,
depthWrite: false,
side: THREE.DoubleSide
});
const back = new THREE.Mesh(
new THREE.BoxGeometry(W, H, 0.025),
glassMaterial
);
back.position.set(0, H / 2 + 0.1, -D / 2);
group.add(back);
const left = new THREE.Mesh(
new THREE.BoxGeometry(0.025, H, D),
glassMaterial
);
left.position.set(-W / 2, H / 2 + 0.1, 0);
group.add(left);
const right = left.clone();
right.position.x = W / 2;
group.add(right);
const edgeMaterial = new THREE.LineBasicMaterial({
color: type === "forced" ? 0x8bcaff : 0xffb89f,
transparent: true,
opacity: 0.22,
depthWrite: false
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(W, H, D)
),
edgeMaterial
);
edges.position.y = H / 2 + 0.1;
group.add(edges);
addBackGrid(group);
let fan = null;
let heater = null;
let heaterLight = null;
let heatHalo = null;
let heatPlume = null;
let ribbons = [];
if (type === "forced") {
fan = createFan(group);
const grille = new THREE.Group();
for (let i = 0; i < 9; i++) {
const slat = new THREE.Mesh(
new THREE.BoxGeometry(W * 0.66, 0.035, 0.035),
new THREE.MeshStandardMaterial({
color: 0x26394a,
metalness: 0.7,
roughness: 0.35
})
);
slat.position.set(
0,
0.13,
-D * 0.31 + (i * D * 0.62) / 8
);
grille.add(slat);
}
group.add(grille);
} else {
heater = new THREE.MeshStandardMaterial({
color: 0x4b1d12,
emissive: 0xff3510,
emissiveIntensity: 1.4,
metalness: 0.45,
roughness: 0.42
});
const plate = new THREE.Mesh(
new THREE.BoxGeometry(W * 0.74, 0.09, D * 0.72),
heater
);
plate.position.y = 0.145;
plate.castShadow = true;
group.add(plate);
const slatMaterial = new THREE.MeshStandardMaterial({
color: 0xff6a24,
emissive: 0xff2608,
emissiveIntensity: 1.2,
metalness: 0.35,
roughness: 0.4
});
for (let i = 0; i < 7; i++) {
const slat = new THREE.Mesh(
new THREE.BoxGeometry(W * 0.68, 0.028, 0.045),
slatMaterial
);
slat.position.set(
0,
0.205,
-D * 0.29 + (i * D * 0.58) / 6
);
group.add(slat);
}
heatHalo = new THREE.Mesh(
new THREE.CircleGeometry(1, 64),
new THREE.MeshBasicMaterial({
color: 0xff4d18,
transparent: true,
opacity: 0.1,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
heatHalo.rotation.x = -Math.PI / 2;
heatHalo.position.y = 0.225;
heatHalo.scale.set(1.05, 0.75, 1);
group.add(heatHalo);
heatPlume = new THREE.Mesh(
new THREE.ConeGeometry(0.72, 1.9, 36, 1, true),
new THREE.MeshBasicMaterial({
color: 0xff8a38,
transparent: true,
opacity: 0.018,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
heatPlume.rotation.z = Math.PI;
heatPlume.position.y = 1.12;
group.add(heatPlume);
heaterLight = new THREE.PointLight(0xff4b19, 8, 4.2, 2);
heaterLight.position.set(0, 0.42, 0);
group.add(heaterLight);
ribbons = createHeatRibbons(group);
}
const particles = createParticleSystem(type, 330);
group.add(particles.points);
const arrows = createArrows(group, type);
const label =
type === "forced"
? makeLabel(
"FORCED CONVECTION",
"EXTERNAL FAN → DIRECTED FLOW",
"#42dcff"
)
: makeLabel(
"NATURAL CONVECTION",
"BOTTOM HEAT → BUOYANT PLUME",
"#ff7043"
);
label.position.set(0, H + 0.72, 0);
group.add(label);
return {
type,
group,
fan,
heater,
heaterLight,
heatHalo,
heatPlume,
ribbons,
particles,
arrows
};
}
const chambers = [
createChamber("forced", -2.72),
createChamber("natural", 2.72)
];
function updateHeatRibbons(chamber, time) {
chamber.ribbons.forEach((ribbon, ribbonIndex) => {
const data = ribbon.userData;
const attribute = ribbon.geometry.attributes.position;
const array = attribute.array;
for (let i = 0; i <= data.segments; i++) {
const progress = i / data.segments;
const index = i * 3;
array[index] =
data.baseX +
Math.sin(time * 1.8 + data.phase + progress * 4.4) *
(0.025 + progress * 0.055);
array[index + 1] = 0.24 + progress * 1.45;
array[index + 2] =
data.baseZ +
Math.cos(time * 1.45 + data.phase + progress * 3.6) *
(0.02 + progress * 0.045);
}
attribute.needsUpdate = true;
ribbon.material.opacity =
(0.025 + state.heat * 0.12) *
(0.7 + 0.3 * Math.sin(time * 2 + ribbonIndex));
});
}
function updateChamber(chamber, dt, time) {
if (chamber.type === "forced") {
chamber.fan.rotor.rotation.x += dt * state.fan * 18;
chamber.fan.jet.material.opacity =
0.004 + state.fan * 0.055;
chamber.fan.bladeMaterial.emissiveIntensity =
0.25 + state.fan * 0.85;
} else {
chamber.heater.emissiveIntensity =
0.12 + state.heat * 2.2;
chamber.heaterLight.intensity =
state.heat * 12;
chamber.heatHalo.material.opacity =
(0.025 + state.heat * 0.09) *
(0.86 + 0.14 * Math.sin(time * 3.1));
const pulse = 1 + Math.sin(time * 2.2) * 0.045;
chamber.heatHalo.scale.set(1.05 * pulse, 0.75 * pulse, 1);
chamber.heatPlume.material.opacity =
state.heat * (0.01 + 0.012 * Math.sin(time * 1.9));
updateHeatRibbons(chamber, time);
}
updateParticles(chamber.particles, dt, time);
updateArrows(chamber, time);
}
function resetCamera() {
cameraState.theta = -0.14;
cameraState.phi = 1.26;
cameraState.radius = 12.7;
updateCamera();
}
function updatePauseUI() {
ui.pause.textContent = state.paused ? "Resume" : "Pause";
ui.pause.setAttribute("aria-pressed", String(state.paused));
ui.runStatus.textContent = state.paused ? "Paused" : "Running";
ui.runStatus.previousElementSibling.style.background =
state.paused ? "#ffb454" : "#55f3b4";
}
ui.fan.addEventListener("input", () => {
state.fan = Number(ui.fan.value) / 100;
ui.fanValue.textContent = `${Math.round(state.fan * 100)}%`;
});
ui.heat.addEventListener("input", () => {
state.heat = Number(ui.heat.value) / 100;
ui.heatValue.textContent = `${Math.round(state.heat * 100)}%`;
});
ui.speed.addEventListener("input", () => {
state.speed = Number(ui.speed.value) / 100;
ui.speedValue.textContent = `${state.speed.toFixed(1)}×`;
});
ui.vectors.addEventListener("change", () => {
state.vectors = ui.vectors.checked;
chambers.forEach((chamber) => {
chamber.arrows.forEach((arrow) => {
arrow.visible = state.vectors;
});
});
});
ui.pause.addEventListener("click", () => {
state.paused = !state.paused;
updatePauseUI();
});
ui.resetParticles.addEventListener("click", () => {
chambers.forEach((chamber) => {
resetParticleSystem(chamber.particles);
});
});
ui.resetCamera.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
!["INPUT", "BUTTON"].includes(document.activeElement.tagName)
) {
event.preventDefault();
state.paused = !state.paused;
updatePauseUI();
}
if (
event.key.toLowerCase() === "r" &&
!["INPUT", "BUTTON"].includes(document.activeElement.tagName)
) {
resetCamera();
}
});
/*
* Camera controls are implemented directly with Pointer Events.
* No addons or external control modules are used.
*/
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.005,
0.48,
1.48
);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
8.5,
19
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
chambers.forEach((chamber) => {
chamber.particles.material.uniforms.uPixelRatio.value =
Math.min(window.devicePixelRatio, 2);
});
});
const clock = new THREE.Clock();
let simulationTime = 0;
renderer.setAnimationLoop(() => {
const frameDelta = Math.min(clock.getDelta(), 0.05);
const simulationDelta = state.paused ? 0 : frameDelta * state.speed;
if (!state.paused) {
simulationTime += simulationDelta;
}
chambers.forEach((chamber) => {
updateChamber(chamber, simulationDelta, simulationTime);
});
updateCamera();
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
ui.loading.classList.add("hidden");
});
</script>
</body>
</html>
```