Prompt specification
Build or customize the Three.js 3D scene "Battery Tokens Along a Route" 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: Battery Tokens Along a Route
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/battery-tokens-along-a-route-x3537
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing energy budgets, charging stops, and feasible routes. Place a robot on a short path with one energy token required per step. Show a charging tile and keep the rule explicitly a game abstraction. Voxel style.
Working Three.js Starter (battery-tokens-along-a-route-x3537):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Energy Router</title>
<style>
:root {
--ink: #172620;
--muted: #68756f;
--paper: #f4f1e8;
--panel: #fffdf7;
--line: #d9ded4;
--teal: #12a995;
--teal-dark: #087c70;
--blue: #5b78e8;
--orange: #e88961;
--red: #d95656;
--green: #2e9b68;
--shadow: 0 18px 45px rgba(35, 56, 47, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
color: var(--ink);
background: #e7ebe2;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
cursor: pointer;
}
.topbar {
min-height: 78px;
padding: 18px clamp(18px, 4vw, 64px);
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
background: rgba(250, 249, 243, 0.94);
border-bottom: 1px solid var(--line);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
color: white;
background: var(--ink);
border-radius: 10px;
box-shadow: 4px 4px 0 #b9c8be;
font: 800 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.brand h1 {
margin: 0 0 3px;
font-size: 20px;
letter-spacing: -0.03em;
}
.brand p {
margin: 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.abstraction-badge {
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border: 1px solid #b8c8bd;
border-radius: 999px;
background: #eef3eb;
color: #436052;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
white-space: nowrap;
}
.abstraction-badge::before {
content: "";
width: 8px;
height: 8px;
background: var(--teal);
border-radius: 2px;
box-shadow: 0 0 0 3px rgba(18, 169, 149, 0.15);
}
.app {
width: min(1500px, 100%);
margin: 0 auto;
padding: clamp(18px, 3vw, 36px);
display: grid;
grid-template-columns: 350px minmax(0, 1fr);
gap: 22px;
}
.sidebar {
display: flex;
flex-direction: column;
gap: 16px;
}
.panel {
background: var(--panel);
border: 1px solid rgba(205, 213, 202, 0.9);
border-radius: 18px;
box-shadow: var(--shadow);
}
.planner {
padding: 22px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--teal-dark);
font: 800 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h2,
h3,
p {
margin-top: 0;
}
.planner > h2 {
margin-bottom: 7px;
font-size: 24px;
letter-spacing: -0.04em;
}
.intro {
margin-bottom: 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.rule-card {
margin-bottom: 21px;
padding: 14px;
background: #fff5d8;
border: 1px solid #eadba5;
border-left: 4px solid #e2b93f;
border-radius: 10px;
}
.rule-card strong {
display: block;
margin-bottom: 5px;
color: #6f5715;
font: 800 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.rule-card p {
margin: 0;
color: #6d6242;
font-size: 12px;
line-height: 1.5;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.section-heading h3 {
margin: 0;
font-size: 13px;
}
.budget-readout {
display: flex;
align-items: baseline;
gap: 5px;
margin: 2px 0 9px;
}
.budget-readout strong {
font-size: 30px;
line-height: 1;
letter-spacing: -0.05em;
}
.budget-readout span {
color: var(--muted);
font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.range {
--pct: 40%;
width: 100%;
height: 6px;
margin: 8px 0 10px;
appearance: none;
background: linear-gradient(
to right,
var(--teal) 0,
var(--teal) var(--pct),
#dce2d9 var(--pct),
#dce2d9 100%
);
border-radius: 99px;
outline: none;
}
.range::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
background: white;
border: 4px solid var(--teal);
border-radius: 50%;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
}
.range::-moz-range-thumb {
width: 12px;
height: 12px;
background: white;
border: 4px solid var(--teal);
border-radius: 50%;
}
.budget-hint {
min-height: 17px;
margin: 0;
color: var(--muted);
font-size: 11px;
}
.route-section {
margin-top: 23px;
padding-top: 19px;
border-top: 1px solid var(--line);
}
.feasible-count {
color: var(--green);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.route-list {
display: grid;
gap: 9px;
}
.route-card {
width: 100%;
padding: 12px;
display: grid;
grid-template-columns: 8px 1fr auto;
gap: 11px;
align-items: start;
text-align: left;
color: var(--ink);
background: #f7f6f0;
border: 1px solid #e0e4dc;
border-radius: 11px;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.route-card:hover {
transform: translateY(-1px);
border-color: #bac7bb;
}
.route-card.selected {
background: white;
border-color: var(--route-color);
box-shadow: 0 0 0 2px color-mix(in srgb, var(--route-color) 14%, transparent);
}
.route-swatch {
width: 8px;
height: 34px;
background: var(--route-color);
border-radius: 3px;
}
.route-name {
display: block;
margin-bottom: 4px;
font-size: 13px;
font-weight: 800;
}
.route-details {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.route-minimum {
display: block;
margin-top: 5px;
color: #46554d;
font: 700 10px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.route-badge {
margin-top: 1px;
padding: 5px 7px;
border-radius: 999px;
font: 800 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
white-space: nowrap;
}
.route-badge.feasible {
color: #21734d;
background: #dff2e8;
}
.route-badge.infeasible {
color: #9c3939;
background: #f7dddd;
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
margin-top: 17px;
}
.primary,
.secondary {
min-height: 43px;
padding: 0 16px;
border-radius: 10px;
font-size: 12px;
font-weight: 800;
transition:
transform 140ms ease,
opacity 140ms ease,
background 140ms ease;
}
.primary {
color: white;
background: var(--ink);
border: 1px solid var(--ink);
}
.primary:hover:not(:disabled) {
transform: translateY(-1px);
background: #284239;
}
.secondary {
color: var(--ink);
background: white;
border: 1px solid var(--line);
}
button:disabled {
cursor: not-allowed;
opacity: 0.48;
}
.event-line {
min-height: 37px;
margin: 13px 0 0;
padding: 10px 11px;
color: #536159;
background: #f2f4ee;
border-radius: 8px;
font: 600 10px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.legend {
padding: 17px 20px;
}
.legend h3 {
margin-bottom: 13px;
font-size: 12px;
}
.legend-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
}
.legend-icon {
width: 15px;
height: 15px;
flex: 0 0 auto;
border-radius: 3px;
}
.legend-icon.start {
background: #6d8cf0;
}
.legend-icon.charge {
background: #f1c64e;
box-shadow: inset 0 0 0 3px #ffe89a;
}
.legend-icon.goal {
background: #55aa7b;
}
.legend-icon.fail {
background: #d95656;
}
.stage {
position: relative;
min-height: 680px;
overflow: hidden;
border: 1px solid #cbd4c8;
border-radius: 22px;
background:
radial-gradient(circle at 65% 22%, rgba(255, 255, 255, 0.95), transparent 28%),
linear-gradient(145deg, #e6eee6, #d8e3d9);
box-shadow: var(--shadow);
isolation: isolate;
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.scene-header {
position: absolute;
top: 20px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.scene-title {
padding: 14px 16px;
background: rgba(255, 253, 247, 0.88);
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 13px;
box-shadow: 0 10px 30px rgba(45, 66, 55, 0.1);
backdrop-filter: blur(10px);
}
.scene-title .eyebrow {
margin-bottom: 5px;
}
.scene-title h2 {
margin: 0 0 4px;
font-size: 20px;
letter-spacing: -0.03em;
}
.scene-title p {
margin: 0;
color: var(--muted);
font-size: 11px;
}
.scene-status {
padding: 10px 13px;
border-radius: 999px;
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
white-space: nowrap;
}
.scene-status.ok {
color: #216e4c;
background: rgba(226, 246, 233, 0.94);
border: 1px solid #afd5bd;
}
.scene-status.bad {
color: #963b3b;
background: rgba(251, 226, 226, 0.95);
border: 1px solid #e2aeae;
}
.hud {
position: absolute;
left: 20px;
right: 20px;
bottom: 42px;
display: grid;
grid-template-columns: 1.35fr 0.8fr 1fr;
gap: 1px;
overflow: hidden;
background: rgba(30, 49, 40, 0.12);
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 14px;
box-shadow: 0 14px 36px rgba(38, 58, 47, 0.16);
backdrop-filter: blur(12px);
pointer-events: none;
}
.hud-cell {
min-width: 0;
padding: 13px 15px;
background: rgba(255, 253, 247, 0.92);
}
.hud-label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.hud-value {
display: flex;
align-items: baseline;
gap: 5px;
font-size: 18px;
font-weight: 850;
letter-spacing: -0.03em;
}
.hud-value small {
color: var(--muted);
font-size: 10px;
font-weight: 600;
}
.energy-slots {
display: flex;
gap: 4px;
margin-top: 8px;
}
.energy-slot {
width: 10px;
height: 10px;
background: #dce3da;
border-radius: 2px;
transition:
background 180ms ease,
transform 180ms ease;
}
.energy-slot.active {
background: var(--teal);
transform: translateY(-1px);
}
.energy-slot.flash {
background: #f0c84d;
box-shadow: 0 0 0 3px rgba(240, 200, 77, 0.18);
}
.camera-help {
position: absolute;
left: 22px;
bottom: 16px;
color: rgba(41, 61, 51, 0.65);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
pointer-events: none;
}
.footer-note {
padding: 0 4px 15px;
color: #69766f;
font-size: 11px;
line-height: 1.5;
}
@media (max-width: 980px) {
.app {
grid-template-columns: 1fr;
}
.stage {
min-height: 610px;
}
.sidebar {
display: grid;
grid-template-columns: minmax(0, 1.4fr) minmax(240px, 0.6fr);
align-items: start;
}
}
@media (max-width: 660px) {
.topbar {
align-items: flex-start;
flex-direction: column;
gap: 13px;
}
.abstraction-badge {
white-space: normal;
}
.app {
padding: 12px;
}
.sidebar {
display: flex;
}
.stage {
min-height: 570px;
}
.scene-header {
top: 12px;
left: 12px;
right: 12px;
}
.scene-title {
max-width: 68%;
padding: 11px 12px;
}
.scene-title h2 {
font-size: 16px;
}
.scene-title p {
font-size: 9px;
}
.hud {
left: 10px;
right: 10px;
bottom: 38px;
grid-template-columns: 1fr 1fr;
}
.hud-cell {
padding: 10px;
}
.hud-cell:last-child {
grid-column: 1 / -1;
}
.camera-help {
left: 12px;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark">ER</div>
<div>
<h1>Voxel Energy Router</h1>
<p>Discrete route planning sandbox</p>
</div>
</div>
<div class="abstraction-badge">
EXPLICIT GAME ABSTRACTION · 1 TILE = 1 TOKEN
</div>
</header>
<main class="app">
<aside class="sidebar">
<section class="panel planner">
<p class="eyebrow">Route planner</p>
<h2>Power the path.</h2>
<p class="intro">
Choose a starting energy budget, inspect charging stops, and launch
the robot along a feasible voxel route.
</p>
<div class="rule-card">
<strong>Abstracted game rule</strong>
<p>
Every tile step costs exactly 1 energy token. Entering a charging
tile spends that token, then adds 4 tokens, capped at 12. This is a
discrete puzzle rule—not a physical battery or power-consumption model.
</p>
</div>
<div class="section-heading">
<h3><label for="budget">Energy budget</label></h3>
<span class="feasible-count" id="feasibleCount">2 FEASIBLE</span>
</div>
<div class="budget-readout">
<strong id="budgetValue">8</strong>
<span>TOKENS AT START</span>
</div>
<input
class="range"
id="budget"
type="range"
min="4"
max="12"
step="1"
value="8"
aria-label="Starting energy budget"
>
<p class="budget-hint" id="budgetHint"></p>
<section class="route-section">
<div class="section-heading">
<h3>Route options</h3>
<span class="feasible-count">SELECT TO PREVIEW</span>
</div>
<div class="route-list" id="routeList"></div>
</section>
<div class="actions">
<button class="primary" id="runButton" type="button">Run selected route</button>
<button class="secondary" id="resetButton" type="button">Reset</button>
</div>
<p class="event-line" id="eventLine" aria-live="polite">
READY · Select a route and run the robot.
</p>
</section>
<section class="panel legend">
<h3>Map legend</h3>
<div class="legend-grid">
<div class="legend-item">
<span class="legend-icon start"></span>
Start tile
</div>
<div class="legend-item">
<span class="legend-icon charge"></span>
Charge +4
</div>
<div class="legend-item">
<span class="legend-icon goal"></span>
Goal
</div>
<div class="legend-item">
<span class="legend-icon fail"></span>
Budget failure
</div>
</div>
</section>
</aside>
<section class="stage" aria-label="Interactive 3D voxel route map">
<div id="viewport"></div>
<div class="scene-header">
<div class="scene-title">
<p class="eyebrow">Active plan</p>
<h2 id="activeRouteName">Ridge Run</h2>
<p id="activeRouteMeta"></p>
</div>
<div class="scene-status ok" id="sceneStatus">FEASIBLE</div>
</div>
<div class="hud">
<div class="hud-cell">
<span class="hud-label">Energy remaining</span>
<div class="hud-value">
<span id="energyValue">8</span>
<small>/ 12 tokens</small>
</div>
<div class="energy-slots" id="energySlots"></div>
</div>
<div class="hud-cell">
<span class="hud-label">Route progress</span>
<div class="hud-value">
<span id="stepValue">0</span>
<small>/ <span id="totalSteps">10</span> steps</small>
</div>
</div>
<div class="hud-cell">
<span class="hud-label">Next charging stop</span>
<div class="hud-value" id="nextChargeValue">Step 4</div>
</div>
</div>
<div class="camera-help">DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET CAMERA</div>
</section>
</main>
<footer class="footer-note">
The route planner intentionally uses a simple token-cost abstraction. It does
not model distance efficiency, real charger rates, battery degradation, or
physical energy consumption.
</footer>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const CAPACITY = 12;
const CHARGE_AMOUNT = 4;
const SPACING = 1.16;
const STEP_DURATION = 0.48;
const routes = [
{
id: "ridge",
name: "Ridge Run",
color: 0x12a995,
css: "#12a995",
path: [
{ x: -2, z: 3 },
{ x: -1, z: 3 },
{ x: 0, z: 3 },
{ x: 1, z: 3 },
{ x: 2, z: 3, charger: true },
{ x: 2, z: 2 },
{ x: 2, z: 1 },
{ x: 2, z: 0 },
{ x: 2, z: -1 },
{ x: 2, z: -2 },
{ x: 2, z: -3, goal: true }
]
},
{
id: "relay",
name: "Canal Relay",
color: 0x5b78e8,
css: "#5b78e8",
path: [
{ x: -2, z: 3 },
{ x: -2, z: 2 },
{ x: -1, z: 2 },
{ x: 0, z: 2 },
{ x: 1, z: 2, charger: true },
{ x: 1, z: 1 },
{ x: 1, z: 0 },
{ x: 1, z: -1 },
{ x: 2, z: -1, charger: true },
{ x: 2, z: -2 },
{ x: 2, z: -3, goal: true }
]
},
{
id: "cargo",
name: "Cargo Detour",
color: 0xe88961,
css: "#e88961",
path: [
{ x: -2, z: 3 },
{ x: -3, z: 3 },
{ x: -3, z: 2 },
{ x: -3, z: 1 },
{ x: -2, z: 1 },
{ x: -1, z: 1 },
{ x: 0, z: 1, charger: true },
{ x: 1, z: 1 },
{ x: 1, z: 0 },
{ x: 1, z: -1 },
{ x: 0, z: -1 },
{ x: -1, z: -1 },
{ x: -1, z: -2 },
{ x: 0, z: -2 },
{ x: 1, z: -2 },
{ x: 2, z: -2 },
{ x: 2, z: -3, goal: true }
]
}
];
const viewport = document.getElementById("viewport");
const budgetInput = document.getElementById("budget");
const budgetValue = document.getElementById("budgetValue");
const budgetHint = document.getElementById("budgetHint");
const routeList = document.getElementById("routeList");
const feasibleCount = document.getElementById("feasibleCount");
const runButton = document.getElementById("runButton");
const resetButton = document.getElementById("resetButton");
const eventLine = document.getElementById("eventLine");
const activeRouteName = document.getElementById("activeRouteName");
const activeRouteMeta = document.getElementById("activeRouteMeta");
const sceneStatus = document.getElementById("sceneStatus");
const energyValue = document.getElementById("energyValue");
const stepValue = document.getElementById("stepValue");
const totalSteps = document.getElementById("totalSteps");
const nextChargeValue = document.getElementById("nextChargeValue");
const energySlots = document.getElementById("energySlots");
let selectedRouteId = "ridge";
let budget = Number(budgetInput.value);
let runner = null;
for (let i = 0; i < CAPACITY; i++) {
const slot = document.createElement("span");
slot.className = "energy-slot active";
energySlots.appendChild(slot);
}
function simulate(route, startingBudget) {
let energy = startingBudget;
let chargeStops = 0;
for (let step = 1; step < route.path.length; step++) {
if (energy <= 0) {
return {
feasible: false,
energy,
chargeStops,
failStep: step - 1
};
}
energy -= 1;
if (route.path[step].charger) {
energy = Math.min(CAPACITY, energy + CHARGE_AMOUNT);
chargeStops++;
}
}
return {
feasible: true,
energy,
chargeStops,
failStep: null
};
}
function minimumBudget(route) {
for (let amount = 1; amount <= CAPACITY; amount++) {
if (simulate(route, amount).feasible) return amount;
}
return CAPACITY;
}
function chargingSteps(route) {
return route.path
.map((tile, index) => (tile.charger ? index : null))
.filter((index) => index !== null);
}
function selectedRoute() {
return routes.find((route) => route.id === selectedRouteId);
}
function setEvent(message) {
eventLine.textContent = message.toUpperCase();
}
// ---------- Three.js scene ----------
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xdce6dd, 18, 34);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0xdce6dd, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel route map. Drag to orbit and scroll to zoom."
);
viewport.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-8, 8, 8, -8, 0.1, 100);
const cameraState = {
yaw: 0.68,
pitch: 0.72,
zoom: 1
};
function updateCamera() {
const distance = 19;
const horizontal = Math.cos(cameraState.pitch) * distance;
camera.position.set(
Math.sin(cameraState.yaw) * horizontal,
Math.sin(cameraState.pitch) * distance,
Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(0, 0.05, 0);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraState.yaw = 0.68;
cameraState.pitch = 0.72;
cameraState.zoom = 1;
updateCamera();
}
function resizeRenderer() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const aspect = width / Math.max(height, 1);
const halfHeight = Math.max(5.8, 6.2 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(viewport);
const hemisphere = new THREE.HemisphereLight(0xf7fff6, 0x657568, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3cf, 3.2);
sun.position.set(-6, 12, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d9ff, 0.65);
fill.position.set(8, 5, -7);
scene.add(fill);
function box(width, height, depth, color, x, y, z, parent = scene) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function worldX(x) {
return x * SPACING;
}
function worldZ(z) {
return z * SPACING;
}
function makeLabel(text, background = "#172620", foreground = "#ffffff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "rgba(255,255,255,0.32)";
context.lineWidth = 6;
context.strokeRect(3, 3, canvas.width - 6, canvas.height - 6);
context.fillStyle = foreground;
context.font = "800 54px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.1, 0.275, 1);
return sprite;
}
// Floating voxel island.
box(10.9, 0.3, 10.9, 0x607968, 0, -0.73, 0);
box(10.55, 0.55, 10.55, 0x78907a, 0, -0.35, 0);
box(10.15, 0.16, 10.15, 0x91a58a, 0, -0.01, 0);
const pathCells = new Set();
routes.forEach((route) => {
route.path.forEach((tile) => {
pathCells.add(`${tile.x},${tile.z}`);
});
});
// Grass-grid voxels.
for (let x = -4; x <= 4; x++) {
for (let z = -4; z <= 4; z++) {
const variation = Math.abs((x * 13 + z * 7) % 4);
const colors = [0x8da582, 0x91a889, 0x86a07e, 0x96aa8b];
const tile = box(
1.07,
0.12,
1.07,
colors[variation],
worldX(x),
0.065,
worldZ(z)
);
tile.castShadow = false;
}
}
// Pale road tiles shared by the routes.
pathCells.forEach((key) => {
const [x, z] = key.split(",").map(Number);
const road = box(
0.96,
0.17,
0.96,
0xe9e5d3,
worldX(x),
0.135,
worldZ(z)
);
road.castShadow = false;
});
// Route overlays.
const routeVisuals = new Map();
routes.forEach((route) => {
const group = new THREE.Group();
scene.add(group);
const material = new THREE.MeshStandardMaterial({
color: route.color,
emissive: route.color,
emissiveIntensity: 0.08,
roughness: 0.48,
metalness: 0.03,
transparent: true,
opacity: 0.16,
depthWrite: false
});
for (let i = 0; i < route.path.length - 1; i++) {
const a = route.path[i];
const b = route.path[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const horizontal = dx !== 0;
const segment = box(
horizontal ? SPACING + 0.1 : 0.13,
0.035,
horizontal ? 0.13 : SPACING + 0.1,
route.color,
worldX((a.x + b.x) / 2),
0.235,
worldZ((a.z + b.z) / 2),
group
);
segment.material = material;
segment.castShadow = false;
segment.receiveShadow = false;
}
route.path.forEach((tile, index) => {
if (index === 0 || index === route.path.length - 1) return;
const marker = box(
0.19,
0.038,
0.19,
route.color,
worldX(tile.x),
0.238,
worldZ(tile.z),
group
);
marker.material = material;
marker.castShadow = false;
marker.receiveShadow = false;
});
routeVisuals.set(route.id, { group, material });
});
// Start tile.
const startTile = routes[0].path[0];
box(
0.78,
0.08,
0.78,
0x6d8cf0,
worldX(startTile.x),
0.255,
worldZ(startTile.z)
);
const startLabel = makeLabel("START", "#526fc3");
startLabel.position.set(worldX(startTile.x), 0.92, worldZ(startTile.z));
scene.add(startLabel);
// Goal arch.
const goalTile = routes[0].path[routes[0].path.length - 1];
const goalGroup = new THREE.Group();
goalGroup.position.set(worldX(goalTile.x), 0, worldZ(goalTile.z));
scene.add(goalGroup);
box(0.2, 1.25, 0.24, 0x3c9667, -0.38, 0.76, 0, goalGroup);
box(0.2, 1.25, 0.24, 0x3c9667, 0.38, 0.76, 0, goalGroup);
box(0.96, 0.2, 0.24, 0x55ae7d, 0, 1.38, 0, goalGroup);
const goalLabel = makeLabel("GOAL", "#347b58");
goalLabel.position.set(0, 1.78, 0);
goalGroup.add(goalLabel);
const goalBeacon = box(
0.24,
0.24,
0.24,
0xa8f0bc,
0,
1.85,
0,
goalGroup
);
// Charging tiles.
const chargerVisuals = new Map();
routes.forEach((route) => {
route.path.forEach((tile) => {
if (!tile.charger) return;
const key = `${tile.x},${tile.z}`;
if (chargerVisuals.has(key)) return;
const group = new THREE.Group();
group.position.set(worldX(tile.x), 0, worldZ(tile.z));
scene.add(group);
box(0.78, 0.08, 0.78, 0xf1c64e, 0, 0.26, 0, group);
const cornerMaterial = new THREE.MeshStandardMaterial({
color: 0xffe89a,
emissive: 0xf1c64e,
emissiveIntensity: 0.25,
roughness: 0.5
});
[
[-0.32, -0.32],
[0.32, -0.32],
[-0.32, 0.32],
[0.32, 0.32]
].forEach(([x, z]) => {
const corner = box(0.12, 0.22, 0.12, 0xffe89a, x, 0.39, z, group);
corner.material = cornerMaterial;
});
const floating = box(
0.3,
0.3,
0.3,
0xf1c64e,
0,
1.25,
0,
group
);
floating.material = cornerMaterial;
const label = makeLabel("+4", "#b88915", "#fff7d8");
label.scale.set(0.66, 0.165, 1);
label.position.set(0, 1.67, 0);
group.add(label);
chargerVisuals.set(key, {
group,
floating,
material: cornerMaterial,
flash: 0,
phase: chargerVisuals.size * 1.7
});
});
});
// Failure marker.
const failureMarker = new THREE.Group();
const failureMaterial = new THREE.MeshStandardMaterial({
color: 0xd95656,
emissive: 0x7c1717,
emissiveIntensity: 0.3,
roughness: 0.5
});
const failureA = box(0.62, 0.11, 0.11, 0xd95656, 0, 0.52, 0, failureMarker);
const failureB = box(0.62, 0.11, 0.11, 0xd95656, 0, 0.52, 0, failureMarker);
failureA.material = failureMaterial;
failureB.material = failureMaterial;
failureA.rotation.z = Math.PI / 4;
failureB.rotation.z = -Math.PI / 4;
failureMarker.visible = false;
scene.add(failureMarker);
// Voxel trees and rocks.
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(worldX(x), 0, worldZ(z));
group.scale.setScalar(scale);
scene.add(group);
box(0.22, 1.05, 0.22, 0x775d3f, 0, 0.65, 0, group);
box(0.9, 0.72, 0.9, 0x3f7651, 0, 1.38, 0, group);
box(0.72, 0.62, 0.72, 0x4b8758, 0.12, 1.9, -0.08, group);
box(0.48, 0.45, 0.48, 0x5c9762, -0.16, 2.34, 0.02, group);
}
[
[-4, -4, 0.9],
[-3, -4, 0.72],
[4, -4, 0.9],
[4, 4, 0.83],
[3, 4, 0.68],
[-4, 0, 0.78],
[4, 0, 0.76],
[-2, -4, 0.65],
[0, -4, 0.68]
].forEach(([x, z, scale]) => addTree(x, z, scale));
function addRock(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(worldX(x), 0, worldZ(z));
group.scale.setScalar(scale);
scene.add(group);
box(0.48, 0.25, 0.42, 0x9ca59c, 0, 0.2, 0, group);
box(0.28, 0.22, 0.3, 0xaeb5ab, 0.16, 0.4, -0.08, group);
}
[
[-4, 2, 0.8],
[4, 1, 0.75],
[-4, -2, 0.65],
[0, 4, 0.7],
[3, -2, 0.55]
].forEach(([x, z, scale]) => addRock(x, z, scale));
// A simple voxel sun and clouds.
const sunCube = box(
0.75,
0.75,
0.75,
0xffdd70,
-5.4,
5.6,
-4.1
);
sunCube.castShadow = false;
function addCloud(x, y, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
box(1.25, 0.35, 0.55, 0xf5fbf4, 0, 0, 0, group).castShadow = false;
box(0.58, 0.52, 0.48, 0xf5fbf4, -0.34, 0.2, 0, group).castShadow = false;
box(0.65, 0.42, 0.48, 0xf5fbf4, 0.36, 0.17, 0, group).castShadow = false;
}
addCloud(-3.2, 4.8, -5.6, 0.85);
addCloud(4.4, 4.3, -3.7, 0.65);
// ---------- Voxel robot ----------
const robotRoot = new THREE.Group();
const robotBody = new THREE.Group();
robotRoot.add(robotBody);
scene.add(robotRoot);
const shadow = box(
0.72,
0.018,
0.6,
0x26372e,
0,
0.025,
0,
robotRoot
);
shadow.material.transparent = true;
shadow.material.opacity = 0.16;
shadow.castShadow = false;
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.15, 0.22, 0);
rightLeg.position.set(0.15, 0.22, 0);
robotBody.add(leftLeg, rightLeg);
box(0.18, 0.23, 0.21, 0x35483f, 0, -0.1, 0, leftLeg);
box(0.18, 0.23, 0.21, 0x35483f, 0, -0.1, 0, rightLeg);
box(0.58, 0.42, 0.48, 0xf7f4e9, 0, 0.48, 0, robotBody);
box(0.29, 0.19, 0.035, 0x20b6a1, 0, 0.47, 0.255, robotBody);
box(0.31, 0.31, 0.22, 0xe88a4d, 0, 0.52, -0.31, robotBody);
box(0.47, 0.29, 0.42, 0xfbfaf4, 0, 0.83, 0.01, robotBody);
box(0.07, 0.07, 0.035, 0x172620, -0.11, 0.86, 0.235, robotBody);
box(0.07, 0.07, 0.035, 0x172620, 0.11, 0.86, 0.235, robotBody);
box(0.045, 0.28, 0.045, 0x35483f, 0.2, 1.02, 0, robotBody);
box(0.12, 0.12, 0.12, 0xf1c64e, 0.2, 1.2, 0, robotBody);
const tokenMaterial = new THREE.MeshStandardMaterial({
color: 0x24c5af,
emissive: 0x0b6f63,
emissiveIntensity: 0.65,
roughness: 0.38
});
const tokenMeshes = [];
for (let row = 0; row < 2; row++) {
for (let column = 0; column < 6; column++) {
const token = box(
0.085,
0.085,
0.085,
0x24c5af,
-0.29 + column * 0.116,
1.17 + row * 0.13,
0,
robotBody
);
token.material = tokenMaterial;
token.castShadow = false;
tokenMeshes.push(token);
}
}
// ---------- Camera controls, implemented directly ----------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
0.35,
1.25
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.7,
1.8
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------- Route and simulation UI ----------
function renderRouteCards() {
const results = routes.map((route) => ({
route,
result: simulate(route, budget),
minimum: minimumBudget(route)
}));
const feasible = results.filter((item) => item.result.feasible).length;
feasibleCount.textContent = `${feasible} FEASIBLE`;
routeList.innerHTML = results
.map(({ route, result, minimum }) => {
const stops = chargingSteps(route);
const stopText = stops.length
? `Charge at step${stops.length > 1 ? "s" : ""} ${stops.join(", ")}`
: "No charging stops";
return `
<button
class="route-card ${route.id === selectedRouteId ? "selected" : ""}"
style="--route-color:${route.css}"
type="button"
data-route="${route.id}"
aria-pressed="${route.id === selectedRouteId}"
>
<span class="route-swatch"></span>
<span>
<span class="route-name">${route.name}</span>
<span class="route-details">
${route.path.length - 1} steps · ${result.chargeStops} charge stop${result.chargeStops === 1 ? "" : "s"}<br>
${stopText}
</span>
<span class="route-minimum">MIN. START: ${minimum} TOKENS</span>
</span>
<span class="route-badge ${result.feasible ? "feasible" : "infeasible"}">
${result.feasible ? "FEASIBLE" : "OVER BUDGET"}
</span>
</button>
`;
})
.join("");
routeList.querySelectorAll("[data-route]").forEach((button) => {
button.addEventListener("click", () => {
selectRoute(button.dataset.route);
});
});
}
function updateRouteVisuals() {
routes.forEach((route) => {
const visual = routeVisuals.get(route.id);
const selected = route.id === selectedRouteId;
visual.group.position.y = selected ? 0.035 : 0;
visual.material.opacity = selected ? 0.96 : 0.15;
visual.material.emissiveIntensity = selected ? 0.38 : 0.04;
visual.material.depthWrite = selected;
});
}
function placeRobotAt(tile) {
robotRoot.position.set(worldX(tile.x), 0.285, worldZ(tile.z));
robotRoot.rotation.y = 0;
robotBody.position.y = 0;
}
function showPredictedFailure(route, result) {
failureMarker.visible = !result.feasible && result.failStep !== null;
if (failureMarker.visible) {
const tile = route.path[result.failStep];
failureMarker.position.set(worldX(tile.x), 0, worldZ(tile.z));
}
}
function updateHUD() {
const route = selectedRoute();
const result = simulate(route, budget);
const currentEnergy = runner ? runner.energy : budget;
const currentStep = runner ? runner.current : 0;
const total = route.path.length - 1;
const stops = chargingSteps(route);
let nextStop = stops.find((step) => step > currentStep);
budgetValue.textContent = String(budget);
budgetInput.style.setProperty(
"--pct",
`${((budget - Number(budgetInput.min)) /
(Number(budgetInput.max) - Number(budgetInput.min))) *
100}%`
);
budgetHint.textContent = result.feasible
? `This route can finish with ${result.energy} token${result.energy === 1 ? "" : "s"} remaining.`
: `Budget runs out near step ${result.failStep + 1}. Increase the starting budget.`;
activeRouteName.textContent = route.name;
activeRouteMeta.textContent =
`${total} tile steps · ${result.chargeStops} charging stop${result.chargeStops === 1 ? "" : "s"}`;
sceneStatus.textContent = result.feasible ? "FEASIBLE" : "NOT FEASIBLE";
sceneStatus.className = `scene-status ${result.feasible ? "ok" : "bad"}`;
energyValue.textContent = String(currentEnergy);
stepValue.textContent = String(currentStep);
totalSteps.textContent = String(total);
nextChargeValue.textContent =
nextStop === undefined ? "No more stops" : `Step ${nextStop}`;
[...energySlots.children].forEach((slot, index) => {
slot.classList.toggle("active", index < currentEnergy);
slot.classList.toggle(
"flash",
Boolean(runner?.chargeFlash > 0 && index < currentEnergy)
);
});
if (runner?.state === "running") {
runButton.textContent = "Running...";
runButton.disabled = true;
} else if (runner?.state === "complete") {
runButton.textContent = "Run again";
runButton.disabled = false;
} else if (runner?.state === "failed") {
runButton.textContent = "Run again";
runButton.disabled = false;
} else {
runButton.textContent = "Run selected route";
runButton.disabled = !result.feasible;
}
}
function resetRunner(message = "Ready · Select a route and run the robot.") {
runner = null;
failureMarker.visible = false;
placeRobotAt(selectedRoute().path[0]);
const route = selectedRoute();
const result = simulate(route, budget);
showPredictedFailure(route, result);
tokenMeshes.forEach((token) => {
token.visible = true;
});
tokenMaterial.color.setHex(0x24c5af);
tokenMaterial.emissive.setHex(0x0b6f63);
updateHUD();
setEvent(message);
}
function selectRoute(id) {
selectedRouteId = id;
renderRouteCards();
updateRouteVisuals();
resetRunner(`Route preview · ${selectedRoute().name}`);
}
function triggerCharger(tile) {
const visual = chargerVisuals.get(`${tile.x},${tile.z}`);
if (visual) visual.flash = 1;
}
function startRoute() {
const route = selectedRoute();
const result = simulate(route, budget);
if (!result.feasible) {
setEvent(
`Cannot run · budget fails near step ${result.failStep + 1}.`
);
return;
}
failureMarker.visible = false;
placeRobotAt(route.path[0]);
runner = {
state: "running",
current: 0,
progress: 0,
energy: budget,
pause: 0.22,
chargeFlash: 0,
route
};
setEvent(`Running ${route.name} · one token per tile step.`);
updateHUD();
}
function angleLerp(from, to, amount) {
const difference = Math.atan2(Math.sin(to - from), Math.cos(to - from));
return from + difference * amount;
}
function updateRunner(delta) {
if (!runner || runner.state !== "running") return;
runner.chargeFlash = Math.max(0, runner.chargeFlash - delta * 2.5);
if (runner.pause > 0) {
runner.pause -= delta;
robotBody.position.y = Math.sin(performance.now() * 0.004) * 0.008;
return;
}
const from = runner.route.path[runner.current];
const to = runner.route.path[runner.current + 1];
runner.progress = Math.min(1, runner.progress + delta / STEP_DURATION);
const eased = runner.progress * runner.progress * (3 - 2 * runner.progress);
robotRoot.position.x = THREE.MathUtils.lerp(
worldX(from.x),
worldX(to.x),
eased
);
robotRoot.position.z = THREE.MathUtils.lerp(
worldZ(from.z),
worldZ(to.z),
eased
);
const targetYaw = Math.atan2(to.x - from.x, to.z - from.z);
robotRoot.rotation.y = angleLerp(
robotRoot.rotation.y,
targetYaw,
1 - Math.exp(-delta * 12)
);
robotBody.position.y = Math.sin(runner.progress * Math.PI) * 0.075;
const legSwing = Math.sin(runner.progress * Math.PI * 2) * 0.22;
leftLeg.rotation.x = legSwing;
rightLeg.rotation.x = -legSwing;
if (runner.progress < 1) return;
runner.current += 1;
runner.progress = 0;
runner.energy -= 1;
const arrivedTile = runner.route.path[runner.current];
if (arrivedTile.charger) {
runner.energy = Math.min(CAPACITY, runner.energy + CHARGE_AMOUNT);
runner.chargeFlash = 1;
triggerCharger(arrivedTile);
setEvent(
`Charging stop · step ${runner.current} · +${CHARGE_AMOUNT} tokens.`
);
}
if (runner.energy <= 0 && runner.current < runner.route.path.length - 1) {
runner.state = "failed";
failureMarker.visible = true;
failureMarker.position.set(
worldX(arrivedTile.x),
0,
worldZ(arrivedTile.z)
);
setEvent(
`Out of energy at step ${runner.current}. The robot cannot move again.`
);
updateHUD();
return;
}
if (runner.current === runner.route.path.length - 1) {
runner.state = "complete";
robotBody.position.y = 0;
setEvent(
`Goal reached · ${runner.energy} token${runner.energy === 1 ? "" : "s"} remaining.`
);
updateHUD();
return;
}
runner.pause = arrivedTile.charger ? 0.78 : 0.13;
updateHUD();
}
budgetInput.addEventListener("input", () => {
budget = Number(budgetInput.value);
renderRouteCards();
resetRunner(
`Budget updated · ${budget} starting tokens. Route feasibility recalculated.`
);
});
runButton.addEventListener("click", startRoute);
resetButton.addEventListener("click", () => {
renderRouteCards();
updateRouteVisuals();
resetRunner("Robot reset · ready at the start tile.");
});
// ---------- Animation loop ----------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateRunner(delta);
chargerVisuals.forEach((visual) => {
visual.flash = Math.max(0, visual.flash - delta * 1.8);
visual.floating.position.y = 1.25 + Math.sin(time * 2.1 + visual.phase) * 0.055;
visual.floating.rotation.y = time * 0.65 + visual.phase;
visual.material.emissiveIntensity = 0.25 + visual.flash * 1.8;
visual.material.color.setHex(visual.flash > 0 ? 0xffe28a : 0xf1c64e);
});
goalBeacon.rotation.y = time * 0.8;
goalBeacon.position.y = 1.85 + Math.sin(time * 2.3) * 0.055;
const flashAmount = runner?.chargeFlash || 0;
tokenMaterial.color.lerpColors(
new THREE.Color(0x24c5af),
new THREE.Color(0xf1c64e),
flashAmount
);
tokenMaterial.emissive.lerpColors(
new THREE.Color(0x0b6f63),
new THREE.Color(0x9b761b),
flashAmount
);
renderer.render(scene, camera);
}
updateCamera();
resizeRenderer();
renderRouteCards();
updateRouteVisuals();
resetRunner("Ready · Select a route and run the robot.");
animate();
</script>
</body>
</html>
```