Prompt specification
Build or customize the Three.js 3D scene "Three Road Tiles With Cost Markers" 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: Three Road Tiles With Cost Markers
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/three-road-tiles-with-cost-markers-x3593
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts weighted routes and travel costs. Display road, mud, and bridge tiles with explicit traversal costs beside one courier token. Separate cost from visual distance and show shared tile dimensions. Voxel style.
Working Three.js Starter (three-road-tiles-with-cost-markers-x3593):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Weighted Routes — Voxel Terrain Planner</title>
<style>
:root {
--bg: #09121e;
--panel: rgba(12, 24, 39, 0.84);
--panel-strong: rgba(10, 21, 35, 0.94);
--line: rgba(170, 204, 229, 0.16);
--text: #edf6fb;
--muted: #91a7b8;
--road: #65e6b2;
--mud: #ff7b72;
--bridge: #ffc857;
--orange: #ff9d45;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--text);
background:
radial-gradient(circle at 48% 40%, #1b3249 0%, #0d1c2c 42%, #07101b 100%);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app,
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: absolute;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--line);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.24),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
header {
position: absolute;
top: 26px;
left: 28px;
max-width: 460px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: #9fc2d9;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: var(--orange);
box-shadow: 0 0 12px rgba(255, 157, 69, 0.5);
}
h1 {
margin: 8px 0 5px;
font-size: clamp(28px, 3.2vw, 46px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.header-meta {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 15px;
}
.meta-chip {
padding: 6px 9px;
border: 1px solid rgba(151, 190, 218, 0.16);
border-radius: 7px;
color: #aec4d3;
background: rgba(13, 28, 43, 0.56);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.route-panel {
position: absolute;
top: 24px;
right: 24px;
width: min(340px, calc(100vw - 48px));
padding: 17px;
border-radius: 15px;
}
.panel-heading {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.panel-heading h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.panel-heading span {
color: var(--muted);
font-size: 10px;
}
.route-list {
display: grid;
gap: 8px;
}
.route-card {
position: relative;
display: grid;
grid-template-columns: 1fr auto;
gap: 7px 12px;
width: 100%;
padding: 12px 13px 11px;
overflow: hidden;
color: var(--text);
text-align: left;
background: rgba(255, 255, 255, 0.025);
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 10px;
cursor: pointer;
transition:
transform 160ms ease,
border-color 160ms ease,
background 160ms ease;
}
.route-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 3px;
background: var(--route-color);
opacity: 0.72;
}
.route-card:hover {
transform: translateX(-2px);
border-color: rgba(255, 255, 255, 0.18);
background: rgba(255, 255, 255, 0.05);
}
.route-card.active {
border-color: color-mix(in srgb, var(--route-color) 65%, transparent);
background: color-mix(in srgb, var(--route-color) 10%, transparent);
}
.route-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 800;
}
.route-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--route-color);
box-shadow: 0 0 12px color-mix(in srgb, var(--route-color) 55%, transparent);
}
.route-cost {
grid-row: 1 / 3;
grid-column: 2;
display: flex;
align-items: baseline;
gap: 4px;
padding-left: 6px;
}
.route-cost strong {
color: var(--route-color);
font-size: 25px;
line-height: 1;
letter-spacing: -0.05em;
}
.route-cost small {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.route-distance {
grid-column: 1;
color: #a9bdca;
font-size: 11px;
line-height: 1.45;
}
.route-distance b {
color: #e6f1f7;
font-weight: 750;
}
.route-note {
grid-column: 1 / -1;
margin-top: 2px;
color: #708b9e;
font-size: 10px;
line-height: 1.35;
}
.run-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 12px;
}
.run-button {
flex: 0 0 auto;
padding: 10px 14px;
color: #07131f;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.09em;
text-transform: uppercase;
background: var(--orange);
border: 0;
border-radius: 8px;
box-shadow: 0 8px 22px rgba(255, 157, 69, 0.18);
cursor: pointer;
transition:
transform 150ms ease,
filter 150ms ease;
}
.run-button:hover {
transform: translateY(-1px);
filter: brightness(1.08);
}
.run-status {
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.cost-rule {
margin-top: 12px;
padding-top: 11px;
color: #7892a4;
font-size: 10px;
line-height: 1.5;
border-top: 1px solid var(--line);
}
.cost-rule b {
color: #b8ccd9;
}
.legend {
position: absolute;
left: 28px;
bottom: 26px;
width: 226px;
padding: 15px;
border-radius: 13px;
}
.legend h2 {
margin: 0 0 11px;
color: #a8c0d1;
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 14px 1fr auto;
align-items: center;
gap: 9px;
min-height: 27px;
color: #d9e7ef;
font-size: 11px;
}
.terrain-swatch {
width: 11px;
height: 11px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}
.legend-row strong {
color: var(--text);
font-size: 11px;
}
.legend-row strong small {
color: var(--muted);
font-size: 9px;
font-weight: 650;
}
.dimension-rule {
margin-top: 10px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.dimension-rule strong {
display: block;
color: #e9f5fa;
font-size: 13px;
letter-spacing: -0.01em;
}
.dimension-rule span {
display: block;
margin-top: 3px;
color: #7892a4;
font-size: 10px;
line-height: 1.4;
}
.controls {
position: absolute;
right: 26px;
bottom: 26px;
display: flex;
align-items: center;
gap: 5px;
padding: 6px;
border-radius: 10px;
}
.control-button {
display: grid;
place-items: center;
width: 31px;
height: 31px;
padding: 0;
color: #d8e7ef;
font-size: 17px;
font-weight: 650;
background: rgba(255, 255, 255, 0.045);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 7px;
cursor: pointer;
}
.control-button:hover {
color: white;
background: rgba(255, 255, 255, 0.1);
}
.control-button.text {
width: auto;
padding: 0 9px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
}
.control-hint {
margin: 0 6px 0 4px;
color: #7892a4;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#dimension-tag {
position: absolute;
z-index: 3;
padding: 7px 9px;
color: #dcebf3;
background: rgba(8, 19, 31, 0.9);
border: 1px solid rgba(155, 196, 221, 0.25);
border-radius: 7px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
transform: translate(-50%, -50%);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.25);
}
#dimension-tag small {
display: block;
margin-top: 2px;
color: #7892a4;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#tooltip {
position: absolute;
z-index: 8;
display: none;
min-width: 160px;
padding: 9px 11px;
color: #edf6fb;
background: rgba(7, 16, 27, 0.95);
border: 1px solid rgba(157, 198, 221, 0.22);
border-radius: 8px;
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
pointer-events: none;
transform: translate(13px, 13px);
}
#tooltip strong {
display: block;
font-size: 11px;
letter-spacing: 0.05em;
}
#tooltip span {
display: block;
margin-top: 3px;
color: #91a7b8;
font-size: 10px;
}
@media (max-width: 820px) {
header {
top: 17px;
left: 17px;
}
h1 {
font-size: 30px;
}
.header-meta {
display: none;
}
.route-panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 12px;
}
.route-list {
display: flex;
overflow-x: auto;
padding-bottom: 3px;
scrollbar-width: none;
}
.route-list::-webkit-scrollbar {
display: none;
}
.route-card {
flex: 0 0 225px;
}
.route-note,
.cost-rule {
display: none;
}
.legend,
.controls {
display: none;
}
#dimension-tag {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="app">
<div id="scene"></div>
<div class="hud">
<header>
<div class="eyebrow">Voxel routing study / 07 × 05</div>
<h1>WEIGHTED<br />ROUTES</h1>
<p class="subtitle">
One courier. Three terrain choices. Cost points and visual distance
are measured independently.
</p>
<div class="header-meta">
<div class="meta-chip">Shared tile: 1.50 × 1.50 m</div>
<div class="meta-chip">Cost ≠ distance</div>
<div class="meta-chip">Couriers: 1</div>
</div>
</header>
<aside class="route-panel panel">
<div class="panel-heading">
<h2>Route comparison</h2>
<span>Entry-cost model</span>
</div>
<div id="route-list" class="route-list"></div>
<div class="run-row">
<button id="run-route" class="run-button">Run route</button>
<div id="run-status" class="run-status">
Courier is waiting on the free start tile.
</div>
</div>
<div class="cost-rule">
<b>Cost</b> is charged when entering a terrain tile.
<b>Distance</b> is the measured route geometry. The start tile is free.
</div>
</aside>
<aside class="legend panel">
<h2>Tile traversal cost</h2>
<div class="legend-row">
<span class="terrain-swatch" style="background:#465766"></span>
<span>Road</span>
<strong>1 <small>CP</small></strong>
</div>
<div class="legend-row">
<span class="terrain-swatch" style="background:#865a3b"></span>
<span>Mud</span>
<strong>3 <small>CP</small></strong>
</div>
<div class="legend-row">
<span class="terrain-swatch" style="background:#b98248"></span>
<span>Bridge</span>
<strong>2 <small>CP</small></strong>
</div>
<div class="dimension-rule">
<strong>1.50 × 1.50 m</strong>
<span>
Every terrain tile uses the same square module. Route distance is
module pitch multiplied by the number of steps.
</span>
</div>
</aside>
<div class="controls panel">
<span class="control-hint">Drag orbit · wheel zoom</span>
<button id="zoom-out" class="control-button" aria-label="Zoom out">−</button>
<button id="zoom-in" class="control-button" aria-label="Zoom in">+</button>
<button id="reset-view" class="control-button text">Reset</button>
</div>
<div id="dimension-tag">
1.50 × 1.50 m
<small>shared tile module</small>
</div>
<div id="tooltip"></div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const routeList = document.getElementById("route-list");
const runButton = document.getElementById("run-route");
const runStatus = document.getElementById("run-status");
const tooltip = document.getElementById("tooltip");
const dimensionTag = document.getElementById("dimension-tag");
const TILE_MODULE = 1.5;
const TILE_VISUAL_SIZE = 1.42;
const COLS = 7;
const ROWS = 5;
const BOARD_WIDTH = COLS * TILE_MODULE;
const BOARD_DEPTH = ROWS * TILE_MODULE;
const terrain = [
["road", "mud", "road", "bridge", "mud", "road", "mud"],
["road", "road", "road", "road", "road", "road", "road"],
["road", "road", "mud", "mud", "mud", "mud", "road"],
["mud", "bridge", "mud", "road", "mud", "bridge", "bridge"],
["road", "road", "bridge", "bridge", "bridge", "bridge", "road"]
];
const terrainInfo = {
road: {
label: "ROAD",
cost: 1,
color: 0x65e6b2,
accent: "#65e6b2"
},
mud: {
label: "MUD",
cost: 3,
color: 0xff7b72,
accent: "#ff7b72"
},
bridge: {
label: "BRIDGE",
cost: 2,
color: 0xffc857,
accent: "#ffc857"
}
};
const routes = [
{
id: "roadway",
name: "Roadway",
color: "#65e6b2",
note: "Cheapest option: 8 road entries.",
coords: [
[0, 2], [0, 1], [1, 1], [2, 1], [3, 1],
[4, 1], [5, 1], [6, 1], [6, 2]
]
},
{
id: "mud-cut",
name: "Mud Cut",
color: "#ff7b72",
note: "Shortest geometry, but mud adds 6 extra points.",
coords: [
[0, 2], [1, 2], [2, 2], [3, 2],
[4, 2], [5, 2], [6, 2]
]
},
{
id: "bridge-walk",
name: "Bridge Walk",
color: "#ffc857",
note: "Scenic detour: 3.0 m longer than the road.",
coords: [
[0, 2], [0, 3], [0, 4], [1, 4], [2, 4],
[3, 4], [4, 4], [5, 4], [6, 4], [6, 3], [6, 2]
]
}
];
routes.forEach((route) => {
route.steps = route.coords.length - 1;
route.distance = route.steps * TILE_MODULE;
route.entries = route.coords.slice(1);
route.cost = route.entries.reduce(
(sum, [column, row]) =>
sum + terrainInfo[terrain[row][column]].cost,
0
);
});
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-10, 10, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0.7, 0, 0);
let azimuth = 0.63;
let elevation = 0.76;
let zoom = 1.02;
function updateCamera() {
const radius = 28;
const horizontal = Math.cos(elevation) * radius;
camera.position.set(
cameraTarget.x + Math.sin(azimuth) * horizontal,
cameraTarget.y + Math.sin(elevation) * radius,
cameraTarget.z + Math.cos(azimuth) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom =
zoom *
Math.min(1, (window.innerWidth / window.innerHeight) / 1.12);
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = 9.2;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
updateCamera();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const hemisphere = new THREE.HemisphereLight(0xc9e8ff, 0x253346, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3df, 2.8);
sun.position.set(5, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x69a9ff, 1.15);
rimLight.position.set(-7, 6, -8);
scene.add(rimLight);
const boardGroup = new THREE.Group();
scene.add(boardGroup);
function makeBox(
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
options = {}
) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.82,
metalness: options.metalness ?? 0.02,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
return mesh;
}
const lowerBase = makeBox(
BOARD_WIDTH + 0.9,
0.52,
BOARD_DEPTH + 0.9,
0x172a3b,
0,
-0.55,
0,
{ roughness: 0.9 }
);
boardGroup.add(lowerBase);
const upperBase = makeBox(
BOARD_WIDTH + 0.38,
0.25,
BOARD_DEPTH + 0.38,
0x294156,
0,
-0.2,
0,
{ roughness: 0.95 }
);
boardGroup.add(upperBase);
const underside = makeBox(
BOARD_WIDTH - 0.2,
0.09,
BOARD_DEPTH - 0.2,
0x0f1d2b,
0,
-0.83,
0
);
boardGroup.add(underside);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.ShadowMaterial({ opacity: 0.27 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.9;
floor.receiveShadow = true;
scene.add(floor);
function gridPosition(column, row) {
return new THREE.Vector3(
(column - (COLS - 1) / 2) * TILE_MODULE,
0,
(row - (ROWS - 1) / 2) * TILE_MODULE
);
}
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeBadge(text, accent, width = 1.03, height = 0.29) {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 88;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(
context,
4,
6,
canvas.width - 8,
canvas.height - 12,
13
);
context.fillStyle = "rgba(7, 16, 27, 0.91)";
context.fill();
context.strokeStyle = accent;
context.lineWidth = 4;
context.stroke();
context.fillStyle = "#eff8fc";
context.font = "800 27px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
const tileMeshes = [];
function addRoadTile(group) {
group.add(
makeBox(
TILE_VISUAL_SIZE,
0.32,
TILE_VISUAL_SIZE,
0x2d3b49,
0,
0.08,
0
)
);
group.add(
makeBox(
1.31,
0.045,
1.31,
0x435466,
0,
0.255,
0,
{ castShadow: false }
)
);
group.add(
makeBox(
0.44,
0.025,
0.045,
0xb8c8cd,
-0.24,
0.285,
0,
{ castShadow: false }
)
);
group.add(
makeBox(
0.44,
0.025,
0.045,
0xb8c8cd,
0.24,
0.285,
0,
{ castShadow: false }
)
);
}
function addMudTile(group, column, row) {
group.add(
makeBox(
TILE_VISUAL_SIZE,
0.32,
TILE_VISUAL_SIZE,
0x64452f,
0,
0.08,
0
)
);
group.add(
makeBox(
1.3,
0.055,
1.3,
0x845a3a,
0,
0.255,
0,
{ roughness: 1 }
)
);
const seed = (column * 11 + row * 7) % 3;
const puddleX = seed === 0 ? -0.28 : seed === 1 ? 0.22 : 0;
const puddleZ = seed === 2 ? 0.24 : -0.22;
group.add(
makeBox(
0.34,
0.035,
0.23,
0x4b4135,
puddleX,
0.295,
puddleZ,
{ roughness: 0.55, castShadow: false }
)
);
group.add(
makeBox(
0.19,
0.12,
0.16,
0x9a6d43,
-puddleX * 0.65,
0.33,
puddleZ * 0.55
)
);
group.add(
makeBox(
0.13,
0.08,
0.14,
0x6e5037,
puddleX * 0.45,
0.32,
-puddleZ * 0.7
)
);
}
function addBridgeTile(group) {
group.add(
makeBox(
TILE_VISUAL_SIZE,
0.16,
TILE_VISUAL_SIZE,
0x24556b,
0,
0.03,
0,
{ roughness: 0.7 }
)
);
const plankColors = [0xa96f3d, 0xb97b45, 0xc38a4e, 0xb57742, 0xa86b3a];
for (let index = 0; index < 5; index += 1) {
group.add(
makeBox(
0.2,
0.17,
1.29,
plankColors[index],
-0.5 + index * 0.25,
0.19,
0,
{ roughness: 0.95 }
)
);
}
group.add(
makeBox(
1.37,
0.075,
0.075,
0x69442f,
0,
0.42,
0.61
)
);
group.add(
makeBox(
1.37,
0.075,
0.075,
0x69442f,
0,
0.42,
-0.61
)
);
for (const x of [-0.57, 0.57]) {
group.add(
makeBox(
0.09,
0.34,
0.09,
0x765038,
x,
0.31,
0.61
)
);
group.add(
makeBox(
0.09,
0.34,
0.09,
0x765038,
x,
0.31,
-0.61
)
);
}
}
for (let row = 0; row < ROWS; row += 1) {
for (let column = 0; column < COLS; column += 1) {
const type = terrain[row][column];
const info = terrainInfo[type];
const position = gridPosition(column, row);
const group = new THREE.Group();
group.position.copy(position);
boardGroup.add(group);
if (type === "road") {
addRoadTile(group);
} else if (type === "mud") {
addMudTile(group, column, row);
} else {
addBridgeTile(group);
}
const pickMesh = group.children[0];
pickMesh.userData = {
type,
cost: info.cost,
column,
row
};
tileMeshes.push(pickMesh);
const badge = makeBadge(
`${info.label} · ${info.cost}`,
info.accent,
1.03,
0.29
);
badge.position.set(0, 0.62, 0);
group.add(badge);
}
}
// Dimension bracket around one representative tile.
const dimensionPoints = [];
function addDimensionSegment(a, b) {
dimensionPoints.push(...a, ...b);
}
const dimensionY = 0.34;
const left = -5.25;
const right = -3.75;
const back = -3.75;
const front = -2.25;
addDimensionSegment(
[left, dimensionY, -2.06],
[right, dimensionY, -2.06]
);
addDimensionSegment(
[left, dimensionY, -2.14],
[left, dimensionY, -1.98]
);
addDimensionSegment(
[right, dimensionY, -2.14],
[right, dimensionY, -1.98]
);
addDimensionSegment(
[-5.43, dimensionY, back],
[-5.43, dimensionY, front]
);
addDimensionSegment(
[-5.51, dimensionY, back],
[-5.35, dimensionY, back]
);
addDimensionSegment(
[-5.51, dimensionY, front],
[-5.35, dimensionY, front]
);
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dimensionPoints, 3)
);
const dimensionLines = new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({
color: 0xa9c6d8,
transparent: true,
opacity: 0.72,
depthTest: false
})
);
dimensionLines.renderOrder = 12;
scene.add(dimensionLines);
// Route overlays.
const routeVisuals = new Map();
for (const route of routes) {
const group = new THREE.Group();
const segments = [];
for (let index = 0; index < route.coords.length - 1; index += 1) {
const [columnA, rowA] = route.coords[index];
const [columnB, rowB] = route.coords[index + 1];
const a = gridPosition(columnA, rowA);
const b = gridPosition(columnB, rowB);
const middle = a.clone().lerp(b, 0.5);
const horizontal = Math.abs(b.x - a.x) > 0;
const length = Math.abs(b.x - a.x) + Math.abs(b.z - a.z);
const geometry = horizontal
? new THREE.BoxGeometry(length, 0.055, 0.12)
: new THREE.BoxGeometry(0.12, 0.055, length);
const material = new THREE.MeshStandardMaterial({
color: route.color,
emissive: route.color,
emissiveIntensity: 0.22,
transparent: true,
opacity: 0.9,
roughness: 0.45,
metalness: 0.05,
depthWrite: false
});
const segment = new THREE.Mesh(geometry, material);
segment.position.set(middle.x, 0.35, middle.z);
segment.castShadow = false;
segment.receiveShadow = false;
segment.renderOrder = 5;
group.add(segment);
segments.push({
mesh: segment,
horizontal,
material
});
}
scene.add(group);
routeVisuals.set(route.id, { group, segments });
}
// Start-tile corner markers.
const startPosition = gridPosition(0, 2);
const markerColor = 0xdcecff;
for (const xOffset of [-0.62, 0.62]) {
for (const zOffset of [-0.62, 0.62]) {
const marker = makeBox(
0.14,
0.045,
0.14,
markerColor,
startPosition.x + xOffset,
0.31,
startPosition.z + zOffset,
{ emissive: markerColor, emissiveIntensity: 0.15 }
);
scene.add(marker);
}
}
// Destination checkpoint.
const destination = gridPosition(6, 2);
const checkpoint = new THREE.Group();
checkpoint.position.copy(destination);
scene.add(checkpoint);
checkpoint.add(
makeBox(
0.09,
0.82,
0.09,
0xcbd9e2,
0.46,
0.65,
0.46
)
);
checkpoint.add(
makeBox(
0.43,
0.27,
0.07,
0x65e6b2,
0.25,
0.98,
0.46,
{
emissive: 0x65e6b2,
emissiveIntensity: 0.16
}
)
);
checkpoint.add(
makeBox(
0.34,
0.045,
0.085,
0x102a27,
0.25,
0.98,
0.505,
{ castShadow: false }
)
);
// Exactly one courier token.
const courier = new THREE.Group();
courier.position.set(startPosition.x, 0.29, startPosition.z);
scene.add(courier);
const courierBody = new THREE.Group();
courier.add(courierBody);
courierBody.add(
makeBox(
0.16,
0.28,
0.18,
0x183047,
-0.13,
0.16,
0
)
);
courierBody.add(
makeBox(
0.16,
0.28,
0.18,
0x183047,
0.13,
0.16,
0
)
);
courierBody.add(
makeBox(
0.25,
0.12,
0.31,
0x0e1c2a,
-0.13,
0.035,
0.04
)
);
courierBody.add(
makeBox(
0.25,
0.12,
0.31,
0x0e1c2a,
0.13,
0.035,
0.04
)
);
courierBody.add(
makeBox(
0.43,
0.43,
0.31,
0xff8e38,
0,
0.48,
0,
{ roughness: 0.72 }
)
);
courierBody.add(
makeBox(
0.29,
0.28,
0.29,
0xe8b38c,
0,
0.83,
0.015
)
);
courierBody.add(
makeBox(
0.34,
0.12,
0.34,
0x53b9d1,
0,
1.015,
0,
{ roughness: 0.55 }
)
);
courierBody.add(
makeBox(
0.39,
0.055,
0.39,
0x267d9b,
0,
0.97,
0,
{ roughness: 0.55 }
)
);
courierBody.add(
makeBox(
0.055,
0.055,
0.025,
0x10202c,
-0.07,
0.85,
0.163,
{ castShadow: false }
)
);
courierBody.add(
makeBox(
0.055,
0.055,
0.025,
0x10202c,
0.07,
0.85,
0.163,
{ castShadow: false }
)
);
// Parcel carried on the courier's back.
courierBody.add(
makeBox(
0.32,
0.31,
0.2,
0xcfaa69,
0,
0.5,
-0.245
)
);
courierBody.add(
makeBox(
0.34,
0.055,
0.215,
0x6f5134,
0,
0.57,
-0.247,
{ castShadow: false }
)
);
courierBody.add(
makeBox(
0.055,
0.32,
0.215,
0x6f5134,
0,
0.5,
-0.248,
{ castShadow: false }
)
);
const courierBadge = makeBadge("COURIER", "#ff9d45", 1.15, 0.31);
courierBadge.position.set(0, 1.45, 0);
courier.add(courierBadge);
let selectedRouteId = routes[0].id;
let animation = null;
function selectedRoute() {
return routes.find((route) => route.id === selectedRouteId);
}
function updateRouteVisuals() {
for (const route of routes) {
const visual = routeVisuals.get(route.id);
const active = route.id === selectedRouteId;
for (const segment of visual.segments) {
segment.material.opacity = active ? 0.96 : 0.16;
segment.material.emissiveIntensity = active ? 0.28 : 0.04;
if (segment.horizontal) {
segment.mesh.scale.z = active ? 1.28 : 0.72;
} else {
segment.mesh.scale.x = active ? 1.28 : 0.72;
}
}
}
document.querySelectorAll(".route-card").forEach((card) => {
card.classList.toggle("active", card.dataset.route === selectedRouteId);
});
}
function resetCourier() {
animation = null;
courier.position.set(startPosition.x, 0.29, startPosition.z);
courier.rotation.y = Math.PI / 2;
runButton.textContent = "Run route";
runStatus.textContent = "Courier is waiting on the free start tile.";
}
function selectRoute(id) {
selectedRouteId = id;
updateRouteVisuals();
resetCourier();
const route = selectedRoute();
runStatus.textContent = `${route.name}: ${route.cost} cost points, ${route.distance.toFixed(1)} m.`;
}
function buildRouteCards() {
for (const route of routes) {
const card = document.createElement("button");
card.type = "button";
card.className = "route-card";
card.dataset.route = route.id;
card.style.setProperty("--route-color", route.color);
card.setAttribute("aria-label", `Select ${route.name}`);
card.innerHTML = `
<div class="route-name">
<span class="route-dot"></span>
${route.name}
</div>
<div class="route-cost">
<strong>${route.cost}</strong>
<small>CP</small>
</div>
<div class="route-distance">
<b>${route.distance.toFixed(1)} m</b> visual distance · ${route.steps} steps
</div>
<div class="route-note">${route.note}</div>
`;
card.addEventListener("click", () => selectRoute(route.id));
routeList.appendChild(card);
}
}
buildRouteCards();
updateRouteVisuals();
resetCourier();
runButton.addEventListener("click", () => {
if (animation) {
resetCourier();
return;
}
const route = selectedRoute();
const startPositionWorld = gridPosition(...route.coords[0]);
courier.position.set(
startPositionWorld.x,
0.29,
startPositionWorld.z
);
animation = {
route,
startedAt: performance.now(),
duration: route.steps * 720
};
runButton.textContent = "Reset courier";
runStatus.textContent = `Moving along ${route.name}…`;
});
function shortestAngleDifference(from, to) {
let difference = (to - from + Math.PI) % (Math.PI * 2) - Math.PI;
if (difference < -Math.PI) difference += Math.PI * 2;
return difference;
}
function updateCourier(now) {
if (!animation) return;
const route = animation.route;
const progress = Math.min(
1,
(now - animation.startedAt) / animation.duration
);
const distance = progress * route.distance;
const segmentIndex = Math.min(
route.coords.length - 2,
Math.floor(distance / TILE_MODULE)
);
const localDistance = distance - segmentIndex * TILE_MODULE;
const [columnA, rowA] = route.coords[segmentIndex];
const [columnB, rowB] = route.coords[segmentIndex + 1];
const a = gridPosition(columnA, rowA);
const b = gridPosition(columnB, rowB);
const t = localDistance / TILE_MODULE;
courier.position.set(
THREE.MathUtils.lerp(a.x, b.x, t),
0.29,
THREE.MathUtils.lerp(a.z, b.z, t)
);
const targetRotation = Math.atan2(b.x - a.x, b.z - a.z);
const difference = shortestAngleDifference(
courier.rotation.y,
targetRotation
);
courier.rotation.y += difference * 0.16;
if (progress >= 1) {
const completed = route;
animation = null;
runButton.textContent = "Replay route";
runStatus.textContent =
`Delivered via ${completed.name}: ${completed.cost} cost points.`;
}
}
// Direct camera controls; no examples/addons are imported.
let pointerDown = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
tooltip.style.display = "none";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 2) {
pointerMoved = true;
}
azimuth -= deltaX * 0.006;
elevation = THREE.MathUtils.clamp(
elevation + deltaY * 0.005,
0.34,
1.35
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
return;
}
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 intersections = raycaster.intersectObjects(tileMeshes, false);
if (intersections.length > 0) {
const data = intersections[0].object.userData;
const info = terrainInfo[data.type];
tooltip.innerHTML = `
<strong>${info.label} TILE · ${info.cost} COST</strong>
<span>${TILE_MODULE.toFixed(2)} × ${TILE_MODULE.toFixed(2)} m shared module</span>
`;
tooltip.style.left = `${event.clientX}px`;
tooltip.style.top = `${event.clientY}px`;
tooltip.style.display = "block";
} else {
tooltip.style.display = "none";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) tooltip.style.display = "none";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoom = THREE.MathUtils.clamp(
zoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.75
);
updateCamera();
},
{ passive: false }
);
document.getElementById("zoom-in").addEventListener("click", () => {
zoom = THREE.MathUtils.clamp(zoom + 0.1, 0.65, 1.75);
updateCamera();
});
document.getElementById("zoom-out").addEventListener("click", () => {
zoom = THREE.MathUtils.clamp(zoom - 0.1, 0.65, 1.75);
updateCamera();
});
document.getElementById("reset-view").addEventListener("click", () => {
azimuth = 0.63;
elevation = 0.76;
zoom = 1.02;
updateCamera();
resetCourier();
});
const projectedTagPosition = new THREE.Vector3();
function updateDimensionTag() {
projectedTagPosition.set(-4.5, 0.84, -2.04).project(camera);
const x = (projectedTagPosition.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projectedTagPosition.y * 0.5 + 0.5) * window.innerHeight;
dimensionTag.style.left = `${x}px`;
dimensionTag.style.top = `${y}px`;
dimensionTag.style.display =
projectedTagPosition.z > -1 && projectedTagPosition.z < 1
? "block"
: "none";
}
function animate(now) {
updateCourier(now);
updateDimensionTag();
renderer.render(scene, camera);
}
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```