Prompt specification
Build or customize the Three.js 3D scene "Water-saving morning routine" 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: Water-saving morning routine
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-saving-morning-routine-x0076
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a toothbrush cup. Stage a bathroom diorama where a character fills a small rinsing cup and closes the faucet; a counter advances only while water visibly flows. Low-poly.
Working Three.js Starter (water-saving-morning-routine-x0076):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Toothbrush Cup — Bathroom Diorama</title>
<style>
:root {
color-scheme: light;
--ink: #233c3c;
--muted: #668080;
--glass: rgba(250, 253, 250, 0.82);
--line: rgba(35, 60, 60, 0.13);
--teal: #177e78;
--water: #36b9d1;
--coral: #ef765f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 26px;
left: 28px;
width: min(370px, calc(100vw - 56px));
padding: 20px 22px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--glass);
box-shadow: 0 18px 50px rgba(37, 70, 68, 0.12);
backdrop-filter: blur(14px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 21px;
height: 2px;
content: "";
border-radius: 2px;
background: var(--teal);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 39px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.style-pill {
position: absolute;
top: 28px;
right: 28px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--glass);
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.bottom-panel {
position: absolute;
right: 28px;
bottom: 25px;
left: 28px;
display: grid;
grid-template-columns: minmax(240px, 1fr) 210px auto;
gap: 20px;
align-items: center;
max-width: 940px;
margin: 0 auto;
padding: 17px 19px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--glass);
box-shadow: 0 18px 50px rgba(37, 70, 68, 0.14);
backdrop-filter: blur(15px);
pointer-events: auto;
}
.status-row {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
font-size: 13px;
font-weight: 750;
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: #9fb0ad;
box-shadow: 0 0 0 4px rgba(159, 176, 173, 0.12);
transition: 180ms ease;
}
.status-dot.flowing {
background: var(--water);
box-shadow: 0 0 0 4px rgba(54, 185, 209, 0.16);
animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
50% {
box-shadow: 0 0 0 7px rgba(54, 185, 209, 0.05);
}
}
.timeline {
position: relative;
height: 5px;
overflow: hidden;
border-radius: 99px;
background: rgba(35, 60, 60, 0.1);
}
.timeline-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #287c76, #58c7c1);
}
.stage-note {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.02em;
}
.counter {
padding-left: 20px;
border-left: 1px solid var(--line);
}
.counter-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.counter-value {
margin-top: 3px;
font-variant-numeric: tabular-nums;
font-size: 29px;
font-weight: 780;
letter-spacing: -0.05em;
}
.counter-value small {
color: var(--muted);
font-size: 12px;
font-weight: 650;
letter-spacing: 0;
}
.counter-note {
color: var(--muted);
font-size: 9px;
}
.counter.live .counter-value {
color: #078f9c;
}
.buttons {
display: flex;
gap: 8px;
}
button {
min-height: 40px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 750;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-1px);
background: white;
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: var(--teal);
background: var(--teal);
color: white;
}
.camera-hint {
position: absolute;
right: 28px;
bottom: 126px;
padding: 7px 10px;
border-radius: 9px;
background: rgba(250, 253, 250, 0.62);
color: var(--muted);
font-size: 9px;
letter-spacing: 0.04em;
backdrop-filter: blur(8px);
}
@media (max-width: 720px) {
.title-card {
top: 16px;
left: 16px;
padding: 16px 18px;
}
.subtitle,
.style-pill,
.camera-hint {
display: none;
}
.bottom-panel {
right: 12px;
bottom: 12px;
left: 12px;
grid-template-columns: 1fr auto;
gap: 12px;
padding: 13px;
}
.counter {
padding-left: 12px;
}
.counter-note,
.stage-note {
display: none;
}
.buttons {
grid-column: 1 / -1;
}
button {
flex: 1;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive low-poly bathroom diorama. Drag to orbit and use the mouse wheel to zoom."
></canvas>
<div class="hud">
<section class="title-card">
<div class="eyebrow">Morning routine · Diorama 01</div>
<h1>Toothbrush Cup</h1>
<p class="subtitle">A small rinse, carefully timed. The water counter advances only while the stream is visible.</p>
</section>
<div class="style-pill">Low-poly bathroom</div>
<div class="camera-hint">Drag to orbit · Wheel to zoom · Double-click to reset</div>
<section class="bottom-panel">
<div>
<div class="status-row">
<span class="status-dot" id="statusDot"></span>
<span id="statusText">Ready</span>
</div>
<div class="timeline">
<div class="timeline-fill" id="timelineFill"></div>
</div>
<div class="stage-note" id="stageNote">The faucet begins closed.</div>
</div>
<div class="counter" id="counterCard">
<div class="counter-label">Flow counter</div>
<div class="counter-value"><span id="counterValue">0.00</span> <small>L</small></div>
<div class="counter-note" id="counterNote">Frozen while stream is hidden</div>
</div>
<div class="buttons">
<button class="primary" id="pauseButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay</button>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and camera
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
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("#dce8e5");
scene.fog = new THREE.Fog("#dce8e5", 17, 29);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0.15, 2.25, -0.75);
const cameraSpherical = new THREE.Spherical(12.6, 1.18, 0.62);
function updateCamera() {
camera.position.setFromSpherical(cameraSpherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight("#f6fffb", "#8ca29c", 2.2));
const sun = new THREE.DirectionalLight("#fff3da", 3.4);
sun.position.set(6.5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b9e8ef", 1.1);
fill.position.set(-6, 5, 2);
scene.add(fill);
// ---------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function addMesh(geometry, mat, position, parent = scene) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function box(width, height, depth, mat, x, y, z, parent = scene) {
return addMesh(
new THREE.BoxGeometry(width, height, depth),
mat,
new THREE.Vector3(x, y, z),
parent
);
}
function setSegment(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = Math.max(direction.length(), 0.0001);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
Y_AXIS,
direction.normalize()
);
mesh.scale.y = length;
}
function segment(a, b, radius, mat, parent = scene, sides = 8) {
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, 1, sides),
mat,
new THREE.Vector3(),
parent
);
setSegment(mesh, a, b);
return mesh;
}
function smooth01(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function smoother01(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * x * (x * (x * 6 - 15) + 10);
}
const palette = {
wall: material("#dce7df"),
wallSide: material("#cbdad4"),
floor: material("#c8d2ca"),
base: material("#a9bbb4"),
cabinet: material("#91aaa2"),
cabinetDark: material("#748e87"),
counter: material("#f4efe3"),
porcelain: material("#f8fbf6"),
basin: material("#94b8b4"),
basinDark: material("#547e7b"),
chrome: material("#b8c9c6", {
roughness: 0.28,
metalness: 0.72
}),
chromeDark: material("#6c817e", {
roughness: 0.35,
metalness: 0.55
}),
coral: material("#ec765f"),
teal: material("#277f79"),
yellow: material("#f1c85c"),
purple: material("#9477ad"),
skin: material("#c98765"),
skinLight: material("#dda17d"),
hair: material("#3e3733"),
shirt: material("#3e9690"),
pants: material("#344d56"),
mirror: material("#b7d4d1", {
roughness: 0.22,
metalness: 0.38
}),
towel: material("#e6a588"),
white: material("#fffdf5"),
plant: material("#6f9f71"),
pot: material("#c88468")
};
// ---------------------------------------------------------------------
// Bathroom architecture
// ---------------------------------------------------------------------
box(11.4, 0.25, 7.2, palette.base, 0, -0.15, -0.25);
box(11.1, 0.09, 6.9, palette.floor, 0, 0.015, -0.25);
box(11.1, 6.2, 0.18, palette.wall, 0, 3.1, -3.58);
box(0.18, 6.2, 6.7, palette.wallSide, -5.48, 3.1, -0.28);
// Wall trim and simple tile lines.
box(10.9, 0.12, 0.08, palette.base, 0, 2.72, -3.47);
for (let x = -4.5; x <= 4.5; x += 1.5) {
box(0.025, 2.55, 0.025, palette.wallSide, x, 1.35, -3.47);
}
for (let y = 0.7; y <= 2.7; y += 0.66) {
box(10.9, 0.025, 0.025, palette.wallSide, 0, y, -3.47);
}
// Bath mat.
const matMaterial = material("#dfb889");
const bathMat = box(3.1, 0.035, 1.35, matMaterial, 0.5, 0.075, 0.15);
bathMat.receiveShadow = true;
for (let i = 0; i < 11; i++) {
box(0.22, 0.04, 0.025, palette.white, -0.85 + i * 0.27, 0.096, 0.15);
}
// Vanity.
box(5.55, 2.12, 1.52, palette.cabinet, 0.2, 1.1, -2.18);
box(5.82, 0.18, 1.86, palette.counter, 0.2, 2.29, -2.14);
box(5.38, 0.12, 1.38, palette.cabinetDark, 0.2, 0.12, -2.15);
for (let i = 0; i < 3; i++) {
const x = -1.58 + i * 1.78;
box(1.63, 1.78, 0.07, i === 1 ? palette.cabinet : palette.cabinetDark, x, 1.18, -1.385);
box(0.28, 0.045, 0.075, palette.chrome, x, 1.45, -1.315);
}
// Sink, with a faceted elliptical bowl.
const basin = addMesh(
new THREE.CylinderGeometry(0.76, 0.59, 0.2, 16, 1, true),
palette.basin,
new THREE.Vector3(0.55, 2.43, -1.67)
);
basin.scale.z = 0.68;
const basinBottom = addMesh(
new THREE.CylinderGeometry(0.6, 0.6, 0.035, 16),
palette.basinDark,
new THREE.Vector3(0.55, 2.405, -1.67)
);
basinBottom.scale.z = 0.68;
const sinkRim = addMesh(
new THREE.TorusGeometry(0.765, 0.085, 6, 18),
palette.porcelain,
new THREE.Vector3(0.55, 2.52, -1.67)
);
sinkRim.rotation.x = Math.PI / 2;
sinkRim.scale.y = 0.68;
// Mirror.
const mirrorFrame = addMesh(
new THREE.TorusGeometry(1.08, 0.09, 8, 24),
palette.chromeDark,
new THREE.Vector3(0.75, 4.48, -3.43)
);
addMesh(
new THREE.CircleGeometry(1.03, 24),
palette.mirror,
new THREE.Vector3(0.75, 4.48, -3.46)
);
// Small wall shelf and bottles.
box(1.55, 0.09, 0.42, palette.counter, -2.62, 4.56, -3.28);
box(0.27, 0.58, 0.25, palette.yellow, -3.05, 4.88, -3.27);
box(0.22, 0.45, 0.22, palette.coral, -2.62, 4.8, -3.27);
box(0.18, 0.68, 0.18, palette.teal, -2.25, 4.94, -3.27);
// Towel rail.
segment(
new THREE.Vector3(-4.15, 3.45, -3.34),
new THREE.Vector3(-3.25, 3.45, -3.34),
0.035,
palette.chrome
);
box(0.68, 1.15, 0.065, palette.towel, -3.72, 2.88, -3.27);
// Soap dispenser and folded towels.
box(0.42, 0.45, 0.34, palette.coral, -1.96, 2.61, -1.61);
box(0.12, 0.2, 0.12, palette.chromeDark, -1.96, 2.9, -1.61);
box(0.82, 0.13, 0.48, palette.white, -1.72, 2.43, -2.48);
box(0.72, 0.12, 0.43, palette.towel, -1.72, 2.56, -2.48);
// Stylized toilet at the right.
box(1.02, 1.34, 0.6, palette.porcelain, 4.18, 1.25, -2.89);
const toiletBody = addMesh(
new THREE.CylinderGeometry(0.53, 0.37, 0.63, 10),
palette.porcelain,
new THREE.Vector3(4.18, 0.58, -2.19)
);
toiletBody.scale.z = 1.18;
const toiletSeat = addMesh(
new THREE.TorusGeometry(0.46, 0.11, 6, 14),
palette.porcelain,
new THREE.Vector3(4.18, 0.9, -2.19)
);
toiletSeat.rotation.x = Math.PI / 2;
toiletSeat.scale.y = 1.16;
const toiletLid = addMesh(
new THREE.CylinderGeometry(0.48, 0.48, 0.07, 14),
palette.porcelain,
new THREE.Vector3(4.18, 0.97, -2.19)
);
toiletLid.scale.z = 1.16;
// Small plant on the vanity.
addMesh(
new THREE.CylinderGeometry(0.21, 0.16, 0.32, 8),
palette.pot,
new THREE.Vector3(3.0, 2.54, -2.43)
);
for (let i = 0; i < 5; i++) {
const angle = (i / 5) * Math.PI * 2;
const leaf = addMesh(
new THREE.SphereGeometry(0.18, 7, 5),
palette.plant,
new THREE.Vector3(
3.0 + Math.cos(angle) * 0.15,
2.82 + (i % 2) * 0.08,
-2.43 + Math.sin(angle) * 0.13
)
);
leaf.scale.set(0.65, 1.5, 0.65);
leaf.rotation.z = Math.cos(angle) * 0.5;
}
// ---------------------------------------------------------------------
// Faucet
// ---------------------------------------------------------------------
const faucetX = 0.55;
const faucetZ = -2.24;
const spoutZ = -1.64;
const nozzleY = 3.26;
addMesh(
new THREE.CylinderGeometry(0.19, 0.22, 0.09, 10),
palette.chromeDark,
new THREE.Vector3(faucetX, 2.42, faucetZ)
);
segment(
new THREE.Vector3(faucetX, 2.43, faucetZ),
new THREE.Vector3(faucetX, 3.43, faucetZ),
0.075,
palette.chrome
);
segment(
new THREE.Vector3(faucetX, 3.43, faucetZ),
new THREE.Vector3(faucetX, 3.43, spoutZ),
0.075,
palette.chrome
);
segment(
new THREE.Vector3(faucetX, 3.43, spoutZ),
new THREE.Vector3(faucetX, nozzleY, spoutZ),
0.065,
palette.chromeDark
);
const nozzle = addMesh(
new THREE.CylinderGeometry(0.085, 0.085, 0.07, 10),
palette.chrome,
new THREE.Vector3(faucetX, nozzleY, spoutZ)
);
// Lever handle pivots around Z.
const leverPivot = new THREE.Group();
leverPivot.position.set(faucetX, 3.42, faucetZ);
scene.add(leverPivot);
addMesh(
new THREE.CylinderGeometry(0.11, 0.11, 0.08, 10),
palette.chromeDark,
new THREE.Vector3(0, 0, 0),
leverPivot
);
segment(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(-0.35, 0, 0),
0.035,
palette.chrome,
leverPivot
);
addMesh(
new THREE.SphereGeometry(0.07, 8, 6),
palette.coral,
new THREE.Vector3(-0.35, 0, 0),
leverPivot
);
leverPivot.rotation.z = 0.75;
// ---------------------------------------------------------------------
// Toothbrush holder and toothbrushes
// ---------------------------------------------------------------------
const toothbrushCup = new THREE.Group();
toothbrushCup.position.set(1.72, 2.39, -1.45);
scene.add(toothbrushCup);
addMesh(
new THREE.CylinderGeometry(0.34, 0.27, 0.59, 10, 1, true),
palette.coral,
new THREE.Vector3(0, 0.295, 0),
toothbrushCup
);
addMesh(
new THREE.CylinderGeometry(0.27, 0.27, 0.055, 10),
palette.coral,
new THREE.Vector3(0, 0.027, 0),
toothbrushCup
);
const brushCupRim = addMesh(
new THREE.TorusGeometry(0.34, 0.026, 6, 14),
palette.coral,
new THREE.Vector3(0, 0.585, 0),
toothbrushCup
);
brushCupRim.rotation.x = Math.PI / 2;
const toothbrushData = [
{ x: -0.13, z: 0.02, angle: -0.1, color: palette.teal },
{ x: 0.01, z: -0.03, angle: 0.08, color: palette.yellow },
{ x: 0.14, z: 0.04, angle: 0.16, color: palette.purple }
];
for (const data of toothbrushData) {
const brush = new THREE.Group();
brush.position.set(data.x, 0.25, data.z);
brush.rotation.z = data.angle;
toothbrushCup.add(brush);
addMesh(
new THREE.CylinderGeometry(0.024, 0.03, 0.8, 7),
data.color,
new THREE.Vector3(0, 0.4, 0),
brush
);
addMesh(
new THREE.BoxGeometry(0.09, 0.15, 0.06),
palette.white,
new THREE.Vector3(0, 0.84, 0),
brush
);
for (let i = 0; i < 3; i++) {
box(0.018, 0.1, 0.025, palette.chromeDark, -0.026 + i * 0.026, 0.85, 0.045, brush);
}
}
// ---------------------------------------------------------------------
// Character
// ---------------------------------------------------------------------
const character = new THREE.Group();
character.position.set(-0.25, 0, -2.83);
scene.add(character);
addMesh(
new THREE.BoxGeometry(0.75, 0.88, 0.4),
palette.shirt,
new THREE.Vector3(0, 3.55, 0),
character
);
addMesh(
new THREE.CylinderGeometry(0.13, 0.15, 0.18, 8),
palette.skin,
new THREE.Vector3(0, 4.03, 0),
character
);
addMesh(
new THREE.SphereGeometry(0.35, 12, 8),
palette.skinLight,
new THREE.Vector3(0, 4.32, 0),
character
);
const hairCap = addMesh(
new THREE.SphereGeometry(0.37, 12, 8, 0, Math.PI * 2, 0, Math.PI * 0.58),
palette.hair,
new THREE.Vector3(0, 4.36, -0.015),
character
);
addMesh(
new THREE.SphereGeometry(0.17, 8, 6),
palette.hair,
new THREE.Vector3(0.25, 4.49, -0.12),
character
);
const eyeLeft = addMesh(
new THREE.SphereGeometry(0.025, 7, 5),
palette.hair,
new THREE.Vector3(-0.115, 4.35, 0.324),
character
);
const eyeRight = addMesh(
new THREE.SphereGeometry(0.025, 7, 5),
palette.hair,
new THREE.Vector3(0.115, 4.35, 0.324),
character
);
addMesh(
new THREE.SphereGeometry(0.055, 7, 5),
palette.skin,
new THREE.Vector3(0, 4.27, 0.35),
character
);
box(0.09, 0.018, 0.018, palette.hair, 0, 4.17, 0.337, character);
// Collar detail.
box(0.31, 0.075, 0.035, palette.white, 0, 4.0, 0.215, character);
// ---------------------------------------------------------------------
// Arms: cylinders are repositioned each frame with a simple 2-bone IK.
// ---------------------------------------------------------------------
const upperLeft = segment(
new THREE.Vector3(),
new THREE.Vector3(0, 1, 0),
0.095,
palette.shirt
);
const lowerLeft = segment(
new THREE.Vector3(),
new THREE.Vector3(0, 1, 0),
0.073,
palette.skin
);
const upperRight = segment(
new THREE.Vector3(),
new THREE.Vector3(0, 1, 0),
0.095,
palette.shirt
);
const lowerRight = segment(
new THREE.Vector3(),
new THREE.Vector3(0, 1, 0),
0.073,
palette.skin
);
const leftHand = addMesh(
new THREE.SphereGeometry(0.095, 8, 6),
palette.skinLight,
new THREE.Vector3()
);
const rightHand = addMesh(
new THREE.SphereGeometry(0.095, 8, 6),
palette.skinLight,
new THREE.Vector3()
);
const leftShoulderMarker = addMesh(
new THREE.SphereGeometry(0.12, 8, 6),
palette.shirt,
new THREE.Vector3()
);
const rightShoulderMarker = addMesh(
new THREE.SphereGeometry(0.12, 8, 6),
palette.shirt,
new THREE.Vector3()
);
function solveElbow(shoulder, target, bendDirection) {
const delta = new THREE.Vector3().subVectors(target, shoulder);
const distance = Math.max(delta.length(), 0.0001);
const direction = delta.clone().normalize();
let upperLength = 0.9;
let lowerLength = 0.9;
if (distance > upperLength + lowerLength) {
upperLength = lowerLength = distance * 0.5;
}
const along =
(upperLength * upperLength - lowerLength * lowerLength + distance * distance) /
(2 * distance);
const perpendicular = bendDirection
.clone()
.addScaledVector(direction, -bendDirection.dot(direction));
if (perpendicular.lengthSq() < 0.00001) {
perpendicular.set(0, 0, 1).cross(direction);
}
perpendicular.normalize();
const height = Math.sqrt(Math.max(0, upperLength * upperLength - along * along));
return shoulder
.clone()
.addScaledVector(direction, along)
.addScaledVector(perpendicular, height);
}
function updateArm(
upper,
lower,
hand,
shoulderMarker,
shoulder,
target,
bend
) {
const elbow = solveElbow(shoulder, target, bend);
setSegment(upper, shoulder, elbow);
setSegment(lower, elbow, target);
hand.position.copy(target);
shoulderMarker.position.copy(shoulder);
}
// ---------------------------------------------------------------------
// Rinsing cup and water
// ---------------------------------------------------------------------
const rinseCup = new THREE.Group();
scene.add(rinseCup);
const rinseMaterial = material("#f1cf72");
addMesh(
new THREE.CylinderGeometry(0.255, 0.205, 0.43, 10, 1, true),
rinseMaterial,
new THREE.Vector3(0, 0.215, 0),
rinseCup
);
addMesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.05, 10),
rinseMaterial,
new THREE.Vector3(0, 0.025, 0),
rinseCup
);
const rinseRim = addMesh(
new THREE.TorusGeometry(0.255, 0.025, 6, 14),
palette.white,
new THREE.Vector3(0, 0.43, 0),
rinseCup
);
rinseRim.rotation.x = Math.PI / 2;
const waterMaterial = material("#42bed1", {
transparent: true,
opacity: 0.78,
roughness: 0.28,
metalness: 0.04
});
const cupWater = addMesh(
new THREE.CylinderGeometry(0.218, 0.19, 1, 10),
waterMaterial,
new THREE.Vector3(0, 0.025, 0),
rinseCup
);
const cupStart = new THREE.Vector3(-0.65, 2.39, -1.18);
const cupUnderFaucet = new THREE.Vector3(faucetX, 2.41, spoutZ);
const cupFinal = new THREE.Vector3(0.68, 2.39, -1.16);
rinseCup.position.copy(cupStart);
// Visible falling stream.
const streamMaterial = material("#52cbe0", {
transparent: true,
opacity: 0.72,
roughness: 0.18,
metalness: 0.02,
depthWrite: false
});
const stream = addMesh(
new THREE.CylinderGeometry(0.035, 0.048, 1, 8),
streamMaterial,
new THREE.Vector3(),
scene
);
stream.castShadow = false;
stream.visible = false;
const droplets = [];
for (let i = 0; i < 15; i++) {
const drop = addMesh(
new THREE.SphereGeometry(0.025, 6, 4),
streamMaterial,
new THREE.Vector3(),
scene
);
drop.castShadow = false;
drop.visible = false;
droplets.push({
mesh: drop,
phase: i / 15,
angle: (i * 2.399),
radius: 0.025 + (i % 3) * 0.012
});
}
const splash = addMesh(
new THREE.TorusGeometry(0.15, 0.012, 5, 12),
streamMaterial,
new THREE.Vector3(),
scene
);
splash.rotation.x = Math.PI / 2;
splash.castShadow = false;
splash.visible = false;
// ---------------------------------------------------------------------
// Routine state
// ---------------------------------------------------------------------
const FLOW_LITERS_PER_SECOND = 0.055;
let routineTime = 0;
let playing = true;
let counterLiters = 0;
let streamStrength = 0;
const pauseButton = document.querySelector("#pauseButton");
const replayButton = document.querySelector("#replayButton");
const statusText = document.querySelector("#statusText");
const statusDot = document.querySelector("#statusDot");
const stageNote = document.querySelector("#stageNote");
const timelineFill = document.querySelector("#timelineFill");
const counterValue = document.querySelector("#counterValue");
const counterCard = document.querySelector("#counterCard");
const counterNote = document.querySelector("#counterNote");
function setPlaying(value) {
playing = value;
pauseButton.textContent = playing ? "Pause" : "Resume";
}
pauseButton.addEventListener("click", () => {
setPlaying(!playing);
});
replayButton.addEventListener("click", () => {
routineTime = 0;
counterLiters = 0;
streamStrength = 0;
rinseCup.position.copy(cupStart);
leverPivot.rotation.z = 0.75;
stream.visible = false;
splash.visible = false;
droplets.forEach((drop) => (drop.mesh.visible = false));
setPlaying(true);
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
}
if (event.key.toLowerCase() === "r") {
replayButton.click();
}
});
function routineState(time) {
const reach = smoother01(time / 1.45);
const opening = smooth01((time - 1.45) / 0.62);
const closing = smooth01((time - 7.2) / 0.66);
const openAmount = opening * (1 - closing);
if (time < 1.45) {
rinseCup.position.lerpVectors(cupStart, cupUnderFaucet, reach);
} else if (time < 7.86) {
rinseCup.position.copy(cupUnderFaucet);
} else {
const place = smoother01((time - 7.86) / 1.55);
rinseCup.position.lerpVectors(cupUnderFaucet, cupFinal, place);
}
// The lever moves from raised/open to lowered/closed.
leverPivot.rotation.z = 0.75 - openAmount * 1.3;
leverPivot.updateMatrixWorld(true);
const leverTip = new THREE.Vector3(-0.35, 0, 0)
.applyMatrix4(leverPivot.matrixWorld);
const moveToLever = smooth01((time - 1.16) / 0.66);
const leaveLever = smooth01((time - 7.88) / 0.8);
const handleBlend = moveToLever * (1 - leaveLever);
return {
reach,
openAmount,
leverTip,
handleBlend
};
}
function updateCharacter(time, state) {
const breathing = Math.sin(time * 2.1) * 0.008;
character.position.y = breathing;
character.position.x = -0.25 + state.reach * 0.045;
character.updateMatrixWorld(true);
const leftShoulder = new THREE.Vector3(-0.37, 3.76, 0.02)
.applyMatrix4(character.matrixWorld);
const rightShoulder = new THREE.Vector3(0.37, 3.76, 0.02)
.applyMatrix4(character.matrixWorld);
const cupHandTarget = rinseCup.position
.clone()
.add(new THREE.Vector3(-0.285, 0.18, 0.035));
const restingRightHand = new THREE.Vector3(0.5, 3.18, 0.34)
.applyMatrix4(character.matrixWorld);
const rightHandTarget = restingRightHand.lerp(
state.leverTip,
state.handleBlend
);
updateArm(
upperLeft,
lowerLeft,
leftHand,
leftShoulderMarker,
leftShoulder,
cupHandTarget,
new THREE.Vector3(-0.15, -0.8, 0.6)
);
updateArm(
upperRight,
lowerRight,
rightHand,
rightShoulderMarker,
rightShoulder,
rightHandTarget,
new THREE.Vector3(0.4, -0.75, 0.5)
);
// Small, periodic blink.
const blinkPhase = time % 4.7;
const blinkScale =
blinkPhase > 4.48 && blinkPhase < 4.59 ? 0.12 : 1;
eyeLeft.scale.y = blinkScale;
eyeRight.scale.y = blinkScale;
}
function updateWater(time, state) {
streamStrength = state.openAmount;
// This visibility test is intentionally the sole gate for the counter.
stream.visible = streamStrength > 0.035;
const fillHeight = THREE.MathUtils.clamp(
(counterLiters / 0.34) * 0.285,
0.001,
0.285
);
cupWater.scale.y = fillHeight;
cupWater.position.y = 0.045 + fillHeight * 0.5;
const surfaceY = rinseCup.position.y + 0.045 + fillHeight;
const streamBottom = surfaceY;
const streamLength = Math.max(nozzleY - streamBottom, 0.01);
stream.position.set(faucetX, (nozzleY + streamBottom) * 0.5, spoutZ);
stream.scale.set(1, streamLength, 1);
streamMaterial.opacity = 0.24 + streamStrength * 0.53;
for (const drop of droplets) {
drop.mesh.visible = stream.visible;
if (stream.visible) {
const travel = (time * 1.65 + drop.phase) % 1;
const y = nozzleY - travel * streamLength;
const angle = drop.angle + time * 0.8;
const radius = drop.radius * Math.sin(travel * Math.PI);
drop.mesh.position.set(
faucetX + Math.cos(angle) * radius,
y,
spoutZ + Math.sin(angle) * radius
);
drop.mesh.scale.setScalar(0.72 + travel * 0.45);
}
}
splash.visible = stream.visible;
if (splash.visible) {
splash.position.set(faucetX, surfaceY + 0.008, spoutZ);
const pulse = 1 + Math.sin(time * 14) * 0.12;
splash.scale.set(pulse, pulse, pulse);
splashMaterialOpacity();
}
}
function splashMaterialOpacity() {
streamMaterial.opacity = Math.max(streamMaterial.opacity, 0.56);
}
function updateHUD(streamVisible) {
let title;
let note;
if (routineTime < 1.45) {
title = "Reaching for the rinse cup";
note = "The cup moves from the counter to the basin.";
} else if (routineTime < 2.07) {
title = "Opening the faucet";
note = "The counter starts when the water becomes visible.";
} else if (routineTime < 7.2) {
title = "Water is flowing";
note = "The live counter advances only with the visible stream.";
} else if (routineTime < 7.86) {
title = "Closing the faucet";
note = "The stream thins, then disappears as the lever closes.";
} else if (routineTime < 9.5) {
title = "Setting the cup down";
note = "The faucet is closed and the counter is frozen.";
} else {
title = "Routine complete";
note = "The toothbrush cup is ready for the morning.";
}
if (!playing) {
title = "Paused";
note = "Camera controls remain available.";
}
statusText.textContent = title;
stageNote.textContent = note;
statusDot.classList.toggle("flowing", streamVisible && playing);
counterValue.textContent = counterLiters.toFixed(2);
counterCard.classList.toggle("live", streamVisible && playing);
counterNote.textContent = streamVisible && playing
? "Counting visible flow"
: "Frozen while stream is hidden";
const progress = THREE.MathUtils.clamp(routineTime / 9.5, 0, 1);
timelineFill.style.width = `${progress * 100}%`;
}
// ---------------------------------------------------------------------
// Custom camera controls: no addons are used.
// ---------------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraSpherical.theta -= dx * 0.006;
cameraSpherical.phi = THREE.MathUtils.clamp(
cameraSpherical.phi - dy * 0.005,
0.55,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.style.cursor = "grab";
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.style.cursor = "grab";
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraSpherical.radius = THREE.MathUtils.clamp(
cameraSpherical.radius + event.deltaY * 0.008,
7.3,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraSpherical.set(12.6, 1.18, 0.62);
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const rawDelta = Math.min(clock.getDelta(), 0.05);
const delta = playing ? rawDelta : 0;
routineTime += delta;
const state = routineState(routineTime);
updateCharacter(routineTime, state);
updateWater(routineTime, state);
// IMPORTANT: the counter cannot advance unless the water mesh is visible.
if (stream.visible && streamStrength > 0.035) {
counterLiters += delta * FLOW_LITERS_PER_SECOND;
}
updateHUD(stream.visible && playing);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```