Prompt specification
Build or customize the Three.js 3D scene "Scale-switch kitchen chemistry" 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: Scale-switch kitchen chemistry
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/scale-switch-kitchen-chemistry-x0112
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: scale-switch kitchen chemistry. Build a kitchen worktop with a magnifying portal into an emulsion diagram; the same control toggles stirring at both scales so macroscopic appearance matches the particle arrangement. Return one self-contained HTML file.
Working Three.js Starter (scale-switch-kitchen-chemistry-x0112):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Linked Emulsion — Scale-Switch Kitchen Chemistry</title>
<style>
:root {
--ink: #173638;
--paper: rgba(247, 241, 226, 0.9);
--teal: #37b8aa;
--gold: #e8b64c;
--line: rgba(23, 54, 56, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e0d3;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 52%, rgba(18, 43, 44, 0.12) 115%),
linear-gradient(180deg, rgba(255,255,255,0.08), transparent 30%);
z-index: 2;
}
#hud {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.title-block {
position: absolute;
top: 24px;
left: 28px;
max-width: 410px;
padding: 17px 19px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 16px 40px rgba(26, 51, 50, 0.12);
backdrop-filter: blur(14px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #47716c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 19px;
height: 2px;
border-radius: 2px;
background: var(--gold);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3vw, 44px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 0;
max-width: 350px;
color: rgba(23, 54, 56, 0.68);
font-size: 12px;
line-height: 1.5;
}
.linked-badge {
position: absolute;
top: 27px;
right: 28px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--paper);
box-shadow: 0 10px 30px rgba(26, 51, 50, 0.1);
backdrop-filter: blur(14px);
color: #386761;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.linked-badge::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(55, 184, 170, 0.16);
}
.readout {
position: absolute;
right: 28px;
bottom: 25px;
width: 265px;
padding: 14px 15px 13px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 16px 40px rgba(26, 51, 50, 0.12);
backdrop-filter: blur(14px);
}
.readout-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#phaseName {
color: #318f83;
font-size: 9px;
}
.readout-row {
display: grid;
grid-template-columns: 55px 1fr;
gap: 8px;
align-items: baseline;
padding: 5px 0;
border-top: 1px solid var(--line);
font-size: 11px;
}
.readout-row span {
color: rgba(23, 54, 56, 0.47);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
}
.readout-row strong {
font-weight: 650;
}
.meter {
height: 4px;
margin-top: 9px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 54, 56, 0.1);
}
#mixFill {
display: block;
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--teal), var(--gold));
transition: width 80ms linear;
}
#controlDock {
position: absolute;
left: 50%;
bottom: 25px;
display: flex;
align-items: center;
gap: 13px;
transform: translateX(-50%);
pointer-events: auto;
}
#stirButton {
display: flex;
align-items: center;
gap: 12px;
min-width: 218px;
height: 54px;
padding: 0 21px 0 14px;
border: 0;
border-radius: 17px;
background: #173f3d;
color: #f8f1df;
box-shadow: 0 14px 32px rgba(21, 54, 53, 0.26);
cursor: pointer;
font: inherit;
transition: transform 160ms ease, background 160ms ease, box-shadow 160ms ease;
}
#stirButton:hover {
transform: translateY(-2px);
background: #20544f;
box-shadow: 0 18px 38px rgba(21, 54, 53, 0.3);
}
#stirButton:active { transform: translateY(0) scale(0.98); }
#stirButton.active {
background: #d9a940;
color: #173638;
}
.button-icon {
position: relative;
width: 31px;
height: 31px;
flex: 0 0 auto;
border: 2px solid currentColor;
border-radius: 50%;
}
.button-icon::before,
.button-icon::after {
content: "";
position: absolute;
left: 50%;
top: 50%;
width: 15px;
height: 2px;
border-radius: 2px;
background: currentColor;
transform: translate(-50%, -50%);
}
.button-icon::after {
width: 7px;
height: 7px;
border-right: 2px solid currentColor;
border-top: 2px solid currentColor;
background: transparent;
transform: translate(-35%, -65%) rotate(45deg);
}
#stirButton.active .button-icon {
animation: spin 1.2s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#stirLabel {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.035em;
white-space: nowrap;
}
.control-note {
width: 145px;
color: rgba(23, 54, 56, 0.62);
font-size: 10px;
line-height: 1.45;
}
.hint {
position: absolute;
left: 28px;
bottom: 26px;
color: rgba(23, 54, 56, 0.52);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
}
.hint span {
display: inline-block;
margin-right: 7px;
padding: 4px 6px;
border: 1px solid rgba(23, 54, 56, 0.16);
border-radius: 6px;
background: rgba(247, 241, 226, 0.65);
}
@media (max-width: 900px) {
.title-block { max-width: 320px; }
.readout { bottom: 96px; right: 18px; width: 235px; }
.linked-badge { right: 18px; top: 18px; }
.hint { display: none; }
#controlDock { bottom: 19px; }
}
@media (max-width: 620px) {
.title-block {
top: 14px;
left: 14px;
right: 14px;
max-width: none;
padding: 13px 15px;
}
.subtitle { display: none; }
h1 { font-size: 29px; }
.linked-badge { display: none; }
.readout {
top: 111px;
bottom: auto;
right: 14px;
width: 207px;
padding: 10px 12px;
}
.readout-row { font-size: 9px; grid-template-columns: 45px 1fr; }
.readout-top { margin-bottom: 5px; }
#controlDock { width: calc(100% - 28px); bottom: 14px; }
#stirButton { width: 100%; min-width: 0; justify-content: center; }
.control-note { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly kitchen with a magnifying portal into an emulsion diagram"></canvas>
<div id="vignette"></div>
<div id="hud">
<section class="title-block">
<div class="eyebrow">Scale-switch kitchen chemistry</div>
<h1>Linked Emulsion</h1>
<p class="subtitle">
A magnifying portal connects the mixing bowl to the particle-scale diagram.
One stirring control drives both views.
</p>
</section>
<div class="linked-badge">Scale link active</div>
<section class="readout" aria-live="polite">
<div class="readout-top">
<span>Emulsion state</span>
<strong id="phaseName">Separated</strong>
</div>
<div class="readout-row">
<span>Macro</span>
<strong id="phaseMacro">Large amber oil pools</strong>
</div>
<div class="readout-row">
<span>Micro</span>
<strong id="phaseMicro">8 large oil domains</strong>
</div>
<div class="meter"><i id="mixFill"></i></div>
</section>
<div id="controlDock">
<button id="stirButton" type="button" aria-pressed="false">
<span class="button-icon" aria-hidden="true"></span>
<span id="stirLabel">Stir both scales</span>
</button>
<div class="control-note">
The same motion fragments the visible oil and rearranges the microscopic droplets.
</div>
</div>
<div class="hint"><span>Drag</span> orbit <span>Scroll</span> zoom <span>Space</span> stir</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const stirButton = document.querySelector("#stirButton");
const stirLabel = document.querySelector("#stirLabel");
const phaseName = document.querySelector("#phaseName");
const phaseMacro = document.querySelector("#phaseMacro");
const phaseMicro = document.querySelector("#phaseMicro");
const mixFill = document.querySelector("#mixFill");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e0d3);
scene.fog = new THREE.Fog(0xe9e0d3, 13, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 0.9, 0);
let orbitTheta = 0.39;
let orbitPhi = 1.04;
let orbitRadius = 10.1;
function updateCamera() {
camera.position.set(
cameraTarget.x + orbitRadius * Math.sin(orbitPhi) * Math.sin(orbitTheta),
cameraTarget.y + orbitRadius * Math.cos(orbitPhi),
cameraTarget.z + orbitRadius * Math.sin(orbitPhi) * Math.cos(orbitTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff7e6, 0x446b68, 2.2);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d2, 3.4);
sun.position.set(-4.5, 8.5, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9e9df, 1.1);
fill.position.set(5, 4, -4);
scene.add(fill);
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
flatShading: true,
roughness: 0.78,
...options
});
function addBox(w, h, d, x, y, z, material, rotationY = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.rotation.y = rotationY;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, material, parent = scene, segments = 8) {
const direction = new THREE.Vector3().subVectors(b, a);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, direction.length(), segments),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Kitchen shell.
const wallMaterial = standard(0xf0e8dc);
const floorMaterial = standard(0xcbbba5);
const cabinetMaterial = standard(0x315952);
const doorMaterial = standard(0x3d6b62);
const counterMaterial = standard(0xe1cfb5, { roughness: 0.62 });
const brassMaterial = standard(0xd6a94d, { metalness: 0.62, roughness: 0.28 });
const darkMaterial = standard(0x183d3b);
const woodMaterial = standard(0xb87943);
addBox(14, 0.22, 14, 0, -2.16, 0, floorMaterial);
addBox(12, 7, 0.18, 0, 1.45, -2.82, wallMaterial);
addBox(9.8, 2.25, 4.8, 0, -0.57, 0.02, cabinetMaterial);
addBox(10.25, 0.25, 5.25, 0, 0.55, 0.02, counterMaterial);
addBox(9.5, 0.17, 0.16, 0, -1.65, 2.48, darkMaterial);
const doorXs = [-3.6, -1.2, 1.2, 3.6];
for (const x of doorXs) {
addBox(2.22, 1.88, 0.12, x, -0.47, 2.46, doorMaterial);
addBox(1.82, 0.48, 0.11, x, 0.16, 2.55, doorMaterial);
cylinderBetween(
new THREE.Vector3(x - 0.3, 0.12, 2.66),
new THREE.Vector3(x + 0.3, 0.12, 2.66),
0.035,
brassMaterial
);
}
const tileMaterials = [
standard(0xe8e3d7),
standard(0xd8e3dc),
standard(0xf0eadc),
standard(0xcbdcd3)
];
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 14; col++) {
const material = tileMaterials[(row * 3 + col * 5) % tileMaterials.length];
addBox(
0.69,
0.62,
0.055,
-4.55 + col * 0.705,
0.99 + row * 0.69,
-2.69,
material
);
}
}
// Shelf and small kitchen objects.
addBox(5.3, 0.12, 0.62, 1.35, 3.42, -2.38, woodMaterial);
const jarMaterial = standard(0xd9d1b7, { roughness: 0.45 });
const jarGlass = standard(0xaac9c0, { roughness: 0.32 });
for (let i = 0; i < 4; i++) {
const x = -0.45 + i * 0.92;
const h = 0.34 + (i % 2) * 0.12;
const jar = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.19, h, 10),
i % 2 ? jarMaterial : jarGlass
);
jar.position.set(x, 3.63, -2.35);
jar.castShadow = true;
scene.add(jar);
const lid = new THREE.Mesh(
new THREE.CylinderGeometry(0.2, 0.2, 0.07, 10),
brassMaterial
);
lid.position.set(x, 3.64 + h / 2, -2.35);
lid.castShadow = true;
scene.add(lid);
}
const board = addBox(1.55, 0.09, 1.08, -3.05, 0.735, -0.75, woodMaterial, -0.13);
board.castShadow = true;
addBox(0.45, 0.075, 0.3, -2.05, 0.735, -0.56, woodMaterial, -0.13);
const bottleMaterial = standard(0xa96828, { roughness: 0.42 });
const bottle = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.34, 0.9, 10),
bottleMaterial
);
bottle.position.set(2.25, 1.14, -1.28);
bottle.castShadow = true;
scene.add(bottle);
const bottleNeck = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.16, 0.35, 10),
bottleMaterial
);
bottleNeck.position.set(2.25, 1.76, -1.28);
bottleNeck.castShadow = true;
scene.add(bottleNeck);
const bottleCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.15, 0.12, 10),
darkMaterial
);
bottleCap.position.set(2.25, 1.98, -1.28);
bottleCap.castShadow = true;
scene.add(bottleCap);
const lemon = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.27, 1),
standard(0xe8c34c, { roughness: 0.65 })
);
lemon.position.set(1.23, 0.93, -1.48);
lemon.scale.set(1.3, 0.85, 0.9);
lemon.rotation.set(0.2, 0.7, 0.1);
lemon.castShadow = true;
scene.add(lemon);
const utensilCup = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.25, 0.46, 9),
standard(0x66847a)
);
utensilCup.position.set(-2.22, 0.91, -1.72);
utensilCup.castShadow = true;
scene.add(utensilCup);
for (let i = 0; i < 3; i++) {
const x = -2.35 + i * 0.12;
const z = -1.7 + (i - 1) * 0.08;
cylinderBetween(
new THREE.Vector3(x, 0.92, z),
new THREE.Vector3(x + (i - 1) * 0.11, 1.67 + i * 0.08, z),
0.035,
woodMaterial
);
}
// Faucet behind the mixing bowl.
const faucetMaterial = standard(0xb7c8c1, { metalness: 0.7, roughness: 0.28 });
cylinderBetween(
new THREE.Vector3(-0.75, 0.68, -1.78),
new THREE.Vector3(-0.75, 1.47, -1.78),
0.055,
faucetMaterial
);
const faucetArc = new THREE.Mesh(
new THREE.TorusGeometry(0.38, 0.055, 8, 24, Math.PI),
faucetMaterial
);
faucetArc.position.set(-0.75, 1.47, -1.78);
faucetArc.rotation.y = Math.PI / 2;
faucetArc.castShadow = true;
scene.add(faucetArc);
cylinderBetween(
new THREE.Vector3(-0.75, 1.47, -2.16),
new THREE.Vector3(-0.75, 1.31, -2.16),
0.055,
faucetMaterial
);
// Shared particle data drives the macro texture and micro diagram.
function seededRandom(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(80421);
const initialDroplets = [
[-0.72, -0.43], [-0.39, -0.68], [-0.10, -0.37], [-0.45, 0.08],
[-0.04, 0.24], [0.38, -0.51], [0.71, -0.13], [0.31, 0.39]
];
const particleDescriptors = [];
for (let i = 0; i < 28; i++) {
const angle = i * 2.3999632297 + 0.28;
const radius = Math.sqrt((i + 0.5) / 28) * 0.97;
const endX = Math.cos(angle) * radius;
const endY = Math.sin(angle) * radius;
const start = i < initialDroplets.length
? { x: initialDroplets[i][0], y: initialDroplets[i][1] }
: { x: endX, y: endY };
const bias = 0.78 + random() * 0.42;
particleDescriptors.push({
startX: start.x,
startY: start.y,
endX,
endY,
currentX: start.x,
currentY: start.y,
currentSize: i < 8 ? 0.215 * bias : 0,
initialSize: i < 8 ? 0.215 * bias : 0,
finalSize: (i < 8 ? 0.056 : 0.062) * bias,
bias,
z: (random() - 0.5) * 0.045
});
}
// Macro bowl.
const bowlX = -0.75;
const bowlZ = 0.25;
const bowlBaseY = 0.68;
const bowlProfile = [
new THREE.Vector2(0, 0),
new THREE.Vector2(0.55, 0),
new THREE.Vector2(0.88, 0.12),
new THREE.Vector2(1.18, 0.47),
new THREE.Vector2(1.25, 0.59),
new THREE.Vector2(1.14, 0.62),
new THREE.Vector2(1.03, 0.48),
new THREE.Vector2(0.78, 0.22),
new THREE.Vector2(0, 0.17)
];
const bowlMaterial = standard(0xf2eee2, { roughness: 0.38, side: THREE.DoubleSide });
const bowl = new THREE.Mesh(new THREE.LatheGeometry(bowlProfile, 32), bowlMaterial);
bowl.position.set(bowlX, bowlBaseY, bowlZ);
bowl.castShadow = true;
bowl.receiveShadow = true;
scene.add(bowl);
const macroCanvas = document.createElement("canvas");
macroCanvas.width = 512;
macroCanvas.height = 512;
const macroContext = macroCanvas.getContext("2d");
const macroTexture = new THREE.CanvasTexture(macroCanvas);
macroTexture.colorSpace = THREE.SRGBColorSpace;
macroTexture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const liquidMaterial = new THREE.MeshStandardMaterial({
map: macroTexture,
roughness: 0.34,
metalness: 0,
side: THREE.DoubleSide
});
const liquid = new THREE.Mesh(new THREE.CircleGeometry(1.025, 48), liquidMaterial);
liquid.rotation.x = -Math.PI / 2;
liquid.position.set(bowlX, bowlBaseY + 0.455, bowlZ);
liquid.receiveShadow = true;
scene.add(liquid);
const spoonPivot = new THREE.Group();
spoonPivot.position.set(bowlX, bowlBaseY + 0.485, bowlZ);
scene.add(spoonPivot);
const spoonHead = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 10, 6),
standard(0xc58a50, { roughness: 0.42 })
);
spoonHead.scale.set(0.82, 0.24, 1.18);
spoonHead.castShadow = true;
spoonPivot.add(spoonHead);
cylinderBetween(
new THREE.Vector3(0.12, 0, 0),
new THREE.Vector3(1.02, 0, 0),
0.028,
standard(0xd39b5e, { roughness: 0.42 }),
spoonPivot
);
const swirlMaterial = new THREE.MeshBasicMaterial({
color: 0xfff8df,
transparent: true,
opacity: 0,
depthWrite: false
});
const macroSwirl = new THREE.Group();
macroSwirl.position.set(bowlX, bowlBaseY + 0.472, bowlZ);
scene.add(macroSwirl);
const macroArcA = new THREE.Mesh(
new THREE.TorusGeometry(0.7, 0.011, 4, 48, Math.PI * 1.42),
swirlMaterial
);
macroArcA.rotation.x = -Math.PI / 2;
macroArcA.rotation.z = 0.4;
macroSwirl.add(macroArcA);
const macroArcB = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.009, 4, 40, Math.PI * 1.15),
swirlMaterial
);
macroArcB.rotation.x = -Math.PI / 2;
macroArcB.rotation.z = 2.5;
macroSwirl.add(macroArcB);
// Micro emulsion diagram.
const microScene = new THREE.Scene();
microScene.background = new THREE.Color(0x10363b);
const microCamera = new THREE.OrthographicCamera(-1.3, 1.3, 1.3, -1.3, 0.1, 20);
microCamera.position.set(0, 0, 6);
microCamera.lookAt(0, 0, 0);
const microTarget = new THREE.WebGLRenderTarget(1024, 1024, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat,
depthBuffer: true
});
microTarget.texture.colorSpace = THREE.SRGBColorSpace;
const microAmbient = new THREE.AmbientLight(0xc8fff3, 1.7);
microScene.add(microAmbient);
const microKey = new THREE.DirectionalLight(0xfff2cc, 2.5);
microKey.position.set(-2, 3, 5);
microScene.add(microKey);
const microBackdrop = new THREE.Mesh(
new THREE.CircleGeometry(1.27, 64),
new THREE.MeshBasicMaterial({ color: 0x153e43 })
);
microBackdrop.position.z = -0.62;
microScene.add(microBackdrop);
const gridVertices = [];
for (let v = -1.2; v <= 1.201; v += 0.3) {
gridVertices.push(-1.2, v, -0.48, 1.2, v, -0.48);
gridVertices.push(v, -1.2, -0.48, v, 1.2, -0.48);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute("position", new THREE.Float32BufferAttribute(gridVertices, 3));
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x76bdb6,
transparent: true,
opacity: 0.11
})
);
microScene.add(grid);
const fieldRing = new THREE.Mesh(
new THREE.TorusGeometry(1.19, 0.009, 4, 80),
new THREE.MeshBasicMaterial({ color: 0x6dd7c9, transparent: true, opacity: 0.45 })
);
fieldRing.position.z = -0.44;
microScene.add(fieldRing);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x85e2e4,
roughness: 0.42,
flatShading: true
});
for (let i = 0; i < 38; i++) {
const a = random() * Math.PI * 2;
const r = Math.sqrt(random()) * 1.12;
const molecule = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.017, 0),
waterMaterial
);
molecule.position.set(Math.cos(a) * r, Math.sin(a) * r, -0.32);
microScene.add(molecule);
}
const oilMaterial = new THREE.MeshStandardMaterial({
color: 0xe8b644,
roughness: 0.36,
metalness: 0.04,
flatShading: true
});
const surfactantMaterial = new THREE.MeshBasicMaterial({
color: 0x8ef0dc,
wireframe: true,
transparent: true,
opacity: 0.62,
depthWrite: false
});
for (const descriptor of particleDescriptors) {
const oil = new THREE.Mesh(new THREE.IcosahedronGeometry(1, 1), oilMaterial);
oil.castShadow = false;
microScene.add(oil);
const shell = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
surfactantMaterial
);
microScene.add(shell);
descriptor.oilMesh = oil;
descriptor.shellMesh = shell;
}
const microSwirlMaterial = new THREE.MeshBasicMaterial({
color: 0x84e4d9,
transparent: true,
opacity: 0,
depthWrite: false
});
const microSwirl = new THREE.Group();
microSwirl.position.z = -0.12;
microScene.add(microSwirl);
const microArc = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.008, 4, 48, Math.PI * 1.45),
microSwirlMaterial
);
microArc.rotation.z = 0.2;
microSwirl.add(microArc);
const microArcSmall = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.006, 4, 40, Math.PI * 1.18),
microSwirlMaterial
);
microArcSmall.rotation.z = 2.1;
microSwirl.add(microArcSmall);
function makeDiagramLabel(initialText, color, scaleX = 0.76) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 96;
const context = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scaleX, scaleX * 96 / 512, 1);
function setText(text, textColor = color) {
context.clearRect(0, 0, 512, 96);
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "800 38px system-ui, sans-serif";
context.lineJoin = "round";
context.lineWidth = 10;
context.strokeStyle = "rgba(7, 29, 32, 0.72)";
context.strokeText(text, 256, 49);
context.fillStyle = textColor;
context.fillText(text, 256, 49);
texture.needsUpdate = true;
}
setText(initialText);
return { sprite, setText };
}
const stateLabel = makeDiagramLabel("SEPARATED", "#f0cf69", 0.86);
stateLabel.sprite.position.set(-0.58, 0.79, 0.8);
microScene.add(stateLabel.sprite);
const oilLabel = makeDiagramLabel("OIL", "#f0cf69", 0.43);
oilLabel.sprite.position.set(0.72, 0.65, 0.8);
microScene.add(oilLabel.sprite);
const waterLabel = makeDiagramLabel("WATER", "#9cebf0", 0.55);
waterLabel.sprite.position.set(-0.72, -0.78, 0.8);
microScene.add(waterLabel.sprite);
const surfactantLabel = makeDiagramLabel("SURFACTANT", "#91ead8", 0.78);
surfactantLabel.sprite.position.set(0.66, -0.78, 0.8);
microScene.add(surfactantLabel.sprite);
// Magnifying portal.
const portal = new THREE.Group();
portal.position.set(-0.87, 2.34, 0.63);
scene.add(portal);
const portalDisk = new THREE.Mesh(
new THREE.CircleGeometry(1.055, 64),
new THREE.MeshBasicMaterial({
map: microTarget.texture,
side: THREE.DoubleSide,
toneMapped: false
})
);
portalDisk.position.z = 0.002;
portal.add(portalDisk);
const portalRim = new THREE.Mesh(
new THREE.TorusGeometry(1.115, 0.095, 8, 48),
brassMaterial
);
portalRim.castShadow = true;
portal.add(portalRim);
const innerRim = new THREE.Mesh(
new THREE.TorusGeometry(1.025, 0.025, 6, 48),
standard(0x4fbfb0, { metalness: 0.45, roughness: 0.26 })
);
innerRim.position.z = 0.018;
portal.add(innerRim);
cylinderBetween(
new THREE.Vector3(0.78, -0.82, 0),
new THREE.Vector3(1.38, -2.03, 0),
0.135,
woodMaterial,
portal
);
cylinderBetween(
new THREE.Vector3(0.74, -0.78, 0),
new THREE.Vector3(0.93, -1.13, 0),
0.155,
brassMaterial,
portal
);
const glint = new THREE.Mesh(
new THREE.TorusGeometry(0.985, 0.012, 4, 28, Math.PI * 0.32),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.48,
depthWrite: false,
toneMapped: false
})
);
glint.position.z = 0.035;
glint.rotation.z = 2.25;
portal.add(glint);
for (let i = 0; i < 4; i++) {
const angle = i * Math.PI / 2 + Math.PI / 4;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.16, 0.035),
standard(0x69d0c0, { emissive: 0x174f49, emissiveIntensity: 0.5 })
);
tick.position.set(Math.cos(angle) * 1.225, Math.sin(angle) * 1.225, 0.015);
tick.rotation.z = angle - Math.PI / 2;
portal.add(tick);
}
const portalLight = new THREE.PointLight(0x66dfd0, 1.25, 3.2, 2);
portalLight.position.copy(portal.position);
scene.add(portalLight);
// Linked stirring state.
let stirring = false;
let blend = 0;
let stirPhase = 0;
let lastPhase = "";
const unmixedColor = new THREE.Color(0xd7e9e2);
const mixedColor = new THREE.Color(0xefd49c);
const tempColor = new THREE.Color();
function toggleStirring() {
stirring = !stirring;
stirButton.classList.toggle("active", stirring);
stirButton.setAttribute("aria-pressed", String(stirring));
stirLabel.textContent = stirring ? "Pause both scales" : "Stir both scales";
}
stirButton.addEventListener("click", toggleStirring);
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && !event.repeat) {
const tag = document.activeElement?.tagName;
if (tag !== "INPUT" && tag !== "TEXTAREA") {
event.preventDefault();
toggleStirring();
}
}
});
function updateMacroTexture(eased, flowX, flowY) {
const ctx = macroContext;
const size = macroCanvas.width;
tempColor.lerpColors(unmixedColor, mixedColor, eased);
const gradient = ctx.createRadialGradient(
size * 0.42, size * 0.35, size * 0.03,
size * 0.5, size * 0.5, size * 0.72
);
gradient.addColorStop(0, `#${tempColor.clone().offsetHSL(0, 0, 0.035).getHexString()}`);
gradient.addColorStop(1, `#${tempColor.clone().offsetHSL(0, 0, -0.035).getHexString()}`);
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, size, size);
const coordinateScale = 0.87;
const maxCoordinate = 1.08;
for (const descriptor of particleDescriptors) {
const radius = descriptor.currentSize;
if (radius < 0.002) continue;
const x = (descriptor.currentX * coordinateScale / maxCoordinate * 0.5 + 0.5) * size;
const y = (1 - (descriptor.currentY * coordinateScale / maxCoordinate * 0.5 + 0.5)) * size;
const pixelRadius = radius * coordinateScale / maxCoordinate * size * 0.5;
ctx.save();
ctx.globalAlpha = 0.94 - eased * 0.42;
ctx.beginPath();
ctx.ellipse(x, y, pixelRadius * 1.08, pixelRadius * 0.86, descriptor.bias * 0.6, 0, Math.PI * 2);
ctx.fillStyle = "#e3ad35";
ctx.fill();
ctx.lineWidth = Math.max(1.5, pixelRadius * 0.07);
ctx.strokeStyle = "rgba(146, 91, 18, 0.38)";
ctx.stroke();
ctx.beginPath();
ctx.ellipse(
x - pixelRadius * 0.25,
y - pixelRadius * 0.22,
pixelRadius * 0.32,
pixelRadius * 0.14,
-0.4,
0,
Math.PI * 2
);
ctx.fillStyle = "rgba(255, 235, 154, 0.58)";
ctx.fill();
ctx.restore();
}
// Fine suspended specks become visible as the emulsion forms.
ctx.save();
ctx.globalAlpha = 0.11 * eased;
ctx.fillStyle = "#8f6525";
for (let i = 0; i < 85; i++) {
const a = i * 2.399963;
const r = Math.sqrt((i + 0.5) / 85) * size * 0.46;
const x = size / 2 + Math.cos(a) * r;
const y = size / 2 + Math.sin(a) * r;
ctx.fillRect(x, y, 1.2, 1.2);
}
ctx.restore();
macroTexture.needsUpdate = true;
}
function updatePhaseUI() {
const phase = blend < 0.22 ? "Separated" : blend < 0.78 ? "Transition" : "Emulsified";
if (phase === lastPhase) return;
lastPhase = phase;
if (phase === "Separated") {
phaseName.textContent = "Separated";
phaseMacro.textContent = "Large amber oil pools";
phaseMicro.textContent = "8 large oil domains";
stateLabel.setText("SEPARATED", "#f0cf69");
} else if (phase === "Transition") {
phaseName.textContent = "Emulsifying";
phaseMacro.textContent = "Droplets fragmenting";
phaseMicro.textContent = "Droplets redistributing";
stateLabel.setText("EMULSIFYING", "#f5d978");
} else {
phaseName.textContent = "Emulsified";
phaseMacro.textContent = "Creamy fine dispersion";
phaseMicro.textContent = "28 small droplets";
stateLabel.setText("EMULSIFIED", "#8ee8cf");
}
}
// Camera controls are implemented directly without examples or addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbitTheta = THREE.MathUtils.clamp(orbitTheta - dx * 0.0045, -0.18, 0.88);
orbitPhi = THREE.MathUtils.clamp(orbitPhi - dy * 0.0035, 0.76, 1.24);
});
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();
orbitRadius = THREE.MathUtils.clamp(
orbitRadius + event.deltaY * 0.006,
7.4,
13.5
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
orbitTheta = 0.39;
orbitPhi = 1.04;
orbitRadius = 10.1;
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
renderer.setAnimationLoop((time) => {
const dt = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
if (stirring) stirPhase += dt * 3.4;
const targetBlend = stirring ? 1 : 0;
const response = stirring ? 1.55 : 2.15;
blend += (targetBlend - blend) * (1 - Math.exp(-dt * response));
if (Math.abs(targetBlend - blend) < 0.0005) blend = targetBlend;
const eased = blend * blend * (3 - 2 * blend);
const flowStrength = stirring ? 0.034 : 0;
const flowAngle = Math.sin(stirPhase * 0.92) * flowStrength;
for (const descriptor of particleDescriptors) {
descriptor.currentX = THREE.MathUtils.lerp(descriptor.startX, descriptor.endX, eased);
descriptor.currentY = THREE.MathUtils.lerp(descriptor.startY, descriptor.endY, eased);
if (descriptor.initialSize > 0) {
descriptor.currentSize = THREE.MathUtils.lerp(
descriptor.initialSize,
descriptor.finalSize,
eased
);
} else {
descriptor.currentSize = descriptor.finalSize * eased;
}
const x = descriptor.currentX - descriptor.currentY * flowAngle;
const y = descriptor.currentY + descriptor.currentX * flowAngle;
const size = descriptor.currentSize;
descriptor.oilMesh.visible = size > 0.001;
descriptor.shellMesh.visible = size > 0.001;
descriptor.oilMesh.position.set(x, y, descriptor.z);
descriptor.shellMesh.position.set(x, y, descriptor.z + 0.012);
descriptor.oilMesh.scale.setScalar(size);
descriptor.shellMesh.scale.setScalar(size * 1.18);
descriptor.shellMesh.rotation.set(
descriptor.bias * 0.8,
stirPhase * 0.12,
descriptor.bias + stirPhase * 0.06
);
}
updateMacroTexture(eased, 0, 0);
swirlMaterial.opacity = 0.08 + eased * 0.2;
microSwirlMaterial.opacity = 0.05 + eased * 0.24;
macroSwirl.rotation.y = -stirPhase * 0.72;
microSwirl.rotation.z = stirPhase * 0.42;
spoonPivot.rotation.y = -0.55 + (stirring ? stirPhase * 0.75 : 0);
updatePhaseUI();
mixFill.style.width = `${Math.round(blend * 100)}%`;
updateCamera();
portal.quaternion.copy(camera.quaternion);
portalLight.position.copy(portal.position);
renderer.setRenderTarget(microTarget);
renderer.render(microScene, microCamera);
renderer.setRenderTarget(null);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```