Prompt specification
Build or customize the Three.js 3D scene "Window cleaning workstation" 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: Window cleaning workstation
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/window-cleaning-workstation-x0067
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Arrange a window, cloth, bucket, and spray bottle; animate distinct spray and wipe phases so a transparent clean patch expands across the marked pane.
Working Three.js Starter (window-cleaning-workstation-x0067):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Window Cleaning</title>
<style>
:root {
color-scheme: light;
--ink: #18332f;
--muted: #58716c;
--panel: rgba(248, 252, 248, 0.86);
--line: rgba(24, 51, 47, 0.14);
--accent: #e98948;
--cyan: #39aeb6;
--cream: #fff8e9;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe9e3;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.header {
position: absolute;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
}
.brand {
padding: 13px 16px 14px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 12px 32px rgba(35, 67, 58, 0.09);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(17px, 2vw, 23px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
}
.phase-card {
position: absolute;
top: 112px;
left: 24px;
width: min(292px, calc(100vw - 48px));
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 12px 32px rgba(35, 67, 58, 0.09);
backdrop-filter: blur(12px);
}
.phase-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.phase-name {
display: flex;
align-items: center;
gap: 9px;
font-size: 14px;
font-weight: 800;
letter-spacing: 0.03em;
text-transform: uppercase;
}
.phase-dot {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(233, 137, 72, 0.13);
}
.phase-card[data-phase="wipe"] .phase-dot {
background: var(--cyan);
box-shadow: 0 0 0 4px rgba(57, 174, 182, 0.13);
}
.phase-card[data-phase="clean"] .phase-dot {
background: #64a96f;
box-shadow: 0 0 0 4px rgba(100, 169, 111, 0.13);
}
.clean-value {
color: var(--muted);
font-size: 12px;
font-variant-numeric: tabular-nums;
}
.phase-hint {
min-height: 18px;
margin-top: 8px;
color: var(--muted);
font-size: 12px;
}
.progress-track {
position: relative;
height: 7px;
margin-top: 14px;
overflow: hidden;
border-radius: 999px;
background: rgba(24, 51, 47, 0.1);
}
.progress-fill {
position: absolute;
inset: 0 auto 0 0;
width: 0%;
border-radius: inherit;
background: linear-gradient(90deg, #ef9d5f, #f3bc76);
transition: background 200ms ease;
}
.phase-card[data-phase="wipe"] .progress-fill {
background: linear-gradient(90deg, #2da6ae, #71c8c8);
}
.phase-card[data-phase="clean"] .progress-fill {
background: linear-gradient(90deg, #579b65, #92c487);
}
.legend {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #78908a;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.controls {
position: absolute;
right: 24px;
bottom: 22px;
display: flex;
gap: 9px;
pointer-events: auto;
}
button {
min-height: 42px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 8px 24px rgba(35, 67, 58, 0.09);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
transform 140ms ease,
background 140ms ease;
}
button:hover {
transform: translateY(-2px);
background: #ffffff;
}
button:active {
transform: translateY(0);
}
.instructions {
position: absolute;
left: 24px;
bottom: 25px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(248, 252, 248, 0.78);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
}
.instructions strong {
color: var(--ink);
font-weight: 750;
}
@media (max-width: 700px) {
.header {
top: 14px;
left: 14px;
right: 14px;
}
.subtitle {
display: none;
}
.phase-card {
top: 90px;
left: 14px;
width: min(270px, calc(100vw - 28px));
}
.controls {
right: 14px;
bottom: 14px;
}
.instructions {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Animated voxel window-cleaning scene"></main>
<div class="hud">
<header class="header">
<div class="brand">
<div class="eyebrow">Voxel Maintenance Unit 04</div>
<h1>Window Cleaning Cycle</h1>
<div class="subtitle">Spray → wipe → transparent reveal</div>
</div>
</header>
<section id="phaseCard" class="phase-card" data-phase="spray">
<div class="phase-row">
<div class="phase-name">
<span class="phase-dot"></span>
<span id="phaseText">Misting</span>
</div>
<div id="cleanValue" class="clean-value">0% clear</div>
</div>
<div id="phaseHint" class="phase-hint">
Applying cleaner to the marked pane…
</div>
<div class="progress-track">
<div id="progressFill" class="progress-fill"></div>
</div>
<div class="legend">
<span>Spray</span>
<span>Wipe</span>
<span>Reveal</span>
</div>
</section>
<div class="instructions">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom ·
<strong>Double-click</strong> to reset
</div>
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay cycle</button>
</div>
</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 host = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdceae4);
scene.fog = new THREE.Fog(0xdceae4, 18, 31);
const camera = new THREE.OrthographicCamera(-7, 7, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.8, -0.1);
let cameraTheta = 0.48;
let cameraPhi = 1.04;
let cameraZoom = 1;
function updateCamera() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const baseHeight = aspect < 0.82 ? 11.8 : 9.0;
const viewHeight = baseHeight / cameraZoom;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
const distance = 17;
camera.position.set(
cameraTarget.x +
distance * Math.sin(cameraTheta) * Math.sin(cameraPhi),
cameraTarget.y + distance * Math.cos(cameraPhi),
cameraTarget.z +
distance * Math.cos(cameraTheta) * Math.sin(cameraPhi)
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
}
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight, false);
updateCamera();
}
window.addEventListener("resize", resize);
updateCamera();
// Custom camera controls; no addon modules are used.
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + deltaY * 0.005,
0.78,
1.25
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.48;
cameraPhi = 1.04;
cameraZoom = 1;
updateCamera();
});
// -------------------------------------------------------------------------
// Lighting and reusable materials
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x647a70, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d8, 3.4);
keyLight.position.set(6, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xa7e5ea, 1.25);
fillLight.position.set(-7, 5, 5);
scene.add(fillLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
...options
});
}
const materials = {
floor: standardMaterial(0xc8b99d),
floorAlt: standardMaterial(0xd7c9ad),
frame: standardMaterial(0x315e59),
frameLight: standardMaterial(0x47766e),
sill: standardMaterial(0x9a6c4f),
bucket: standardMaterial(0x58aeb2),
bucketDark: standardMaterial(0x31878d),
bucketRim: standardMaterial(0x83c9c6),
bottle: standardMaterial(0x9cd7bd),
bottleDark: standardMaterial(0x5ba88c),
nozzle: standardMaterial(0x273c3a),
orange: standardMaterial(0xe98b4b),
cream: standardMaterial(0xfff2d2),
clothA: standardMaterial(0xf29a7d),
clothB: standardMaterial(0xffd1ad),
dark: standardMaterial(0x263b39)
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
typeof material === "number"
? standardMaterial(material)
: material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Deterministic randomness keeps the scene repeatable.
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6d2b79f5;
let result = value;
result = Math.imul(result ^ (result >>> 15), result | 1);
result ^= result + Math.imul(result ^ (result >>> 7), result | 61);
return ((result ^ (result >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(1984);
// -------------------------------------------------------------------------
// Room, tiled floor, and exterior voxel skyline
// -------------------------------------------------------------------------
addBox(
scene,
[14, 0.24, 10],
[0, -0.16, 0.5],
materials.floor,
false
);
for (let row = 0; row < 8; row++) {
for (let column = 0; column < 13; column++) {
const material =
(row + column) % 2 === 0
? materials.floor
: materials.floorAlt;
addBox(
scene,
[0.96, 0.035, 0.96],
[-5.85 + column, -0.018, -2.7 + row],
material,
false
);
}
}
addBox(
scene,
[14, 8, 0.2],
[0, 3.7, -3.25],
standardMaterial(0x91cbd1),
false
);
const buildingColors = [
0x6f9fa0,
0x789d9b,
0x638b8b,
0x83aaa0,
0x6f939e
];
for (let index = 0; index < 16; index++) {
const width = 0.55 + random() * 0.7;
const height = 0.8 + random() * 2.4;
const depth = 0.4 + random() * 0.35;
const x = -7.2 + index * 0.95;
addBox(
scene,
[width, height, depth],
[x, height / 2 + 0.12, -2.55 - random() * 0.25],
standardMaterial(buildingColors[index % buildingColors.length])
);
for (let windowIndex = 0; windowIndex < 3; windowIndex++) {
if (random() > 0.28) {
addBox(
scene,
[0.09, 0.11, 0.025],
[
x - width * 0.22 + windowIndex * width * 0.22,
height * (0.42 + random() * 0.3),
-2.55 - depth * 0.53
],
standardMaterial(0xd7e6cf),
false
);
}
}
}
// Blocky clouds.
[
[-3.8, 5.8, 0.9],
[1.2, 6.2, 1.25],
[4.7, 5.5, 0.8]
].forEach(([x, y, width]) => {
addBox(
scene,
[width, 0.28, 0.2],
[x, y, -2.95],
standardMaterial(0xe9f3e9),
false
);
addBox(
scene,
[width * 0.55, 0.38, 0.22],
[x - width * 0.08, y + 0.17, -2.93],
standardMaterial(0xf5f6e9),
false
);
});
// -------------------------------------------------------------------------
// Window and transparent marked pane
// -------------------------------------------------------------------------
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb8eef0,
transparent: true,
opacity: 0.19,
roughness: 0.14,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const glass = addBox(
scene,
[6.05, 4.35, 0.055],
[0, 3.82, -0.79],
glassMaterial,
false
);
glass.renderOrder = 2;
addBox(
scene,
[0.38, 4.95, 0.34],
[-3.27, 3.8, -0.55],
materials.frame
);
addBox(
scene,
[0.38, 4.95, 0.34],
[3.27, 3.8, -0.55],
materials.frame
);
addBox(
scene,
[6.92, 0.38, 0.34],
[0, 6.07, -0.55],
materials.frame
);
addBox(
scene,
[6.92, 0.38, 0.34],
[0, 1.53, -0.55],
materials.frame
);
addBox(
scene,
[7.25, 0.2, 0.78],
[0, 1.3, -0.33],
materials.sill
);
addBox(
scene,
[6.82, 0.075, 0.06],
[0, 6.285, -0.35],
materials.frameLight
);
// Small frame highlights.
addBox(
scene,
[0.07, 4.72, 0.06],
[-3.07, 3.8, -0.36],
materials.frameLight
);
addBox(
scene,
[0.07, 4.72, 0.06],
[3.07, 3.8, -0.36],
materials.frameLight
);
// Bright corner markers identify the target pane.
const markerZ = -0.67;
for (const xSign of [-1, 1]) {
for (const ySign of [-1, 1]) {
const x = xSign * 2.82;
const y = 3.82 + ySign * 1.93;
addBox(
scene,
[0.36, 0.075, 0.065],
[x - xSign * 0.14, y, markerZ],
materials.orange,
false
);
addBox(
scene,
[0.075, 0.36, 0.065],
[x, y - ySign * 0.14, markerZ],
materials.orange,
false
);
}
}
// Subtle diagonal reflections on the glass.
const reflectionMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.11,
depthWrite: false
});
const reflectionA = addBox(
scene,
[0.07, 3.5, 0.015],
[-1.55, 3.82, -0.69],
reflectionMaterial,
false
);
reflectionA.rotation.z = -0.38;
reflectionA.renderOrder = 3;
const reflectionB = addBox(
scene,
[0.045, 2.4, 0.015],
[1.72, 4.1, -0.69],
reflectionMaterial,
false
);
reflectionB.rotation.z = -0.38;
reflectionB.renderOrder = 3;
// -------------------------------------------------------------------------
// Voxel grime: each small cube disappears as the cloth passes
// -------------------------------------------------------------------------
const grimeColumns = 32;
const grimeRows = 23;
const grimeCount = grimeColumns * grimeRows;
const grimeGeometry = new THREE.BoxGeometry(1, 1, 1);
const grimeMaterial = standardMaterial(0xffffff, {
vertexColors: true,
roughness: 0.96
});
const grime = new THREE.InstancedMesh(
grimeGeometry,
grimeMaterial,
grimeCount
);
grime.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
grime.frustumCulled = false;
grime.receiveShadow = true;
scene.add(grime);
const grimeData = [];
const dummy = new THREE.Object3D();
const grimeColor = new THREE.Color();
const grimePalette = [
0x765f43,
0x80694b,
0x675844,
0x8d7654,
0x74684d
];
const grimeMinX = -2.73;
const grimeMaxX = 2.73;
const grimeMinY = 1.98;
const grimeMaxY = 5.64;
const grimeStepX = (grimeMaxX - grimeMinX) / grimeColumns;
const grimeStepY = (grimeMaxY - grimeMinY) / grimeRows;
let grimeIndex = 0;
for (let row = 0; row < grimeRows; row++) {
for (let column = 0; column < grimeColumns; column++) {
const x =
grimeMinX + (column + 0.5) * grimeStepX +
(random() - 0.5) * 0.025;
const y =
grimeMinY + (row + 0.5) * grimeStepY +
(random() - 0.5) * 0.025;
const depth = 0.024 + random() * 0.048;
grimeData.push({
x,
y,
z: -0.695 + depth * 0.5,
scaleX: 0.145 + random() * 0.024,
scaleY: 0.142 + random() * 0.025,
scaleZ: depth,
color: grimePalette[Math.floor(random() * grimePalette.length)]
});
grime.setColorAt(
grimeIndex,
grimeColor.setHex(grimeData[grimeIndex].color)
);
grimeIndex++;
}
}
grime.instanceColor.needsUpdate = true;
// -------------------------------------------------------------------------
// Voxel bucket
// -------------------------------------------------------------------------
const bucket = new THREE.Group();
bucket.position.set(-3.7, 0, 0.95);
scene.add(bucket);
addBox(bucket, [0.8, 0.15, 0.65], [0, 0.16, 0], materials.bucketDark);
addBox(bucket, [0.94, 0.28, 0.74], [0, 0.39, 0], materials.bucket);
addBox(bucket, [1.08, 0.33, 0.86], [0, 0.68, 0], materials.bucket);
addBox(bucket, [1.2, 0.11, 0.12], [0, 0.88, 0.42], materials.bucketRim);
addBox(bucket, [1.2, 0.11, 0.12], [0, 0.88, -0.42], materials.bucketRim);
addBox(bucket, [0.12, 0.11, 0.82], [-0.55, 0.88, 0], materials.bucketRim);
addBox(bucket, [0.12, 0.11, 0.82], [0.55, 0.88, 0], materials.bucketRim);
addBox(bucket, [0.08, 0.72, 0.08], [-0.47, 1.25, 0], materials.dark);
addBox(bucket, [0.08, 0.72, 0.08], [0.47, 1.25, 0], materials.dark);
addBox(bucket, [1.02, 0.08, 0.08], [0, 1.61, 0], materials.dark);
// -------------------------------------------------------------------------
// Voxel spray bottle
// -------------------------------------------------------------------------
const bottle = new THREE.Group();
scene.add(bottle);
addBox(bottle, [0.55, 0.13, 0.45], [0, 0.12, 0], materials.bottleDark);
addBox(bottle, [0.62, 0.78, 0.48], [0, 0.57, 0], materials.bottle);
addBox(bottle, [0.46, 0.2, 0.4], [0, 1.04, 0], materials.bottle);
addBox(bottle, [0.23, 0.19, 0.25], [0, 1.23, 0], materials.bottleDark);
addBox(bottle, [0.46, 0.22, 0.32], [0, 1.4, -0.03], materials.bottleDark);
addBox(bottle, [0.18, 0.15, 0.36], [0, 1.41, -0.28], materials.nozzle);
addBox(bottle, [0.12, 0.26, 0.1], [0, 1.24, -0.17], materials.orange);
addBox(bottle, [0.39, 0.34, 0.025], [0, 0.64, 0.253], materials.cream);
const nozzleTip = new THREE.Object3D();
nozzleTip.position.set(0, 1.42, -0.49);
bottle.add(nozzleTip);
const bottleRestPosition = new THREE.Vector3(3.7, 0, 1.4);
const bottleAimPosition = new THREE.Vector3(2.55, 0.72, 0.72);
const bottleRestRotation = new THREE.Euler(0, 0.3, -0.08);
const bottleAimRotation = new THREE.Euler(0.42, 0.72, -0.06);
const bottleRestQuaternion = new THREE.Quaternion().setFromEuler(
bottleRestRotation
);
const bottleAimQuaternion = new THREE.Quaternion().setFromEuler(
bottleAimRotation
);
bottle.position.copy(bottleRestPosition);
bottle.quaternion.copy(bottleRestQuaternion);
// -------------------------------------------------------------------------
// Voxel cloth
// -------------------------------------------------------------------------
const cloth = new THREE.Group();
scene.add(cloth);
const clothRestPosition = new THREE.Vector3(-3.66, 1.48, 0.98);
cloth.position.copy(clothRestPosition);
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 5; column++) {
addBox(
cloth,
[0.135, 0.135, 0.07],
[
(column - 2) * 0.145,
(row - 1.5) * 0.145,
(row + column) % 2 === 0 ? 0.012 : -0.012
],
(row + column) % 2 === 0 ? materials.clothA : materials.clothB,
false
);
}
}
// Small cloth tassels.
addBox(cloth, [0.11, 0.11, 0.07], [-0.34, -0.34, 0], materials.clothA, false);
addBox(cloth, [0.11, 0.11, 0.07], [0.34, -0.34, 0], materials.clothA, false);
// -------------------------------------------------------------------------
// Spray mist made from small instanced cubes
// -------------------------------------------------------------------------
const particleCount = 110;
const particleMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.065, 0.065, 0.065),
new THREE.MeshBasicMaterial({
color: 0xbaf4f2,
transparent: true,
opacity: 0.72,
depthWrite: false
}),
particleCount
);
particleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particleMesh.frustumCulled = false;
particleMesh.renderOrder = 4;
scene.add(particleMesh);
const particles = Array.from({ length: particleCount }, () => ({
active: false,
age: 0,
duration: 1,
start: new THREE.Vector3(),
control: new THREE.Vector3(),
end: new THREE.Vector3(),
rotation: 0
}));
const particleDummy = new THREE.Object3D();
const nozzleWorld = new THREE.Vector3();
function spawnParticle() {
const particle = particles.find((item) => !item.active);
if (!particle) return;
nozzleTip.getWorldPosition(nozzleWorld);
particle.active = true;
particle.age = 0;
particle.duration = 0.62 + random() * 0.38;
particle.start.copy(nozzleWorld);
particle.end.set(
-2.35 + random() * 4.7,
2.2 + random() * 3.15,
-0.65
);
particle.control
.copy(particle.start)
.lerp(particle.end, 0.48);
particle.control.x += (random() - 0.5) * 0.55;
particle.control.y += 0.25 + random() * 0.38;
particle.control.z += (random() - 0.5) * 0.35;
particle.rotation = random() * Math.PI;
}
function clearParticles() {
particles.forEach((particle) => {
particle.active = false;
});
}
// -------------------------------------------------------------------------
// Small cleaning sparkles
// -------------------------------------------------------------------------
const sparkleCount = 30;
const sparkleMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.055, 0.055, 0.055),
new THREE.MeshBasicMaterial({
color: 0xffefad,
transparent: true,
opacity: 0.9,
depthWrite: false
}),
sparkleCount
);
sparkleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
sparkleMesh.frustumCulled = false;
sparkleMesh.renderOrder = 5;
scene.add(sparkleMesh);
const sparkles = Array.from({ length: sparkleCount }, () => ({
active: false,
age: 0,
duration: 0.65,
position: new THREE.Vector3(),
velocity: new THREE.Vector3()
}));
const sparkleDummy = new THREE.Object3D();
function spawnSparkle() {
const sparkle = sparkles.find((item) => !item.active);
if (!sparkle) return;
sparkle.active = true;
sparkle.age = 0;
sparkle.duration = 0.45 + random() * 0.35;
sparkle.position.copy(cloth.position);
sparkle.position.x += (random() - 0.5) * 0.8;
sparkle.position.y += (random() - 0.5) * 0.55;
sparkle.position.z = -0.48;
sparkle.velocity.set(
(random() - 0.5) * 0.65,
0.25 + random() * 0.65,
0.12 + random() * 0.25
);
}
// -------------------------------------------------------------------------
// Animation phases
// -------------------------------------------------------------------------
const SPRAY_DURATION = 4.2;
const WIPE_DURATION = 7.2;
const HOLD_DURATION = 1.4;
const CYCLE_DURATION =
SPRAY_DURATION + WIPE_DURATION + HOLD_DURATION;
const WIPE_ROWS = 7;
const WIPE_TOP = 5.48;
const WIPE_BOTTOM = 2.08;
const WIPE_ROW_GAP = (WIPE_TOP - WIPE_BOTTOM) / (WIPE_ROWS - 1);
const WIPE_LEFT = -2.52;
const WIPE_RIGHT = 2.52;
const phaseCard = document.getElementById("phaseCard");
const phaseText = document.getElementById("phaseText");
const phaseHint = document.getElementById("phaseHint");
const progressFill = document.getElementById("progressFill");
const cleanValue = document.getElementById("cleanValue");
const pauseButton = document.getElementById("pauseButton");
const replayButton = document.getElementById("replayButton");
let simulationTime = 0;
let previousPhase = "";
let paused = false;
let spawnAccumulator = 0;
let sparkleAccumulator = 0;
function smoothstep(minimum, maximum, value) {
const t = THREE.MathUtils.clamp(
(value - minimum) / (maximum - minimum),
0,
1
);
return t * t * (3 - 2 * t);
}
function getPhaseState(time) {
const cycleTime = time % CYCLE_DURATION;
if (cycleTime < SPRAY_DURATION) {
return {
name: "spray",
progress: cycleTime / SPRAY_DURATION,
cycleTime
};
}
if (cycleTime < SPRAY_DURATION + WIPE_DURATION) {
return {
name: "wipe",
progress:
(cycleTime - SPRAY_DURATION) / WIPE_DURATION,
cycleTime
};
}
return {
name: "clean",
progress:
(cycleTime - SPRAY_DURATION - WIPE_DURATION) /
HOLD_DURATION,
cycleTime
};
}
function getWipeRoute(progress) {
const routeProgress = THREE.MathUtils.clamp(
(progress - 0.12) / 0.88,
0,
1
);
const rowPosition = routeProgress * WIPE_ROWS;
const row = Math.min(WIPE_ROWS - 1, Math.floor(rowPosition));
const localProgress = Math.min(1, rowPosition - row);
const x =
row % 2 === 0
? THREE.MathUtils.lerp(WIPE_LEFT, WIPE_RIGHT, localProgress)
: THREE.MathUtils.lerp(WIPE_RIGHT, WIPE_LEFT, localProgress);
const y = WIPE_TOP - row * WIPE_ROW_GAP;
return { row, localProgress, x, y };
}
function isTileClean(tileX, tileY, route) {
if (route.localProgress >= 0.999 && route.row === WIPE_ROWS - 1) {
return true;
}
for (let row = 0; row <= route.row; row++) {
const rowY = WIPE_TOP - row * WIPE_ROW_GAP;
if (Math.abs(tileY - rowY) > 0.34) continue;
if (row < route.row) return true;
if (row % 2 === 0) {
if (tileX <= route.x) return true;
} else if (tileX >= route.x) {
return true;
}
}
return false;
}
let visibleGrimeCount = grimeCount;
function updateGrime(phase, route) {
let changed = false;
grimeData.forEach((tile, index) => {
const clean =
phase === "clean" ||
(phase === "wipe" && isTileClean(tile.x, tile.y, route));
const scaleX = clean ? 0.0001 : tile.scaleX;
const scaleY = clean ? 0.0001 : tile.scaleY;
const scaleZ = clean ? 0.0001 : tile.scaleZ;
dummy.position.set(tile.x, tile.y, tile.z);
dummy.scale.set(scaleX, scaleY, scaleZ);
dummy.updateMatrix();
grime.setMatrixAt(index, dummy.matrix);
if (clean) changed = true;
});
grime.instanceMatrix.needsUpdate = true;
visibleGrimeCount = changed
? grimeData.reduce(
(count, tile, index) => {
const clean =
phase === "clean" ||
(phase === "wipe" &&
isTileClean(tile.x, tile.y, route));
return count + (clean ? 0 : 1);
},
0
)
: grimeCount;
const cleanPercent = Math.round(
(1 - visibleGrimeCount / grimeCount) * 100
);
cleanValue.textContent = `${cleanPercent}% clear`;
}
function updateBottle(phase, phaseState) {
if (phase === "spray") {
const lift =
smoothstep(0.05, 0.23, phaseState.progress) *
(1 - smoothstep(0.91, 1, phaseState.progress));
bottle.position.lerpVectors(
bottleRestPosition,
bottleAimPosition,
lift
);
bottle.quaternion.slerpQuaternions(
bottleRestQuaternion,
bottleAimQuaternion,
lift
);
} else {
const returnAmount =
phase === "wipe"
? 1 - smoothstep(0, 0.18, phaseState.progress)
: 0;
bottle.position.lerpVectors(
bottleAimPosition,
bottleRestPosition,
returnAmount
);
bottle.quaternion.slerpQuaternions(
bottleAimQuaternion,
bottleRestQuaternion,
returnAmount
);
}
}
function updateCloth(phase, phaseState) {
if (phase === "spray") {
cloth.position.copy(clothRestPosition);
cloth.position.y += Math.sin(simulationTime * 2.5) * 0.018;
cloth.rotation.set(0, 0, -0.08);
return;
}
const route = getWipeRoute(phaseState.progress);
if (phaseState.progress < 0.12) {
const transition = smoothstep(
0,
0.12,
phaseState.progress
);
cloth.position.lerpVectors(
clothRestPosition,
new THREE.Vector3(route.x, route.y, -0.51),
transition
);
} else {
cloth.position.set(
route.x,
route.y + Math.sin(simulationTime * 7) * 0.035,
-0.51
);
}
cloth.rotation.set(
0,
0.03,
Math.sin(simulationTime * 6.5) * 0.055
);
}
function updateParticles(deltaTime) {
particles.forEach((particle, index) => {
if (!particle.active) {
particleDummy.position.set(0, -100, 0);
particleDummy.scale.setScalar(0);
particleDummy.updateMatrix();
particleMesh.setMatrixAt(index, particleDummy.matrix);
return;
}
particle.age += deltaTime;
const t = Math.min(1, particle.age / particle.duration);
if (t >= 1) {
particle.active = false;
particleDummy.scale.setScalar(0);
particleDummy.updateMatrix();
particleMesh.setMatrixAt(index, particleDummy.matrix);
return;
}
const inverse = 1 - t;
particleDummy.position.set(
inverse * inverse * particle.start.x +
2 * inverse * t * particle.control.x +
t * t * particle.end.x,
inverse * inverse * particle.start.y +
2 * inverse * t * particle.control.y +
t * t * particle.end.y,
inverse * inverse * particle.start.z +
2 * inverse * t * particle.control.z +
t * t * particle.end.z
);
const scale =
Math.sin(Math.PI * Math.min(1, t * 1.25)) *
(0.75 + (1 - t) * 0.7);
particleDummy.scale.setScalar(Math.max(0, scale));
particleDummy.rotation.set(
particle.rotation + t * 4,
particle.rotation + t * 3,
t * 5
);
particleDummy.updateMatrix();
particleMesh.setMatrixAt(index, particleDummy.matrix);
});
particleMesh.instanceMatrix.needsUpdate = true;
}
function updateSparkles(deltaTime) {
sparkles.forEach((sparkle, index) => {
if (!sparkle.active) {
sparkleDummy.position.set(0, -100, 0);
sparkleDummy.scale.setScalar(0);
sparkleDummy.updateMatrix();
sparkleMesh.setMatrixAt(index, sparkleDummy.matrix);
return;
}
sparkle.age += deltaTime;
const t = sparkle.age / sparkle.duration;
if (t >= 1) {
sparkle.active = false;
sparkleDummy.scale.setScalar(0);
sparkleDummy.updateMatrix();
sparkleMesh.setMatrixAt(index, sparkleDummy.matrix);
return;
}
sparkle.position.addScaledVector(sparkle.velocity, deltaTime);
sparkleDummy.position.copy(sparkle.position);
sparkleDummy.rotation.set(t * 4, t * 5, t * 3);
sparkleDummy.scale.setScalar(Math.sin(t * Math.PI));
sparkleDummy.updateMatrix();
sparkleMesh.setMatrixAt(index, sparkleDummy.matrix);
});
sparkleMesh.instanceMatrix.needsUpdate = true;
}
function updateHud(phase, phaseState) {
if (phase !== previousPhase) {
previousPhase = phase;
phaseCard.dataset.phase = phase;
if (phase === "spray") {
phaseText.textContent = "Misting";
phaseHint.textContent =
"Applying cleaner to the marked pane…";
} else if (phase === "wipe") {
phaseText.textContent = "Wiping";
phaseHint.textContent =
"The cloth is lifting the voxel grime…";
} else {
phaseText.textContent = "Clean";
phaseHint.textContent =
"The transparent marked pane is revealed.";
}
}
progressFill.style.width = `${phaseState.progress * 100}%`;
}
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
replayButton.addEventListener("click", () => {
simulationTime = 0;
previousPhase = "";
spawnAccumulator = 0;
sparkleAccumulator = 0;
paused = false;
pauseButton.textContent = "Pause";
clearParticles();
});
// -------------------------------------------------------------------------
// Main animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const deltaTime = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += deltaTime;
}
const phaseState = getPhaseState(simulationTime);
const phase = phaseState.name;
const route = getWipeRoute(
phase === "wipe" ? phaseState.progress : 0
);
if (phase === "spray" && previousPhase !== "spray") {
clearParticles();
spawnAccumulator = 0;
}
updateBottle(phase, phaseState);
updateCloth(phase, phaseState);
bottle.updateMatrixWorld(true);
if (phase === "spray" && !paused) {
const canSpray =
phaseState.progress > 0.16 &&
phaseState.progress < 0.96;
if (canSpray) {
spawnAccumulator += deltaTime * 27;
while (spawnAccumulator >= 1) {
spawnParticle();
spawnAccumulator -= 1;
}
}
}
if (phase === "wipe" && !paused) {
sparkleAccumulator += deltaTime * 8;
while (sparkleAccumulator >= 1) {
spawnSparkle();
sparkleAccumulator -= 1;
}
}
updateParticles(deltaTime);
updateSparkles(deltaTime);
updateGrime(phase, route);
updateHud(phase, phaseState);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
resize();
</script>
</body>
</html>
```