Prompt specification
Build or customize the Three.js 3D scene "Shelter availability classroom model" 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: Shelter availability classroom model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/shelter-availability-classroom-model-x0696
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts hermit-crab shell use. Create a tide-pool board with fictional crab sizes and shell capacities; users redistribute empty shells and observe compatibility constraints without presenting the game as an ecological population forecast. Voxel style.
Working Three.js Starter (shelter-availability-classroom-model-x0696):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shell Swap — Fictional Tide-Pool Board</title>
<style>
:root {
color-scheme: light;
--ink: #173f45;
--muted: #567579;
--paper: #f7fbf7;
--panel: #eef7f1;
--line: #c9ddd6;
--teal: #168b8c;
--cyan: #33c7d1;
--green: #49a875;
--orange: #e87645;
--red: #c94f52;
--gold: #e9b44c;
--shadow: 0 18px 50px rgba(28, 73, 74, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: #dceee7;
}
button {
font: inherit;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
width: 100%;
height: 100%;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 35% 22%, rgba(255, 255, 255, 0.7), transparent 40%),
#dceee7;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.stage-header {
position: absolute;
top: 22px;
left: 24px;
right: 24px;
z-index: 3;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.title-block {
max-width: 520px;
padding: 15px 17px;
border: 1px solid rgba(255, 255, 255, 0.74);
border-radius: 17px;
background: rgba(248, 252, 248, 0.8);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.eyebrow {
margin: 0 0 4px;
color: var(--teal);
font-size: 0.69rem;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.3rem, 2.2vw, 2rem);
line-height: 1.05;
letter-spacing: -0.04em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 0.84rem;
line-height: 1.45;
}
.scenario-badge {
flex: 0 0 auto;
max-width: 230px;
padding: 10px 13px;
border: 1px solid rgba(201, 79, 82, 0.22);
border-radius: 13px;
color: #824348;
background: rgba(255, 241, 235, 0.88);
box-shadow: var(--shadow);
font-size: 0.71rem;
font-weight: 750;
line-height: 1.35;
backdrop-filter: blur(10px);
}
.scene-hint {
position: absolute;
left: 24px;
bottom: 24px;
z-index: 4;
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: calc(100% - 48px);
pointer-events: none;
}
.hint-pill {
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 999px;
color: #355d61;
background: rgba(249, 253, 249, 0.82);
box-shadow: 0 6px 20px rgba(35, 78, 79, 0.1);
font-size: 0.7rem;
font-weight: 700;
backdrop-filter: blur(8px);
}
#status {
position: absolute;
left: 50%;
bottom: 25px;
z-index: 5;
width: min(520px, calc(100% - 48px));
padding: 13px 16px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 14px;
color: var(--ink);
background: rgba(250, 253, 249, 0.94);
box-shadow: var(--shadow);
font-size: 0.82rem;
font-weight: 700;
line-height: 1.4;
text-align: center;
pointer-events: none;
transform: translateX(-50%);
transition:
color 160ms ease,
border-color 160ms ease,
background 160ms ease;
}
#status.good {
color: #216747;
border-color: rgba(73, 168, 117, 0.4);
background: rgba(239, 252, 242, 0.96);
}
#status.bad {
color: #8a363b;
border-color: rgba(201, 79, 82, 0.42);
background: rgba(255, 240, 238, 0.96);
}
#status.warn {
color: #76571e;
border-color: rgba(233, 180, 76, 0.46);
background: rgba(255, 249, 228, 0.96);
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: var(--muted);
background: #dceee7;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
aside {
min-width: 0;
overflow-y: auto;
padding: 23px 22px 26px;
border-left: 1px solid rgba(76, 128, 124, 0.2);
background:
linear-gradient(rgba(238, 247, 241, 0.96), rgba(244, 250, 245, 0.98)),
repeating-linear-gradient(
0deg,
transparent,
transparent 19px,
rgba(22, 139, 140, 0.05) 20px
);
box-shadow: -12px 0 35px rgba(33, 81, 81, 0.08);
scrollbar-width: thin;
scrollbar-color: #a9c9c0 transparent;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.panel-heading h2 {
margin: 0;
font-size: 1.12rem;
letter-spacing: -0.025em;
}
.mini-tag {
padding: 5px 8px;
border-radius: 999px;
color: #316b66;
background: #d8ece3;
font-size: 0.62rem;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.notice {
margin: 15px 0 18px;
padding: 12px 13px;
border: 1px solid #d8d7bd;
border-radius: 12px;
color: #655b35;
background: #fff9df;
font-size: 0.76rem;
line-height: 1.48;
}
.notice strong {
color: #504823;
}
.section {
margin-top: 18px;
}
.section-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin: 0 0 8px;
font-size: 0.76rem;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-title span {
color: var(--muted);
font-size: 0.65rem;
font-weight: 650;
letter-spacing: 0;
text-transform: none;
}
.rule-card {
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.62);
font-size: 0.76rem;
line-height: 1.48;
}
.rule-card code {
padding: 2px 5px;
border-radius: 5px;
color: #156e70;
background: #dff1eb;
font-family: inherit;
font-weight: 800;
}
.selection-card {
margin-top: 12px;
padding: 12px 13px;
border: 1px solid #b9d9d1;
border-radius: 12px;
background: rgba(255, 255, 255, 0.72);
}
.selection-card .label {
color: var(--muted);
font-size: 0.64rem;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#selectionName {
margin-top: 3px;
font-size: 1rem;
font-weight: 850;
}
#selectionMeta {
margin-top: 5px;
color: var(--muted);
font-size: 0.74rem;
line-height: 1.45;
}
.data-list {
display: grid;
gap: 6px;
}
.data-row {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
align-items: center;
gap: 8px;
min-height: 42px;
padding: 7px 9px;
border: 1px solid transparent;
border-radius: 10px;
background: rgba(255, 255, 255, 0.52);
transition: 140ms ease;
}
.data-row.selected {
border-color: #4dcbd2;
background: #e6f8f7;
box-shadow: inset 3px 0 0 #24aeb8;
}
.data-row.fits {
border-color: rgba(73, 168, 117, 0.38);
background: rgba(236, 249, 239, 0.78);
}
.data-row.blocked {
border-color: rgba(201, 79, 82, 0.32);
background: rgba(255, 239, 236, 0.72);
}
.data-main {
min-width: 0;
}
.data-name {
display: flex;
align-items: center;
gap: 7px;
font-size: 0.78rem;
font-weight: 800;
}
.dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.09);
}
.data-sub {
margin-top: 2px;
color: var(--muted);
font-size: 0.66rem;
line-height: 1.35;
}
.value {
padding: 4px 7px;
border-radius: 7px;
color: #285f62;
background: #e3efea;
font-size: 0.67rem;
font-weight: 850;
white-space: nowrap;
}
.value.yes {
color: #286d49;
background: #d8f0df;
}
.value.no {
color: #913d42;
background: #f7dcda;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 18px;
}
button {
min-height: 39px;
padding: 9px 11px;
border: 1px solid #a9ccc2;
border-radius: 10px;
color: #255d5f;
background: rgba(255, 255, 255, 0.76);
cursor: pointer;
font-size: 0.74rem;
font-weight: 800;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover:not(:disabled) {
border-color: #56aaa4;
background: #ffffff;
transform: translateY(-1px);
}
button:active:not(:disabled) {
transform: translateY(1px);
}
button.primary {
color: white;
border-color: var(--teal);
background: var(--teal);
}
button.primary:hover {
background: #117a7b;
}
button:disabled {
opacity: 0.42;
cursor: default;
}
.activity {
margin: 0;
padding: 0;
list-style: none;
}
.activity li {
position: relative;
padding: 0 0 10px 16px;
color: #527074;
font-size: 0.71rem;
line-height: 1.4;
}
.activity li::before {
position: absolute;
top: 5px;
left: 0;
width: 6px;
height: 6px;
border-radius: 2px;
background: #72aaa2;
content: "";
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 5px;
padding: 5px 7px;
border: 1px solid var(--line);
border-radius: 8px;
color: #4f7073;
background: rgba(255, 255, 255, 0.54);
font-size: 0.64rem;
font-weight: 750;
}
.legend-swatch {
width: 9px;
height: 9px;
border-radius: 3px;
}
.footnote {
margin: 20px 0 0;
padding-top: 14px;
border-top: 1px solid var(--line);
color: #708588;
font-size: 0.66rem;
line-height: 1.5;
}
@media (max-width: 860px) {
html,
body {
overflow: auto;
}
#app {
grid-template-columns: 1fr;
grid-template-rows: minmax(58vh, 1fr) minmax(0, 42vh);
}
aside {
border-top: 1px solid rgba(76, 128, 124, 0.2);
border-left: 0;
}
.stage-header {
top: 12px;
left: 12px;
right: 12px;
}
.scenario-badge {
display: none;
}
.title-block {
padding: 11px 13px;
}
.subtitle {
display: none;
}
#status {
bottom: 13px;
width: calc(100% - 24px);
}
.scene-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="app">
<section id="stage" aria-label="Interactive fictional tide-pool board">
<canvas
id="scene"
tabindex="0"
aria-label="Voxel tide-pool board. Select a shell and then a crab or board tile."
></canvas>
<div class="stage-header">
<div class="title-block">
<p class="eyebrow">Tide-pool mechanics board</p>
<h1>Shell Swap</h1>
<p class="subtitle">
Redistribute fictional shells, compare their capacity with each
crab, and watch simple fit rules reject incompatible swaps.
</p>
</div>
<div class="scenario-badge">
FICTIONAL SCENARIO · NOT AN ECOLOGICAL POPULATION FORECAST
</div>
</div>
<div class="scene-hint">
<span class="hint-pill">Drag to orbit</span>
<span class="hint-pill">Scroll to zoom</span>
<span class="hint-pill">Click a shell, then a crab</span>
<span class="hint-pill">F = auto-fit · R = reset</span>
</div>
<div id="status" role="status" aria-live="polite">
Select an empty shell from a blue-ringed tile to begin.
</div>
<div id="loading">Building tide-pool board…</div>
</section>
<aside aria-label="Compatibility controls and information">
<div class="panel-heading">
<h2>Fictional fit study</h2>
<span class="mini-tag">Scenario 01</span>
</div>
<div class="notice">
<strong>Game mechanics, not ecology.</strong> All crabs, sizes, shells,
and capacities are invented for this puzzle. The board does not estimate
real populations, habitat carrying capacity, or future population trends.
</div>
<section class="section">
<h3 class="section-title">Compatibility rule <span>simple capacity check</span></h3>
<div class="rule-card">
A shell may be assigned only when
<code>shell capacity ≥ crab size</code>.
Each crab can hold one shell, and each shell has one location.
Selecting an occupied shell also allows a swap or return to an empty tile.
</div>
<div id="selectionCard" class="selection-card">
<div class="label">Selected shell</div>
<div id="selectionName">None selected</div>
<div id="selectionMeta">
Choose a shell on the board. Its fit results will appear here.
</div>
</div>
</section>
<section class="section">
<h3 class="section-title">Fictional crabs <span>required size</span></h3>
<div id="crabList" class="data-list"></div>
</section>
<section class="section">
<h3 class="section-title">Fictional shells <span>available capacity</span></h3>
<div id="shellList" class="data-list"></div>
</section>
<section class="section">
<h3 class="section-title">Legend <span>board colors</span></h3>
<div class="legend">
<span class="legend-item">
<span class="legend-swatch" style="background:#33c7d1"></span>
Empty shell
</span>
<span class="legend-item">
<span class="legend-swatch" style="background:#49a875"></span>
Occupied shell
</span>
<span class="legend-item">
<span class="legend-swatch" style="background:#e87645"></span>
Crab
</span>
<span class="legend-item">
<span class="legend-swatch" style="background:#e9b44c"></span>
Selected
</span>
</div>
</section>
<div class="buttons">
<button id="fitButton" class="primary" type="button">Auto-fit available</button>
<button id="undoButton" type="button" disabled>Undo</button>
<button id="resetButton" type="button" style="grid-column: 1 / -1">
Reset fictional board
</button>
</div>
<section class="section">
<h3 class="section-title">Activity <span>most recent first</span></h3>
<ul id="activity" class="activity"></ul>
</section>
<p class="footnote">
The decorative tide pool and voxel animals are visual shorthand only.
Capacity values are arbitrary puzzle values, not measured biological data.
</p>
</aside>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and scene setup
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const canvas = $("scene");
const statusEl = $("status");
const activityEl = $("activity");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
$("loading").textContent = "This scene requires WebGL.";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
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(0xdceee7);
scene.fog = new THREE.Fog(0xdceee7, 18, 34);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
let cameraYaw = 0.57;
let cameraPitch = 0.82;
let cameraRadius = 14;
const clock = new THREE.Clock();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0.02,
...options
});
const materials = {
ground: standard(0xc8ddd2),
base: standard(0x6b8580),
baseEdge: standard(0x486761),
stoneA: standard(0x8fa39b),
stoneB: standard(0xa8b8ae),
stoneC: standard(0x718a83),
tileA: standard(0xb7c3a7),
tileB: standard(0xaeba9d),
tileC: standard(0xc4c7a9),
sand: standard(0xd8c596),
kelp: standard(0x3d8866),
kelpLight: standard(0x65a979),
anemone: standard(0xe98276),
anemoneLight: standard(0xf1ae91),
dark: standard(0x263e3d),
eyeWhite: standard(0xf8f1d2),
crabLeg: standard(0xa64c34),
wood: standard(0x927151),
woodLight: standard(0xb58a60)
};
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x4fc6c6,
transparent: true,
opacity: 0.24,
roughness: 0.25,
metalness: 0.02,
depthWrite: false
});
// -------------------------------------------------------------------------
// Scene lights
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf5fff4, 0x54716c, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff4d7, 3.0);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x78d7d5, 1.35);
rimLight.position.set(7, 5, -7);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function addBox(
parent,
width,
height,
depth,
x,
y,
z,
material,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(text, background, foreground = "#183f42", width = 1.4) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 320;
labelCanvas.height = 84;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
roundedRect(ctx, 7, 7, 306, 70, 17);
ctx.fillStyle = background;
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = "rgba(255,255,255,0.7)";
ctx.stroke();
ctx.fillStyle = foreground;
ctx.font = "800 30px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 160, 43);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width * 84 / 320, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Board and cells
// -------------------------------------------------------------------------
const COLS = 5;
const ROWS = 4;
const STEP = 1.35;
const TILE = 1.24;
const boardWidth = COLS * STEP + 0.72;
const boardDepth = ROWS * STEP + 0.72;
const ground = addBox(
scene,
40,
0.12,
40,
0,
-0.78,
0,
materials.ground,
false
);
ground.receiveShadow = true;
addBox(
scene,
boardWidth,
0.62,
boardDepth,
0,
-0.37,
0,
materials.base
);
addBox(
scene,
boardWidth + 0.12,
0.13,
boardDepth + 0.12,
0,
-0.68,
0,
materials.baseEdge
);
// Narrow raised rim pieces give the base a constructed "board" appearance.
addBox(
scene,
boardWidth + 0.1,
0.18,
0.14,
0,
-0.03,
boardDepth / 2,
materials.wood
);
addBox(
scene,
boardWidth + 0.1,
0.18,
0.14,
0,
-0.03,
-boardDepth / 2,
materials.wood
);
addBox(
scene,
0.14,
0.18,
boardDepth + 0.1,
boardWidth / 2,
-0.03,
0,
materials.woodLight
);
addBox(
scene,
0.14,
0.18,
boardDepth + 0.1,
-boardWidth / 2,
-0.03,
0,
materials.woodLight
);
function cellPosition(cellId) {
const row = Number(cellId.slice(1, cellId.indexOf('c')));
const col = Number(cellId.slice(3));
return {
x: (col - (COLS - 1) / 2) * STEP,
z: (row - (ROWS - 1) / 2) * STEP
};
}
const cellMap = new Map();
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const id = `r${row}c${col}`;
const pos = cellPosition(id);
const tileMaterials = [
materials.tileA,
materials.tileB,
materials.tileC
];
const tile = addBox(
scene,
TILE,
0.32,
TILE,
pos.x,
0.11,
pos.z,
tileMaterials[(row + col) % tileMaterials.length]
);
tile.userData = { kind: "cell", cellId: id };
const water = addBox(
scene,
TILE * 0.94,
0.022,
TILE * 0.94,
pos.x,
0.284,
pos.z,
waterMaterial,
false
);
water.userData = { kind: "cell", cellId: id };
// Small inset strips create a square voxel-pool boundary.
const stripMaterial = materials.stoneC;
const half = TILE * 0.49;
const stripLength = TILE * 0.84;
addBox(
scene,
stripLength,
0.045,
0.045,
pos.x,
0.275,
pos.z - half,
stripMaterial,
false
);
addBox(
scene,
stripLength,
0.045,
0.045,
pos.x,
0.275,
pos.z + half,
stripMaterial,
false
);
addBox(
scene,
0.045,
0.045,
stripLength,
pos.x - half,
0.275,
pos.z,
stripMaterial,
false
);
addBox(
scene,
0.045,
0.045,
stripLength,
pos.x + half,
0.275,
pos.z,
stripMaterial,
false
);
cellMap.set(id, { id, row, col, x: pos.x, z: pos.z });
}
}
// -------------------------------------------------------------------------
// Fictional puzzle state
// -------------------------------------------------------------------------
const crabs = [
{
id: "c1",
name: "Pip",
size: 2,
cell: "r1c1",
color: 0xe97845
},
{
id: "c2",
name: "Nib",
size: 3,
cell: "r1c3",
color: 0xd95b48
},
{
id: "c3",
name: "Rook",
size: 4,
cell: "r2c0",
color: 0xc9703d
},
{
id: "c4",
name: "Mica",
size: 4,
cell: "r2c2",
color: 0xef8550
},
{
id: "c5",
name: "Ember",
size: 5,
cell: "r3c1",
color: 0xde6044
},
{
id: "c6",
name: "Grit",
size: 6,
cell: "r3c4",
color: 0xb9523f
}
];
const shells = [
{
id: "s1",
name: "Spiral",
cap: 3,
owner: "c1",
cell: "r1c1",
color: 0xf0c59d
},
{
id: "s2",
name: "Box",
cap: 5,
owner: "c3",
cell: "r2c0",
color: 0xe8ae9e
},
{
id: "s3",
name: "Crown",
cap: 7,
owner: "c5",
cell: "r3c1",
color: 0xf3d58f
},
{
id: "s4",
name: "Bud",
cap: 3,
owner: null,
cell: "r0c1",
color: 0xd8e4ad
},
{
id: "s5",
name: "Fork",
cap: 4,
owner: null,
cell: "r0c3",
color: 0xefb2b4
},
{
id: "s6",
name: "Chamber",
cap: 6,
owner: null,
cell: "r3c3",
color: 0xa9d2cd
}
];
const initialShellState = shells.map((shell) => ({
id: shell.id,
owner: shell.owner,
cell: shell.cell
}));
const crabMap = new Map(crabs.map((crab) => [crab.id, crab]));
const shellMap = new Map(shells.map((shell) => [shell.id, shell]));
let selectedShell = null;
let hoveredCell = null;
let history = [];
// -------------------------------------------------------------------------
// Voxel marker groups
// -------------------------------------------------------------------------
function createCellMarker(color) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const armLength = TILE * 0.22;
const offset = TILE * 0.43;
const thickness = 0.055;
addBox(
group,
armLength,
thickness,
thickness,
-offset,
0,
-offset,
material,
false
);
addBox(
group,
armLength,
thickness,
thickness,
offset,
0,
-offset,
material,
false
);
addBox(
group,
armLength,
thickness,
thickness,
-offset,
0,
offset,
material,
false
);
addBox(
group,
armLength,
thickness,
thickness,
offset,
0,
offset,
material,
false
);
group.position.y = 0.316;
group.userData.markerMaterial = material;
scene.add(group);
return group;
}
const shellMarkers = new Map();
const crabMarkers = new Map();
for (const shell of shells) {
shellMarkers.set(shell.id, createCellMarker(0x33c7d1));
}
for (const crab of crabs) {
crabMarkers.set(crab.id, createCellMarker(0xe87645));
}
const hoverOverlay = new THREE.Mesh(
new THREE.BoxGeometry(TILE * 0.95, 0.018, TILE * 0.95),
new THREE.MeshBasicMaterial({
color: 0xfff0a4,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
hoverOverlay.position.y = 0.321;
hoverOverlay.visible = false;
scene.add(hoverOverlay);
const pulseOverlay = new THREE.Mesh(
new THREE.BoxGeometry(TILE * 1.02, 0.025, TILE * 1.02),
new THREE.MeshBasicMaterial({
color: 0xe65a5a,
transparent: true,
opacity: 0,
depthWrite: false
})
);
pulseOverlay.position.y = 0.329;
pulseOverlay.visible = false;
scene.add(pulseOverlay);
let pulseStart = -10;
// -------------------------------------------------------------------------
// Voxel crab construction
// -------------------------------------------------------------------------
function buildCrab(crab) {
const group = new THREE.Group();
group.userData = { kind: "crab", crabId: crab.id };
const shellColor = standard(crab.color);
const shellDark = standard(new THREE.Color(crab.color).multiplyScalar(0.76));
const scale = 0.81 + crab.size * 0.035;
addBox(group, 0.58, 0.22, 0.42, 0, 0.19, 0, shellColor);
addBox(group, 0.46, 0.1, 0.34, 0, 0.04, 0, shellDark);
addBox(group, 0.37, 0.24, 0.3, 0, 0.29, 0.29, shellColor);
// Eye stalks and blocky eyes.
for (const side of [-1, 1]) {
addBox(
group,
0.065,
0.19,
0.065,
side * 0.115,
0.49,
0.32,
shellDark
);
addBox(
group,
0.105,
0.105,
0.105,
side * 0.115,
0.61,
0.33,
materials.eyeWhite
);
addBox(
group,
0.047,
0.052,
0.05,
side * 0.115,
0.62,
0.385,
materials.dark
);
}
// Voxel walking legs.
for (const side of [-1, 1]) {
for (let i = 0; i < 3; i++) {
const z = 0.16 - i * 0.18;
const upper = addBox(
group,
0.34,
0.085,
0.1,
side * 0.37,
0.12,
z,
shellDark
);
upper.rotation.z = side * 0.22;
const lower = addBox(
group,
0.18,
0.07,
0.085,
side * 0.54,
0.065,
z,
shellColor
);
lower.rotation.z = side * -0.12;
}
// Blocky claw arm and claw.
addBox(
group,
0.28,
0.085,
0.11,
side * 0.33,
0.2,
0.34,
shellDark
);
addBox(
group,
0.23,
0.16,
0.2,
side * 0.48,
0.24,
0.46,
shellColor
);
addBox(
group,
0.08,
0.075,
0.11,
side * 0.5,
0.34,
0.52,
shellDark
);
}
// Tiny front antennae.
addBox(group, 0.035, 0.035, 0.2, -0.08, 0.38, 0.49, shellDark);
addBox(group, 0.035, 0.035, 0.2, 0.08, 0.38, 0.49, shellDark);
const label = makeLabel(
`${crab.name} · S${crab.size}`,
"rgba(255,247,225,0.94)",
"#633728",
1.38
);
label.position.set(-0.08, 1.03, 0);
label.userData = { kind: "crab", crabId: crab.id };
group.add(label);
group.scale.setScalar(scale);
const pos = cellPosition(crab.cell);
group.position.set(pos.x, 0.31, pos.z);
scene.add(group);
return group;
}
for (const crab of crabs) {
crab.mesh = buildCrab(crab);
}
// -------------------------------------------------------------------------
// Voxel shell construction
// -------------------------------------------------------------------------
function buildShell(shell) {
const group = new THREE.Group();
group.userData = { kind: "shell", shellId: shell.id };
const shellMaterial = standard(shell.color);
const shellLight = standard(new THREE.Color(shell.color).lerp(
new THREE.Color(0xffffff),
0.22
));
const shellDark = standard(new THREE.Color(shell.color).multiplyScalar(0.7));
const turns = 3.25;
const pointCount = 31;
for (let i = 0; i < pointCount; i++) {
const t = i / (pointCount - 1);
const angle = t * Math.PI * 2 * turns;
const radius = 0.08 + t * 0.57;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius * 0.92;
const z = Math.sin(angle * 0.5) * 0.055;
const cubeSize = 0.12 + t * 0.045;
const cube = addBox(
group,
cubeSize,
cubeSize,
cubeSize * 0.86,
x,
y,
z,
i % 4 === 0 ? shellLight : shellMaterial
);
cube.userData = { kind: "shell", shellId: shell.id };
}
// Blocky shell opening.
const opening = addBox(
group,
0.21,
0.18,
0.16,
0.39,
-0.22,
0.11,
materials.dark
);
opening.userData = { kind: "shell", shellId: shell.id };
const rim = addBox(
group,
0.29,
0.065,
0.19,
0.39,
-0.105,
0.11,
shellDark
);
rim.userData = { kind: "shell", shellId: shell.id };
const label = makeLabel(
`${shell.name} · C${shell.cap}`,
"rgba(241,251,246,0.95)",
"#24595d",
1.45
);
label.position.set(0.08, 0.86, 0);
label.userData = { kind: "shell", shellId: shell.id };
group.add(label);
const scale = 0.72 + shell.cap * 0.047;
group.scale.setScalar(scale);
scene.add(group);
return group;
}
for (const shell of shells) {
shell.mesh = buildShell(shell);
}
// -------------------------------------------------------------------------
// Decorative tide-pool scenery, all built from voxel-like boxes
// -------------------------------------------------------------------------
function addRock(x, z, size, material = materials.stoneB) {
const group = new THREE.Group();
addBox(group, size, size * 0.62, size * 0.8, 0, 0, 0, material);
addBox(
group,
size * 0.72,
size * 0.45,
size * 0.62,
size * 0.1,
size * 0.47,
-size * 0.03,
materials.stoneC
);
addBox(
group,
size * 0.48,
size * 0.28,
size * 0.4,
-size * 0.28,
size * 0.37,
size * 0.16,
material
);
group.position.set(x, 0.02, z);
group.rotation.y = (x * 0.31 + z * 0.17) % Math.PI;
scene.add(group);
}
const rocks = [
[-3.67, -2.85, 0.55],
[-3.82, -1.7, 0.38],
[-3.72, 0.55, 0.47],
[-3.86, 2.25, 0.42],
[3.7, -2.83, 0.43],
[3.82, -1.55, 0.54],
[3.73, 0.15, 0.37],
[3.78, 2.35, 0.5],
[-2.2, -3.12, 0.36],
[-0.6, -3.16, 0.29],
[1.05, -3.1, 0.41],
[2.5, -3.05, 0.3]
];
for (const [x, z, size] of rocks) {
addRock(x, z, size);
}
function addKelp(x, z, variant = 0) {
const group = new THREE.Group();
const height = 0.55 + variant * 0.08;
addBox(
group,
0.075,
height,
0.075,
0,
height / 2,
0,
materials.kelp
);
addBox(
group,
0.12,
0.18,
0.1,
0.055,
height * 0.56,
0,
materials.kelpLight
);
addBox(
group,
0.14,
0.16,
0.1,
-0.065,
height * 0.82,
0,
materials.kelpLight
);
group.position.set(x, 0.26, z);
scene.add(group);
}
addKelp(-2.83, -2.35, 0);
addKelp(-2.6, -2.46, 1);
addKelp(2.74, -2.23, 2);
addKelp(2.9, 1.4, 1);
addKelp(-3.02, 1.72, 2);
function addAnemone(x, z) {
const group = new THREE.Group();
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const stem = addBox(
group,
0.09,
0.28,
0.09,
Math.cos(angle) * 0.13,
0.12,
Math.sin(angle) * 0.13,
materials.anemone
);
stem.rotation.z = Math.cos(angle) * 0.28;
}
addBox(group, 0.2, 0.12, 0.2, 0, 0.26, 0, materials.anemoneLight);
group.position.set(x, 0.27, z);
scene.add(group);
}
addAnemone(2.68, 2.56);
addAnemone(-2.45, 2.76);
const bubbles = [];
for (let i = 0; i < 13; i++) {
const material = new THREE.MeshPhysicalMaterial({
color: 0xd8ffff,
transparent: true,
opacity: 0.34,
roughness: 0.1,
transmission: 0.15,
depthWrite: false
});
const size = 0.06 + (i % 4) * 0.018;
const bubble = new THREE.Mesh(
new THREE.BoxGeometry(size, size, size),
material
);
const column = i % 3;
const row = Math.floor(i / 3) % 4;
bubble.position.set(
cellPosition(`r${row}c${column}`).x + (i % 2 ? 0.24 : -0.19),
0.42,
cellPosition(`r${row}c${column}`).z - 0.12
);
bubble.userData = {
baseY: bubble.position.y,
phase: i * 0.173,
speed: 0.14 + (i % 5) * 0.022
};
scene.add(bubble);
bubbles.push(bubble);
}
const boardLabel = makeLabel(
"TIDE-POOL // CAPACITY STUDY",
"rgba(24,74,74,0.86)",
"#e7fff6",
2.65
);
boardLabel.position.set(0, 0.62, -3.28);
scene.add(boardLabel);
// -------------------------------------------------------------------------
// State synchronization
// -------------------------------------------------------------------------
function shellOwnerForCrab(crabId) {
return shells.find((shell) => shell.owner === crabId) || null;
}
function syncVisuals() {
for (const shell of shells) {
const cell = cellMap.get(shell.cell);
const owner = shell.owner ? crabMap.get(shell.owner) : null;
if (owner) {
shell.mesh.position.set(cell.x + 0.28, 0.56, cell.z - 0.12);
shell.mesh.rotation.y = -0.12;
} else {
shell.mesh.position.set(cell.x, 0.56, cell.z);
shell.mesh.rotation.y = 0;
}
const marker = shellMarkers.get(shell.id);
marker.position.x = cell.x;
marker.position.z = cell.z;
const markerMaterial = marker.userData.markerMaterial;
markerMaterial.color.setHex(
selectedShell?.id === shell.id
? 0xe9b44c
: shell.owner
? 0x49a875
: 0x33c7d1
);
marker.scale.setScalar(selectedShell?.id === shell.id ? 1.06 : 1);
}
for (const crab of crabs) {
const cell = cellMap.get(crab.cell);
const marker = crabMarkers.get(crab.id);
marker.position.x = cell.x;
marker.position.z = cell.z;
marker.visible = !shellOwnerForCrab(crab.id);
marker.userData.markerMaterial.opacity = 0.55;
}
}
function renderPanel() {
const selected = selectedShell;
$("selectionName").textContent = selected
? `${selected.name} · capacity ${selected.cap}`
: "None selected";
if (selected) {
const compatible = crabs.filter((crab) => crab.size <= selected.cap);
const blocked = crabs.filter((crab) => crab.size > selected.cap);
$("selectionMeta").innerHTML = selected.owner
? `Currently on <strong>${crabMap.get(selected.owner).name}</strong>. ` +
`It can move to ${compatible.length} of ${crabs.length} fictional crabs.`
: `Available now. It can fit ${compatible.length} of ${crabs.length} fictional crabs. ` +
(blocked.length
? `Too small for ${blocked.map((crab) => crab.name).join(", ")}.`
: "It clears every fictional crab size.");
} else {
$("selectionMeta").textContent =
"Choose a shell on the board. Its fit results will appear here.";
}
$("crabList").innerHTML = crabs
.map((crab) => {
let rowClass = "";
let statusClass = "";
let status = `Needs size ${crab.size}`;
if (selected) {
const fits = selected.cap >= crab.size;
rowClass = fits ? "fits" : "blocked";
statusClass = fits ? "yes" : "no";
status = fits ? "Compatible" : "Too small";
}
return `
<div class="data-row ${rowClass}">
<div class="data-main">
<div class="data-name">
<span class="dot" style="background:#${crab.color.toString(16).padStart(6, "0")}"></span>
${crab.name}
</div>
<div class="data-sub">Fictional crab · required size ${crab.size}</div>
</div>
<span class="value ${statusClass}">${status}</span>
</div>
`;
})
.join("");
$("shellList").innerHTML = shells
.map((shell) => {
const owner = shell.owner ? crabMap.get(shell.owner) : null;
const selectedClass = selected?.id === shell.id ? "selected" : "";
return `
<div class="data-row ${selectedClass}">
<div class="data-main">
<div class="data-name">
<span class="dot" style="background:#${shell.color.toString(16).padStart(6, "0")}"></span>
${shell.name}
</div>
<div class="data-sub">
${owner ? `On ${owner.name}` : "Empty / available"}
</div>
</div>
<span class="value">Cap ${shell.cap}</span>
</div>
`;
})
.join("");
$("undoButton").disabled = history.length === 0;
}
function setStatus(message, type = "info") {
statusEl.textContent = message;
statusEl.className = type;
}
function addLog(message) {
const item = document.createElement("li");
item.textContent = message;
activityEl.prepend(item);
while (activityEl.children.length > 6) {
activityEl.lastElementChild.remove();
}
}
function selectShell(shell) {
selectedShell = shell;
syncVisuals();
renderPanel();
if (shell.owner) {
setStatus(
`Selected ${shell.name} (capacity ${shell.cap}) on ${
crabMap.get(shell.owner).name
}. Choose another crab or an empty tile.`
);
} else {
setStatus(
`Selected ${shell.name} (capacity ${shell.cap}). Choose a crab to test compatibility.`
);
}
}
function clearSelection() {
selectedShell = null;
syncVisuals();
renderPanel();
setStatus("Selection cleared. Choose an empty shell from a blue-ringed tile.");
}
function snapshot() {
history.push(
shells.map((shell) => ({
id: shell.id,
owner: shell.owner,
cell: shell.cell
}))
);
if (history.length > 30) {
history.shift();
}
}
function restoreSnapshot(snapshotData) {
for (const saved of snapshotData) {
const shell = shellMap.get(saved.id);
shell.owner = saved.owner;
shell.cell = saved.cell;
}
}
function pulseCell(cellId) {
const cell = cellMap.get(cellId);
pulseOverlay.position.x = cell.x;
pulseOverlay.position.z = cell.z;
pulseOverlay.visible = true;
pulseStart = clock.getElapsedTime();
}
function attemptAssignment(shell, crab) {
if (shell.cap < crab.size) {
pulseCell(crab.cell);
crab.mesh.userData.shakeUntil = clock.getElapsedTime() + 0.65;
const message =
`Move refused: ${shell.name} has capacity ${shell.cap}, but ` +
`${crab.name} requires size ${crab.size}.`;
setStatus(message, "bad");
addLog(message);
return;
}
if (shell.owner === crab.id) {
setStatus(`${shell.name} is already assigned to ${crab.name}.`, "warn");
return;
}
snapshot();
const targetShell = shellOwnerForCrab(crab.id);
const sourceCrab = shell.owner ? crabMap.get(shell.owner) : null;
if (sourceCrab) {
if (targetShell) {
// Swap the two shells and their crab assignments.
targetShell.owner = sourceCrab.id;
targetShell.cell = sourceCrab.cell;
shell.owner = crab.id;
shell.cell = crab.cell;
const message =
`Swapped ${shell.name} onto ${crab.name} and ` +
`${targetShell.name} onto ${sourceCrab.name}. Both capacities fit.`;
setStatus(message, "good");
addLog(message);
} else {
shell.owner = crab.id;
shell.cell = crab.cell;
const message =
`Moved ${shell.name} from ${sourceCrab.name} to ${crab.name}. ` +
`Capacity ${shell.cap} fits size ${crab.size}.`;
setStatus(message, "good");
addLog(message);
}
} else {
if (targetShell) {
// The displaced shell returns to the selected shell's old cache tile.
targetShell.owner = null;
targetShell.cell = shell.cell;
}
shell.owner = crab.id;
shell.cell = crab.cell;
const message =
`Assigned ${shell.name} (capacity ${shell.cap}) to ` +
`${crab.name} (size ${crab.size}).`;
setStatus(message, "good");
addLog(message);
}
syncVisuals();
renderPanel();
}
function moveShellToEmptyCell(shell, cellId) {
const crab = crabs.find((item) => item.cell === cellId);
if (crab) {
attemptAssignment(shell, crab);
return;
}
if (shell.cell === cellId) {
setStatus(`${shell.name} is already on that tile.`, "warn");
return;
}
snapshot();
const previousCrab = shell.owner ? crabMap.get(shell.owner) : null;
shell.owner = null;
shell.cell = cellId;
const message = previousCrab
? `Returned ${shell.name} to an empty tile; ${
previousCrab.name
} is now without a shell.`
: `Moved ${shell.name} to an empty tile.`;
setStatus(message, previousCrab ? "warn" : "good");
addLog(message);
syncVisuals();
renderPanel();
}
function undo() {
if (!history.length) return;
const previous = history.pop();
restoreSnapshot(previous);
selectedShell = null;
syncVisuals();
renderPanel();
setStatus("Undid the last shell redistribution.", "good");
addLog("Undid the last shell redistribution.");
}
function autoFit() {
const unownedCrabs = crabs
.filter((crab) => !shellOwnerForCrab(crab.id))
.sort((a, b) => b.size - a.size);
const available = shells
.filter((shell) => !shell.owner)
.sort((a, b) => a.cap - b.cap);
const assignments = [];
for (const crab of unownedCrabs) {
const shell = available.find((item) => item.cap >= crab.size);
if (!shell) continue;
if (!assignments.length) snapshot();
shell.owner = crab.id;
shell.cell = crab.cell;
available.splice(available.indexOf(shell), 1);
assignments.push({ crab, shell });
}
if (!assignments.length) {
setStatus("No additional compatible shell assignments are available.", "warn");
return;
}
selectedShell = null;
syncVisuals();
renderPanel();
const message =
`Auto-fit placed ${assignments.length} available shell${
assignments.length === 1 ? "" : "s"
} using the capacity rule.`;
setStatus(message, "good");
addLog(message);
for (const assignment of assignments) {
addLog(
`${assignment.shell.name} (C${assignment.shell.cap}) → ${
assignment.crab.name
} (S${assignment.crab.size}).`
);
}
}
function resetBoard() {
restoreSnapshot(initialShellState);
history = [];
selectedShell = null;
syncVisuals();
renderPanel();
setStatus("Reset to the original fictional board.", "good");
addLog("Reset the fictional board.");
}
// -------------------------------------------------------------------------
// Picking and direct camera controls
// -------------------------------------------------------------------------
function updatePointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pick(event) {
updatePointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(scene.children, true);
for (const intersection of intersections) {
const data = intersection.object.userData;
if (data.kind) {
return { ...data, object: intersection.object };
}
}
return null;
}
function updateHover(event) {
const hit = pick(event);
let nextCell = null;
if (hit.kind === "cell") {
nextCell = hit.cellId;
} else if (hit.kind === "crab") {
nextCell = crabMap.get(hit.crabId).cell;
} else if (hit.kind === "shell") {
nextCell = shellMap.get(hit.shellId).cell;
}
hoveredCell = nextCell;
if (hoveredCell) {
const cell = cellMap.get(hoveredCell);
hoverOverlay.position.x = cell.x;
hoverOverlay.position.z = cell.z;
hoverOverlay.visible = true;
canvas.style.cursor = "pointer";
} else {
hoverOverlay.visible = false;
canvas.style.cursor = pointerIsDown ? "grabbing" : "default";
}
}
let pointerIsDown = false;
let pointerMoved = false;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerIsDown = true;
pointerMoved = false;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerIsDown) {
const totalDistance = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (totalDistance > 4) {
pointerMoved = true;
}
if (pointerMoved) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
cameraYaw -= dx * 0.007;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0055,
0.35,
1.35
);
updateCamera();
}
previousX = event.clientX;
previousY = event.clientY;
return;
}
updateHover(event);
});
canvas.addEventListener("pointerup", (event) => {
const wasClick = !pointerMoved;
pointerIsDown = false;
canvas.style.cursor = "default";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (!wasClick) return;
const hit = pick(event);
if (!hit) {
clearSelection();
return;
}
if (hit.kind === "shell") {
selectShell(shellMap.get(hit.shellId));
return;
}
if (hit.kind === "crab") {
const crab = crabMap.get(hit.crabId);
if (selectedShell) {
attemptAssignment(selectedShell, crab);
} else {
const ownedShell = shellOwnerForCrab(crab.id);
if (ownedShell) {
selectShell(ownedShell);
} else {
setStatus(
`${crab.name} needs a shell of capacity ${crab.size} or greater. ` +
`Select an empty shell first.`,
"warn"
);
}
}
return;
}
if (hit.kind === "cell") {
const cellId = hit.cellId;
const crab = crabs.find((item) => item.cell === cellId);
if (selectedShell) {
if (crab) {
attemptAssignment(selectedShell, crab);
} else {
moveShellToEmptyCell(selectedShell, cellId);
}
} else if (crab) {
const ownedShell = shellOwnerForCrab(crab.id);
if (ownedShell) {
selectShell(ownedShell);
} else {
setStatus(
`${crab.name} has no shell. Select an empty shell to test it.`,
"warn"
);
}
} else {
setStatus("Select a shell before choosing an empty tile.");
}
}
});
canvas.addEventListener("pointercancel", () => {
pointerIsDown = false;
pointerMoved = false;
canvas.style.cursor = "default";
});
canvas.addEventListener("pointerleave", () => {
if (!pointerIsDown) {
hoveredCell = null;
hoverOverlay.visible = false;
}
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const zoomFactor = Math.exp(-event.deltaY * 0.0011);
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * zoomFactor,
0.65,
1.9
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
function updateCamera() {
const direction = new THREE.Vector3(
Math.sin(cameraYaw) * Math.cos(cameraPitch),
Math.sin(cameraPitch),
Math.cos(cameraYaw) * Math.cos(cameraPitch)
);
camera.position.copy(cameraTarget).addScaledVector(direction, cameraRadius);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resize() {
const width = Math.max(stage.clientWidth, 1);
const height = Math.max(stage.clientHeight, 1);
const aspect = width / height;
// Preserve the entire board on narrow or wide displays.
const viewHeight = Math.max(8.65, 9.25 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
new ResizeObserver(resize).observe(stage);
resize();
updateCamera();
// -------------------------------------------------------------------------
// Buttons and keyboard shortcuts
// -------------------------------------------------------------------------
$("fitButton").addEventListener("click", autoFit);
$("undoButton").addEventListener("click", undo);
$("resetButton").addEventListener("click", resetBoard);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.key === "Escape") {
clearSelection();
} else if (event.key.toLowerCase() === "f") {
autoFit();
} else if (event.key.toLowerCase() === "r") {
resetBoard();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
waterMaterial.opacity = 0.225 + Math.sin(elapsed * 0.8) * 0.018;
hoverOverlay.material.opacity =
0.16 + (Math.sin(elapsed * 4.5) + 1) * 0.035;
for (const bubble of bubbles) {
const data = bubble.userData;
const rise = (elapsed * data.speed + data.phase) % 1.35;
bubble.position.y = data.baseY + rise;
bubble.rotation.y = elapsed * 0.35 + data.phase;
bubble.rotation.z = elapsed * 0.18;
}
for (const crab of crabs) {
const shakeUntil = crab.mesh.userData.shakeUntil || 0;
if (elapsed < shakeUntil) {
const remaining = shakeUntil - elapsed;
crab.mesh.rotation.z =
Math.sin(remaining * 48) * 0.07 * (remaining / 0.65);
} else {
crab.mesh.rotation.z = 0;
}
}
if (pulseOverlay.visible) {
const pulseAge = elapsed - pulseStart;
if (pulseAge > 0.7) {
pulseOverlay.visible = false;
pulseOverlay.material.opacity = 0;
} else {
const scale = 1 + Math.sin(pulseAge * 16) * 0.07;
pulseOverlay.scale.set(scale, 1, scale);
pulseOverlay.material.opacity = 0.34 * (1 - pulseAge / 0.7);
}
}
renderer.render(scene, camera);
}
// Initial UI and scene state.
syncVisuals();
renderPanel();
addLog("Board ready. Three fictional shells are available.");
addLog("Select a blue-ringed shell, then choose a crab.");
animate();
$("loading").style.display = "none";
</script>
</body>
</html>
```