Prompt specification
Build or customize the Three.js 3D scene "Coverage-planning toy cleaner" 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: Coverage-planning toy cleaner
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/coverage-planning-toy-cleaner-x0064
Scene Description Prompt:
Use Three.js from cdnjs to create a room-scale cleaning puzzle with movable obstacles and a manual mop cursor; visited tiles change subtly, and a counter measures reachable floor coverage without double-counting. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (coverage-planning-toy-cleaner-x0064):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Cleanroom</title>
<style>
:root {
--cyan: #72ffe3;
--cyan-soft: rgba(114, 255, 227, 0.16);
--amber: #ffc266;
--red: #ff7187;
--panel: rgba(9, 20, 32, 0.84);
--line: rgba(155, 218, 232, 0.18);
--text: #e8f7fb;
--muted: #91aebc;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111b;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
user-select: none;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 4;
}
.panel {
position: absolute;
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 18px;
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.mission {
top: 18px;
left: 18px;
width: 310px;
padding: 19px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: 27px;
line-height: 1.02;
letter-spacing: -0.045em;
}
.mission p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.objective {
display: flex;
align-items: center;
gap: 11px;
margin-top: 15px;
padding: 12px;
border: 1px solid rgba(114, 255, 227, 0.15);
border-radius: 13px;
background: var(--cyan-soft);
}
.objective-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 16px var(--cyan);
}
.objective strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.objective small {
display: block;
color: #b8d0d9;
font-size: 11px;
line-height: 1.4;
}
.text-button {
margin-top: 13px;
padding: 0;
border: 0;
background: none;
color: var(--cyan);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 750;
}
.stats {
top: 18px;
right: 18px;
width: 286px;
padding: 18px;
}
.stats-head {
display: flex;
justify-content: space-between;
align-items: center;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status-chip {
padding: 5px 8px;
border-radius: 999px;
color: var(--amber);
background: rgba(255, 194, 102, 0.12);
font-size: 9px;
}
.status-chip.complete {
color: var(--cyan);
background: rgba(114, 255, 227, 0.12);
}
.coverage {
display: flex;
align-items: baseline;
gap: 4px;
margin-top: 7px;
}
.coverage strong {
font-size: 46px;
line-height: 1;
letter-spacing: -0.07em;
}
.coverage span {
color: var(--cyan);
font-size: 20px;
font-weight: 800;
}
.coverage-label {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
}
.progress {
height: 7px;
margin: 14px 0 16px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.075);
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #36b8b1, var(--cyan));
box-shadow: 0 0 18px rgba(114, 255, 227, 0.35);
transition: width 180ms ease;
}
.mini-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.mini {
padding: 10px 7px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
text-align: center;
}
.mini b {
display: block;
font-size: 18px;
}
.mini span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.counter-note {
margin: 12px 0 0;
color: #718e9c;
font-size: 10px;
line-height: 1.4;
}
.controls {
bottom: 18px;
left: 18px;
width: 310px;
padding: 15px 17px;
}
.controls h2 {
margin: 0 0 9px;
color: #dff8ff;
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control-row {
display: grid;
grid-template-columns: 22px 1fr;
gap: 8px;
align-items: center;
margin-top: 7px;
color: var(--muted);
font-size: 11px;
}
.key {
display: grid;
min-width: 22px;
height: 22px;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.12);
border-bottom-color: rgba(255, 255, 255, 0.24);
border-radius: 6px;
background: rgba(255, 255, 255, 0.055);
color: #d8f4fb;
font-size: 10px;
font-weight: 750;
}
.toolbar {
position: absolute;
right: 18px;
bottom: 18px;
display: flex;
gap: 7px;
pointer-events: auto;
}
.toolbar button {
min-width: 38px;
height: 38px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: var(--panel);
color: #cde8ef;
cursor: pointer;
backdrop-filter: blur(12px);
font: inherit;
font-size: 11px;
font-weight: 750;
transition: border-color 150ms, color 150ms, transform 150ms;
}
.toolbar button:hover {
border-color: rgba(114, 255, 227, 0.55);
color: var(--cyan);
transform: translateY(-1px);
}
#toast {
position: fixed;
left: 50%;
bottom: 76px;
z-index: 8;
max-width: min(520px, calc(100vw - 32px));
padding: 11px 16px;
border: 1px solid rgba(114, 255, 227, 0.3);
border-radius: 12px;
background: rgba(8, 25, 36, 0.94);
color: #dffaff;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
font-size: 12px;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition: opacity 180ms, transform 180ms;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#helpModal {
position: fixed;
inset: 0;
z-index: 12;
display: grid;
place-items: center;
padding: 20px;
background: rgba(2, 8, 14, 0.72);
opacity: 0;
pointer-events: none;
transition: opacity 160ms;
backdrop-filter: blur(8px);
}
#helpModal.open {
opacity: 1;
pointer-events: auto;
}
.help-card {
width: min(440px, 100%);
padding: 25px;
border: 1px solid var(--line);
border-radius: 20px;
background: #0b1b29;
box-shadow: 0 30px 90px rgba(0, 0, 0, 0.55);
}
.help-card h2 {
margin: 0 0 16px;
font-size: 24px;
letter-spacing: -0.035em;
}
.help-card p {
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
.help-card button {
width: 100%;
margin-top: 8px;
padding: 11px;
border: 0;
border-radius: 11px;
background: var(--cyan);
color: #06201f;
cursor: pointer;
font: inherit;
font-weight: 850;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #07111b;
color: var(--cyan);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 220ms;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.mission {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.mission p {
max-width: 480px;
}
.objective {
margin-top: 10px;
}
.stats {
top: auto;
right: 12px;
bottom: 64px;
width: 242px;
padding: 14px;
}
.coverage strong {
font-size: 36px;
}
.controls {
display: none;
}
.toolbar {
right: 12px;
bottom: 12px;
}
.toolbar button {
padding: 0 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<section class="panel mission">
<div class="eyebrow">Room-scale cleaning puzzle</div>
<h1>Voxel Cleanroom</h1>
<p>
Move the amber voxel blocks to reconnect the room, then sweep the
cyan mop cursor across every reachable floor tile.
</p>
<div class="objective">
<div class="objective-dot"></div>
<div>
<strong>Objective</strong>
<small id="goalText">Calculating reachable floor…</small>
</div>
</div>
<button class="text-button" id="helpButton">View controls →</button>
</section>
<section class="panel stats">
<div class="stats-head">
<span>Reachable coverage</span>
<span class="status-chip" id="statusChip">Calculating</span>
</div>
<div class="coverage">
<strong id="coveragePercent">0</strong><span>%</span>
</div>
<div class="coverage-label" id="coverageLabel">0 / 0 reachable tiles</div>
<div class="progress"><div class="progress-fill" id="progressFill"></div></div>
<div class="mini-grid">
<div class="mini"><b id="visitedCount">0</b><span>Visited</span></div>
<div class="mini"><b id="cleanedCount">0</b><span>Cleaned</span></div>
<div class="mini"><b id="reachableCount">0</b><span>Reachable</span></div>
</div>
<p class="counter-note">
Set-based counting: cleaning or crossing a tile again never adds progress.
</p>
</section>
<section class="panel controls">
<h2>Manual controls</h2>
<div class="control-row">
<span class="key">↕</span>
<span>Hold left mouse and sweep the floor to mop.</span>
</div>
<div class="control-row">
<span class="key">◆</span>
<span>Drag an amber voxel block to relocate it.</span>
</div>
<div class="control-row">
<span class="key">↻</span>
<span>Right-drag to orbit; mouse wheel to zoom.</span>
</div>
<div class="control-row">
<span class="key">←↑↓→</span>
<span>Move and clean one tile at a time.</span>
</div>
</section>
<nav class="toolbar" aria-label="Game controls">
<button id="resetWipeButton" title="Clear visited and cleaned tiles">Reset wipe</button>
<button id="resetRoomButton" title="Reset blocks, player, and cleaning">Reset room</button>
<button id="topButton" title="Top view">Top</button>
<button id="isoButton" title="Isometric view">Iso</button>
</nav>
</div>
<div id="toast" role="status"></div>
<div id="helpModal" aria-hidden="true">
<div class="help-card">
<h2>How to clean the room</h2>
<p>
The amber blocks divide the room and can seal off walkable tiles.
Drag them to empty floor tiles. As the layout changes, the reachable
area is recalculated with a flood fill.
</p>
<p>
Hold the left mouse button and move across floor tiles to guide the
cyan mop. Visited floor becomes slightly lighter, while cleaned floor
becomes darker and glossier. The counter uses unique tile IDs, so
repeated cleaning cannot inflate the score.
</p>
<p>
Reach 100% while every walkable tile belongs to the reachable region
to certify the room.
</p>
<button id="closeHelpButton">Start cleaning</button>
</div>
</div>
<div id="loading">Building voxel room…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Configuration and grid helpers
// -------------------------------------------------------------------------
const CELL = 1;
const COLS = 13;
const ROWS = 10;
const HALF_X = (COLS - 1) / 2;
const HALF_Z = (ROWS - 1) / 2;
const START = { x: 2, z: 6 };
const cellKey = (x, z) => `${x},${z}`;
const parseKey = (key) => key.split(",").map(Number);
const fixedCells = new Set();
// A long partition creates two one-tile doorways.
for (let z = 2; z <= 7; z++) fixedCells.add(cellKey(5, z));
// Low fixed counters furnish the far half of the room.
for (const x of [8, 9, 10]) {
fixedCells.add(cellKey(x, 2));
fixedCells.add(cellKey(x, 6));
}
const floorList = [];
const floorSet = new Set();
for (let z = 1; z < ROWS - 1; z++) {
for (let x = 1; x < COLS - 1; x++) {
const key = cellKey(x, z);
if (!fixedCells.has(key)) {
floorList.push(key);
floorSet.add(key);
}
}
}
const obstacleDefinitions = [
{
x: 5,
z: 1,
name: "North gate block",
colors: [0xf39b54, 0xffc266, 0xd86b43]
},
{
x: 5,
z: 8,
name: "South gate block",
colors: [0xf39b54, 0xffc266, 0xd86b43]
},
{
x: 9,
z: 4,
name: "Storage block",
colors: [0x8e70d8, 0xb28cff, 0x6651b6]
}
];
const state = {
player: { ...START },
visited: new Set(),
cleaned: new Set(),
walkable: new Set(),
reachable: new Set(),
obstacles: [],
target: null,
hoverCell: null,
routeActive: false,
cleanRoute: false,
pointerMode: null,
pointerDown: false,
draggingObstacle: null,
dragOffset: new THREE.Vector3(),
dragCandidate: null,
dragValid: false,
goalHit: false,
routeBlocked: false
};
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111b);
scene.fog = new THREE.Fog(0x07111b, 24, 46);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById("scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc9f6ff, 0x182034, 1.8);
scene.add(hemisphere);
const ambient = new THREE.AmbientLight(0x8bb6d6, 0.32);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xffefd2, 3.1);
sun.position.set(8, 15, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.00045;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x58d8ff, 1.25);
rimLight.position.set(-9, 7, -10);
scene.add(rimLight);
const cameraState = {
radius: 19,
theta: 0.54,
phi: 0.86,
target: new THREE.Vector3(0, 0, 0),
dragging: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const { radius, theta, phi, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
// -------------------------------------------------------------------------
// Voxel room construction
// -------------------------------------------------------------------------
const worldFor = (x, z, y = 0) =>
new THREE.Vector3((x - HALF_X) * CELL, y, (z - HALF_Z) * CELL);
function nearestCell(point) {
const x = Math.round(point.x / CELL + HALF_X);
const z = Math.round(point.z / CELL + HALF_Z);
return { x, z, key: cellKey(x, z) };
}
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x07111b,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.52;
ground.receiveShadow = true;
scene.add(ground);
const floorBase = new THREE.Mesh(
new THREE.BoxGeometry(COLS * CELL, 0.32, ROWS * CELL),
new THREE.MeshStandardMaterial({
color: 0x0e202d,
roughness: 0.82,
metalness: 0.04
})
);
floorBase.position.y = -0.32;
floorBase.receiveShadow = true;
scene.add(floorBase);
const floorMeshes = [];
const floorMap = new Map();
const baseColors = {
left: new THREE.Color(0x193548),
right: new THREE.Color(0x20364a),
visited: new THREE.Color(0x285064),
cleaned: new THREE.Color(0x347b78)
};
const floorGeometry = new THREE.BoxGeometry(
CELL * 0.92,
0.16,
CELL * 0.92
);
for (const key of floorList) {
const [x, z] = parseKey(key);
const baseColor = x >= 6 ? baseColors.right : baseColors.left;
const material = new THREE.MeshStandardMaterial({
color: baseColor,
roughness: 0.84,
metalness: 0.035,
emissive: 0x000000,
emissiveIntensity: 1
});
const tile = new THREE.Mesh(floorGeometry, material);
tile.position.copy(worldFor(x, z, -0.08));
tile.receiveShadow = true;
tile.castShadow = false;
tile.userData.key = key;
tile.userData.targetColor = baseColor.clone();
tile.userData.targetEmissive = new THREE.Color(0x000000);
tile.userData.targetRoughness = 0.84;
tile.userData.targetMetalness = 0.035;
scene.add(tile);
floorMeshes.push(tile);
floorMap.set(key, tile);
}
const solidMeshes = [];
const solidGeometry = new THREE.BoxGeometry(0.9, 0.72, 0.9);
function addSolidVoxelCell(x, z, levels, color, accent) {
const group = new THREE.Group();
group.position.copy(worldFor(x, z));
for (let layer = 0; layer < levels; layer++) {
const material = new THREE.MeshStandardMaterial({
color: layer % 2 === 0 ? color : accent,
roughness: 0.7,
metalness: 0.08,
emissive: layer % 2 === 0 ? 0x000000 : 0x08131c,
emissiveIntensity: 0.35
});
const cube = new THREE.Mesh(solidGeometry, material);
cube.position.set(
layer % 2 === 0 ? 0 : 0.018,
0.36 + layer * 0.74,
layer % 2 === 0 ? 0 : -0.014
);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
solidMeshes.push(cube);
}
scene.add(group);
}
// Low border walls form a readable dollhouse-style room.
for (let x = 0; x < COLS; x++) {
addSolidVoxelCell(x, 0, 1, 0x263b5c, 0x344d72);
addSolidVoxelCell(x, ROWS - 1, 1, 0x263b5c, 0x344d72);
}
for (let z = 1; z < ROWS - 1; z++) {
addSolidVoxelCell(0, z, 1, 0x263b5c, 0x344d72);
addSolidVoxelCell(COLS - 1, z, 1, 0x263b5c, 0x344d72);
}
// Tall partition and fixed counters.
for (let z = 2; z <= 7; z++) {
addSolidVoxelCell(5, z, 2, 0x566078, 0x747d94);
}
for (const x of [8, 9, 10]) {
addSolidVoxelCell(x, 2, 1, 0x244c5b, 0x397284);
addSolidVoxelCell(x, 6, 1, 0x244c5b, 0x397284);
}
// Amber pads reveal the two important one-tile doorways.
const gateMaterials = [];
for (const [x, z] of [[5, 1], [5, 8]]) {
const material = new THREE.MeshBasicMaterial({
color: 0xffbd5c,
transparent: true,
opacity: 0.24,
depthWrite: false
});
gateMaterials.push(material);
const pad = new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.025, 0.78),
material
);
pad.position.copy(worldFor(x, z, 0.018));
scene.add(pad);
}
// -------------------------------------------------------------------------
// Movable voxel obstacles
// -------------------------------------------------------------------------
const obstacleGroups = [];
const miniCubeGeometry = new THREE.BoxGeometry(0.235, 0.235, 0.235);
function createVoxelObstacle(definition, index) {
const group = new THREE.Group();
const materials = [];
for (let layer = 0; layer < 3; layer++) {
for (let ix = 0; ix < 3; ix++) {
for (let iz = 0; iz < 3; iz++) {
const colorIndex =
(ix + iz + layer + index) % definition.colors.length;
const material = new THREE.MeshStandardMaterial({
color: definition.colors[colorIndex],
roughness: 0.54,
metalness: 0.11,
emissive: definition.colors[colorIndex],
emissiveIntensity: 0.025
});
materials.push(material);
const cube = new THREE.Mesh(miniCubeGeometry, material);
cube.position.set(
(ix - 1) * 0.25,
0.13 + layer * 0.25,
(iz - 1) * 0.25
);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
}
}
}
// A small top handle makes each obstacle read as a movable crate.
const handle = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.055, 0.075),
new THREE.MeshStandardMaterial({
color: 0x182431,
roughness: 0.48,
metalness: 0.35
})
);
handle.position.y = 0.9;
handle.castShadow = true;
group.add(handle);
const obstacle = {
...definition,
originalX: definition.x,
originalZ: definition.z,
group,
materials,
hovered: false
};
group.userData.obstacle = obstacle;
group.position.copy(worldFor(definition.x, definition.z));
scene.add(group);
obstacleGroups.push(group);
state.obstacles.push(obstacle);
}
obstacleDefinitions.forEach(createVoxelObstacle);
// -------------------------------------------------------------------------
// Manual mop and floor reticle
// -------------------------------------------------------------------------
const mopRoot = new THREE.Group();
const mopVisual = new THREE.Group();
mopRoot.add(mopVisual);
scene.add(mopRoot);
const mopHeadMaterial = new THREE.MeshStandardMaterial({
color: 0x73ffe2,
roughness: 0.26,
metalness: 0.28,
emissive: 0x21d7bf,
emissiveIntensity: 0.42
});
const mopHead = new THREE.Mesh(
new THREE.BoxGeometry(0.66, 0.09, 0.28),
mopHeadMaterial
);
mopHead.position.y = 0.09;
mopHead.castShadow = true;
mopVisual.add(mopHead);
const bristleMaterial = new THREE.MeshStandardMaterial({
color: 0x1b8f91,
roughness: 0.8,
metalness: 0.02
});
for (let i = 0; i < 7; i++) {
const bristle = new THREE.Mesh(
new THREE.BoxGeometry(0.065, 0.045, 0.25),
bristleMaterial
);
bristle.position.set((i - 3) * 0.083, 0.025, 0);
bristle.castShadow = true;
mopVisual.add(bristle);
}
const handle = new THREE.Mesh(
new THREE.BoxGeometry(0.065, 1.15, 0.065),
new THREE.MeshStandardMaterial({
color: 0xb6f9ed,
roughness: 0.3,
metalness: 0.32,
emissive: 0x1d8b84,
emissiveIntensity: 0.18
})
);
handle.position.set(0.22, 0.67, 0);
handle.rotation.z = -0.32;
handle.castShadow = true;
mopVisual.add(handle);
const grip = new THREE.Mesh(
new THREE.BoxGeometry(0.105, 0.26, 0.09),
new THREE.MeshStandardMaterial({
color: 0x153544,
roughness: 0.64,
metalness: 0.12
})
);
grip.position.set(0.405, 1.13, 0);
grip.rotation.z = -0.32;
grip.castShadow = true;
mopVisual.add(grip);
const mopLight = new THREE.PointLight(0x54ffe1, 3.2, 3.2, 2);
mopLight.position.y = 0.3;
mopVisual.add(mopLight);
mopRoot.position.copy(worldFor(state.player.x, state.player.z));
const reticleGroup = new THREE.Group();
reticleGroup.visible = false;
scene.add(reticleGroup);
const reticleMaterial = new THREE.LineBasicMaterial({
color: 0x72ffe3,
transparent: true,
opacity: 0.95
});
const reticle = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.86, 0.06, 0.86)),
reticleMaterial
);
reticleGroup.add(reticle);
const reticleFill = new THREE.Mesh(
new THREE.PlaneGeometry(0.76, 0.76),
new THREE.MeshBasicMaterial({
color: 0x72ffe3,
transparent: true,
opacity: 0.08,
depthWrite: false,
side: THREE.DoubleSide
})
);
reticleFill.rotation.x = -Math.PI / 2;
reticleFill.position.y = -0.025;
reticleGroup.add(reticleFill);
function placeReticle(key, color = 0x72ffe3) {
if (!key) {
reticleGroup.visible = false;
return;
}
const [x, z] = parseKey(key);
reticleGroup.visible = true;
reticleGroup.position.copy(worldFor(x, z, 0.055));
reticleMaterial.color.setHex(color);
reticleFill.material.color.setHex(color);
}
// -------------------------------------------------------------------------
// Unique-tile state, reachability, and pathfinding
// -------------------------------------------------------------------------
function markVisited(key) {
if (state.visited.has(key)) return;
state.visited.add(key);
updateTileTargets();
updateStats();
}
function markCleaned(key) {
if (!floorSet.has(key)) return;
const wasNew = !state.cleaned.has(key);
state.visited.add(key);
state.cleaned.add(key);
updateTileTargets();
updateStats();
return wasNew;
}
function updateTileTargets() {
for (const [key, tile] of floorMap) {
const cleaned = state.cleaned.has(key);
const visited = state.visited.has(key);
const [x] = parseKey(key);
tile.userData.targetColor.copy(
cleaned
? baseColors.cleaned
: visited
? baseColors.visited
: x >= 6
? baseColors.right
: baseColors.left
);
tile.userData.targetEmissive.setHex(
cleaned ? 0x092e2d : visited ? 0x06151d : 0x000000
);
tile.userData.targetRoughness = cleaned ? 0.3 : 0.84;
tile.userData.targetMetalness = cleaned ? 0.16 : 0.035;
}
}
function neighbors(x, z) {
return [
{ x: x + 1, z },
{ x: x - 1, z },
{ x, z: z + 1 },
{ x, z: z - 1 }
];
}
function floodFrom(startKey, allowed) {
const visited = new Set();
if (!allowed.has(startKey)) return visited;
const queue = [startKey];
visited.add(startKey);
for (let i = 0; i < queue.length; i++) {
const key = queue[i];
const [x, z] = parseKey(key);
for (const next of neighbors(x, z)) {
const nextKey = cellKey(next.x, next.z);
if (allowed.has(nextKey) && !visited.has(nextKey)) {
visited.add(nextKey);
queue.push(nextKey);
}
}
}
return visited;
}
function findPath(start, goal, allowed) {
const startKey = cellKey(start.x, start.z);
const goalKey = cellKey(goal.x, goal.z);
if (!allowed.has(startKey) || !allowed.has(goalKey)) return null;
if (startKey === goalKey) return [startKey];
const parent = new Map([[startKey, null]]);
const queue = [startKey];
for (let i = 0; i < queue.length; i++) {
const currentKey = queue[i];
const [x, z] = parseKey(currentKey);
for (const next of neighbors(x, z)) {
const nextKey = cellKey(next.x, next.z);
if (allowed.has(nextKey) && !parent.has(nextKey)) {
parent.set(nextKey, currentKey);
if (nextKey === goalKey) {
const path = [goalKey];
let cursor = goalKey;
while (cursor !== startKey) {
cursor = parent.get(cursor);
path.unshift(cursor);
}
return path;
}
queue.push(nextKey);
}
}
}
return null;
}
function recomputeReachability() {
const occupied = new Set(
state.obstacles.map((obstacle) => cellKey(obstacle.x, obstacle.z))
);
state.walkable = new Set(
floorList.filter((key) => !occupied.has(key))
);
state.reachable = floodFrom(
cellKey(state.player.x, state.player.z),
state.walkable
);
updateStats();
}
function updateStats() {
const cleanReachable = [...state.cleaned].filter((key) =>
state.reachable.has(key)
).length;
const reachableTotal = state.reachable.size;
const percent = reachableTotal
? Math.round((cleanReachable / reachableTotal) * 100)
: 0;
const sealed = state.walkable.size - state.reachable.size;
const fullyConnected = sealed === 0;
const complete = fullyConnected && percent === 100;
document.getElementById("coveragePercent").textContent = percent;
document.getElementById("coverageLabel").textContent =
`${cleanReachable} / ${reachableTotal} reachable tiles`;
document.getElementById("progressFill").style.width = `${percent}%`;
document.getElementById("visitedCount").textContent = state.visited.size;
document.getElementById("cleanedCount").textContent = state.cleaned.size;
document.getElementById("reachableCount").textContent = reachableTotal;
const chip = document.getElementById("statusChip");
const goal = document.getElementById("goalText");
if (sealed > 0) {
chip.textContent = `${sealed} sealed`;
chip.classList.remove("complete");
goal.textContent =
`Move amber blocks to reconnect ${sealed} currently sealed floor tiles.`;
} else if (percent === 100) {
chip.textContent = "Certified";
chip.classList.add("complete");
goal.textContent = "Room certified: every reachable tile is clean.";
} else {
chip.textContent = "Connected";
chip.classList.remove("complete");
goal.textContent = `Mop the remaining ${reachableTotal - cleanReachable} reachable tiles.`;
}
if (complete && !state.goalHit) {
state.goalHit = true;
showToast("Room certified — all reachable floor is clean!");
}
if (!complete) state.goalHit = false;
}
// -------------------------------------------------------------------------
// Obstacle placement and validation
// -------------------------------------------------------------------------
function setObstacleCell(obstacle, x, z) {
if (obstacle.x === x && obstacle.z === z) return;
obstacle.x = x;
obstacle.z = z;
obstacle.group.position.copy(worldFor(x, z));
recomputeReachability();
}
function validObstacleDestination(obstacle, x, z) {
const key = cellKey(x, z);
if (!floorSet.has(key)) return false;
if (key === cellKey(state.player.x, state.player.z)) return false;
for (const other of state.obstacles) {
if (
other !== obstacle &&
cellKey(other.x, other.z) === key
) {
return false;
}
}
return true;
}
// -------------------------------------------------------------------------
// Pointer picking and interaction
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const planeHit = new THREE.Vector3();
function updateRay(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function pickFloorCell() {
const hits = raycaster.intersectObjects(floorMeshes, false);
if (hits.length) return hits[0].object.userData.key;
const baseHits = raycaster.intersectObject(floorBase, false);
if (baseHits.length) {
const cell = nearestCell(baseHits[0].point);
if (floorSet.has(cell.key)) return cell.key;
}
return null;
}
function pickObstacle() {
const hits = raycaster.intersectObjects(obstacleGroups, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.obstacle) object = object.parent;
if (object?.userData.obstacle) return object.userData.obstacle;
}
return null;
}
function updateHover(event) {
updateRay(event);
if (state.pointerMode === "obstacle") {
if (!raycaster.ray.intersectPlane(dragPlane, planeHit)) return;
const desired = planeHit.clone().add(state.dragOffset);
const cell = nearestCell(desired);
state.dragCandidate = cell.key;
state.dragValid = validObstacleDestination(
state.draggingObstacle,
cell.x,
cell.z
);
placeReticle(
cell.key,
state.dragValid ? 0xffc266 : 0xff7187
);
return;
}
if (state.pointerMode === "camera") return;
const cell = pickFloorCell();
state.hoverCell = cell;
if (state.routeActive && state.target) {
placeReticle(
state.target,
state.reachable.has(state.target) ? 0x72ffe3 : 0xff7187
);
} else if (cell) {
placeReticle(
cell,
state.reachable.has(cell) ? 0x72ffe3 : 0xff7187
);
} else {
placeReticle(null);
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
event.preventDefault();
updateRay(event);
renderer.domElement.setPointerCapture(event.pointerId);
if (event.button === 2 || event.button === 1) {
state.pointerMode = "camera";
state.pointerDown = true;
cameraState.dragging = true;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
return;
}
if (event.button !== 0) return;
const obstacle = pickObstacle();
if (obstacle) {
state.pointerMode = "obstacle";
state.pointerDown = true;
state.draggingObstacle = obstacle;
state.routeActive = false;
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
state.dragOffset.set(
obstacle.group.position.x - planeHit.x,
0,
obstacle.group.position.z - planeHit.z
);
} else {
state.dragOffset.set(0, 0, 0);
}
obstacle.hovered = true;
return;
}
const cell = pickFloorCell();
if (cell) {
const [x, z] = parseKey(cell);
state.pointerMode = "route";
state.pointerDown = true;
state.target = { x, z, key: cell };
state.routeActive = true;
state.cleanRoute = true;
state.routeBlocked = false;
markVisited(cell);
if (cell === cellKey(state.player.x, state.player.z)) {
markCleaned(cell);
state.routeActive = false;
} else {
const path = findPath(
state.player,
state.target,
state.walkable
);
if (!path) {
state.routeBlocked = true;
showToast(
"That area is sealed. Move an amber block to reconnect the room."
);
}
}
placeReticle(
cell,
state.reachable.has(cell) ? 0x72ffe3 : 0xff7187
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (state.pointerMode === "camera") {
const dx = event.clientX - cameraState.lastX;
const dy = event.clientY - cameraState.lastY;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
cameraState.theta -= dx * 0.0075;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.006,
0.07,
1.34
);
updateCamera();
return;
}
updateHover(event);
if (
state.pointerMode === "obstacle" &&
state.pointerDown &&
state.draggingObstacle
) {
if (!raycaster.ray.intersectPlane(dragPlane, planeHit)) return;
const desired = planeHit.clone().add(state.dragOffset);
const cell = nearestCell(desired);
const valid = validObstacleDestination(
state.draggingObstacle,
cell.x,
cell.z
);
state.dragCandidate = cell.key;
state.dragValid = valid;
if (valid) {
setObstacleCell(state.draggingObstacle, cell.x, cell.z);
}
placeReticle(cell.key, valid ? 0xffc266 : 0xff7187);
return;
}
if (
state.pointerMode === "route" &&
state.pointerDown &&
(event.buttons & 1)
) {
const cell = pickFloorCell();
if (cell) {
const [x, z] = parseKey(cell);
state.target = { x, z, key: cell };
state.routeActive = true;
state.cleanRoute = true;
markVisited(cell);
placeReticle(
cell,
state.reachable.has(cell) ? 0x72ffe3 : 0xff7187
);
}
}
});
function endPointer(event) {
if (state.pointerMode === "obstacle" && state.draggingObstacle) {
state.draggingObstacle.hovered = false;
showToast(
`${state.draggingObstacle.name} moved — reachable floor recalculated.`
);
}
state.pointerMode = null;
state.pointerDown = false;
state.draggingObstacle = null;
state.dragValid = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
11,
29
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Mop movement
// -------------------------------------------------------------------------
let moveAnimation = null;
function startMove(next, shouldClean) {
moveAnimation = {
from: mopRoot.position.clone(),
to: worldFor(next.x, next.z),
next: { x: next.x, z: next.z },
elapsed: 0,
duration: 0.115,
shouldClean
};
}
function updateMovement(delta) {
if (!moveAnimation && state.routeActive && state.target) {
const path = findPath(
state.player,
state.target,
state.walkable
);
if (!path) {
state.routeBlocked = true;
state.routeActive = false;
placeReticle(state.target.key, 0xff7187);
return;
}
if (path.length <= 1) {
state.routeActive = false;
markCleaned(cellKey(state.player.x, state.player.z));
return;
}
const [nextX, nextZ] = parseKey(path[1]);
startMove({ x: nextX, z: nextZ }, state.cleanRoute);
}
if (!moveAnimation) return;
moveAnimation.elapsed += delta;
const t = Math.min(
moveAnimation.elapsed / moveAnimation.duration,
1
);
const eased = t * t * (3 - 2 * t);
mopRoot.position.lerpVectors(
moveAnimation.from,
moveAnimation.to,
eased
);
if (t >= 1) {
const completed = moveAnimation;
state.player = { ...completed.next };
const key = cellKey(state.player.x, state.player.z);
markVisited(key);
if (completed.shouldClean) markCleaned(key);
moveAnimation = null;
if (
state.routeActive &&
state.target &&
state.target.key === key
) {
state.routeActive = false;
}
}
}
function stepPlayer(dx, dz) {
state.routeActive = false;
state.target = null;
if (moveAnimation) return;
const next = {
x: state.player.x + dx,
z: state.player.z + dz
};
const key = cellKey(next.x, next.z);
if (!state.walkable.has(key)) {
showToast("That tile is blocked by a voxel.");
return;
}
startMove(next, true);
}
// -------------------------------------------------------------------------
// Keyboard controls
// -------------------------------------------------------------------------
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (
event.key === "ArrowUp" ||
event.key === "ArrowDown" ||
event.key === "ArrowLeft" ||
event.key === "ArrowRight"
) {
event.preventDefault();
if (event.key === "ArrowUp") stepPlayer(0, -1);
if (event.key === "ArrowDown") stepPlayer(0, 1);
if (event.key === "ArrowLeft") stepPlayer(-1, 0);
if (event.key === "ArrowRight") stepPlayer(1, 0);
}
if (key === "r") resetWipes();
if (key === "t") setTopView();
if (key === "i") setIsoView();
});
// -------------------------------------------------------------------------
// UI and resets
// -------------------------------------------------------------------------
let toastTimer = null;
function showToast(message, duration = 2600) {
const toast = document.getElementById("toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, duration);
}
function resetWipes() {
state.visited.clear();
state.cleaned.clear();
state.visited.add(cellKey(state.player.x, state.player.z));
state.goalHit = false;
updateTileTargets();
updateStats();
showToast("Cleaning data reset.");
}
function resetRoom() {
state.visited.clear();
state.cleaned.clear();
state.player = { ...START };
state.target = null;
state.routeActive = false;
state.cleanRoute = false;
state.goalHit = false;
moveAnimation = null;
mopRoot.position.copy(worldFor(state.player.x, state.player.z));
for (const obstacle of state.obstacles) {
obstacle.x = obstacle.originalX;
obstacle.z = obstacle.originalZ;
obstacle.group.position.copy(worldFor(obstacle.x, obstacle.z));
}
state.visited.add(cellKey(state.player.x, state.player.z));
updateTileTargets();
recomputeReachability();
setIsoView();
showToast("Room reset.");
}
function setTopView() {
cameraState.radius = 16.8;
cameraState.theta = 0;
cameraState.phi = 0.075;
updateCamera();
}
function setIsoView() {
cameraState.radius = 19;
cameraState.theta = 0.54;
cameraState.phi = 0.86;
updateCamera();
}
document.getElementById("resetWipeButton").addEventListener("click", resetWipes);
document.getElementById("resetRoomButton").addEventListener("click", resetRoom);
document.getElementById("topButton").addEventListener("click", setTopView);
document.getElementById("isoButton").addEventListener("click", setIsoView);
const helpModal = document.getElementById("helpModal");
function openHelp() {
helpModal.classList.add("open");
helpModal.setAttribute("aria-hidden", "false");
}
function closeHelp() {
helpModal.classList.remove("open");
helpModal.setAttribute("aria-hidden", "true");
}
document.getElementById("helpButton").addEventListener("click", openHelp);
document.getElementById("closeHelpButton").addEventListener("click", closeHelp);
helpModal.addEventListener("click", (event) => {
if (event.target === helpModal) closeHelp();
});
window.addEventListener("keydown", (event) => {
if (event.key === "Escape") closeHelp();
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateMovement(delta);
// Smoothly transition floor colors and surface finish.
const colorBlend = 1 - Math.exp(-delta * 11);
for (const tile of floorMeshes) {
tile.material.color.lerp(
tile.userData.targetColor,
colorBlend
);
tile.material.emissive.lerp(
tile.userData.targetEmissive,
colorBlend
);
tile.material.roughness = THREE.MathUtils.lerp(
tile.material.roughness,
tile.userData.targetRoughness,
colorBlend
);
tile.material.metalness = THREE.MathUtils.lerp(
tile.material.metalness,
tile.userData.targetMetalness,
colorBlend
);
}
// Crate hover feedback.
for (const obstacle of state.obstacles) {
const highlighted =
obstacle.hovered || obstacle === state.draggingObstacle;
const targetScale = highlighted ? 1.065 : 1;
const scale = THREE.MathUtils.lerp(
obstacle.group.scale.x,
targetScale,
1 - Math.exp(-delta * 14)
);
obstacle.group.scale.setScalar(scale);
for (const material of obstacle.materials) {
material.emissiveIntensity = THREE.MathUtils.lerp(
material.emissiveIntensity,
highlighted ? 0.2 : 0.025,
1 - Math.exp(-delta * 12)
);
}
}
// Gate pads pulse gently.
for (const material of gateMaterials) {
material.opacity = 0.18 + Math.sin(elapsed * 2.6) * 0.055;
}
// The mop head bobs slightly while the cursor travels.
mopVisual.position.y =
Math.sin(elapsed * 4.8) * (moveAnimation ? 0.018 : 0.008);
if (reticleGroup.visible) {
const pulse = 1 + Math.sin(elapsed * 5.5) * 0.018;
reticleGroup.scale.set(pulse, 1, pulse);
reticleMaterial.opacity = 0.78 + Math.sin(elapsed * 5.5) * 0.15;
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// Initial state.
state.visited.add(cellKey(state.player.x, state.player.z));
updateTileTargets();
recomputeReachability();
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```