Prompt specification
Build or customize the Three.js 3D scene "Solar Survey Rover Game" 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: Solar Survey Rover Game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/solar-survey-rover-game-x3539
Scene Description Prompt:
Build a fictional voxel rover game with shaded terrain, recharge pads, sample goals, and explicit toy energy costs. Three.js from cdnjs, one HTML file.
Working Three.js Starter (solar-survey-rover-game-x3539):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Voxel Rover: Pebble-9 Survey</title>
<style>
:root {
--ink: #102a31;
--panel: rgba(247, 251, 238, 0.91);
--line: rgba(16, 42, 49, 0.18);
--mint: #36d6a3;
--pink: #ff4fa3;
--cyan: #43d9ff;
--yellow: #ffce4b;
--shadow: 0 12px 32px rgba(20, 57, 64, 0.18);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9e6eb;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
touch-action: none;
}
button {
font: inherit;
cursor: pointer;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 48%, rgba(23, 70, 78, 0.12) 100%),
linear-gradient(to bottom, rgba(255,255,255,0.08), transparent 30%);
z-index: 1;
}
#hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.panel {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.eyebrow {
font: 800 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
opacity: 0.64;
}
.brand {
position: absolute;
top: 18px;
left: 18px;
width: 250px;
padding: 15px 16px;
border-radius: 16px;
}
.brand-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 6px;
}
.brand-icon {
width: 34px;
height: 34px;
display: grid;
place-items: center;
border-radius: 9px;
background: var(--ink);
color: var(--yellow);
font-size: 20px;
transform: rotate(-4deg);
box-shadow: 3px 3px 0 rgba(255, 206, 75, 0.45);
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1;
letter-spacing: -0.02em;
}
.brand p {
margin: 5px 0 0;
font-size: 11px;
opacity: 0.68;
}
.mission {
position: absolute;
top: 129px;
left: 18px;
width: 270px;
padding: 14px 16px;
border-radius: 15px;
}
.mission-heading {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.mission h2 {
margin: 0;
font-size: 14px;
}
#missionCount {
font: 800 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
padding: 6px 8px;
border-radius: 8px;
background: rgba(16, 42, 49, 0.08);
}
#missionText {
min-height: 34px;
margin: 10px 0 12px;
font-size: 12px;
line-height: 1.45;
color: rgba(16, 42, 49, 0.76);
}
#sampleDots {
display: flex;
gap: 6px;
}
.sample-dot {
height: 7px;
flex: 1;
border-radius: 99px;
background: rgba(16, 42, 49, 0.13);
overflow: hidden;
position: relative;
}
.sample-dot::after {
content: "";
position: absolute;
inset: 0;
transform: scaleX(0);
transform-origin: left;
background: var(--pink);
transition: transform 0.35s ease;
}
.sample-dot.done::after { transform: scaleX(1); }
.battery {
position: absolute;
top: 18px;
right: 18px;
width: 236px;
padding: 14px 15px;
border-radius: 16px;
}
.battery-top {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
}
#energyValue {
font: 900 29px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.06em;
}
.battery-max {
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
opacity: 0.5;
margin-left: 3px;
}
.battery-track {
height: 13px;
margin-top: 10px;
padding: 3px;
border-radius: 5px;
background: rgba(16, 42, 49, 0.11);
overflow: hidden;
}
#batteryFill {
width: 87.5%;
height: 100%;
border-radius: 3px;
background:
repeating-linear-gradient(
90deg,
transparent 0,
transparent 11px,
rgba(247, 251, 238, 0.4) 11px,
rgba(247, 251, 238, 0.4) 13px
),
linear-gradient(90deg, #23b887, #52e4a9);
transition: width 0.2s ease, filter 0.15s ease;
}
#batteryFill.low {
background:
repeating-linear-gradient(
90deg,
transparent 0,
transparent 11px,
rgba(247, 251, 238, 0.4) 11px,
rgba(247, 251, 238, 0.4) 13px
),
linear-gradient(90deg, #ec7955, #ffbd53);
}
.battery.flash #batteryFill { filter: brightness(1.45); }
.cost-panel {
position: absolute;
top: 161px;
right: 18px;
width: 236px;
padding: 13px 14px;
border-radius: 15px;
}
.cost-title {
margin-bottom: 8px;
}
.cost-row {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 5px 0;
border-top: 1px solid rgba(16, 42, 49, 0.08);
font: 700 10px/1.25 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
.cost-row span { opacity: 0.68; }
.cost-row b { white-space: nowrap; }
.cost-row.charge b { color: #008ca8; }
.cost-row.deliver b { color: #18835f; }
.map-panel {
position: absolute;
right: 18px;
bottom: 18px;
width: 184px;
padding: 10px;
border-radius: 16px;
}
.map-header {
display: flex;
justify-content: space-between;
margin: 0 2px 8px;
}
#minimap {
display: block;
width: 162px;
height: 162px;
border-radius: 10px;
background: #d7e8d1;
image-rendering: pixelated;
}
.map-legend {
display: flex;
flex-wrap: wrap;
gap: 5px 9px;
margin-top: 9px;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.map-legend i {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 4px;
border-radius: 2px;
vertical-align: -1px;
}
.controls {
position: absolute;
left: 18px;
bottom: 18px;
width: 270px;
padding: 12px 14px;
border-radius: 15px;
}
.control-line {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
font-size: 11px;
opacity: 0.74;
}
kbd {
min-width: 25px;
height: 24px;
display: inline-grid;
place-items: center;
padding: 0 6px;
border: 1px solid rgba(16, 42, 49, 0.2);
border-bottom-width: 3px;
border-radius: 6px;
background: rgba(255, 255, 255, 0.65);
color: var(--ink);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#prompt {
position: absolute;
left: 50%;
bottom: 24px;
transform: translate(-50%, 15px);
max-width: min(520px, calc(100vw - 32px));
padding: 11px 16px;
border-radius: 12px;
background: rgba(16, 42, 49, 0.91);
color: #f7fbea;
box-shadow: 0 10px 28px rgba(16, 42, 49, 0.26);
font: 800 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
opacity: 0;
transition: opacity 0.18s ease, transform 0.18s ease;
}
#prompt.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast {
position: absolute;
left: 50%;
top: 24px;
transform: translate(-50%, -12px);
padding: 10px 15px;
border-radius: 10px;
background: #fff7cc;
border: 1px solid rgba(16, 42, 49, 0.12);
box-shadow: var(--shadow);
font: 800 11px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
opacity: 0;
transition: opacity 0.18s ease, transform 0.18s ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#worldLabels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
transform: translate(-50%, -100%);
padding: 6px 8px;
border-radius: 7px;
white-space: nowrap;
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
box-shadow: 0 5px 12px rgba(16, 42, 49, 0.18);
border: 1px solid rgba(255,255,255,0.5);
}
.world-label.sample {
color: #8c164e;
background: rgba(255, 215, 235, 0.93);
}
.world-label.pad {
color: #006b83;
background: rgba(203, 246, 255, 0.94);
}
.world-label.base {
color: #0b6546;
background: rgba(214, 255, 228, 0.94);
}
.overlay {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
padding: 20px;
background: rgba(47, 91, 97, 0.28);
backdrop-filter: blur(7px);
-webkit-backdrop-filter: blur(7px);
transition: opacity 0.25s ease;
}
.overlay.hidden {
opacity: 0;
pointer-events: none;
}
.modal {
width: min(520px, 100%);
padding: 28px;
border-radius: 24px;
background: #f7fbea;
border: 1px solid rgba(255,255,255,0.9);
box-shadow: 0 28px 80px rgba(16, 42, 49, 0.28);
}
.modal h2 {
margin: 9px 0 8px;
max-width: 430px;
font-size: clamp(30px, 7vw, 49px);
line-height: 0.96;
letter-spacing: -0.055em;
}
.modal p {
margin: 0;
max-width: 430px;
color: rgba(16, 42, 49, 0.72);
font-size: 13px;
line-height: 1.55;
}
.modal-costs {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 8px;
margin: 20px 0;
}
.modal-cost {
padding: 10px 11px;
border-radius: 11px;
background: rgba(16, 42, 49, 0.06);
display: flex;
justify-content: space-between;
gap: 8px;
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.modal-cost b {
color: #bc3e32;
}
.modal-cost.charge b { color: #008ca8; }
.modal-cost.deliver b { color: #18835f; }
#primaryButton {
width: 100%;
padding: 15px 18px;
border: 0;
border-radius: 12px;
background: var(--ink);
color: #fff8c8;
box-shadow: 4px 5px 0 #ffce4b;
font: 900 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
transition: transform 0.12s ease, box-shadow 0.12s ease;
}
#primaryButton:hover {
transform: translate(-1px, -1px);
box-shadow: 6px 7px 0 #ffce4b;
}
#primaryButton:active {
transform: translate(2px, 3px);
box-shadow: 2px 2px 0 #ffce4b;
}
.modal-foot {
margin-top: 17px;
text-align: center;
font: 700 9px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
opacity: 0.55;
}
#touchControls {
position: absolute;
left: 18px;
bottom: 18px;
display: none;
grid-template-columns: repeat(3, 48px);
grid-template-rows: repeat(2, 48px);
gap: 5px;
pointer-events: auto;
}
#touchControls button {
border: 1px solid rgba(255,255,255,0.8);
border-radius: 12px;
background: rgba(247,251,238,0.9);
color: var(--ink);
box-shadow: var(--shadow);
font: 900 16px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
user-select: none;
-webkit-user-select: none;
touch-action: none;
}
#touchControls button:active {
background: #ffdf73;
transform: translateY(2px);
}
.touch-up { grid-column: 2; grid-row: 1; }
.touch-left { grid-column: 1; grid-row: 2; }
.touch-down { grid-column: 2; grid-row: 2; }
.touch-right { grid-column: 3; grid-row: 2; }
@media (max-width: 780px) {
.brand { width: 205px; }
.mission { top: 125px; width: 220px; }
.battery { width: 188px; }
.cost-panel { top: 157px; width: 188px; }
.map-panel { display: none; }
.controls { display: none; }
#touchControls { display: grid; }
#prompt { bottom: 132px; }
}
@media (max-width: 520px) {
.brand { left: 10px; top: 10px; width: 185px; padding: 12px; }
.brand p { display: none; }
.mission { left: 10px; top: 91px; width: 185px; padding: 12px; }
.mission .eyebrow { font-size: 8px; }
.mission h2 { font-size: 12px; }
#missionText { font-size: 10px; min-height: 28px; }
.battery { right: 10px; top: 10px; width: 142px; padding: 11px; }
#energyValue { font-size: 23px; }
.cost-panel { right: 10px; top: 122px; width: 142px; padding: 10px; }
.cost-title { display: none; }
.cost-row { font-size: 8px; padding: 4px 0; }
#prompt { bottom: 130px; width: calc(100vw - 20px); }
.modal { padding: 22px; }
.modal-costs { gap: 6px; }
.modal-cost { font-size: 8px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Voxel rover game"></canvas>
<div id="vignette"></div>
<div id="worldLabels"></div>
<div id="hud">
<section class="brand panel">
<div class="eyebrow">Pebble-9 field unit</div>
<div class="brand-row">
<div class="brand-icon">◆</div>
<div>
<h1>VOXEL ROVER</h1>
<p>Surveyor-7 / toy power unit</p>
</div>
</div>
</section>
<section class="mission panel">
<div class="mission-heading">
<div>
<div class="eyebrow">Active objective</div>
<h2>Recover the pink samples</h2>
</div>
<div id="missionCount">0 / 3</div>
</div>
<div id="missionText">Locate three pink sample goals, then bring them back to the green home dock.</div>
<div id="sampleDots">
<div class="sample-dot"></div>
<div class="sample-dot"></div>
<div class="sample-dot"></div>
</div>
</section>
<section class="battery panel" id="batteryPanel">
<div class="battery-top">
<div>
<div class="eyebrow">Toy energy</div>
<div style="margin-top:4px">
<span id="energyValue">28</span><span class="battery-max">/ 32</span>
</div>
</div>
<div style="font-size:22px">▰</div>
</div>
<div class="battery-track"><div id="batteryFill"></div></div>
</section>
<section class="cost-panel panel">
<div class="eyebrow cost-title">Explicit toy energy costs</div>
<div class="cost-row"><span>DRIVE</span><b>−1 / CELL</b></div>
<div class="cost-row"><span>EXTRACT</span><b>−4 / SAMPLE</b></div>
<div class="cost-row charge"><span>CHARGE PAD</span><b>+8 / PAD</b></div>
<div class="cost-row deliver"><span>DELIVER</span><b>0 ENERGY</b></div>
<div class="cost-row charge"><span>TOW HOME</span><b>+8 / R</b></div>
</section>
<section class="map-panel panel">
<div class="map-header">
<span class="eyebrow">Local map</span>
<span class="eyebrow">N ↑</span>
</div>
<canvas id="minimap" width="230" height="230"></canvas>
<div class="map-legend">
<span><i style="background:#ff4fa3"></i>SAMPLE</span>
<span><i style="background:#43d9ff"></i>CHARGE</span>
<span><i style="background:#36d6a3"></i>HOME</span>
<span><i style="background:#ffce4b"></i>ROVER</span>
</div>
</section>
<section class="controls panel">
<div class="eyebrow">Field controls</div>
<div class="control-line">
<kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd>
<span>or arrows: drive on the map grid</span>
</div>
<div class="control-line">
<kbd>E</kbd><span>extract sample / deliver at home</span>
</div>
<div class="control-line">
<kbd>R</kbd><span>tow home (+8 energy)</span>
<kbd>C</kbd><span>reset camera</span>
</div>
<div class="control-line">
<span>Drag the world to orbit · scroll to zoom</span>
</div>
</section>
<div id="prompt"></div>
<div id="toast"></div>
<div id="touchControls">
<button class="touch-up" data-key="w" aria-label="Drive north">▲</button>
<button class="touch-left" data-key="a" aria-label="Drive west">◀</button>
<button class="touch-down" data-key="s" aria-label="Drive south">▼</button>
<button class="touch-right" data-key="d" aria-label="Drive east">▶</button>
</div>
</div>
<div class="overlay" id="overlay">
<div class="modal">
<div class="eyebrow">A very small planetary rover</div>
<h2 id="modalTitle">Pocket planet. Big mission.</h2>
<p id="modalCopy">
Surveyor-7 has landed on Pebble-9. Find all three pink samples,
extract them for 4 toy-energy each, and return them to the green home dock.
</p>
<div class="modal-costs" id="modalCosts">
<div class="modal-cost"><span>DRIVE / CELL</span><b>−1</b></div>
<div class="modal-cost"><span>EXTRACT / SAMPLE</span><b>−4</b></div>
<div class="modal-cost charge"><span>CHARGE / PAD</span><b>+8</b></div>
<div class="modal-cost deliver"><span>DELIVER</span><b>0</b></div>
</div>
<button id="primaryButton">DEPLOY ROVER</button>
<div class="modal-foot">
W A S D / ARROWS TO DRIVE · E TO INTERACT · DRAG TO ORBIT<br>
Fictional science. Extremely serious tiny wheels.
</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";
// -------------------------------------------------------------------------
// Core scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9e6eb);
scene.fog = new THREE.Fog(0xb9e6eb, 38, 78);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
150
);
const cameraState = {
azimuth: 0.72,
elevation: 0.78,
radius: 28,
target: new THREE.Vector3(0, 0.7, 0)
};
const clock = new THREE.Clock();
const GRID = 23;
const HALF = Math.floor(GRID / 2);
const MAX_ENERGY = 32;
const START_ENERGY = 28;
const energyEl = document.querySelector("#energyValue");
const batteryFillEl = document.querySelector("#batteryFill");
const batteryPanelEl = document.querySelector("#batteryPanel");
const missionCountEl = document.querySelector("#missionCount");
const missionTextEl = document.querySelector("#missionText");
const sampleDotEls = [...document.querySelectorAll(".sample-dot")];
const promptEl = document.querySelector("#prompt");
const toastEl = document.querySelector("#toast");
const worldLabelsEl = document.querySelector("#worldLabels");
const minimap = document.querySelector("#minimap");
const minimapCtx = minimap.getContext("2d");
const overlayEl = document.querySelector("#overlay");
const modalTitleEl = document.querySelector("#modalTitle");
const modalCopyEl = document.querySelector("#modalCopy");
const modalCostsEl = document.querySelector("#modalCosts");
const primaryButtonEl = document.querySelector("#primaryButton");
const hemisphere = new THREE.HemisphereLight(0xeafcff, 0x64836e, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.1);
sun.position.set(-13, 24, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x73d9ff, 0.8);
fillLight.position.set(15, 8, -14);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Terrain and game data
// -------------------------------------------------------------------------
let randomSeed = 91357;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const base = { x: 0, z: 0, name: "HOME DOCK" };
const samples = [
{ id: 1, x: -6, z: -5, status: "active" },
{ id: 2, x: 6, z: -4, status: "active" },
{ id: 3, x: 5, z: 6, status: "active" }
];
const pads = [
{ id: 1, x: -3, z: -3 },
{ id: 2, x: 2, z: -2 },
{ id: 3, x: 7, z: 1 },
{ id: 4, x: 1, z: 4 },
{ id: 5, x: -4, z: 2 }
];
const featureCells = [base, ...samples, ...pads];
const levels = new Map();
const terrainCells = [];
function cellKey(x, z) {
return `${x},${z}`;
}
function inBounds(x, z) {
return x >= -HALF && x <= HALF && z >= -HALF && z <= HALF;
}
for (let z = -HALF; z <= HALF; z++) {
for (let x = -HALF; x <= HALF; x++) {
let level = 0;
const chance = random();
if (chance < 0.19) level = 1;
if (Math.max(Math.abs(x), Math.abs(z)) === HALF && random() < 0.42) level = 2;
if (Math.hypot(x + 7, z - 6) < 2.5) level = 2;
if (Math.hypot(x - 7, z + 6) < 2.1) level = 1;
levels.set(cellKey(x, z), level);
terrainCells.push({ x, z, level });
}
}
// Flatten all important locations and their immediate approach.
for (const feature of featureCells) {
levels.set(cellKey(feature.x, feature.z), 0);
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
const key = cellKey(feature.x + dx, feature.z + dz);
if (levels.has(key)) {
levels.set(key, Math.min(levels.get(key), 1));
}
}
}
}
function levelAt(x, z) {
return levels.get(cellKey(x, z)) ?? 0;
}
function terrainTop(x, z) {
return levelAt(x, z) + 1;
}
// -------------------------------------------------------------------------
// Voxel terrain
// -------------------------------------------------------------------------
const terrainTopColors = [
new THREE.Color(0x86bd78),
new THREE.Color(0x9ac982),
new THREE.Color(0x75ad78),
new THREE.Color(0xb1c887),
new THREE.Color(0x8fb675)
];
const dirtColors = [
new THREE.Color(0x9b795d),
new THREE.Color(0x896b55),
new THREE.Color(0x765e4f)
];
const blockInstances = [];
for (const cell of terrainCells) {
const topColor = terrainTopColors[Math.floor(random() * terrainTopColors.length)];
const cellTint = 0.91 + random() * 0.16;
for (let layer = 0; layer <= cell.level; layer++) {
const color = (layer === cell.level ? topColor : dirtColors[layer % dirtColors.length])
.clone()
.multiplyScalar(cellTint);
blockInstances.push({
x: cell.x,
y: layer + 0.5,
z: cell.z,
color
});
}
}
const blockGeometry = new THREE.BoxGeometry(0.985, 0.985, 0.985);
const blockMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.98,
metalness: 0,
flatShading: true
});
const terrainMesh = new THREE.InstancedMesh(
blockGeometry,
blockMaterial,
blockInstances.length
);
terrainMesh.castShadow = true;
terrainMesh.receiveShadow = true;
terrainMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const dummy = new THREE.Object3D();
blockInstances.forEach((block, index) => {
dummy.position.set(block.x, block.y, block.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
terrainMesh.setMatrixAt(index, dummy.matrix);
terrainMesh.setColorAt(index, block.color);
});
terrainMesh.instanceMatrix.needsUpdate = true;
terrainMesh.instanceColor.needsUpdate = true;
scene.add(terrainMesh);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(160, 160),
new THREE.MeshStandardMaterial({
color: 0xbad7bb,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Material and object helpers
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
function makeBox(parent, size, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Home dock
// -------------------------------------------------------------------------
const baseGroup = new THREE.Group();
baseGroup.position.set(base.x, terrainTop(base.x, base.z), base.z);
scene.add(baseGroup);
base.group = baseGroup;
const baseGreen = standardMaterial(0x36d6a3, {
emissive: 0x0a5f42,
emissiveIntensity: 0.45
});
const darkMaterial = standardMaterial(0x18333a);
const whiteMaterial = standardMaterial(0xf4f5dc);
const yellowMaterial = standardMaterial(0xffce4b);
const blueMaterial = standardMaterial(0x4ac9eb, {
emissive: 0x0b5b72,
emissiveIntensity: 0.35
});
makeBox(baseGroup, [1.5, 0.12, 1.5], baseGreen, 0, 0.06, 0);
makeBox(baseGroup, [0.18, 1.15, 0.18], darkMaterial, 0.48, 0.62, -0.48);
makeBox(baseGroup, [0.72, 0.4, 0.09], baseGreen, 0.76, 1.04, -0.48);
makeBox(baseGroup, [0.28, 0.12, 0.28], yellowMaterial, -0.48, 0.16, 0.48);
const baseBeacon = makeBox(baseGroup, [0.25, 0.25, 0.25], yellowMaterial, 0.48, 1.27, -0.48);
// -------------------------------------------------------------------------
// Sample goals
// -------------------------------------------------------------------------
const pinkMaterial = standardMaterial(0xff4fa3, {
emissive: 0xa31461,
emissiveIntensity: 0.7,
roughness: 0.45
});
const palePinkMaterial = standardMaterial(0xffa6ce, {
emissive: 0x8e174f,
emissiveIntensity: 0.45,
roughness: 0.5
});
const beamMaterial = new THREE.MeshBasicMaterial({
color: 0xff72b8,
transparent: true,
opacity: 0.13,
depthWrite: false
});
for (const sample of samples) {
const group = new THREE.Group();
const top = terrainTop(sample.x, sample.z);
group.position.set(sample.x, top, sample.z);
scene.add(group);
makeBox(group, [0.44, 0.66, 0.44], pinkMaterial, 0, 0.44, 0);
makeBox(group, [0.27, 0.42, 0.27], palePinkMaterial, 0.28, 0.27, 0.05);
makeBox(group, [0.26, 0.36, 0.26], palePinkMaterial, -0.22, 0.24, -0.1);
makeBox(group, [0.22, 0.28, 0.22], pinkMaterial, 0.08, 0.9, 0);
const beam = makeBox(group, [0.12, 2.2, 0.12], beamMaterial, 0, 1.05, 0);
beam.castShadow = false;
beam.receiveShadow = false;
sample.group = group;
sample.baseY = top;
}
// -------------------------------------------------------------------------
// Recharge pads
// -------------------------------------------------------------------------
for (const pad of pads) {
const group = new THREE.Group();
group.position.set(pad.x, terrainTop(pad.x, pad.z), pad.z);
scene.add(group);
const platformMaterial = standardMaterial(0x43d9ff, {
emissive: 0x007b99,
emissiveIntensity: 0.65
});
const platform = makeBox(group, [1.15, 0.12, 1.15], platformMaterial, 0, 0.06, 0);
const plus = new THREE.Group();
group.add(plus);
makeBox(plus, [0.58, 0.09, 0.15], whiteMaterial, 0, 0.16, 0);
makeBox(plus, [0.15, 0.09, 0.58], whiteMaterial, 0, 0.16, 0);
const beam = makeBox(
group,
[0.08, 1.45, 0.08],
new THREE.MeshBasicMaterial({
color: 0x43d9ff,
transparent: true,
opacity: 0.2,
depthWrite: false
}),
0,
0.74,
0
);
beam.castShadow = false;
pad.group = group;
pad.platform = platform;
pad.platformMaterial = platformMaterial;
pad.plus = plus;
pad.beam = beam;
}
// -------------------------------------------------------------------------
// Decorative voxel trees and clouds
// -------------------------------------------------------------------------
const trunkMaterial = standardMaterial(0x80583f);
const leafMaterials = [
standardMaterial(0x4d9c67),
standardMaterial(0x5eae70),
standardMaterial(0x3d865f)
];
function nearFeature(x, z, radius = 3) {
return featureCells.some(feature => Math.hypot(feature.x - x, feature.z - z) < radius);
}
let treesMade = 0;
for (let attempt = 0; attempt < 100 && treesMade < 15; attempt++) {
const x = Math.floor(random() * (GRID - 4)) - (GRID - 4) / 2;
const z = Math.floor(random() * (GRID - 4)) - (GRID - 4) / 2;
if (!inBounds(x, z) || nearFeature(x, z, 3.5)) continue;
const group = new THREE.Group();
group.position.set(x, terrainTop(x, z), z);
scene.add(group);
makeBox(group, [0.22, 0.85, 0.22], trunkMaterial, 0, 0.42, 0);
makeBox(group, [0.95, 0.62, 0.95], leafMaterials[treesMade % leafMaterials.length], 0, 1.03, 0);
makeBox(group, [0.58, 0.42, 0.58], leafMaterials[(treesMade + 1) % leafMaterials.length], 0.18, 1.48, -0.08);
makeBox(group, [0.48, 0.38, 0.48], leafMaterials[(treesMade + 2) % leafMaterials.length], -0.2, 1.4, 0.12);
treesMade++;
}
const cloudMaterial = new THREE.MeshLambertMaterial({
color: 0xf8fff3,
transparent: true,
opacity: 0.82
});
const clouds = [];
for (let i = 0; i < 5; i++) {
const group = new THREE.Group();
group.position.set(-20 + i * 10, 11 + random() * 3, -18 + random() * 32);
makeBox(group, [2.1, 0.7, 1.0], cloudMaterial, 0, 0, 0);
makeBox(group, [1.1, 0.9, 1.1], cloudMaterial, -0.8, 0.18, 0);
makeBox(group, [1.3, 0.8, 1.0], cloudMaterial, 0.95, 0.1, 0.1);
group.traverse(child => {
if (child.isMesh) {
child.castShadow = false;
child.receiveShadow = false;
}
});
scene.add(group);
clouds.push(group);
}
// -------------------------------------------------------------------------
// Rover
// -------------------------------------------------------------------------
const rover = new THREE.Group();
rover.position.set(base.x, terrainTop(base.x, base.z), base.z);
scene.add(rover);
const roverYellow = standardMaterial(0xffc84d, {
roughness: 0.65
});
const roverCream = standardMaterial(0xf7efd0);
const roverDark = standardMaterial(0x18343c);
const roverWindow = standardMaterial(0x54d4e7, {
emissive: 0x0c6475,
emissiveIntensity: 0.35,
roughness: 0.35
});
const tireMaterial = standardMaterial(0x17262b);
makeBox(rover, [0.88, 0.26, 1.08], roverYellow, 0, 0.42, 0);
makeBox(rover, [0.66, 0.12, 0.78], roverCream, 0, 0.61, 0.02);
makeBox(rover, [0.58, 0.42, 0.58], roverWindow, 0, 0.87, -0.13);
makeBox(rover, [0.68, 0.08, 0.43], blueMaterial, 0, 1.12, 0.18);
makeBox(rover, [0.12, 0.58, 0.12], roverDark, 0.28, 1.25, -0.34);
makeBox(rover, [0.18, 0.18, 0.18], yellowMaterial, 0.28, 1.58, -0.34);
makeBox(rover, [0.18, 0.12, 0.18], roverWindow, 0, 0.68, 0.57);
const wheels = [];
for (const x of [-0.49, 0.49]) {
for (const z of [-0.36, 0.36]) {
const wheel = makeBox(rover, [0.18, 0.32, 0.32], tireMaterial, x, 0.22, z);
wheels.push(wheel);
}
}
// -------------------------------------------------------------------------
// World-space DOM labels
// -------------------------------------------------------------------------
const labelEntries = [];
function createWorldLabel(text, className, type, item, offsetY) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
worldLabelsEl.appendChild(element);
labelEntries.push({ element, type, item, offsetY });
return element;
}
for (const sample of samples) {
sample.labelElement = createWorldLabel(
`◇ SAMPLE ${sample.id}`,
"sample",
"sample",
sample,
1.55
);
}
for (const pad of pads) {
pad.labelElement = createWorldLabel(
"+ CHARGE +8",
"pad",
"pad",
pad,
1.25
);
}
base.labelElement = createWorldLabel(
"⌂ HOME DOCK",
"base",
"base",
base,
1.55
);
// -------------------------------------------------------------------------
// Game state
// -------------------------------------------------------------------------
let started = false;
let mode = "intro";
let energy = START_ENERGY;
let carried = 0;
let delivered = 0;
let roverCell = { x: base.x, z: base.z };
let movement = null;
let elapsed = 0;
let toastTimer = 0;
let lastBlockedToast = 0;
let lastHudUpdate = 0;
let lastMapUpdate = 0;
const usedPads = new Set();
const pressedKeys = new Set();
const directionKeys = {
w: { x: 0, z: -1 },
arrowup: { x: 0, z: -1 },
s: { x: 0, z: 1 },
arrowdown: { x: 0, z: 1 },
a: { x: -1, z: 0 },
arrowleft: { x: -1, z: 0 },
d: { x: 1, z: 0 },
arrowright: { x: 1, z: 0 }
};
function setEnergy(value) {
energy = Math.max(0, Math.min(MAX_ENERGY, Math.round(value)));
updateEnergyUI();
batteryPanelEl.classList.remove("flash");
void batteryPanelEl.offsetWidth;
batteryPanelEl.classList.add("flash");
}
function updateEnergyUI() {
energyEl.textContent = energy;
batteryFillEl.style.width = `${(energy / MAX_ENERGY) * 100}%`;
batteryFillEl.classList.toggle("low", energy <= 8);
}
function showToast(message, duration = 2200) {
toastEl.textContent = message;
toastEl.classList.add("show");
window.clearTimeout(toastTimer);
toastTimer = window.setTimeout(() => toastEl.classList.remove("show"), duration);
}
function distanceTo(cell) {
return Math.abs(roverCell.x - cell.x) + Math.abs(roverCell.z - cell.z);
}
function updateLabelText() {
for (const sample of samples) {
if (sample.status !== "active") {
sample.labelElement.textContent = `◇ SAMPLE ${sample.id}`;
continue;
}
sample.labelElement.textContent = `◇ SAMPLE ${sample.id} · ${distanceTo(sample)}`;
}
for (const pad of pads) {
pad.labelElement.textContent = usedPads.has(pad.id)
? "+ PAD USED"
: "+ CHARGE +8";
}
base.labelElement.textContent = `⌂ HOME DOCK · ${distanceTo(base)}`;
}
function updateMission() {
missionCountEl.textContent = `${delivered} / ${samples.length}`;
sampleDotEls.forEach((dot, index) => {
dot.classList.toggle("done", index < delivered);
});
if (delivered >= samples.length) {
missionTextEl.textContent = "All samples delivered. Survey complete!";
return;
}
if (carried > 0) {
missionTextEl.textContent =
`Return ${carried} sample${carried === 1 ? "" : "s"} to the green home dock. Delivery costs 0 energy.`;
return;
}
const activeSamples = samples.filter(sample => sample.status === "active");
activeSamples.sort((a, b) => distanceTo(a) - distanceTo(b));
const nearest = activeSamples[0];
missionTextEl.textContent = nearest
? `Nearest goal: Sample ${nearest.id}, ${distanceTo(nearest)} cells away. Extraction costs 4 energy.`
: "Return the carried samples to the green home dock.";
}
function updatePrompt() {
const activeSample = samples.find(
sample => sample.status === "active" &&
sample.x === roverCell.x &&
sample.z === roverCell.z
);
if (activeSample) {
promptEl.textContent = `E — EXTRACT SAMPLE ${activeSample.id} [−4 ENERGY]`;
promptEl.classList.add("show");
return;
}
if (roverCell.x === base.x && roverCell.z === base.z && carried > 0) {
promptEl.textContent = `E — DELIVER ${carried} SAMPLE${carried === 1 ? "" : "S"} [0 ENERGY]`;
promptEl.classList.add("show");
return;
}
if (energy <= 0) {
promptEl.textContent = "TOY JUICE EMPTY — PRESS R TO TOW HOME [+8 ENERGY]";
promptEl.classList.add("show");
return;
}
promptEl.textContent = "";
promptEl.classList.remove("show");
}
function updateHud() {
updateEnergyUI();
updateMission();
updatePrompt();
updateLabelText();
}
function attemptMove(direction) {
if (!started || movement || mode !== "playing") return;
if (energy <= 0) {
const now = performance.now();
if (now - lastBlockedToast > 900) {
showToast("Out of toy juice. Press R to tow home.");
lastBlockedToast = now;
}
return;
}
const nextX = roverCell.x + direction.x;
const nextZ = roverCell.z + direction.z;
if (!inBounds(nextX, nextZ)) {
const now = performance.now();
if (now - lastBlockedToast > 700) {
showToast("Survey boundary reached.");
lastBlockedToast = now;
}
return;
}
const currentLevel = levelAt(roverCell.x, roverCell.z);
const nextLevel = levelAt(nextX, nextZ);
if (Math.abs(nextLevel - currentLevel) > 1) {
const now = performance.now();
if (now - lastBlockedToast > 700) {
showToast("Too steep for this tiny rover.");
lastBlockedToast = now;
}
return;
}
setEnergy(energy - 1);
movement = {
fromX: rover.position.x,
fromY: rover.position.y,
fromZ: rover.position.z,
toX: nextX,
toY: terrainTop(nextX, nextZ),
toZ: nextZ,
t: 0,
duration: 0.19
};
}
function handleArrival() {
const pad = pads.find(item => item.x === roverCell.x && item.z === roverCell.z);
if (pad && !usedPads.has(pad.id)) {
usedPads.add(pad.id);
setEnergy(energy + 8);
showToast(`CHARGE PAD +8 ENERGY · PAD ${pad.id} IS NOW USED`);
pad.plus.visible = false;
pad.beam.visible = false;
pad.platformMaterial.color.setHex(0x607d83);
pad.platformMaterial.emissive.setHex(0x000000);
}
if (energy <= 0) {
showToast("ENERGY EMPTY — FIND A CHARGE PAD OR PRESS R");
}
updateHud();
}
function interact() {
if (!started || mode !== "playing" || movement) return;
const sample = samples.find(
item => item.status === "active" &&
item.x === roverCell.x &&
item.z === roverCell.z
);
if (sample) {
if (energy < 4) {
showToast("Need 4 toy energy to extract this sample.");
return;
}
setEnergy(energy - 4);
sample.status = "extracted";
sample.group.visible = false;
carried++;
showToast(`SAMPLE ${sample.id} SECURED · ${carried} CARRIED`);
updateHud();
return;
}
if (roverCell.x === base.x && roverCell.z === base.z && carried > 0) {
const amount = carried;
delivered += amount;
carried = 0;
samples.forEach(item => {
if (item.status === "extracted") item.status = "delivered";
});
showToast(`${amount} SAMPLE${amount === 1 ? "" : "S"} DELIVERED · 0 ENERGY COST`);
if (delivered >= samples.length) {
winGame();
} else {
updateHud();
}
return;
}
showToast("Move onto a pink sample or the green home dock, then press E.");
}
function towHome() {
if (!started || mode !== "playing") return;
movement = null;
roverCell = { x: base.x, z: base.z };
rover.position.set(base.x, terrainTop(base.x, base.z), base.z);
setEnergy(energy + 8);
showToast("SERVICE BEACON TOW · +8 ENERGY · SAMPLES SAFE");
updateHud();
}
function winGame() {
mode = "won";
pressedKeys.clear();
promptEl.classList.remove("show");
modalTitleEl.textContent = "Pebble-9 secured!";
modalCopyEl.textContent =
`All three samples are back at the home dock. Surveyor-7 used ${elapsed.toFixed(1)} seconds ` +
"and remains a very capable toy.";
modalCostsEl.innerHTML = `
<div class="modal-cost deliver"><span>SAMPLES</span><b>3 / 3</b></div>
<div class="modal-cost"><span>TIME</span><b>${elapsed.toFixed(1)}s</b></div>
<div class="modal-cost deliver"><span>DELIVERY COST</span><b>0</b></div>
<div class="modal-cost charge"><span>ENERGY LEFT</span><b>${energy}</b></div>
`;
primaryButtonEl.textContent = "RUN ANOTHER SURVEY";
overlayEl.classList.remove("hidden");
updateHud();
}
// -------------------------------------------------------------------------
// Inputs
// -------------------------------------------------------------------------
window.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (["arrowup", "arrowdown", "arrowleft", "arrowright", " "].includes(key)) {
event.preventDefault();
}
if (key === "e" || key === " ") {
if (!event.repeat) interact();
return;
}
if (key === "r") {
if (!event.repeat) towHome();
return;
}
if (key === "c") {
resetCamera();
return;
}
if (directionKeys[key]) {
pressedKeys.add(key);
}
});
window.addEventListener("keyup", event => {
pressedKeys.delete(event.key.toLowerCase());
});
window.addEventListener("blur", () => {
pressedKeys.clear();
});
primaryButtonEl.addEventListener("click", () => {
if (mode === "intro") {
mode = "playing";
started = true;
overlayEl.classList.add("hidden");
pressedKeys.clear();
showToast("SURVEY STARTED · FIND THE PINK SAMPLES");
updateHud();
} else {
location.reload();
}
});
document.querySelectorAll("#touchControls button").forEach(button => {
const key = button.dataset.key;
button.addEventListener("pointerdown", event => {
event.preventDefault();
button.setPointerCapture(event.pointerId);
if (key === "e") {
interact();
} else {
pressedKeys.add(key);
}
});
const release = event => {
event.preventDefault();
pressedKeys.delete(key);
};
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
});
// -------------------------------------------------------------------------
// Manual camera controls — no external controls addon
// -------------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + dy * 0.004,
0.42,
1.25
);
});
function stopDragging(event) {
dragging = false;
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
15,
40
);
}, { passive: false });
function resetCamera() {
cameraState.azimuth = 0.72;
cameraState.elevation = 0.78;
cameraState.radius = 28;
cameraState.target.set(0, 0.7, 0);
}
function updateCamera() {
const horizontalRadius = cameraState.radius * Math.cos(cameraState.elevation);
camera.position.set(
cameraState.target.x + Math.sin(cameraState.azimuth) * horizontalRadius,
cameraState.target.y + Math.sin(cameraState.elevation) * cameraState.radius,
cameraState.target.z + Math.cos(cameraState.azimuth) * horizontalRadius
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// Minimap
// -------------------------------------------------------------------------
function drawMinimap() {
const ctx = minimapCtx;
const size = minimap.width;
const margin = 8;
const cellSize = (size - margin * 2) / GRID;
ctx.clearRect(0, 0, size, size);
ctx.fillStyle = "#d8e8d1";
ctx.fillRect(0, 0, size, size);
for (const cell of terrainCells) {
const px = margin + (cell.x + HALF) * cellSize;
const py = margin + (cell.z + HALF) * cellSize;
if (cell.level === 0) ctx.fillStyle = "#a7c987";
else if (cell.level === 1) ctx.fillStyle = "#7eaf77";
else ctx.fillStyle = "#668f69";
ctx.fillRect(px, py, cellSize + 0.4, cellSize + 0.4);
}
function mapPosition(x, z) {
return {
x: margin + (x + HALF + 0.5) * cellSize,
y: margin + (z + HALF + 0.5) * cellSize
};
}
// Home.
const homePosition = mapPosition(base.x, base.z);
ctx.fillStyle = "#36d6a3";
ctx.fillRect(
homePosition.x - cellSize * 0.38,
homePosition.y - cellSize * 0.38,
cellSize * 0.76,
cellSize * 0.76
);
// Pads.
for (const pad of pads) {
const position = mapPosition(pad.x, pad.z);
ctx.fillStyle = usedPads.has(pad.id) ? "#68848b" : "#43d9ff";
ctx.fillRect(
position.x - cellSize * 0.25,
position.y - cellSize * 0.25,
cellSize * 0.5,
cellSize * 0.5
);
}
// Samples.
for (const sample of samples) {
if (sample.status !== "active") continue;
const position = mapPosition(sample.x, sample.z);
ctx.save();
ctx.translate(position.x, position.y);
ctx.rotate(Math.PI / 4);
ctx.fillStyle = "#ff4fa3";
ctx.fillRect(-cellSize * 0.31, -cellSize * 0.31, cellSize * 0.62, cellSize * 0.62);
ctx.restore();
}
// Rover.
const roverPosition = mapPosition(rover.position.x, rover.position.z);
ctx.save();
ctx.translate(roverPosition.x, roverPosition.y);
ctx.rotate(-rover.rotation.y);
ctx.fillStyle = "#ffce4b";
ctx.strokeStyle = "#17323a";
ctx.lineWidth = 1.4;
ctx.beginPath();
ctx.moveTo(0, cellSize * 0.48);
ctx.lineTo(-cellSize * 0.36, -cellSize * 0.36);
ctx.lineTo(cellSize * 0.36, -cellSize * 0.36);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.restore();
}
// -------------------------------------------------------------------------
// Label projection
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateWorldLabels() {
for (const entry of labelEntries) {
const { type, item, offsetY } = entry;
if (type === "sample" && item.status !== "active") {
entry.element.style.display = "none";
continue;
}
let anchor;
if (type === "sample") {
anchor = new THREE.Vector3(item.x, item.group.position.y + offsetY, item.z);
} else if (type === "pad") {
anchor = new THREE.Vector3(item.x, item.group.position.y + offsetY, item.z);
} else {
anchor = new THREE.Vector3(item.x, item.group.position.y + offsetY, item.z);
}
projected.copy(anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.08 &&
Math.abs(projected.y) < 1.08;
entry.element.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
entry.element.style.left = `${x}px`;
entry.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function heldDirection() {
const priority = ["w", "arrowup", "s", "arrowdown", "a", "arrowleft", "d", "arrowright"];
for (const key of priority) {
if (pressedKeys.has(key)) return directionKeys[key];
}
return null;
}
function shortestAngleDifference(from, to) {
return Math.atan2(Math.sin(to - from), Math.cos(to - from));
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
if (started && mode === "playing") {
elapsed += dt;
if (!movement) {
const direction = heldDirection();
if (direction) attemptMove(direction);
}
}
if (movement) {
movement.t += dt / movement.duration;
const t = Math.min(movement.t, 1);
const smooth = t * t * (3 - 2 * t);
rover.position.x = THREE.MathUtils.lerp(movement.fromX, movement.toX, smooth);
rover.position.y = THREE.MathUtils.lerp(movement.fromY, movement.toY, smooth);
rover.position.z = THREE.MathUtils.lerp(movement.fromZ, movement.toZ, smooth);
const targetAngle = Math.atan2(
movement.toX - movement.fromX,
movement.toZ - movement.fromZ
);
rover.rotation.y += shortestAngleDifference(rover.rotation.y, targetAngle) * Math.min(1, dt * 14);
for (const wheel of wheels) {
wheel.rotation.x -= dt * 9;
}
if (movement.t >= 1) {
roverCell.x = movement.toX;
roverCell.z = movement.toZ;
rover.position.set(roverCell.x, movement.toY, roverCell.z);
movement = null;
handleArrival();
}
} else {
rover.position.y = terrainTop(roverCell.x, roverCell.z) + Math.sin(time * 3.2) * 0.012;
}
for (const sample of samples) {
if (sample.status !== "active") continue;
sample.group.position.y = sample.baseY + Math.sin(time * 2.1 + sample.id) * 0.075;
sample.group.rotation.y += dt * 0.42;
}
for (const pad of pads) {
if (usedPads.has(pad.id)) continue;
pad.plus.rotation.y = Math.sin(time * 1.4 + pad.id) * 0.13;
pad.beam.scale.y = 0.9 + Math.sin(time * 2 + pad.id) * 0.12;
}
baseBeacon.position.y = 1.27 + Math.sin(time * 2.6) * 0.05;
for (let i = 0; i < clouds.length; i++) {
const cloud = clouds[i];
cloud.position.x += dt * (0.18 + i * 0.025);
if (cloud.position.x > 28) cloud.position.x = -28;
}
if (time - lastHudUpdate > 0.12) {
updateHud();
lastHudUpdate = time;
}
if (time - lastMapUpdate > 0.08) {
drawMinimap();
lastMapUpdate = time;
}
updateCamera();
updateWorldLabels();
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Resize and startup
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
updateHud();
updateCamera();
drawMinimap();
animate();
</script>
</body>
</html>
```