mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
viewer: an overview map of the world's chunks, with click to fly there (VIEWER-OVERVIEW-MAP)
725 lines
37 KiB
HTML
725 lines
37 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Map Viewer</title>
|
|
<style>
|
|
:root { --bg: #0d1014; --fg: #e6e8eb; --dim: #8b929b; --accent: #5fb3ff; --warn: #ffb454; --panel: rgba(13,16,20,.82); }
|
|
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 13px/1.45 ui-monospace, Consolas, monospace; overflow: hidden; }
|
|
canvas { display: block; }
|
|
#hud { position: fixed; top: 12px; left: 12px; background: var(--panel); padding: 10px 12px; border-radius: 8px; min-width: 250px; pointer-events: none; }
|
|
#hud b { color: var(--accent); font-weight: 600; }
|
|
#banner { position: fixed; top: 12px; right: 12px; max-width: 360px; background: var(--panel); border-left: 3px solid var(--warn); padding: 10px 12px; border-radius: 8px; }
|
|
#banner.hidden { display: none; }
|
|
#help { position: fixed; bottom: 12px; left: 12px; background: var(--panel); padding: 10px 12px; border-radius: 8px; color: var(--dim); }
|
|
#help kbd { color: var(--fg); background: #1c2129; padding: 0 5px; border-radius: 3px; }
|
|
#start { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.45); cursor: pointer; font-size: 15px; }
|
|
#start.hidden { display: none; }
|
|
/* Above #start, so the map stays clickable while "Click to fly" covers the rest of the page; opaque, so that
|
|
prompt does not show through it in a narrow window. */
|
|
#map { position: fixed; right: 12px; top: 50%; transform: translateY(-50%); z-index: 1; background: var(--bg); padding: 8px; border-radius: 8px; }
|
|
#map.hidden { display: none; }
|
|
#map canvas { width: min(420px, calc(100vw - 40px)); height: min(420px, calc(100vh - 300px)); min-height: 120px; border-radius: 4px; cursor: crosshair; }
|
|
#map-legend, #map-info { width: min(420px, calc(100vw - 40px)); margin-top: 6px; }
|
|
#map-legend { display: flex; flex-wrap: wrap; gap: 0 12px; color: var(--dim); font-size: 11px; }
|
|
#map-legend i { font-style: normal; }
|
|
#map-info { min-height: 2.9em; overflow-wrap: anywhere; }
|
|
@media (max-width: 760px) {
|
|
#hud { right: 12px; min-width: 0; }
|
|
#banner { top: auto; bottom: 104px; left: 12px; max-width: none; }
|
|
#help { right: 12px; }
|
|
#map canvas { height: min(420px, calc(100vh - 420px)); }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="hud"></div>
|
|
<div id="banner" class="hidden">
|
|
<b>TEST TERRAIN</b>: generated in the browser. <span id="world-status"></span><br>
|
|
Build chunks with <code>python -I tools/world_build.py <work>\assets --out <work>\world</code>
|
|
and serve with <code>python -I viewer/serve.py --world <work>\world</code>.
|
|
</div>
|
|
<div id="help">
|
|
<kbd>W A S D</kbd> move · <kbd>Q</kbd>/<kbd>E</kbd> down/up · <kbd>Shift</kbd> fast · <kbd>C</kbd> slow · wheel: speed<br>
|
|
<kbd>F</kbd> wireframe · <kbd>T</kbd> textures/clay · <kbd>G</kbd> chunk bounds · <kbd>R</kbd> back to spawn · <kbd>P</kbd> copy link to this view · <kbd>Esc</kbd> release mouse
|
|
<br><kbd>M</kbd> overview map: click on it to fly there, wheel over it to zoom
|
|
</div>
|
|
<div id="map" class="hidden">
|
|
<canvas role="img" aria-label="Overview map: every chunk of the world seen from above, with the camera"></canvas>
|
|
<div id="map-legend">
|
|
<span><i style="color:#5fb3ff">■</i> loaded</span> <span><i style="color:#b7dcff">■</i> loading</span>
|
|
<span><i style="color:#ffb454">■</i> failed</span> <span><i style="color:#3a4553">■</i> not loaded</span>
|
|
<span>▲ camera</span> <span>○ spawn</span>
|
|
</div>
|
|
<div id="map-info"></div>
|
|
</div>
|
|
<div id="start">Click to fly</div>
|
|
|
|
<!-- three.js comes from a CDN pinned to an exact version, and every file of its module graph carries a
|
|
Subresource Integrity hash: the import map's "integrity" table (Chromium 127+, Firefox 138+, Safari 18+) and,
|
|
for older browsers, the modulepreload links, whose fetch the module imports reuse. A changed file fails to
|
|
load instead of running. Bumping the version means updating every URL and hash here
|
|
(openssl dgst -sha384 -binary FILE | openssl base64 -A); tests/test_viewer_cdn_integrity.py checks the pairing. -->
|
|
<script type="importmap">
|
|
{ "imports": {
|
|
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
|
|
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
|
|
},
|
|
"integrity": {
|
|
"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js": "sha384-61S/Nu32S3E5+n+KpCOTb2eRYps6fVKm+9Gz1QBvSePFthb46f063Aa/qe/lykFZ",
|
|
"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/loaders/GLTFLoader.js": "sha384-oxY9vSeh7Mrt5WmhN8xfWoDRnmoW9krEiRBY7vJKaK90jMUidM0fhbUwcblpBgTu",
|
|
"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/utils/BufferGeometryUtils.js": "sha384-jHGXS4+FuVOBnZkNme2ury3Ccg5DWDLYK22xX/WGdZCZsjRM4J+3n6Qc4CXx1Hjf"
|
|
} }
|
|
</script>
|
|
<link rel="modulepreload" crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js" integrity="sha384-61S/Nu32S3E5+n+KpCOTb2eRYps6fVKm+9Gz1QBvSePFthb46f063Aa/qe/lykFZ">
|
|
<link rel="modulepreload" crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/loaders/GLTFLoader.js" integrity="sha384-oxY9vSeh7Mrt5WmhN8xfWoDRnmoW9krEiRBY7vJKaK90jMUidM0fhbUwcblpBgTu">
|
|
<link rel="modulepreload" crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/utils/BufferGeometryUtils.js" integrity="sha384-jHGXS4+FuVOBnZkNme2ury3Ccg5DWDLYK22xX/WGdZCZsjRM4J+3n6Qc4CXx1Hjf">
|
|
<script type="module">
|
|
import * as THREE from "three";
|
|
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
|
|
|
|
// ---------- world description ----------
|
|
// world/world.json: { "spawn": [x,y,z], "chunks": [ { "id", "url", "bounds": [minx,miny,minz,maxx,maxy,maxz] } ] }
|
|
// A world built with world_build --lod-sets also has "lod_bands": { tier: [start, end] } (metres), and each
|
|
// chunk names its "lod" tier and its grid "cell": [minx, minz, maxx, maxz].
|
|
// Each chunk is a .glb in world space (Y up, metres). Validate the manifest before
|
|
// the camera and streaming code use its coordinates and chunk identifiers.
|
|
const WORLD_MANIFEST_TIMEOUT_MS = 10_000;
|
|
|
|
function manifestError(world) {
|
|
const isPosition = (value, length) =>
|
|
Array.isArray(value) && value.length === length && value.every(Number.isFinite);
|
|
const isNonemptyString = value => typeof value === "string" && value.trim().length > 0;
|
|
if (!isPosition(world?.spawn, 3)) return "spawn must contain three finite coordinates";
|
|
if (!Array.isArray(world.chunks)) return "chunks must be an array";
|
|
const ids = new Set();
|
|
for (const [i, chunk] of world.chunks.entries()) {
|
|
if (!isNonemptyString(chunk?.id) || ids.has(chunk.id)) return `chunks[${i}].id must be a unique, nonempty string`;
|
|
if (!isNonemptyString(chunk.url)) return `chunks[${i}].url must be a nonempty string`;
|
|
const b = chunk.bounds;
|
|
if (!isPosition(b, 6) || b[0] > b[3] || b[1] > b[4] || b[2] > b[5])
|
|
return `chunks[${i}].bounds must contain six finite, ordered coordinates`;
|
|
if (chunk.lod !== undefined) {
|
|
const band = isNonemptyString(chunk.lod) ? world.lod_bands?.[chunk.lod] : undefined;
|
|
if (!isPosition(band, 2) || band[0] < 0 || band[0] > band[1])
|
|
return `chunks[${i}].lod must name a lod_bands entry with two finite, ordered distances`;
|
|
const c = chunk.cell;
|
|
if (!isPosition(c, 4) || c[0] > c[2] || c[1] > c[3])
|
|
return `chunks[${i}].cell must contain four finite, ordered coordinates`;
|
|
}
|
|
ids.add(chunk.id);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Camera in the URL hash: #cam=x,y,z,yaw,pitch. Returns null for anything that is not five finite numbers,
|
|
// so a hand-edited or stale link falls back to the spawn point instead of putting the camera at NaN.
|
|
function parseCamHash(hash) {
|
|
const m = /^#?cam=([^&]*)/.exec(typeof hash === "string" ? hash : "");
|
|
if (!m) return null;
|
|
const parts = m[1].split(",");
|
|
if (parts.length !== 5 || parts.some(t => t.trim() === "")) return null;
|
|
const v = parts.map(Number);
|
|
return v.every(Number.isFinite) ? { pos: v.slice(0, 3), yaw: v[3], pitch: v[4] } : null;
|
|
}
|
|
function formatCamHash(pos, yaw, pitch) {
|
|
return "#cam=" + [...pos, yaw, pitch].map((n, i) => n.toFixed(i < 3 ? 1 : 3)).join(",");
|
|
}
|
|
|
|
async function loadWorld() {
|
|
if (new URLSearchParams(location.search).has("test")) return testWorld("Test mode was requested.");
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), WORLD_MANIFEST_TIMEOUT_MS);
|
|
let world, stage = "fetch";
|
|
try {
|
|
const response = await fetch("world/world.json", { cache: "no-store", signal: controller.signal });
|
|
if (response.status === 404) return testWorld("world/world.json was not found.");
|
|
if (!response.ok) return testWorld(`world/world.json returned HTTP ${response.status}.`);
|
|
stage = "read";
|
|
const text = await response.text();
|
|
stage = "parse";
|
|
world = JSON.parse(text);
|
|
} catch {
|
|
if (controller.signal.aborted) return testWorld("world/world.json download timed out.");
|
|
if (stage === "read") return testWorld("world/world.json could not be read.");
|
|
if (stage === "parse") return testWorld("world/world.json could not be read as valid JSON.");
|
|
return testWorld("world/world.json could not be loaded.");
|
|
} finally {
|
|
clearTimeout(timer);
|
|
}
|
|
const error = manifestError(world);
|
|
if (error) return testWorld(`Invalid world/world.json: ${error}.`);
|
|
return { ...world, test: false };
|
|
}
|
|
|
|
function testWorld(reason) {
|
|
const size = 256, n = 24, chunks = [];
|
|
for (let z = -n / 2; z < n / 2; z++)
|
|
for (let x = -n / 2; x < n / 2; x++)
|
|
chunks.push({ id: `test_${x}_${z}`, test: [x, z, size],
|
|
bounds: [x * size, -50, z * size, (x + 1) * size, 200, (z + 1) * size] });
|
|
return { test: true, reason, spawn: [0, 120, 0], chunks };
|
|
}
|
|
|
|
// Deterministic test terrain: rolling hills plus a grid of boxes. Nothing here comes from the game.
|
|
function hash(x, z) { const s = Math.sin(x * 127.1 + z * 311.7) * 43758.5453; return s - Math.floor(s); }
|
|
function noise(x, z) {
|
|
const xi = Math.floor(x), zi = Math.floor(z), xf = x - xi, zf = z - zi;
|
|
const u = xf * xf * (3 - 2 * xf), v = zf * zf * (3 - 2 * zf);
|
|
const a = hash(xi, zi), b = hash(xi + 1, zi), c = hash(xi, zi + 1), d = hash(xi + 1, zi + 1);
|
|
return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;
|
|
}
|
|
function height(x, z) {
|
|
return 60 * noise(x / 900, z / 900) + 18 * noise(x / 210, z / 210) + 4 * noise(x / 40, z / 40) - 30;
|
|
}
|
|
function buildTestChunk([cx, cz, size]) {
|
|
const g = new THREE.Group(), seg = 32;
|
|
const geo = new THREE.PlaneGeometry(size, size, seg, seg);
|
|
geo.rotateX(-Math.PI / 2);
|
|
const pos = geo.attributes.position, col = [];
|
|
for (let i = 0; i < pos.count; i++) {
|
|
const x = pos.getX(i) + cx * size + size / 2, z = pos.getZ(i) + cz * size + size / 2, y = height(x, z);
|
|
pos.setXYZ(i, x, y, z);
|
|
const t = THREE.MathUtils.clamp((y + 30) / 90, 0, 1);
|
|
col.push(0.22 + 0.35 * t, 0.36 + 0.25 * t, 0.24 + 0.1 * t);
|
|
}
|
|
geo.setAttribute("color", new THREE.Float32BufferAttribute(col, 3));
|
|
geo.computeVertexNormals();
|
|
// Sample the shared height field on both sides of each border for matching edge normals.
|
|
const normal = geo.attributes.normal, stride = seg + 1, step = size / seg;
|
|
for (let i = 0; i < pos.count; i++) {
|
|
const row = Math.floor(i / stride), column = i % stride;
|
|
if (row > 0 && row < seg && column > 0 && column < seg) continue;
|
|
const x = pos.getX(i), z = pos.getZ(i);
|
|
const nx = height(x - step, z) - height(x + step, z);
|
|
const nz = height(x, z - step) - height(x, z + step);
|
|
const length = Math.hypot(nx, 2 * step, nz);
|
|
normal.setXYZ(i, nx / length, 2 * step / length, nz / length);
|
|
}
|
|
g.add(new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.95 })));
|
|
const box = new THREE.BoxGeometry(1, 1, 1); box.translate(0, 0.5, 0);
|
|
const count = 18, inst = new THREE.InstancedMesh(box, new THREE.MeshStandardMaterial({ color: 0x9aa4b1, roughness: 0.7 }), count);
|
|
const m = new THREE.Matrix4();
|
|
for (let i = 0; i < count; i++) {
|
|
const x = cx * size + hash(cx * 31 + i, cz) * size, z = cz * size + hash(cx, cz * 17 + i) * size;
|
|
const w = 8 + 22 * hash(i, cx + cz), h = 6 + 70 * hash(cz + i, cx) ** 3;
|
|
m.compose(new THREE.Vector3(x, height(x, z) - 1, z), new THREE.Quaternion(), new THREE.Vector3(w, h, w));
|
|
inst.setMatrixAt(i, m);
|
|
}
|
|
g.add(inst);
|
|
return g;
|
|
}
|
|
|
|
// ---------- scene ----------
|
|
let renderer;
|
|
try {
|
|
renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
|
|
} catch (error) {
|
|
const start = document.getElementById("start");
|
|
start.setAttribute("role", "alert");
|
|
start.style.cursor = "default";
|
|
start.textContent = "Could not start WebGL. Check that WebGL is enabled, then reload.";
|
|
throw error;
|
|
}
|
|
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
|
|
renderer.setSize(innerWidth, innerHeight);
|
|
document.body.prepend(renderer.domElement);
|
|
const scene = new THREE.Scene();
|
|
scene.background = new THREE.Color(0x9cc3e6);
|
|
scene.fog = new THREE.Fog(0x9cc3e6, 900, 2600);
|
|
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.5, 6000);
|
|
scene.add(new THREE.HemisphereLight(0xdfefff, 0x4a4030, 1.1));
|
|
const sun = new THREE.DirectionalLight(0xfff2dd, 1.6); sun.position.set(0.4, 1, 0.3); scene.add(sun);
|
|
addEventListener("resize", () => { camera.aspect = innerWidth / innerHeight; camera.updateProjectionMatrix(); renderer.setSize(innerWidth, innerHeight); });
|
|
|
|
// ---------- freecam ----------
|
|
const keys = new Set(); let yaw = 0, pitch = -0.25, speed = 40, locked = false;
|
|
// Pointer lock when the browser allows it; embedded views (iframes, preview panes) fall back to drag-to-look.
|
|
const start = document.getElementById("start");
|
|
let dragLook = false, dragging = false;
|
|
function enableDragLook() {
|
|
if (locked || dragLook) return;
|
|
dragLook = true;
|
|
start.classList.add("hidden");
|
|
document.getElementById("help").insertAdjacentHTML("beforeend", "<br>mouse lock unavailable here: drag to look");
|
|
}
|
|
start.onclick = async () => {
|
|
try { await renderer.domElement.requestPointerLock(); }
|
|
catch { enableDragLook(); }
|
|
};
|
|
document.addEventListener("pointerlockerror", enableDragLook);
|
|
document.addEventListener("pointerlockchange", () => { locked = document.pointerLockElement === renderer.domElement; start.classList.toggle("hidden", locked || dragLook); });
|
|
renderer.domElement.addEventListener("mousedown", e => { if (e.button === 0) dragging = true; });
|
|
addEventListener("mouseup", e => { if (e.button === 0) dragging = false; });
|
|
addEventListener("mousemove", e => {
|
|
if ((e.buttons & 1) === 0) dragging = false; // a release outside the window can skip mouseup
|
|
if (!locked && !(dragLook && dragging)) return;
|
|
yaw -= e.movementX * 0.0022; pitch = THREE.MathUtils.clamp(pitch - e.movementY * 0.0022, -1.55, 1.55);
|
|
});
|
|
addEventListener("wheel", e => {
|
|
// A trackpad's sideways scroll (deltaX) is a pan, not a speed change: only a mostly vertical wheel moves it.
|
|
if (!e.deltaY || Math.abs(e.deltaX) > Math.abs(e.deltaY)) return;
|
|
speed = THREE.MathUtils.clamp(speed * (e.deltaY < 0 ? 1.15 : 1 / 1.15), 1, 3000);
|
|
}, { passive: true });
|
|
addEventListener("blur", () => { keys.clear(); dragging = false; });
|
|
const world = await loadWorld();
|
|
addEventListener("keydown", e => {
|
|
keys.add(e.code);
|
|
// Leave modified shortcuts to the browser.
|
|
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
if (e.code === "KeyF" && !e.repeat) wire = !wire, applyWire();
|
|
if (e.code === "KeyT" && !e.repeat) clay = !clay, applyClay();
|
|
if (e.code === "KeyG" && !e.repeat) toggleBounds();
|
|
if (e.code === "KeyM" && !e.repeat) toggleMap();
|
|
if (e.code === "KeyR") camera.position.fromArray(world.spawn);
|
|
if (e.code === "KeyP" && !e.repeat) {
|
|
history.replaceState(null, "", formatCamHash(camera.position.toArray(), yaw, pitch));
|
|
navigator.clipboard?.writeText(location.href).catch(() => {});
|
|
}
|
|
});
|
|
addEventListener("keyup", e => keys.delete(e.code));
|
|
|
|
// ---------- chunk streaming ----------
|
|
const LOAD_R = 1400, UNLOAD_R = 2000, MAX_INFLIGHT = 4;
|
|
const CHUNK_DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
// Three total attempts per chunk, with 1s/2s backoff. Failed chunks keep their budget
|
|
// when leaving and returning; successfully loaded chunks start fresh after unloading.
|
|
const MAX_LOAD_ATTEMPTS = 3, RETRY_DELAY_MS = 1000;
|
|
const loader = new GLTFLoader();
|
|
const live = new Map(); let inflight = 0, wire = false, clay = false, failed = 0;
|
|
const originalMaterials = new WeakMap();
|
|
const clayMat = new THREE.MeshStandardMaterial({ color: 0xb8c0ca, roughness: 0.85, wireframe: wire, side: THREE.DoubleSide });
|
|
const boundsGroup = new THREE.Group(); boundsGroup.visible = false; scene.add(boundsGroup);
|
|
function toggleBounds() {
|
|
boundsGroup.visible = !boundsGroup.visible;
|
|
if (!boundsGroup.visible || boundsGroup.children.length) return;
|
|
for (const { bounds: [minX, minY, minZ, maxX, maxY, maxZ] } of world.chunks) {
|
|
const box = new THREE.Box3(new THREE.Vector3(minX, minY, minZ), new THREE.Vector3(maxX, maxY, maxZ));
|
|
boundsGroup.add(new THREE.Box3Helper(box, 0x5fb3ff));
|
|
}
|
|
}
|
|
function applyClay(root) {
|
|
clayMat.wireframe = wire;
|
|
(root ? [root] : [...live.values()].map(c => c.obj).filter(Boolean)).forEach(o =>
|
|
o.traverse(n => {
|
|
if (!n.isMesh || !n.material) return;
|
|
if (!originalMaterials.has(n)) originalMaterials.set(n, n.material);
|
|
n.material = clay ? clayMat : originalMaterials.get(n);
|
|
}));
|
|
}
|
|
function applyWire(root) {
|
|
clayMat.wireframe = wire;
|
|
(root ? [root] : [...live.values()].map(c => c.obj).filter(Boolean)).forEach(o =>
|
|
o.traverse(n => {
|
|
const orig = originalMaterials.get(n);
|
|
if (orig) [].concat(orig).forEach(m => m.wireframe = wire);
|
|
if (n.material && n.material !== orig) [].concat(n.material).forEach(m => m.wireframe = wire);
|
|
}));
|
|
}
|
|
function disposeObject(obj) {
|
|
const disposedGeo = new Set(), disposedMat = new Set(), disposedTex = new Set();
|
|
const bitmaps = new Set(), skeletons = new Set();
|
|
obj.traverse(n => {
|
|
if (n.geometry && !disposedGeo.has(n.geometry)) { disposedGeo.add(n.geometry); n.geometry.dispose(); }
|
|
if (n.isInstancedMesh && typeof n.dispose === "function") n.dispose();
|
|
if (n.isSkinnedMesh && n.skeleton) skeletons.add(n.skeleton);
|
|
const allMats = [].concat(n.material || [], originalMaterials.get(n) || []);
|
|
for (const m of allMats) {
|
|
if (!m || m === clayMat || disposedMat.has(m)) continue;
|
|
disposedMat.add(m);
|
|
for (const v of Object.values(m)) {
|
|
if (v && v.isTexture && !disposedTex.has(v)) {
|
|
disposedTex.add(v);
|
|
if (typeof ImageBitmap !== "undefined" && v.image instanceof ImageBitmap) bitmaps.add(v.image);
|
|
v.dispose();
|
|
}
|
|
}
|
|
m.dispose();
|
|
}
|
|
originalMaterials.delete(n);
|
|
});
|
|
for (const skeleton of skeletons) skeleton.dispose();
|
|
for (const bitmap of bitmaps) bitmap.close();
|
|
}
|
|
function dist(c, p) {
|
|
const b = c.bounds;
|
|
const dx = Math.max(b[0] - p.x, 0, p.x - b[3]), dy = Math.max(b[1] - p.y, 0, p.y - b[4]), dz = Math.max(b[2] - p.z, 0, p.z - b[5]);
|
|
return Math.hypot(dx, dy, dz);
|
|
}
|
|
// Choose one loaded tier per cell. Prefer the tier whose band contains the camera's horizontal
|
|
// distance, and use a cached tier within LOD_SLACK while the preferred tier is unavailable.
|
|
const LOD_SLACK = 32;
|
|
function cellDist(c, p) {
|
|
const r = c.cell;
|
|
return Math.hypot(Math.max(r[0] - p.x, 0, p.x - r[2]), Math.max(r[1] - p.z, 0, p.z - r[3]));
|
|
}
|
|
// The distance used to rank a chunk, and whether it should load (slack 0) or stay loaded (LOD_SLACK).
|
|
function range(def, p) {
|
|
const band = def.lod !== undefined ? world.lod_bands?.[def.lod] : undefined;
|
|
if (!band) { const d = dist(def, p); return { d, load: d < LOAD_R, keep: d <= UNLOAD_R }; }
|
|
const d = cellDist(def, p);
|
|
return { d, load: d >= band[0] && d < band[1], keep: d >= band[0] - LOD_SLACK && d < band[1] + LOD_SLACK };
|
|
}
|
|
function drop(id) {
|
|
const c = live.get(id); if (!c) return;
|
|
live.delete(id);
|
|
c.controller?.abort();
|
|
if (c.obj) {
|
|
scene.remove(c.obj);
|
|
disposeObject(c.obj);
|
|
}
|
|
}
|
|
const MAX_PREALLOC_BYTES = 1024 * 1024 * 1024;
|
|
function parseContentLength(response) {
|
|
if (!response.headers || typeof response.headers.get !== "function" || response.headers.get("content-encoding")) {
|
|
return null;
|
|
}
|
|
const raw = response.headers.get("content-length");
|
|
if (typeof raw !== "string") return null;
|
|
const trimmed = raw.trim();
|
|
if (!/^\d+$/.test(trimmed)) return null;
|
|
const n = Number(trimmed);
|
|
return Number.isSafeInteger(n) && n > 0 && n <= MAX_PREALLOC_BYTES ? n : null;
|
|
}
|
|
async function readChunkBytes(response, signal, resetDeadline) {
|
|
if (!response.body || typeof response.body.getReader !== "function") {
|
|
return response.arrayBuffer();
|
|
}
|
|
const reader = response.body.getReader();
|
|
const cancelReader = () => {
|
|
try { Promise.resolve(reader.cancel(signal.reason)).catch(() => {}); } catch {}
|
|
};
|
|
signal.addEventListener("abort", cancelReader, { once: true });
|
|
const expected = parseContentLength(response);
|
|
let target = null;
|
|
if (expected !== null) {
|
|
try { target = new Uint8Array(expected); } catch {}
|
|
}
|
|
let parts = null, offset = 0;
|
|
try {
|
|
if (signal.aborted) cancelReader();
|
|
for (;;) {
|
|
signal.throwIfAborted();
|
|
const { done, value } = await reader.read();
|
|
signal.throwIfAborted();
|
|
if (done) break;
|
|
if (!value || value.byteLength === 0) continue;
|
|
resetDeadline();
|
|
const chunk = value instanceof Uint8Array
|
|
? value
|
|
: ArrayBuffer.isView(value)
|
|
? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
|
|
: new Uint8Array(value);
|
|
if (target) {
|
|
if (offset + chunk.byteLength <= target.byteLength) {
|
|
target.set(chunk, offset);
|
|
offset += chunk.byteLength;
|
|
continue;
|
|
}
|
|
parts = offset > 0 ? [target.subarray(0, offset), chunk] : [chunk];
|
|
target = null;
|
|
offset += chunk.byteLength;
|
|
continue;
|
|
}
|
|
if (!parts) parts = [];
|
|
parts.push(chunk);
|
|
offset += chunk.byteLength;
|
|
}
|
|
} finally {
|
|
signal.removeEventListener("abort", cancelReader);
|
|
try { reader.releaseLock(); } catch {}
|
|
}
|
|
if (target) return offset === target.byteLength ? target.buffer : target.buffer.slice(0, offset);
|
|
if (!parts || parts.length === 0) return new ArrayBuffer(0);
|
|
if (parts.length === 1 && parts[0].byteOffset === 0 && parts[0].byteLength === parts[0].buffer.byteLength) {
|
|
return parts[0].buffer;
|
|
}
|
|
const merged = new Uint8Array(offset);
|
|
let at = 0;
|
|
for (const part of parts) { merged.set(part, at); at += part.byteLength; }
|
|
return merged.buffer;
|
|
}
|
|
async function loadChunk(entry) {
|
|
const { def, controller } = entry;
|
|
inflight++;
|
|
let timer, active = true;
|
|
const resetDeadline = () => {
|
|
clearTimeout(timer);
|
|
if (active) timer = setTimeout(() => controller.abort(), CHUNK_DOWNLOAD_TIMEOUT_MS);
|
|
};
|
|
try {
|
|
let response, bytes;
|
|
timer = setTimeout(() => controller.abort(), CHUNK_DOWNLOAD_TIMEOUT_MS);
|
|
try {
|
|
response = await fetch(def.url, { signal: controller.signal });
|
|
if (!response.ok) throw new Error(`Chunk request failed: ${response.status}`);
|
|
bytes = await readChunkBytes(response, controller.signal, resetDeadline);
|
|
controller.signal.throwIfAborted();
|
|
} finally {
|
|
// Bound idle stalls on headers or between body chunks, not the non-cancellable GLB parse.
|
|
active = false;
|
|
clearTimeout(timer);
|
|
}
|
|
const url = response.url || def.url;
|
|
const resourcePath = THREE.LoaderUtils.extractUrlBase(url.split(/[?#]/, 1)[0]);
|
|
const g = await loader.parseAsync(bytes, resourcePath);
|
|
// GLTFLoader cannot cancel a parse already underway. Dispose its late result.
|
|
if (live.get(def.id) !== entry) { disposeObject(g.scene); return; }
|
|
entry.obj = g.scene; scene.add(g.scene); applyClay(g.scene); applyWire(g.scene);
|
|
} catch {
|
|
if (live.get(def.id) !== entry) return;
|
|
failed++;
|
|
entry.retryAt = performance.now() + RETRY_DELAY_MS * 2 ** (entry.attempts - 1);
|
|
} finally {
|
|
entry.loading = false;
|
|
entry.controller = null;
|
|
entry.previous = null;
|
|
inflight--;
|
|
}
|
|
}
|
|
function stream(now = performance.now()) {
|
|
const p = camera.position;
|
|
const visibleTiers = new Map();
|
|
for (const [id, c] of live) {
|
|
if (!c.obj && !c.loading) continue;
|
|
const { load, keep } = range(c.def, p);
|
|
if (keep) {
|
|
// Orphan HD has its own band and no replacement in the LOD chain.
|
|
if (c.obj && c.def.lod === "orphan_hd") {
|
|
c.obj.visible = load;
|
|
} else if (c.obj && c.def.lod !== undefined) {
|
|
const cell = c.def.cell.join(",");
|
|
const current = visibleTiers.get(cell);
|
|
c.obj.visible = !current || (load && !current.inBand);
|
|
if (c.obj.visible) {
|
|
if (current) current.obj.visible = false;
|
|
visibleTiers.set(cell, { obj: c.obj, inBand: load });
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
drop(id);
|
|
// A canceled retry does not erase the previous failures or spend another attempt.
|
|
if (c.previous) live.set(id, c.previous);
|
|
}
|
|
// Always evict stale chunks, but only look for new work when a slot is free.
|
|
if (inflight >= MAX_INFLIGHT) return;
|
|
|
|
const want = [];
|
|
for (const def of world.chunks) {
|
|
const entry = live.get(def.id);
|
|
if (entry && (entry.obj || entry.loading || entry.attempts >= MAX_LOAD_ATTEMPTS || now < entry.retryAt)) continue;
|
|
const { d: distance, load } = range(def, p);
|
|
if (load) want.push({ def, distance });
|
|
}
|
|
want.sort((a, b) => a.distance - b.distance);
|
|
for (const { def } of want) {
|
|
if (inflight >= MAX_INFLIGHT) break;
|
|
const previous = live.get(def.id);
|
|
const entry = { def, obj: null, loading: true, attempts: (previous?.attempts || 0) + 1, retryAt: 0, previous };
|
|
live.set(def.id, entry);
|
|
if (world.test) { entry.loading = false; entry.obj = buildTestChunk(def.test); scene.add(entry.obj); applyClay(entry.obj); applyWire(entry.obj); continue; }
|
|
entry.controller = new AbortController();
|
|
void loadChunk(entry);
|
|
}
|
|
}
|
|
|
|
// ---------- overview map ----------
|
|
// M opens a map of the whole world from above, drawn from the manifest alone: one rectangle per chunk (its grid
|
|
// cell when it names one, otherwise its bounds), coloured by what the streaming code is doing with it. +x is
|
|
// right and +z is down, so a placed world (RAGE x, y, z -> glTF x, z, -y) has north at the top. A click moves the
|
|
// camera there. Nothing here touches the page until the map is first opened.
|
|
const MAP_PAD = 10, MAP_MAX_ZOOM = 64, MAP_REDRAW_MS = 100, MAP_JUMP_CLEARANCE = 10;
|
|
// In drawing order: what is loaded, loading or failed stays on top of the idle tiers of the same cell.
|
|
const MAP_COLORS = { idle: "#3a4553", loaded: "#5fb3ff", loading: "#b7dcff", failed: "#ffb454" };
|
|
const MAP_STATES = Object.keys(MAP_COLORS);
|
|
let mapOpen = false, mapCanvas = null, mapInfo = null, mapExtent = null, mapZoom = 1, mapCenter = null;
|
|
let mapCursor = null, mapDrawnAt = -Infinity;
|
|
// manifestError() checks a chunk's cell only when the chunk names a tier, so only then is it used.
|
|
function mapRect(def) { const b = def.bounds; return def.lod !== undefined ? def.cell : [b[0], b[2], b[3], b[5]]; }
|
|
function mapState(entry) {
|
|
if (entry?.obj) return "loaded";
|
|
if (entry?.loading) return "loading";
|
|
return entry?.attempts >= MAX_LOAD_ATTEMPTS ? "failed" : "idle";
|
|
}
|
|
function worldExtent() {
|
|
let e = null;
|
|
for (const def of world.chunks) {
|
|
const r = mapRect(def);
|
|
e = e ? [Math.min(e[0], r[0]), Math.min(e[1], r[1]), Math.max(e[2], r[2]), Math.max(e[3], r[3])] : [...r];
|
|
}
|
|
const [x, , z] = world.spawn;
|
|
return e ?? [x - 512, z - 512, x + 512, z + 512]; // a world without chunks still shows the camera and the spawn
|
|
}
|
|
// Metres to CSS pixels on the map canvas: pixel = metres * scale + offset. At zoom 1 the whole extent fits,
|
|
// centred, with the same scale on both axes.
|
|
function mapTransform(width = mapCanvas.clientWidth, height = mapCanvas.clientHeight) {
|
|
const [minX, minZ, maxX, maxZ] = mapExtent;
|
|
const fit = Math.min(Math.max(width - 2 * MAP_PAD, 1) / Math.max(maxX - minX, 1),
|
|
Math.max(height - 2 * MAP_PAD, 1) / Math.max(maxZ - minZ, 1));
|
|
const [cx, cz] = mapCenter ?? [(minX + maxX) / 2, (minZ + maxZ) / 2], scale = fit * mapZoom;
|
|
return { fit, scale, x: width / 2 - cx * scale, y: height / 2 - cz * scale, width, height };
|
|
}
|
|
function mapPoint(e, t = mapTransform()) { return [(e.offsetX - t.x) / t.scale, (e.offsetY - t.y) / t.scale]; }
|
|
function drawMap(now = performance.now()) {
|
|
mapDrawnAt = now;
|
|
const t = mapTransform(), { width, height, scale } = t;
|
|
if (!(width > 0 && height > 0)) return;
|
|
const dpr = Math.min(devicePixelRatio || 1, 2), pw = Math.round(width * dpr), ph = Math.round(height * dpr);
|
|
if (mapCanvas.width !== pw || mapCanvas.height !== ph) { mapCanvas.width = pw; mapCanvas.height = ph; }
|
|
const ctx = mapCanvas.getContext("2d");
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
ctx.clearRect(0, 0, width, height);
|
|
const rects = Object.fromEntries(MAP_STATES.map(state => [state, []]));
|
|
for (const def of world.chunks) {
|
|
const r = mapRect(def);
|
|
const x = r[0] * scale + t.x, y = r[1] * scale + t.y, w = (r[2] - r[0]) * scale, h = (r[3] - r[1]) * scale;
|
|
if (x > width || y > height || x + w < 0 || y + h < 0) continue;
|
|
rects[mapState(live.get(def.id))].push(x, y, w, h);
|
|
}
|
|
for (const state of MAP_STATES) {
|
|
const list = rects[state];
|
|
if (!list.length) continue;
|
|
ctx.fillStyle = MAP_COLORS[state];
|
|
ctx.beginPath();
|
|
for (let i = 0; i < list.length; i += 4) {
|
|
// A gap between neighbours once a cell is wide enough to show one; a cell is never thinner than a pixel.
|
|
const gap = list[i + 2] > 4 && list[i + 3] > 4 ? 0.5 : 0;
|
|
ctx.rect(list[i] + gap, list[i + 1] + gap, Math.max(list[i + 2] - 2 * gap, 1), Math.max(list[i + 3] - 2 * gap, 1));
|
|
}
|
|
ctx.fill();
|
|
}
|
|
const p = camera.position, px = p.x * scale + t.x, py = p.z * scale + t.y;
|
|
ctx.lineWidth = 1;
|
|
if (!world.lod_bands) { // the radius inside which chunks load; a world with LOD sets has a band per tier instead
|
|
ctx.strokeStyle = "rgba(230,232,235,.35)";
|
|
ctx.beginPath(); ctx.arc(px, py, LOAD_R * scale, 0, 2 * Math.PI); ctx.stroke();
|
|
}
|
|
ctx.strokeStyle = "#e6e8eb";
|
|
ctx.beginPath(); ctx.arc(world.spawn[0] * scale + t.x, world.spawn[2] * scale + t.y, 4, 0, 2 * Math.PI); ctx.stroke();
|
|
// The camera: an arrow along the view direction (yaw 0 looks down -z, which is up here), kept on the canvas
|
|
// when the camera is outside the part of the world the map shows.
|
|
const cx = Math.min(Math.max(px, 8), width - 8), cy = Math.min(Math.max(py, 8), height - 8);
|
|
const dx = -Math.sin(yaw), dz = -Math.cos(yaw);
|
|
ctx.fillStyle = "#ffffff"; ctx.strokeStyle = "#0d1014";
|
|
ctx.beginPath();
|
|
ctx.moveTo(cx + dx * 9, cy + dz * 9);
|
|
ctx.lineTo(cx - dx * 5 - dz * 5, cy - dz * 5 + dx * 5);
|
|
ctx.lineTo(cx - dx * 5 + dz * 5, cy - dz * 5 - dx * 5);
|
|
ctx.closePath(); ctx.fill(); ctx.stroke();
|
|
// Scale bar: the largest of 1, 2 or 5 x 10^n metres that is at most a quarter of the map's width.
|
|
const span = width / 4 / scale, pow = 10 ** Math.floor(Math.log10(span));
|
|
const bar = Number(((span >= 5 * pow ? 5 : span >= 2 * pow ? 2 : 1) * pow).toPrecision(1));
|
|
ctx.fillStyle = "#e6e8eb"; ctx.font = "11px ui-monospace, Consolas, monospace";
|
|
ctx.fillRect(MAP_PAD, height - MAP_PAD - 2, bar * scale, 2);
|
|
ctx.fillText(bar >= 1000 ? `${bar / 1000} km` : `${bar} m`, MAP_PAD, height - MAP_PAD - 6);
|
|
if (world.layout === "placements") ctx.fillText("N ↑", width - MAP_PAD - 22, MAP_PAD + 10);
|
|
describeMapCursor(t);
|
|
}
|
|
function refreshMap(now = performance.now()) { if (mapOpen && now - mapDrawnAt >= MAP_REDRAW_MS) drawMap(now); }
|
|
// The line under the map: what is under the cursor, or how to use the map when the cursor is elsewhere. Every
|
|
// redraw writes it again, so a chunk's state changes under a cursor that rests. Chunk ids come from world.json,
|
|
// so they are written as text.
|
|
function describeMapCursor(t = mapTransform()) {
|
|
if (!mapCursor) { mapInfo.textContent = `${world.chunks.length} chunks · click: fly there · wheel: zoom`; return; }
|
|
const [x, z] = mapPoint(mapCursor, t);
|
|
let hit = null, more = 0;
|
|
for (const def of world.chunks) {
|
|
const r = mapRect(def);
|
|
if (x < r[0] || x > r[2] || z < r[1] || z > r[3]) continue;
|
|
const state = mapState(live.get(def.id));
|
|
if (hit) more++;
|
|
if (!hit || MAP_STATES.indexOf(state) > MAP_STATES.indexOf(hit.state)) hit = { id: def.id, state };
|
|
}
|
|
// A placed world's RAGE X and Y, the numbers world_build.py --region takes.
|
|
mapInfo.textContent = `x ${Math.round(x)}, z ${Math.round(z)}` +
|
|
(world.layout === "placements" ? ` (RAGE X ${Math.round(x)}, Y ${Math.round(-z)})` : "") +
|
|
(hit ? ` · ${hit.id}: ${hit.state === "idle" ? "not loaded" : hit.state}${more ? ` (+${more} more here)` : ""}` : "");
|
|
}
|
|
function mapJump(e) {
|
|
const [x, z] = mapPoint(e), p = camera.position;
|
|
// Arrive above what stands there: the top of the full-detail chunks whose bounds hold the point, or the height
|
|
// the camera already has if that is more. Coarser LOD tiers are left out, since one distant model in them can
|
|
// span kilometres.
|
|
let top = -Infinity;
|
|
for (const { bounds: b, lod } of world.chunks)
|
|
if ((lod === undefined || lod === "hd" || lod === "orphan_hd") && x >= b[0] && x <= b[3] && z >= b[2] && z <= b[5])
|
|
top = Math.max(top, b[4]);
|
|
p.x = x; p.z = z; p.y = Math.max(p.y, top + MAP_JUMP_CLEARANCE);
|
|
drawMap();
|
|
}
|
|
function mapWheel(e) {
|
|
e.stopPropagation(); // over the map the wheel zooms the map; the window's handler would change the speed too
|
|
if (!e.deltaY || Math.abs(e.deltaX) > Math.abs(e.deltaY)) return;
|
|
const t = mapTransform(), [x, z] = mapPoint(e, t);
|
|
const zoom = Math.min(Math.max(mapZoom * (e.deltaY < 0 ? 1.25 : 0.8), 1), MAP_MAX_ZOOM);
|
|
if (zoom < 1.01) { mapZoom = 1; mapCenter = null; }
|
|
else {
|
|
// Keep the point under the cursor where it is.
|
|
mapZoom = zoom;
|
|
mapCenter = [x - (e.offsetX - t.width / 2) / (t.fit * zoom), z - (e.offsetY - t.height / 2) / (t.fit * zoom)];
|
|
}
|
|
drawMap();
|
|
}
|
|
function toggleMap() {
|
|
const panel = document.getElementById("map");
|
|
mapOpen = !mapOpen;
|
|
panel.classList.toggle("hidden", !mapOpen);
|
|
if (!mapOpen) return;
|
|
if (!mapCanvas) {
|
|
mapCanvas = panel.querySelector("canvas"); mapInfo = document.getElementById("map-info");
|
|
mapExtent = worldExtent();
|
|
const cursor = at => { mapCursor = at && { offsetX: at.offsetX, offsetY: at.offsetY }; describeMapCursor(); };
|
|
mapCanvas.addEventListener("mousemove", cursor);
|
|
mapCanvas.addEventListener("mouseleave", () => cursor(null));
|
|
mapCanvas.addEventListener("click", mapJump);
|
|
mapCanvas.addEventListener("wheel", mapWheel, { passive: true });
|
|
}
|
|
// The map is used with the cursor, which pointer lock hides. A click outside the map locks the mouse again.
|
|
if (document.pointerLockElement) document.exitPointerLock();
|
|
drawMap();
|
|
}
|
|
|
|
// ---------- main loop ----------
|
|
camera.position.fromArray(world.spawn);
|
|
function applyCamHash(hash) {
|
|
const cam = parseCamHash(hash);
|
|
if (cam) { camera.position.fromArray(cam.pos); yaw = cam.yaw; pitch = THREE.MathUtils.clamp(cam.pitch, -1.55, 1.55); }
|
|
}
|
|
applyCamHash(location.hash);
|
|
// A camera link opened in a tab that already shows the viewer changes only the hash, and the page does not reload:
|
|
// follow it, so a pasted link, or Back and Forward between two of them, moves the camera there.
|
|
addEventListener("hashchange", () => applyCamHash(location.hash));
|
|
if (world.test) {
|
|
document.getElementById("world-status").textContent = world.reason;
|
|
document.getElementById("banner").classList.remove("hidden");
|
|
}
|
|
const hud = document.getElementById("hud"), clock = new THREE.Clock();
|
|
let fpsT = 0, fpsN = 0, fps = 0;
|
|
const fwd = new THREE.Vector3(), right = new THREE.Vector3(), up = new THREE.Vector3(0, 1, 0);
|
|
renderer.setAnimationLoop(() => {
|
|
const dt = Math.min(clock.getDelta(), 0.1);
|
|
camera.quaternion.setFromEuler(new THREE.Euler(pitch, yaw, 0, "YXZ"));
|
|
camera.getWorldDirection(fwd); right.crossVectors(fwd, up).normalize();
|
|
// Slow is C, not Ctrl: with a movement key held, Ctrl makes browser shortcuts that a page cannot all cancel
|
|
// (Ctrl+W closes the tab; S, D and E open the save and bookmark dialogs and the address bar).
|
|
const v = new THREE.Vector3(), s = speed * (keys.has("ShiftLeft") || keys.has("ShiftRight") ? 5 : 1) * (keys.has("KeyC") ? 0.2 : 1);
|
|
if (keys.has("KeyW")) v.add(fwd); if (keys.has("KeyS")) v.sub(fwd);
|
|
if (keys.has("KeyD")) v.add(right); if (keys.has("KeyA")) v.sub(right);
|
|
if (keys.has("KeyE")) v.y += 1; if (keys.has("KeyQ")) v.y -= 1;
|
|
if (v.lengthSq()) camera.position.addScaledVector(v.normalize(), s * dt);
|
|
stream();
|
|
refreshMap();
|
|
renderer.render(scene, camera);
|
|
fpsN++; fpsT += dt; if (fpsT > 0.5) { fps = fpsN / fpsT; fpsN = 0; fpsT = 0; }
|
|
const p = camera.position, loaded = [...live.values()].filter(c => c.obj).length;
|
|
hud.innerHTML = `<b>${world.test ? "test terrain" : "world"}</b><br>pos ${p.x.toFixed(1)}, ${p.y.toFixed(1)}, ${p.z.toFixed(1)}<br>` +
|
|
`speed ${speed.toFixed(0)} m/s · ${fps.toFixed(0)} fps<br>chunks ${loaded}/${world.chunks.length} loaded, ${inflight} loading` +
|
|
(failed ? `, <span style="color:var(--warn)">${failed} load errors</span>` : "") +
|
|
`<br>draw calls ${renderer.info.render.calls}, tris ${(renderer.info.render.triangles / 1000).toFixed(0)}k`;
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|