Prompt specification
Build or customize the Three.js 3D scene "River Crossing Construction Level" 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: River Crossing Construction Level
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/river-crossing-construction-level-x3443
Scene Description Prompt:
Use Three.js from cdnjs to build an original voxel river scene with limited bridge modules and clear placement sockets. Require a connected path from shore to shore under explicit game support rules. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (river-crossing-construction-level-x3443):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blockcurrent — The Crooked Ford</title>
<style>
:root {
--ink: #08282d;
--panel: rgba(7, 35, 40, 0.9);
--panel-soft: rgba(16, 56, 60, 0.76);
--line: rgba(164, 239, 226, 0.22);
--mint: #73f2d1;
--cyan: #65d9ee;
--gold: #ffd166;
--cream: #fff3d2;
--muted: #a4c9c8;
--danger: #ff7f73;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9bd3d7;
color: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas.dragging { cursor: grabbing; }
.brand {
position: fixed;
top: 20px;
left: 22px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 35, 40, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 5px;
color: #eafff8;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
border: 2px solid var(--mint);
transform: rotate(45deg);
box-shadow: 0 0 12px rgba(115, 242, 209, 0.8);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.95;
letter-spacing: -0.055em;
}
.subtitle {
margin-top: 8px;
color: #e0f6ed;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 6;
top: 116px;
left: 20px;
width: 306px;
max-height: calc(100vh - 190px);
overflow: auto;
padding: 17px;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(27, 76, 77, 0.38), transparent 44%),
var(--panel);
box-shadow: 0 22px 60px rgba(0, 28, 33, 0.28);
backdrop-filter: blur(18px) saturate(1.15);
scrollbar-width: thin;
scrollbar-color: rgba(115, 242, 209, 0.4) transparent;
}
.section + .section {
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
color: var(--mint);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.objective {
margin: 0;
color: #eefdf7;
font-size: 14px;
line-height: 1.5;
}
.objective strong { color: var(--gold); }
.module-list {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.module {
position: relative;
min-height: 100px;
padding: 11px;
overflow: hidden;
border: 1px solid rgba(180, 239, 226, 0.17);
border-radius: 13px;
background: rgba(255, 255, 255, 0.055);
color: var(--cream);
text-align: left;
cursor: pointer;
transition: 160ms ease;
}
.module:hover:not(:disabled) {
transform: translateY(-2px);
border-color: rgba(115, 242, 209, 0.45);
background: rgba(115, 242, 209, 0.09);
}
.module.selected {
border-color: var(--mint);
background: rgba(115, 242, 209, 0.13);
box-shadow: inset 0 0 0 1px rgba(115, 242, 209, 0.13);
}
.module:disabled {
opacity: 0.36;
cursor: not-allowed;
}
.module .key {
position: absolute;
top: 8px;
right: 9px;
color: rgba(255, 255, 255, 0.4);
font-size: 10px;
font-weight: 800;
}
.module-icon {
position: relative;
width: 53px;
height: 26px;
margin-bottom: 11px;
}
.module-icon::before,
.module-icon::after {
content: "";
position: absolute;
left: 0;
top: 7px;
width: 53px;
height: 12px;
border-radius: 2px;
background: #ce8148;
box-shadow: inset 0 -3px rgba(65, 38, 24, 0.22);
}
.module-icon::after {
left: 21px;
top: 0;
width: 12px;
height: 26px;
background: var(--gold);
box-shadow: none;
}
.curve .module-icon::before { background: #c67842; }
.curve .module-icon::after { background: var(--mint); }
.module strong {
display: block;
font-size: 13px;
}
.module small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.count {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 23px;
height: 23px;
margin-left: 5px;
border-radius: 7px;
background: rgba(0, 0, 0, 0.24);
color: var(--gold);
font-size: 12px;
font-weight: 900;
}
.orientation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
padding: 10px;
border-radius: 11px;
background: rgba(0, 0, 0, 0.18);
}
.orientation span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.orientation strong {
display: block;
margin-top: 2px;
color: var(--cream);
font-size: 12px;
letter-spacing: 0.02em;
text-transform: none;
}
.mini-button {
flex: 0 0 auto;
padding: 8px 10px;
border: 1px solid rgba(115, 242, 209, 0.3);
border-radius: 9px;
background: rgba(115, 242, 209, 0.09);
color: var(--mint);
font-size: 10px;
font-weight: 900;
cursor: pointer;
}
.rules {
margin: 0;
padding: 0;
list-style: none;
counter-reset: rules;
}
.rules li {
position: relative;
margin: 0 0 10px;
padding-left: 28px;
color: #cde4df;
font-size: 11px;
line-height: 1.42;
counter-increment: rules;
}
.rules li::before {
content: counter(rules);
position: absolute;
top: 0;
left: 0;
display: grid;
width: 18px;
height: 18px;
place-items: center;
border: 1px solid rgba(115, 242, 209, 0.35);
border-radius: 5px;
color: var(--mint);
font-size: 9px;
font-weight: 900;
}
.rules strong { color: #fff8df; }
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px 14px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.dot {
width: 9px;
height: 9px;
border: 2px solid var(--cyan);
border-radius: 2px;
box-shadow: 0 0 8px rgba(101, 217, 238, 0.6);
}
.dot.gold {
border-color: var(--gold);
box-shadow: 0 0 8px rgba(255, 209, 102, 0.65);
}
.dot.green {
border-color: var(--mint);
background: var(--mint);
box-shadow: 0 0 8px rgba(115, 242, 209, 0.8);
}
.status-card {
position: fixed;
z-index: 6;
top: 22px;
right: 22px;
width: min(330px, calc(100vw - 44px));
display: flex;
gap: 11px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(7, 35, 40, 0.86);
box-shadow: 0 14px 42px rgba(0, 28, 33, 0.2);
backdrop-filter: blur(14px);
}
.status-lamp {
flex: 0 0 auto;
width: 10px;
height: 10px;
margin-top: 4px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 12px rgba(255, 209, 102, 0.8);
}
.status-card[data-state="good"] .status-lamp {
background: var(--mint);
box-shadow: 0 0 12px rgba(115, 242, 209, 0.9);
}
.status-card[data-state="bad"] .status-lamp {
background: var(--danger);
box-shadow: 0 0 12px rgba(255, 127, 115, 0.8);
}
.status-card strong {
display: block;
font-size: 12px;
}
.status-card p {
margin: 3px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.toolbar {
position: fixed;
z-index: 8;
left: 50%;
bottom: 19px;
display: flex;
gap: 8px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(7, 35, 40, 0.9);
box-shadow: 0 18px 50px rgba(0, 28, 33, 0.3);
backdrop-filter: blur(16px);
transform: translateX(-50%);
}
.tool {
min-width: 92px;
padding: 10px 13px;
border: 1px solid transparent;
border-radius: 11px;
background: rgba(255, 255, 255, 0.065);
color: #eafff7;
font-size: 11px;
font-weight: 850;
cursor: pointer;
transition: 150ms ease;
}
.tool:hover {
border-color: rgba(115, 242, 209, 0.38);
background: rgba(115, 242, 209, 0.1);
transform: translateY(-1px);
}
.tool.primary {
background: var(--mint);
color: #073238;
}
.tool:disabled {
opacity: 0.38;
cursor: not-allowed;
transform: none;
}
.tool kbd {
margin-left: 5px;
opacity: 0.55;
font-family: inherit;
font-size: 9px;
}
.hint {
position: fixed;
z-index: 4;
right: 24px;
bottom: 23px;
color: rgba(239, 255, 249, 0.8);
font-size: 10px;
font-weight: 700;
text-align: right;
text-shadow: 0 2px 8px rgba(0, 40, 45, 0.5);
pointer-events: none;
}
.world-label {
position: fixed;
z-index: 3;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 8px;
background: rgba(7, 35, 40, 0.72);
color: white;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
pointer-events: none;
transform: translate(-50%, -50%);
box-shadow: 0 5px 18px rgba(0, 30, 35, 0.2);
}
#startLabel { border-color: rgba(115, 242, 209, 0.65); }
#goalLabel { border-color: rgba(255, 209, 102, 0.7); }
.toast {
position: fixed;
z-index: 12;
left: 50%;
top: 92px;
max-width: min(430px, calc(100vw - 30px));
padding: 10px 14px;
border: 1px solid rgba(255, 209, 102, 0.45);
border-radius: 10px;
background: rgba(7, 35, 40, 0.92);
color: var(--cream);
font-size: 11px;
font-weight: 750;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -8px);
transition: 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.win-panel {
position: fixed;
z-index: 10;
right: 22px;
bottom: 92px;
width: min(310px, calc(100vw - 44px));
padding: 19px;
border: 1px solid rgba(115, 242, 209, 0.5);
border-radius: 18px;
background: rgba(7, 35, 40, 0.94);
box-shadow: 0 22px 70px rgba(0, 28, 33, 0.36);
backdrop-filter: blur(18px);
opacity: 0;
pointer-events: none;
transform: translateY(12px) scale(0.97);
transition: 220ms ease;
}
.win-panel.show {
opacity: 1;
pointer-events: auto;
transform: translateY(0) scale(1);
}
.win-cube {
width: 34px;
height: 34px;
margin-bottom: 12px;
border: 2px solid var(--mint);
background: rgba(115, 242, 209, 0.17);
box-shadow: 8px -8px 0 -5px var(--gold), 0 0 25px rgba(115, 242, 209, 0.25);
transform: rotate(45deg);
}
.win-panel h2 {
margin: 0;
font-size: 22px;
letter-spacing: -0.04em;
}
.win-panel p {
margin: 7px 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.win-panel button {
width: 100%;
padding: 10px;
border: 0;
border-radius: 10px;
background: var(--mint);
color: #073238;
font-size: 11px;
font-weight: 900;
cursor: pointer;
}
@media (max-width: 820px) {
.brand { top: 15px; left: 15px; }
.subtitle { display: none; }
.panel {
top: auto;
bottom: 84px;
left: 12px;
width: calc(100vw - 24px);
max-height: 35vh;
padding: 13px;
}
.status-card { top: 78px; right: 12px; }
.hint { display: none; }
.toolbar { bottom: 12px; width: calc(100vw - 24px); justify-content: center; }
.tool { min-width: 0; flex: 1; padding: 10px 7px; }
.tool kbd { display: none; }
.win-panel { right: 12px; bottom: 78px; }
.world-label { display: none; }
}
@media (max-height: 700px) and (min-width: 821px) {
.panel { top: 95px; max-height: calc(100vh - 165px); }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="brand">
<div class="eyebrow">Blockcurrent trial 01</div>
<h1>The Crooked Ford</h1>
<div class="subtitle">A limited-module river crossing</div>
</header>
<aside class="panel">
<section class="section">
<div class="section-title">
<span>Objective</span>
<span id="progress">0 / 9</span>
</div>
<p class="objective">
Join the <strong>west dock</strong> to the <strong>east lantern</strong>
with one continuous supported bridge.
</p>
</section>
<section class="section">
<div class="section-title"><span>Bridge inventory</span></div>
<div class="module-list">
<button class="module selected" data-type="straight" type="button">
<span class="key">1</span>
<span class="module-icon"></span>
<strong>Straight beam <span class="count" id="straightCount">5</span></strong>
<small>Two opposite connections</small>
</button>
<button class="module curve" data-type="curve" type="button">
<span class="key">2</span>
<span class="module-icon"></span>
<strong>Curve <span class="count" id="curveCount">4</span></strong>
<small>Two perpendicular connections</small>
</button>
</div>
<div class="orientation">
<div>
<span>Facing</span>
<strong id="orientationText">West ↔ East</strong>
</div>
<button class="mini-button" id="rotateButton" type="button">ROTATE R</button>
</div>
</section>
<section class="section">
<div class="section-title"><span>Game support rules</span></div>
<ol class="rules">
<li><strong>Use the lit sockets only.</strong> Modules cannot be placed on open water.</li>
<li><strong>Every module needs support:</strong> at least one open edge must meet the dock or another module.</li>
<li><strong>Edges must match.</strong> A beam connects only to an open edge facing it.</li>
<li><strong>Inventory is finite:</strong> 5 straight beams and 4 curves.</li>
<li><strong>Win condition:</strong> one uninterrupted chain must connect both shores.</li>
</ol>
</section>
<section class="section">
<div class="section-title"><span>Socket legend</span></div>
<div class="legend">
<span><i class="dot"></i> Open socket</span>
<span><i class="dot gold"></i> Compatible</span>
<span><i class="dot green"></i> Active route</span>
</div>
</section>
</aside>
<div class="status-card" id="statusCard" data-state="normal">
<i class="status-lamp"></i>
<div>
<strong id="statusTitle">Build the crossing</strong>
<p id="statusText">Place a supported beam beside the west dock.</p>
</div>
</div>
<div class="toolbar">
<button class="tool" id="undoButton" type="button" disabled>Undo <kbd>Z</kbd></button>
<button class="tool" id="resetButton" type="button">Reset</button>
<button class="tool primary" id="checkButton" type="button">Check route</button>
</div>
<div class="hint">Drag to orbit · wheel to zoom · click a socket to place</div>
<div class="world-label" id="startLabel">WEST DOCK</div>
<div class="world-label" id="goalLabel">EAST LANTERN</div>
<div class="toast" id="toast"></div>
<div class="win-panel" id="winPanel">
<div class="win-cube"></div>
<h2>Shore secured.</h2>
<p>
Your bridge forms one continuous supported path from the western dock
to the eastern lantern.
</p>
<button id="inspectButton" type="button">Inspect the bridge</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = id => document.getElementById(id);
const sceneHost = $("scene");
// ------------------------------------------------------------
// Renderer, scene, lights, and camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9bd3d7);
scene.fog = new THREE.Fog(0x9bd3d7, 22, 43);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.45, 0.4);
let cameraTheta = 0.72;
let cameraPhi = 0.88;
let cameraRadius = 18.5;
function updateCamera() {
const horizontal = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xe9ffff, 0x6b4936, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0c5, 3.1);
sun.position.set(-8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -17;
sun.shadow.camera.right = 17;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x75dfff, 1.15);
rim.position.set(9, 7, -12);
scene.add(rim);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function standardMaterial(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
...extra
});
}
function addBox(parent, sx, sy, sz, x, y, z, material, cast = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let randomSeed = 14837;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function createInstances(blocks, material) {
const mesh = new THREE.InstancedMesh(unitBox, material, blocks.length);
const dummy = new THREE.Object3D();
blocks.forEach((block, index) => {
dummy.position.set(block.x, block.y, block.z);
dummy.scale.set(block.sx, block.sy, block.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (block.color !== undefined) {
mesh.setColorAt(index, new THREE.Color(block.color));
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Voxel river and banks
// ------------------------------------------------------------
const waterBlocks = [];
for (let x = -4; x <= 4; x++) {
for (let z = -6; z <= 6; z++) {
const shade = random();
waterBlocks.push({
x,
y: -0.18,
z,
sx: 0.965,
sy: 0.25,
sz: 0.965,
color: new THREE.Color().setHSL(
0.515 + shade * 0.025,
0.58,
0.40 + shade * 0.07
)
});
}
}
createInstances(
waterBlocks,
standardMaterial(0xffffff, {
roughness: 0.26,
metalness: 0.08
})
);
const riverBed = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
standardMaterial(0x245f6b, { roughness: 1 })
);
riverBed.rotation.x = -Math.PI / 2;
riverBed.position.y = -0.35;
riverBed.receiveShadow = true;
scene.add(riverBed);
const dirtBlocks = [];
const grassBlocks = [];
for (const side of [-1, 1]) {
for (let x = 5; x <= 10; x++) {
for (let z = -6; z <= 6; z++) {
const worldX = side * x;
const variation = random();
dirtBlocks.push({
x: worldX,
y: -0.48,
z,
sx: 0.985,
sy: 0.94,
sz: 0.985,
color: new THREE.Color().setHSL(
0.065,
0.42,
0.25 + variation * 0.055
)
});
dirtBlocks.push({
x: worldX,
y: 0.42,
z,
sx: 0.985,
sy: 0.84,
sz: 0.985,
color: new THREE.Color().setHSL(
0.075,
0.44,
0.34 + variation * 0.07
)
});
grassBlocks.push({
x: worldX,
y: 0.91,
z,
sx: 0.99,
sy: 0.14,
sz: 0.99,
color: new THREE.Color().setHSL(
0.285 + variation * 0.035,
0.38,
0.41 + variation * 0.09
)
});
}
}
}
createInstances(dirtBlocks, standardMaterial(0xffffff));
createInstances(grassBlocks, standardMaterial(0xffffff));
const rockMaterial = standardMaterial(0x71827e);
const trunkMaterial = standardMaterial(0x70462e);
const leafMaterials = [
standardMaterial(0x3c9563),
standardMaterial(0x4aa86e),
standardMaterial(0x2f7f5b)
];
function voxelTree(x, z, scale = 1, leafIndex = 0) {
const group = new THREE.Group();
group.position.set(x, 0.98, z);
group.scale.setScalar(scale);
addBox(group, 0.34, 1.35, 0.34, 0, 0.68, 0, trunkMaterial);
addBox(group, 1.15, 0.85, 1.15, 0, 1.58, 0, leafMaterials[leafIndex]);
addBox(group, 0.82, 0.72, 0.82, 0.05, 2.15, -0.03, leafMaterials[(leafIndex + 1) % 3]);
addBox(group, 0.48, 0.46, 0.48, -0.03, 2.68, 0.01, leafMaterials[leafIndex]);
scene.add(group);
}
voxelTree(-8.8, -3.9, 1.05, 0);
voxelTree(-7.2, 4.2, 0.88, 1);
voxelTree(-9.5, 2.1, 0.72, 2);
voxelTree(7.2, -4.0, 0.92, 1);
voxelTree(8.8, 3.7, 1.08, 0);
voxelTree(6.5, 4.6, 0.72, 2);
for (const side of [-1, 1]) {
for (let i = 0; i < 10; i++) {
const x = side * (4.72 + random() * 0.18);
const z = -6 + random() * 12;
if (Math.abs(z - 1) < 0.55) continue;
addBox(
scene,
0.26 + random() * 0.22,
0.25 + random() * 0.3,
0.28 + random() * 0.22,
x,
0.08,
z,
rockMaterial
);
}
}
const foamMaterial = standardMaterial(0xd9ffff, {
emissive: 0x8ddde5,
emissiveIntensity: 0.14,
roughness: 0.45
});
const foam = [];
for (let i = 0; i < 17; i++) {
const mesh = addBox(
scene,
0.13 + random() * 0.24,
0.045,
0.055,
-3.8 + random() * 7.6,
-0.015,
-6 + random() * 12,
foamMaterial,
false
);
foam.push({
mesh,
speed: 0.45 + random() * 0.65,
offset: random() * 12
});
}
// ------------------------------------------------------------
// Dock anchors and route board
// ------------------------------------------------------------
const dockWood = standardMaterial(0x9a5e38);
const dockDark = standardMaterial(0x5a3928);
const startBeaconMaterial = standardMaterial(0x5ff5d2, {
emissive: 0x28d9b5,
emissiveIntensity: 1.15
});
const goalBeaconMaterial = standardMaterial(0xffd166, {
emissive: 0xe99e26,
emissiveIntensity: 1.15
});
function createDock(x, beaconMaterial) {
const group = new THREE.Group();
group.position.set(x, 0, 1);
addBox(group, 0.94, 0.18, 0.92, 0, 1.12, 0, dockWood);
addBox(group, 0.94, 0.15, 0.08, 0, 1.25, -0.32, dockDark);
addBox(group, 0.94, 0.15, 0.08, 0, 1.25, 0.32, dockDark);
for (const px of [-0.34, 0.34]) {
for (const pz of [-0.34, 0.34]) {
addBox(group, 0.12, 1.35, 0.12, px, 0.43, pz, dockDark);
}
}
addBox(group, 0.24, 0.3, 0.24, 0, 1.51, 0.37, beaconMaterial);
scene.add(group);
}
createDock(-4, startBeaconMaterial);
createDock(4, goalBeaconMaterial);
addBox(scene, 0.85, 0.16, 0.64, -4.58, 1.12, 1, dockWood);
addBox(scene, 0.85, 0.16, 0.64, 4.58, 1.12, 1, dockWood);
const DIRS = {
E: { x: 1, z: 0, label: "East" },
W: { x: -1, z: 0, label: "West" },
N: { x: 0, z: 1, label: "North" },
S: { x: 0, z: -1, label: "South" }
};
const OPPOSITE = { E: "W", W: "E", N: "S", S: "N" };
const anchors = new Map([
["-4,1", { key: "-4,1", dir: "E", type: "start" }],
["4,1", { key: "4,1", dir: "W", type: "goal" }]
]);
const boardKeys = [];
for (let x = -3; x <= 3; x++) {
for (let z = 0; z <= 2; z++) {
boardKeys.push(`${x},${z}`);
}
}
const socketObjects = new Map();
const socketHits = [];
const socketGeometry = new THREE.BoxGeometry(0.78, 0.045, 0.78);
const socketEdgeGeometry = new THREE.EdgesGeometry(socketGeometry);
for (const key of boardKeys) {
const [x, z] = key.split(",").map(Number);
const material = new THREE.MeshBasicMaterial({
color: 0x65d9ee,
transparent: true,
opacity: 0.09,
depthWrite: false
});
const hit = new THREE.Mesh(socketGeometry, material);
hit.position.set(x, 0.065, z);
hit.userData.socketKey = key;
scene.add(hit);
socketHits.push(hit);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x65d9ee,
transparent: true,
opacity: 0.62
});
const edge = new THREE.LineSegments(socketEdgeGeometry, edgeMaterial);
edge.position.copy(hit.position);
edge.position.y += 0.015;
scene.add(edge);
socketObjects.set(key, { hit, edge, material, edgeMaterial });
}
// ------------------------------------------------------------
// Game state and logical bridge rules
// ------------------------------------------------------------
const placements = new Map();
const history = [];
const inventory = { straight: 5, curve: 4 };
let selectedType = "straight";
let orientation = 0;
let hoveredKey = null;
let currentPath = null;
let wasWon = false;
const moduleLayer = new THREE.Group();
const routeLayer = new THREE.Group();
const confettiLayer = new THREE.Group();
scene.add(moduleLayer, routeLayer, confettiLayer);
function moduleDirections(type, rot = 0) {
if (type === "straight") {
return rot % 2 === 0 ? ["W", "E"] : ["N", "S"];
}
return [
["W", "S"],
["N", "W"],
["E", "N"],
["S", "E"]
][((rot % 4) + 4) % 4];
}
function directionLabel(type, rot = 0) {
if (type === "straight") {
return rot % 2 === 0 ? "West ↔ East" : "North ↔ South";
}
const labels = {
W: "West",
E: "East",
N: "North",
S: "South"
};
const [a, b] = moduleDirections("curve", rot);
return `${labels[a]} + ${labels[b]}`;
}
function neighborKey(key, dir) {
const [x, z] = key.split(",").map(Number);
return `${x + DIRS[dir].x},${z + DIRS[dir].z}`;
}
function hasSupport(candidate) {
for (const dir of moduleDirections(candidate.type, candidate.orientation)) {
const adjacentKey = neighborKey(candidate.key, dir);
const anchor = anchors.get(adjacentKey);
if (anchor && anchor.dir === OPPOSITE[dir]) {
return true;
}
const neighbor = placements.get(adjacentKey);
if (
neighbor &&
moduleDirections(neighbor.type, neighbor.orientation).includes(OPPOSITE[dir])
) {
return true;
}
}
return false;
}
function buildGraph() {
const adjacency = new Map();
function addEdge(a, b) {
if (!adjacency.has(a)) adjacency.set(a, []);
if (!adjacency.has(b)) adjacency.set(b, []);
adjacency.get(a).push(b);
adjacency.get(b).push(a);
}
for (const [key, piece] of placements) {
for (const dir of moduleDirections(piece.type, piece.orientation)) {
const adjacentKey = neighborKey(key, dir);
const anchor = anchors.get(adjacentKey);
const neighbor = placements.get(adjacentKey);
if (anchor && anchor.dir === OPPOSITE[dir]) {
addEdge(key, adjacentKey);
} else if (
neighbor &&
moduleDirections(neighbor.type, neighbor.orientation).includes(OPPOSITE[dir])
) {
addEdge(key, adjacentKey);
}
}
}
for (const anchor of anchors.values()) {
const adjacentKey = neighborKey(anchor.key, anchor.dir);
const neighbor = placements.get(adjacentKey);
if (
neighbor &&
moduleDirections(neighbor.type, neighbor.orientation).includes(
OPPOSITE[anchor.dir]
)
) {
addEdge(anchor.key, adjacentKey);
}
}
return adjacency;
}
function findShorePath() {
const adjacency = buildGraph();
const start = "-4,1";
const goal = "4,1";
const queue = [start];
const previous = new Map([[start, null]]);
while (queue.length) {
const current = queue.shift();
if (current === goal) {
const path = [];
let cursor = current;
while (cursor !== null) {
path.push(cursor);
cursor = previous.get(cursor);
}
return path.reverse();
}
for (const next of adjacency.get(current) || []) {
if (!previous.has(next)) {
previous.set(next, current);
queue.push(next);
}
}
}
return null;
}
function snapshot() {
return [...placements.entries()].map(([key, piece]) => ({
key,
...piece
}));
}
function restoreSnapshot(snap) {
for (const child of [...moduleLayer.children]) {
moduleLayer.remove(child);
}
placements.clear();
for (const piece of snap) {
placements.set(piece.key, {
type: piece.type,
orientation: piece.orientation
});
const visual = createModule(piece.type, piece.orientation, false);
const [x, z] = piece.key.split(",").map(Number);
visual.position.set(x, 1.12, z);
moduleLayer.add(visual);
}
rebuildGhost();
updateUI();
evaluateRoute(false);
}
// ------------------------------------------------------------
// Bridge module visuals
// ------------------------------------------------------------
function createModule(type, rot, ghost = false) {
const group = new THREE.Group();
const wood = ghost
? new THREE.MeshBasicMaterial({
color: 0xd38a4d,
transparent: true,
opacity: 0.43,
depthWrite: false
})
: standardMaterial(type === "curve" ? 0xc67842 : 0xb66f3e);
const dark = ghost
? new THREE.MeshBasicMaterial({
color: 0x5a3928,
transparent: true,
opacity: 0.35,
depthWrite: false
})
: standardMaterial(0x65402b);
const accent = ghost
? new THREE.MeshBasicMaterial({
color: type === "curve" ? 0x73f2d1 : 0xffd166,
transparent: true,
opacity: 0.72,
depthWrite: false
})
: standardMaterial(type === "curve" ? 0x67e8ce : 0xffc857, {
emissive: type === "curve" ? 0x1a8e7c : 0xa76d11,
emissiveIntensity: 0.72,
roughness: 0.55
});
if (type === "straight") {
for (let i = 0; i < 5; i++) {
addBox(
group,
0.155,
0.18,
0.72,
(i - 2) * 0.17,
0,
0,
wood
);
}
addBox(group, 0.88, 0.12, 0.075, 0, 0.23, -0.41, dark);
addBox(group, 0.88, 0.12, 0.075, 0, 0.23, 0.41, dark);
for (const x of [-0.37, 0.37]) {
addBox(group, 0.11, 0.34, 0.11, x, 0.12, -0.39, dark);
addBox(group, 0.11, 0.34, 0.11, x, 0.12, 0.39, dark);
}
for (const x of [-0.30, -0.15, 0, 0.15, 0.30]) {
addBox(group, 0.10, 0.065, 0.18, x, 0.12, 0, accent);
}
} else {
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
addBox(
group,
0.225,
0.18,
0.225,
x * 0.25,
0,
z * 0.25,
wood
);
}
}
const route = [
[-0.35, 0],
[-0.18, 0],
[0, 0],
[0, -0.18],
[0, -0.35]
];
for (const [x, z] of route) {
addBox(group, 0.145, 0.07, 0.145, x, 0.13, z, accent);
}
addBox(group, 0.1, 0.31, 0.1, -0.36, 0.14, -0.36, dark);
addBox(group, 0.1, 0.31, 0.1, 0.36, 0.14, -0.36, dark);
}
group.rotation.y = rot * Math.PI / 2;
return group;
}
let ghost = null;
function rebuildGhost() {
if (ghost) moduleLayer.remove(ghost);
ghost = createModule(selectedType, orientation, true);
ghost.visible = false;
moduleLayer.add(ghost);
}
function updateGhost() {
if (!ghost) return;
if (!hoveredKey || placements.has(hoveredKey)) {
ghost.visible = false;
return;
}
const candidate = {
key: hoveredKey,
type: selectedType,
orientation
};
const valid =
inventory[selectedType] > 0 && hasSupport(candidate);
const [x, z] = hoveredKey.split(",").map(Number);
ghost.visible = true;
ghost.position.set(x, 1.12, z);
ghost.traverse(object => {
if (object.isMesh) {
object.material.color.set(valid ? 0x9dffb0 : 0xff7f73);
}
});
}
function updateSocketVisuals() {
const routeKeys = new Set(
currentPath ? currentPath.filter(key => placements.has(key)) : []
);
for (const [key, visual] of socketObjects) {
const occupied = placements.has(key);
const compatible =
!occupied &&
inventory[selectedType] > 0 &&
hasSupport({
key,
type: selectedType,
orientation
});
const hovered = key === hoveredKey;
const inRoute = routeKeys.has(key);
let color = 0x65d9ee;
let opacity = compatible ? 0.18 : 0.085;
let edgeOpacity = compatible ? 0.95 : 0.55;
if (occupied) {
color = 0xffd166;
opacity = 0.035;
edgeOpacity = 0.25;
}
if (inRoute) {
color = 0x73f2d1;
opacity = 0.14;
edgeOpacity = 1;
}
if (hovered) {
color = 0xffffff;
opacity = 0.28;
edgeOpacity = 1;
}
visual.material.color.setHex(color);
visual.material.opacity = opacity;
visual.edgeMaterial.color.setHex(color);
visual.edgeMaterial.opacity = edgeOpacity;
}
}
// ------------------------------------------------------------
// Route markers, feedback, and victory
// ------------------------------------------------------------
const markerGeometry = new THREE.BoxGeometry(0.17, 0.17, 0.17);
const markerMaterial = standardMaterial(0x9dffe0, {
emissive: 0x38e6bc,
emissiveIntensity: 1.8,
roughness: 0.35
});
function clearRouteMarkers() {
while (routeLayer.children.length) {
routeLayer.remove(routeLayer.children[0]);
}
}
function showRouteMarkers(path) {
clearRouteMarkers();
for (const key of path) {
if (!placements.has(key)) continue;
const [x, z] = key.split(",").map(Number);
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
marker.position.set(x, 1.47, z);
marker.castShadow = true;
routeLayer.add(marker);
}
}
function setStatus(title, text, state = "normal") {
$("statusTitle").textContent = title;
$("statusText").textContent = text;
$("statusCard").dataset.state = state;
}
let toastTimer = null;
function toast(message) {
$("toast").textContent = message;
$("toast").classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => $("toast").classList.remove("show"), 2400);
}
const confetti = [];
function spawnConfetti() {
const colors = [0x73f2d1, 0xffd166, 0x65d9ee, 0xfff3d2];
for (let i = 0; i < 34; i++) {
const material = standardMaterial(colors[i % colors.length], {
emissive: colors[i % colors.length],
emissiveIntensity: 0.25
});
const mesh = new THREE.Mesh(unitBox, material);
const size = 0.08 + random() * 0.11;
mesh.scale.setScalar(size);
mesh.position.set(4 + (random() - 0.5) * 1.2, 2.2 + random(), 1 + (random() - 0.5) * 1.2);
mesh.castShadow = true;
confettiLayer.add(mesh);
confetti.push({
mesh,
velocity: new THREE.Vector3(
(random() - 0.5) * 3.1,
2.4 + random() * 3.2,
(random() - 0.5) * 3.1
),
spin: new THREE.Vector3(random() * 4, random() * 4, random() * 4)
});
}
}
function evaluateRoute(showFeedback = true) {
currentPath = findShorePath();
updateSocketVisuals();
if (currentPath) {
showRouteMarkers(currentPath);
setStatus(
"Shore-to-shore route confirmed",
"The bridge is continuous, supported, and reaches the east lantern.",
"good"
);
if (!wasWon) {
wasWon = true;
$("winPanel").classList.add("show");
spawnConfetti();
}
if (showFeedback) toast("Route confirmed — the crossing is complete.");
} else {
clearRouteMarkers();
const placed = placements.size;
const remaining = inventory.straight + inventory.curve;
if (remaining === 0) {
setStatus(
"All modules used",
"The bridge does not reach the east dock. Undo a turn and try another route.",
"bad"
);
} else {
setStatus(
`${placed} of 9 modules placed`,
"Extend the supported chain toward the east lantern. Matching edges glow gold.",
"normal"
);
}
}
}
// ------------------------------------------------------------
// Placement and UI
// ------------------------------------------------------------
function placeModule(key) {
if (!key || placements.has(key)) {
if (key && placements.has(key)) {
toast("That socket is occupied. Use Undo to lift the last module.");
}
return;
}
if (inventory[selectedType] <= 0) {
toast(`No ${selectedType} modules remain.`);
return;
}
const candidate = {
key,
type: selectedType,
orientation
};
if (!hasSupport(candidate)) {
toast("Support rule: this module must connect to the dock or an existing module.");
return;
}
history.push(snapshot());
placements.set(key, {
type: selectedType,
orientation
});
const visual = createModule(selectedType, orientation, false);
const [x, z] = key.split(",").map(Number);
visual.position.set(x, 1.12, z);
moduleLayer.add(visual);
inventory[selectedType]--;
hoveredKey = null;
rebuildGhost();
updateUI();
evaluateRoute(false);
}
function undo() {
if (!history.length) return;
const previous = history.pop();
restoreSnapshot(previous);
$("winPanel").classList.remove("show");
wasWon = false;
toast("Last placement removed.");
}
function reset() {
history.length = 0;
placements.clear();
inventory.straight = 5;
inventory.curve = 4;
currentPath = null;
wasWon = false;
hoveredKey = null;
for (const child of [...moduleLayer.children]) {
moduleLayer.remove(child);
}
clearRouteMarkers();
for (const child of [...confettiLayer.children]) {
confettiLayer.remove(child);
}
confetti.length = 0;
$("winPanel").classList.remove("show");
rebuildGhost();
updateUI();
setStatus(
"Build the crossing",
"Place a supported beam beside the west dock.",
"normal"
);
}
function selectType(type) {
if (inventory[type] <= 0) return;
selectedType = type;
orientation = 0;
rebuildGhost();
updateUI();
}
function rotateSelected() {
orientation = (orientation + (selectedType === "straight" ? 1 : 1)) %
(selectedType === "straight" ? 2 : 4);
rebuildGhost();
updateUI();
updateGhost();
}
function updateUI() {
$("straightCount").textContent = inventory.straight;
$("curveCount").textContent = inventory.curve;
$("progress").textContent = `${placements.size} / 9`;
$("orientationText").textContent = directionLabel(selectedType, orientation);
$("undoButton").disabled = history.length === 0;
document.querySelectorAll(".module").forEach(button => {
const type = button.dataset.type;
button.classList.toggle("selected", type === selectedType);
button.disabled = inventory[type] <= 0;
});
updateSocketVisuals();
updateGhost();
}
document.querySelectorAll(".module").forEach(button => {
button.addEventListener("click", () => selectType(button.dataset.type));
});
$("rotateButton").addEventListener("click", rotateSelected);
$("undoButton").addEventListener("click", undo);
$("resetButton").addEventListener("click", reset);
$("checkButton").addEventListener("click", () => evaluateRoute(true));
$("inspectButton").addEventListener("click", () => $("winPanel").classList.remove("show"));
window.addEventListener("keydown", event => {
if (event.key === "1") selectType("straight");
if (event.key === "2") selectType("curve");
if (event.key.toLowerCase() === "r") rotateSelected();
if (event.key.toLowerCase() === "z") undo();
});
// ------------------------------------------------------------
// Direct camera controls and socket picking
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let dragMoved = 0;
let previousPointerX = 0;
let previousPointerY = 0;
function raycastSocket(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);
const hits = raycaster.intersectObjects(socketHits, false);
return hits.length ? hits[0].object.userData.socketKey : null;
}
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
dragMoved = 0;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging) {
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
dragMoved += Math.abs(dx) + Math.abs(dy);
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.46, 1.25);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
updateCamera();
} else {
const nextKey = raycastSocket(event);
if (nextKey !== hoveredKey) {
hoveredKey = nextKey;
updateSocketVisuals();
updateGhost();
}
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (dragging && dragMoved < 7) {
const key = raycastSocket(event);
placeModule(key);
}
dragging = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) {
hoveredKey = null;
updateSocketVisuals();
updateGhost();
}
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
12,
27
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Labels, resizing, and animation
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionWorldLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible = projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.05);
for (let i = 0; i < foam.length; i++) {
const item = foam[i];
let z = item.mesh.position.z + item.speed * delta;
if (z > 6.3) z = -6.3;
item.mesh.position.z = z;
item.mesh.position.x += Math.sin(elapsed * 0.65 + item.offset) * 0.002;
}
for (const visual of socketObjects.values()) {
const pulse = 1 + Math.sin(elapsed * 2.1 + visual.hit.position.x) * 0.035;
visual.edge.scale.set(pulse, 1, pulse);
}
routeLayer.children.forEach((marker, index) => {
marker.rotation.y = elapsed * 1.4 + index;
marker.position.y = 1.47 + Math.sin(elapsed * 3 + index * 0.6) * 0.045;
});
for (let i = confetti.length - 1; i >= 0; i--) {
const item = confetti[i];
item.velocity.y -= 5.8 * delta;
item.mesh.position.addScaledVector(item.velocity, delta);
item.mesh.rotation.x += item.spin.x * delta;
item.mesh.rotation.y += item.spin.y * delta;
item.mesh.rotation.z += item.spin.z * delta;
if (item.mesh.position.y < 0.1) {
confettiLayer.remove(item.mesh);
confetti.splice(i, 1);
}
}
positionWorldLabel($("startLabel"), -4, 2.05, 1);
positionWorldLabel($("goalLabel"), 4, 2.05, 1);
renderer.render(scene, camera);
}
rebuildGhost();
updateUI();
animate();
</script>
</body>
</html>
```