Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Build a 2 MB first view of the hero port from the camera frustum (Part of #884)

Nguyen Xuan Ane6418d4992e7
diff --git a/public/showcase/port/scene.first.glb b/public/showcase/port/scene.first.glb
new file mode 100644
index 00000000..e7350ef9
Binary files /dev/null and b/public/showcase/port/scene.first.glb differ
diff --git a/public/showcase/port/scene.json b/public/showcase/port/scene.json
index 786ba425..be83b31c 100644
--- a/public/showcase/port/scene.json
+++ b/public/showcase/port/scene.json
@@ -3,6 +3,7 @@
"title": "Container port",
"blurb": "A container ship at berth between quay cranes and a stacked yard.",
"glb": "/showcase/port/scene.glb",
+ "glbFirstView": "/showcase/port/scene.first.glb",
"glbRest": ["/showcase/port/scene.rest.glb"],
"hdriPreview": "/showcase/port/sky.preview.hdr",
"hdri": "/showcase/port/sky.hdr",
diff --git a/scripts/build-hero-first-view.mjs b/scripts/build-hero-first-view.mjs
new file mode 100644
index 00000000..908bd672
--- /dev/null
+++ b/scripts/build-hero-first-view.mjs
@@ -0,0 +1,90 @@
+// Builds the "first view" of a hero world: a crop of the full world that holds only what the camera sees during the first seconds of
+// the flight, small enough for the home page to download and draw within a few seconds on a slow link while the full world streams in.
+//
+// node scripts/build-hero-first-view.mjs [--scene port] [--flight-seconds 5] [--near-pixels 3] [--far-pixels 6]
+// [--near-texture 512] [--far-texture 128] [--webp-quality 70] [--output <file>]
+//
+// The full world is the committed pair public/showcase/<scene>/scene.glb and scene.rest.glb. This script joins them in memory and
+// reads the flight from src/hero-lab/loop/scenes.js. A triangle no camera pose of the first --flight-seconds sees is dropped. The
+// rest is cut into bands by its distance from the nearest pose, and each band is simplified until the error is --near-pixels on
+// screen (--far-pixels beyond 400 m), so near geometry keeps its mesh and the far yard is coarse. Textures of materials seen from
+// nearer than 400 m keep --near-texture pixels, the rest get --far-texture. Every material that is still in view keeps its name,
+// so the page swaps to the full world with few shader changes. Nothing is read from the network.
+import { writeFileSync } from 'node:fs';
+import { dirname, join, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { NodeIO } from '@gltf-transform/core';
+import { ALL_EXTENSIONS, EXTMeshoptCompression } from '@gltf-transform/extensions';
+import { dequantize, mergeDocuments, meshopt, prune } from '@gltf-transform/functions';
+import { MeshoptDecoder, MeshoptEncoder, MeshoptSimplifier } from 'meshoptimizer';
+import { CROP_DEFAULTS, cropDocument, sampleFlightPoses, scaleTexturesByDistance } from './hero-first-view-crop.mjs';
+import { recodeGlbTextures } from './recode-glb-textures.mjs';
+
+const SCRIPT_DIRECTORY = dirname(fileURLToPath(import.meta.url));
+const DEFAULTS = { scene: 'port', flightSeconds: CROP_DEFAULTS.flightSeconds, nearPixels: CROP_DEFAULTS.nearPixels, farPixels: CROP_DEFAULTS.farPixels, nearTexture: 512, farTexture: 128, webpQuality: 70, output: '' };
+
+export function parseArguments(argv) {
+ const options = { ...DEFAULTS };
+ for (let index = 0; index < argv.length; index++) {
+ const name = argv[index];
+ if (!name.startsWith('--')) throw new Error(`Unexpected argument ${name}.`);
+ const key = name.slice(2).replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
+ if (!(key in DEFAULTS)) throw new Error(`Unknown option ${name}.`);
+ const raw = argv[++index];
+ options[key] = typeof DEFAULTS[key] === 'number' ? Number(raw) : raw;
+ if (typeof DEFAULTS[key] === 'number' && !(options[key] > 0)) throw new Error(`Option ${name} needs a positive number.`);
+ }
+ return options;
+}
+
+async function readJoinedWorld(directory) {
+ await Promise.all([MeshoptDecoder.ready, MeshoptEncoder.ready]);
+ const io = new NodeIO()
+ .registerExtensions([...ALL_EXTENSIONS, EXTMeshoptCompression])
+ .registerDependencies({ 'meshopt.decoder': MeshoptDecoder, 'meshopt.encoder': MeshoptEncoder });
+ const document = await io.read(join(directory, 'scene.glb'));
+ const rest = await io.read(join(directory, 'scene.rest.glb'));
+ mergeDocuments(document, rest);
+ // The merge brings the rest's scene along as a second scene; its nodes join the first scene.
+ const [firstScene, ...otherScenes] = document.getRoot().listScenes();
+ for (const scene of otherScenes) {
+ for (const node of scene.listChildren()) firstScene.addChild(node);
+ scene.dispose();
+ }
+ firstScene.setExtras({});
+ // A GLB holds one buffer: every accessor moves to the first one.
+ const [firstBuffer, ...otherBuffers] = document.getRoot().listBuffers();
+ for (const accessor of document.getRoot().listAccessors()) accessor.setBuffer(firstBuffer);
+ for (const buffer of otherBuffers) buffer.dispose();
+ // The simplifier reads float positions; the committed worlds store them quantized.
+ await document.transform(dequantize());
+ document.createExtension(EXTMeshoptCompression).setRequired(true).setEncoderOptions({ method: EXTMeshoptCompression.EncoderMethod.FILTER });
+ return { io, document };
+}
+
+export async function buildFirstView(options) {
+ const directory = resolve(SCRIPT_DIRECTORY, '..', 'public', 'showcase', options.scene);
+ await MeshoptSimplifier.ready;
+ const { io, document } = await readJoinedWorld(directory);
+ const poses = sampleFlightPoses(options.scene, options.flightSeconds);
+ const cropOptions = { ...CROP_DEFAULTS, flightSeconds: options.flightSeconds, nearPixels: options.nearPixels, farPixels: options.farPixels };
+ const { nearestByMaterial, totals } = cropDocument(document, poses, cropOptions);
+ await scaleTexturesByDistance(document, nearestByMaterial, { farFrom: cropOptions.farFrom, nearTexture: options.nearTexture, farTexture: options.farTexture, webpQuality: options.webpQuality });
+ await document.transform(prune({ keepLeaves: false }));
+ await document.transform(meshopt({ encoder: MeshoptEncoder, level: 'medium', quantizePosition: 16, quantizeNormal: 8, quantizeTexcoord: 16 }));
+ const written = Buffer.from(await io.writeBinary(document));
+ const recoded = await recodeGlbTextures(written, { webpQuality: options.webpQuality, maxTexture: options.nearTexture });
+ const report = `triangles per band ${totals.join('/')} (${totals.reduce((sum, count) => sum + count, 0)} total)`;
+ return { glb: recoded.glb, report };
+}
+
+async function main() {
+ const options = parseArguments(process.argv.slice(2));
+ const output = options.output || resolve(SCRIPT_DIRECTORY, '..', 'public', 'showcase', options.scene, 'scene.first.glb');
+ const { glb, report } = await buildFirstView(options);
+ writeFileSync(output, glb);
+ console.log(report);
+ console.log(JSON.stringify({ output, bytes: glb.length }));
+}
+
+if (import.meta.url === `file:///${process.argv[1].replace(/\\/g, '/')}`) await main();
diff --git a/scripts/hero-first-view-crop.mjs b/scripts/hero-first-view-crop.mjs
new file mode 100644
index 00000000..798c9f79
--- /dev/null
+++ b/scripts/hero-first-view-crop.mjs
@@ -0,0 +1,235 @@
+// The camera crop behind scripts/build-hero-first-view.mjs: keeps only the triangles the camera sees during the first seconds of a
+// hero flight, and gives each distance band its own simplification, so what the camera passes close keeps its detail while the far
+// yard is drawn with a fraction of its triangles. Nothing is read from the network.
+import { MeshoptSimplifier } from 'meshoptimizer';
+import sharp from 'sharp';
+import { SCENES } from '../src/hero-lab/loop/scenes.js';
+import { createCameraSpline } from '../src/hero-lab/camera-spline.js';
+
+const POSE_SAMPLES = 60;
+const UNUSED = 0xffffffff;
+const MINIMUM_INDEX_COUNT = 36;
+
+export const CROP_DEFAULTS = {
+ flightSeconds: 5,
+ // Upper edges (metres from the nearest camera pose) of the distance bands; the last band has no edge.
+ bandEdges: [100, 170, 260, 400, 650],
+ nearPixels: 3,
+ farPixels: 6,
+ farFrom: 400,
+ height: 900,
+ aspect: 2.2,
+ margin: 1.1,
+};
+
+// The camera poses of the flight from 0 to flightSeconds, with the basis of each one.
+export function sampleFlightPoses(sceneId, flightSeconds) {
+ const scene = SCENES.find(entry => entry.id === sceneId);
+ if (!scene) throw new Error(`No scene ${sceneId} in scenes.js.`);
+ const route = createCameraSpline(scene.keys);
+ const poses = [];
+ for (let index = 0; index <= POSE_SAMPLES; index++) {
+ const seconds = (index / POSE_SAMPLES) * flightSeconds;
+ const pose = route(seconds / scene.seconds);
+ poses.push(poseWithBasis(pose));
+ }
+ return poses;
+}
+
+function poseWithBasis(pose) {
+ const forward = normalized(subtract(pose.target, pose.eye));
+ const right = normalized(cross(forward, [0, 1, 0]));
+ const up = cross(right, forward);
+ return { eye: pose.eye, forward, right, up, tanVertical: Math.tan((pose.fov * Math.PI) / 360) };
+}
+
+function subtract(left, right) {
+ return [left[0] - right[0], left[1] - right[1], left[2] - right[2]];
+}
+
+function dot(left, right) {
+ return left[0] * right[0] + left[1] * right[1] + left[2] * right[2];
+}
+
+function cross(left, right) {
+ return [left[1] * right[2] - left[2] * right[1], left[2] * right[0] - left[0] * right[2], left[0] * right[1] - left[1] * right[0]];
+}
+
+function normalized(vector) {
+ const length = Math.hypot(vector[0], vector[1], vector[2]);
+ return [vector[0] / length, vector[1] / length, vector[2] / length];
+}
+
+// Nearest distance from the camera poses to a sphere that any of them sees; Infinity when none does.
+export function nearestVisibleDistance(poses, center, radius, options) {
+ let nearest = Infinity;
+ for (const pose of poses) {
+ const relative = subtract(center, pose.eye);
+ const depth = dot(relative, pose.forward);
+ if (depth + radius < 0) continue;
+ const tanVertical = pose.tanVertical * options.margin;
+ const tanHorizontal = tanVertical * options.aspect;
+ const sideSlack = radius * Math.sqrt(1 + tanHorizontal * tanHorizontal);
+ const upSlack = radius * Math.sqrt(1 + tanVertical * tanVertical);
+ if (Math.abs(dot(relative, pose.right)) > tanHorizontal * depth + sideSlack) continue;
+ if (Math.abs(dot(relative, pose.up)) > tanVertical * depth + upSlack) continue;
+ nearest = Math.min(nearest, Math.max(0, Math.hypot(relative[0], relative[1], relative[2]) - radius));
+ }
+ return nearest;
+}
+
+function bandOf(distance, edges) {
+ let band = 0;
+ while (band < edges.length && distance >= edges[band]) band++;
+ return band;
+}
+
+function worldPoint(matrix, array, vertex) {
+ const x = array[vertex * 3];
+ const y = array[vertex * 3 + 1];
+ const z = array[vertex * 3 + 2];
+ return [
+ matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12],
+ matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13],
+ matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14],
+ ];
+}
+
+function largestScale(matrix) {
+ return Math.max(Math.hypot(matrix[0], matrix[1], matrix[2]), Math.hypot(matrix[4], matrix[5], matrix[6]), Math.hypot(matrix[8], matrix[9], matrix[10]));
+}
+
+// Per-vertex texture coordinates and normals, so the simplifier does not smear a texture atlas or fold a surface it must keep.
+function attributesFor(primitive) {
+ const coordinates = primitive.getAttribute('TEXCOORD_0');
+ const normals = primitive.getAttribute('NORMAL');
+ if (!coordinates || !normals) return null;
+ const count = coordinates.getCount();
+ const values = new Float32Array(count * 5);
+ const element = [0, 0, 0];
+ for (let index = 0; index < count; index++) {
+ coordinates.getElement(index, element);
+ values[index * 5] = element[0];
+ values[index * 5 + 1] = element[1];
+ normals.getElement(index, element);
+ values.set(element, index * 5 + 2);
+ }
+ return { values, weights: [1, 1, 0.5, 0.5, 0.5] };
+}
+
+function simplifyBand(indices, positions, attributes, absoluteError) {
+ if (indices.length <= MINIMUM_INDEX_COUNT) return indices;
+ const target = MINIMUM_INDEX_COUNT;
+ const flags = ['LockBorder', 'ErrorAbsolute', 'Permissive', 'Sparse'];
+ const [reduced] = attributes
+ ? MeshoptSimplifier.simplifyWithAttributes(indices, positions, 3, attributes.values, 5, attributes.weights, null, target, absoluteError, flags)
+ : MeshoptSimplifier.simplify(indices, positions, 3, target, absoluteError, flags);
+ return reduced;
+}
+
+// A new accessor holding the listed source elements (raw values, so normalised data stays encoded). `slots[source]` is the target slot.
+function pickElements(document, source, slots, count) {
+ const size = source.getElementSize();
+ const raw = source.getArray();
+ const array = new raw.constructor(count * size);
+ for (let index = 0; index < slots.length; index++) {
+ if (slots[index] !== UNUSED) array.set(raw.subarray(index * size, (index + 1) * size), slots[index] * size);
+ }
+ return document.createAccessor().setType(source.getType()).setNormalized(source.getNormalized()).setArray(array);
+}
+
+function bandError(band, options, metresPerPixelAtOneMetre) {
+ const edges = options.bandEdges;
+ const lowerEdge = band === 0 ? 0 : edges[band - 1];
+ const pixels = lowerEdge >= options.farFrom ? options.farPixels : options.nearPixels;
+ return pixels * Math.max(lowerEdge, 30) * metresPerPixelAtOneMetre;
+}
+
+// Crops and simplifies one primitive in place. Returns what it kept: triangles per band and the nearest distance seen.
+export function cropPrimitive(document, node, primitive, poses, options, metresPerPixelAtOneMetre) {
+ const source = primitive.getIndices().getArray();
+ const raw = primitive.getAttribute('POSITION').getArray();
+ const matrix = node.getWorldMatrix();
+ const scale = largestScale(matrix);
+ const bandIndices = Array.from({ length: options.bandEdges.length + 1 }, () => []);
+ let nearest = Infinity;
+ for (let triangle = 0; triangle < source.length; triangle += 3) {
+ const corners = [0, 1, 2].map(corner => worldPoint(matrix, raw, source[triangle + corner]));
+ const center = [0, 1, 2].map(axis => (corners[0][axis] + corners[1][axis] + corners[2][axis]) / 3);
+ const radius = Math.max(...corners.map(corner => Math.hypot(corner[0] - center[0], corner[1] - center[1], corner[2] - center[2])));
+ const distance = nearestVisibleDistance(poses, center, radius, options);
+ if (distance === Infinity) continue;
+ nearest = Math.min(nearest, distance);
+ bandIndices[bandOf(distance, options.bandEdges)].push(source[triangle], source[triangle + 1], source[triangle + 2]);
+ }
+ const positions = Float32Array.from(raw);
+ const attributes = attributesFor(primitive);
+ const kept = [];
+ const triangles = [];
+ bandIndices.forEach((indices, band) => {
+ const reduced = simplifyBand(Uint32Array.from(indices), positions, attributes, bandError(band, options, metresPerPixelAtOneMetre) / scale);
+ triangles.push(reduced.length / 3);
+ for (const index of reduced) kept.push(index);
+ });
+ if (kept.length === 0) return { empty: true, triangles, nearest };
+ const finalIndices = Uint32Array.from(kept);
+ const [remap, unique] = MeshoptSimplifier.compactMesh(finalIndices);
+ for (const semantic of primitive.listSemantics()) {
+ primitive.setAttribute(semantic, pickElements(document, primitive.getAttribute(semantic), remap, unique));
+ }
+ const indexArray = unique <= 65535 ? Uint16Array.from(finalIndices) : finalIndices;
+ primitive.setIndices(document.createAccessor().setType('SCALAR').setArray(indexArray));
+ return { empty: false, triangles, nearest };
+}
+
+// Crops every mesh of the document. Nodes with nothing in view are removed. Returns the nearest distance per material.
+export function cropDocument(document, poses, options) {
+ const narrowestFov = Math.min(...poses.map(pose => 2 * Math.atan(pose.tanVertical)));
+ const metresPerPixelAtOneMetre = (2 * Math.tan(narrowestFov / 2)) / options.height;
+ const nearestByMaterial = new Map();
+ const totals = new Array(options.bandEdges.length + 1).fill(0);
+ for (const node of document.getRoot().listNodes()) {
+ const mesh = node.getMesh();
+ if (!mesh) continue;
+ for (const primitive of mesh.listPrimitives()) {
+ const result = cropPrimitive(document, node, primitive, poses, options, metresPerPixelAtOneMetre);
+ result.triangles.forEach((count, band) => (totals[band] += count));
+ if (result.empty) {
+ mesh.removePrimitive(primitive);
+ primitive.dispose();
+ continue;
+ }
+ const material = primitive.getMaterial();
+ nearestByMaterial.set(material, Math.min(nearestByMaterial.get(material) ?? Infinity, result.nearest));
+ }
+ if (mesh.listPrimitives().length === 0) {
+ node.setMesh(null);
+ mesh.dispose();
+ }
+ }
+ return { nearestByMaterial, totals };
+}
+
+function texturesOf(material) {
+ return [material.getBaseColorTexture(), material.getNormalTexture(), material.getOcclusionTexture(), material.getEmissiveTexture(), material.getMetallicRoughnessTexture()].filter(Boolean);
+}
+
+// Textures of materials seen from nearer than farFrom keep nearTexture pixels, the rest get farTexture.
+export async function scaleTexturesByDistance(document, nearestByMaterial, options) {
+ const limits = new Map();
+ for (const [material, nearest] of nearestByMaterial) {
+ const limit = nearest < options.farFrom ? options.nearTexture : options.farTexture;
+ for (const texture of texturesOf(material)) limits.set(texture, Math.max(limits.get(texture) ?? 0, limit));
+ }
+ for (const [texture, limit] of limits) {
+ const [width, height] = texture.getSize() ?? [0, 0];
+ if (Math.max(width, height) <= limit) continue;
+ const scale = limit / Math.max(width, height);
+ const resized = await sharp(Buffer.from(texture.getImage()))
+ .resize(Math.max(1, Math.round(width * scale)), Math.max(1, Math.round(height * scale)), { kernel: 'lanczos3' })
+ .webp({ quality: options.webpQuality, effort: 6 })
+ .toBuffer();
+ texture.setImage(new Uint8Array(resized));
+ texture.setMimeType('image/webp');
+ }
+}
diff --git a/scripts/measure-hero-load.mjs b/scripts/measure-hero-load.mjs
new file mode 100644
index 00000000..fd8660f1
--- /dev/null
+++ b/scripts/measure-hero-load.mjs
@@ -0,0 +1,296 @@
+// Measures how the home hero loads in headless Chrome on the GPU, on a throttled or unthrottled link, from a built site folder.
+//
+// node scripts/measure-hero-load.mjs --dist <built site folder> [--label name] [--runs 3] [--conditions fast4g,slow,none]
+// [--viewports desktop,phone] [--out directory]
+// node scripts/measure-hero-load.mjs --dist <folder> --stills <directory> stills before and after the swap, with PSNR
+// node scripts/measure-hero-load.mjs --dist <folder> --video <file.mp4> CDP screencast of the first 8 s on Fast 4G
+//
+// Build the folder with `npx vite build --outDir <folder>`. The folder is served by a small static server on a free port from 5280
+// up, with brotli for text, as a CDN would. Network time comes from the DevTools throttle, which GPU load does not change.
+// posterEndMs responseEnd of the poster image
+// firstFrameMs the first draw on a world canvas (the first live 3D frame)
+// detailMs when the full world is drawn (the 'detailed' loop event); without staged loading it equals firstFrameMs
+// bytesBeforeFirstFrame, bytesTotal transfer bytes (compressed size on the wire) up to the first live frame, and up to detailMs
+import { createServer } from 'node:http';
+import { mkdirSync, readFileSync, writeFileSync, existsSync, statSync, rmSync } from 'node:fs';
+import { extname, join, normalize, resolve } from 'node:path';
+import { brotliCompressSync, constants } from 'node:zlib';
+import { spawnSync } from 'node:child_process';
+import { chromium } from 'playwright';
+import sharp from 'sharp';
+
+const CHROME_PATH = 'C:/Program Files/Google/Chrome/Application/chrome.exe';
+const GPU_ARGUMENTS = ['--use-angle=d3d11', '--enable-gpu', '--ignore-gpu-blocklist'];
+const FIRST_FRAME_MARKER = 'hero-first-world-draw';
+const CONDITIONS = {
+ fast4g: { label: 'Fast 4G', downloadBitsPerSecond: 9e6, uploadBitsPerSecond: 1.5e6, latency: 60 },
+ slow: { label: '1.6 Mbit/s, 150 ms', downloadBitsPerSecond: 1.6e6, uploadBitsPerSecond: 0.75e6, latency: 150 },
+ none: { label: 'unthrottled', downloadBitsPerSecond: 0, uploadBitsPerSecond: 0, latency: 0 },
+};
+const VIEWPORTS = {
+ desktop: { width: 1440, height: 900, deviceScaleFactor: 1, isMobile: false },
+ phone: { width: 375, height: 812, deviceScaleFactor: 2, isMobile: true },
+};
+const CONTENT_TYPES = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.json': 'application/json', '.svg': 'image/svg+xml', '.webp': 'image/webp', '.glb': 'model/gltf-binary', '.hdr': 'application/octet-stream', '.png': 'image/png', '.woff2': 'font/woff2' };
+const TEXT_TYPES = new Set(['.html', '.js', '.css', '.json', '.svg']);
+
+function parseArguments(argv) {
+ const options = { dist: null, label: 'build', runs: 3, conditions: ['fast4g', 'slow', 'none'], viewports: ['desktop', 'phone'], out: null, stills: null, video: null, timeoutSeconds: 120 };
+ for (let index = 0; index < argv.length; index++) {
+ const flag = argv[index];
+ const value = () => argv[++index];
+ if (flag === '--dist') options.dist = resolve(value());
+ else if (flag === '--label') options.label = value();
+ else if (flag === '--runs') options.runs = Number(value());
+ else if (flag === '--conditions') options.conditions = value().split(',');
+ else if (flag === '--viewports') options.viewports = value().split(',');
+ else if (flag === '--out') options.out = resolve(value());
+ else if (flag === '--stills') options.stills = resolve(value());
+ else if (flag === '--video') options.video = resolve(value());
+ else if (flag === '--timeout') options.timeoutSeconds = Number(value());
+ else throw new Error(`Unknown argument ${flag}.`);
+ }
+ if (!options.dist || !existsSync(join(options.dist, 'index.html'))) throw new Error('Pass --dist <folder with index.html>.');
+ return options;
+}
+
+async function startServer(root) {
+ const compressed = new Map();
+ const server = createServer((request, response) => {
+ const pathname = decodeURIComponent(new URL(request.url, 'http://localhost').pathname);
+ let file = normalize(join(root, pathname === '/' ? 'index.html' : pathname));
+ if (!file.startsWith(root)) return response.writeHead(403).end();
+ if (!existsSync(file) || statSync(file).isDirectory()) file = existsSync(`${file}.html`) ? `${file}.html` : null;
+ if (!file) return response.writeHead(404).end('not found');
+ const extension = extname(file);
+ const headers = { 'content-type': CONTENT_TYPES[extension] ?? 'application/octet-stream', 'cache-control': 'no-store' };
+ let body = readFileSync(file);
+ if (TEXT_TYPES.has(extension) && /\bbr\b/.test(request.headers['accept-encoding'] ?? '')) {
+ if (!compressed.has(file)) compressed.set(file, brotliCompressSync(body, { params: { [constants.BROTLI_PARAM_QUALITY]: 5 } }));
+ body = compressed.get(file);
+ headers['content-encoding'] = 'br';
+ }
+ headers['content-length'] = body.length;
+ response.writeHead(200, headers).end(body);
+ });
+ for (let port = 5280; port < 5400; port++) {
+ const listening = await new Promise(resolveListening => {
+ server.once('error', () => resolveListening(false));
+ server.listen(port, '127.0.0.1', () => resolveListening(true));
+ });
+ if (listening) return { server, origin: `http://127.0.0.1:${port}` };
+ }
+ throw new Error('No free port from 5280 to 5399.');
+}
+
+// Runs in the page before any script: stamps the moment the page marks the live canvas as shown (data-live on the stage), which is the
+// first live 3D frame a visitor sees. The poster fades out only after that.
+function installFrameTimer(marker) {
+ window.__firstWorldDraw = null;
+ new MutationObserver(() => {
+ if (window.__firstWorldDraw === null && document.querySelector('#stage')?.dataset.live === 'true') {
+ window.__firstWorldDraw = performance.now();
+ console.debug(marker);
+ }
+ }).observe(document, { subtree: true, childList: true, attributes: true, attributeFilter: ['data-live'] });
+}
+
+async function openPage(browser, viewport, condition) {
+ const context = await browser.newContext({ viewport: { width: viewport.width, height: viewport.height }, deviceScaleFactor: viewport.deviceScaleFactor, isMobile: viewport.isMobile });
+ const page = await context.newPage();
+ const session = await context.newCDPSession(page);
+ await session.send('Network.enable');
+ await session.send('Network.setCacheDisabled', { cacheDisabled: true });
+ if (condition.downloadBitsPerSecond > 0) {
+ await session.send('Network.emulateNetworkConditions', {
+ offline: false,
+ latency: condition.latency,
+ downloadThroughput: condition.downloadBitsPerSecond / 8,
+ uploadThroughput: condition.uploadBitsPerSecond / 8,
+ });
+ }
+ await page.addInitScript(installFrameTimer, FIRST_FRAME_MARKER);
+ return { context, page, session };
+}
+
+function watchBytes(session, page) {
+ const state = { total: 0, atFirstFrame: null, timeline: [], firstRequestAt: null };
+ session.on('Network.requestWillBeSent', event => { state.firstRequestAt ??= event.timestamp; });
+ session.on('Network.dataReceived', event => {
+ state.total += event.encodedDataLength;
+ state.timeline.push([(event.timestamp - state.firstRequestAt) * 1000, state.total]);
+ });
+ page.on('console', message => {
+ if (message.text() === FIRST_FRAME_MARKER) state.atFirstFrame = state.total;
+ });
+ return state;
+}
+
+function bytesAt(state, milliseconds) {
+ let bytes = 0;
+ for (const [at, total] of state.timeline) if (at <= milliseconds) bytes = total;
+ return bytes;
+}
+
+async function measureOnce(browser, options, viewportName, conditionName) {
+ const condition = CONDITIONS[conditionName];
+ const { context, page, session } = await openPage(browser, VIEWPORTS[viewportName], condition);
+ const bytes = watchBytes(session, page);
+ const errors = [];
+ page.on('pageerror', error => errors.push(error.message));
+ await page.goto(`${options.origin}/`, { waitUntil: 'commit' });
+ const finished = async () => page.evaluate(() => {
+ const events = window.__loop?.events ?? [];
+ const detailed = events.find(event => event.kind === 'detailed');
+ const failed = events.find(event => event.kind === 'failed');
+ return { first: window.__firstWorldDraw, detailed: detailed?.at ?? null, failed: failed?.message ?? null, state: window.__loop?.detail?.() ?? null, mode: window.__heroMode };
+ });
+ const deadline = Date.now() + options.timeoutSeconds * 1000;
+ let status = await finished();
+ while (Date.now() < deadline && (status.first === null || (status.state === 'pending' && status.detailed === null)) && !status.failed) {
+ await new Promise(resolveWait => setTimeout(resolveWait, 100));
+ status = await finished();
+ }
+ const poster = await page.evaluate(() => {
+ const entry = performance.getEntriesByType('resource').find(resource => resource.name.includes('poster-b-port'));
+ return entry ? Math.round(entry.responseEnd) : null;
+ });
+ const firstFrameMs = status.first === null ? null : Math.round(status.first);
+ const detailMs = status.detailed ?? firstFrameMs;
+ const result = {
+ viewport: viewportName,
+ condition: condition.label,
+ mode: status.mode,
+ posterEndMs: poster,
+ firstFrameMs,
+ detailMs,
+ bytesBeforeFirstFrame: bytes.atFirstFrame,
+ bytesAtDetail: detailMs === null ? null : bytesAt(bytes, detailMs),
+ errors: [...errors, ...(status.failed ? [status.failed] : [])],
+ };
+ await context.close();
+ return result;
+}
+
+const median = values => {
+ const present = values.filter(value => value !== null && value !== undefined).sort((first, second) => first - second);
+ return present.length ? present[Math.floor(present.length / 2)] : null;
+};
+
+async function runMatrix(browser, options) {
+ const rows = [];
+ for (const viewportName of options.viewports) {
+ for (const conditionName of options.conditions) {
+ const runs = [];
+ for (let run = 0; run < options.runs; run++) runs.push(await measureOnce(browser, options, viewportName, conditionName));
+ const summary = {
+ build: options.label,
+ viewport: viewportName,
+ condition: CONDITIONS[conditionName].label,
+ mode: runs[0].mode,
+ runs: runs.length,
+ posterEndMs: median(runs.map(run => run.posterEndMs)),
+ firstFrameMs: median(runs.map(run => run.firstFrameMs)),
+ firstFrameRangeMs: [Math.min(...runs.map(run => run.firstFrameMs ?? Infinity)), Math.max(...runs.map(run => run.firstFrameMs ?? 0))],
+ detailMs: median(runs.map(run => run.detailMs)),
+ detailRangeMs: [Math.min(...runs.map(run => run.detailMs ?? Infinity)), Math.max(...runs.map(run => run.detailMs ?? 0))],
+ bytesBeforeFirstFrame: median(runs.map(run => run.bytesBeforeFirstFrame)),
+ bytesAtDetail: median(runs.map(run => run.bytesAtDetail)),
+ errors: runs.flatMap(run => run.errors),
+ };
+ rows.push(summary);
+ console.log(JSON.stringify(summary));
+ }
+ }
+ if (options.out) {
+ mkdirSync(options.out, { recursive: true });
+ writeFileSync(join(options.out, `${options.label}.json`), JSON.stringify(rows, null, 2));
+ }
+}
+
+async function captureStage(page, file) {
+ const buffer = await page.screenshot({ clip: await page.evaluate(() => {
+ const box = document.querySelector('#stage').getBoundingClientRect();
+ return { x: box.x, y: box.y, width: box.width, height: box.height };
+ }) });
+ writeFileSync(file, buffer);
+ return buffer;
+}
+
+async function peakSignalToNoise(firstBuffer, secondBuffer) {
+ const [first, second] = await Promise.all([firstBuffer, secondBuffer].map(buffer => sharp(buffer).removeAlpha().raw().toBuffer({ resolveWithObject: true })));
+ if (first.data.length !== second.data.length) throw new Error('Stills differ in size.');
+ let squared = 0;
+ for (let index = 0; index < first.data.length; index++) squared += (first.data[index] - second.data[index]) ** 2;
+ const meanSquared = squared / first.data.length;
+ return meanSquared === 0 ? Infinity : 10 * Math.log10((255 * 255) / meanSquared);
+}
+
+// Stills of the first view and the full world at the same pose: capture mode draws exactly the frames the page asks for.
+async function captureStills(browser, options) {
+ mkdirSync(options.stills, { recursive: true });
+ const report = [];
+ for (const flightSeconds of [0.5, 2.5, 5]) {
+ const { context, page } = await openPage(browser, VIEWPORTS.desktop, CONDITIONS.none);
+ await page.goto(`${options.origin}/?capture=1&detail=hold`, { waitUntil: 'load' });
+ await page.waitForFunction(() => window.__loop?.ready === true, null, { timeout: options.timeoutSeconds * 1000 });
+ for (let elapsed = 0; elapsed < flightSeconds - 1e-6; elapsed += 0.05) await page.evaluate(() => window.__loop.step(0.05));
+ await page.evaluate(() => window.__loop.step(0));
+ await page.waitForTimeout(1500);
+ const label = String(flightSeconds).replace('.', '_');
+ const before = await captureStage(page, join(options.stills, `before-swap-${label}s.png`));
+ const swapState = await page.evaluate(() => window.__loop.releaseDetail());
+ await page.evaluate(() => window.__loop.step(0));
+ await page.waitForTimeout(500);
+ const after = await captureStage(page, join(options.stills, `after-swap-${label}s.png`));
+ const psnr = await peakSignalToNoise(before, after);
+ report.push({ flightSeconds, swapState, psnrDecibels: Number(psnr.toFixed(2)) });
+ console.log(JSON.stringify(report.at(-1)));
+ await context.close();
+ }
+ writeFileSync(join(options.stills, 'psnr.json'), JSON.stringify(report, null, 2));
+}
+
+// A CDP screencast of the first 8 s on Fast 4G, encoded with ffmpeg at 960x540 as a preview.
+async function recordVideo(browser, options) {
+ const frameDirectory = join(options.video, '..', 'frames-temp');
+ rmSync(frameDirectory, { recursive: true, force: true });
+ mkdirSync(frameDirectory, { recursive: true });
+ const { context, page, session } = await openPage(browser, VIEWPORTS.desktop, CONDITIONS.fast4g);
+ const frames = [];
+ session.on('Page.screencastFrame', async event => {
+ frames.push({ at: event.metadata.timestamp, data: event.data });
+ await session.send('Page.screencastFrameAck', { sessionId: event.sessionId }).catch(() => {});
+ });
+ await session.send('Page.startScreencast', { format: 'jpeg', quality: 70, maxWidth: 960, maxHeight: 540, everyNthFrame: 1 });
+ await page.goto(`${options.origin}/`, { waitUntil: 'commit' });
+ await page.waitForTimeout(8000);
+ await session.send('Page.stopScreencast');
+ await context.close();
+ const lines = [];
+ frames.forEach((frame, index) => {
+ const name = `frame-${String(index).padStart(5, '0')}.jpg`;
+ writeFileSync(join(frameDirectory, name), Buffer.from(frame.data, 'base64'));
+ const duration = index + 1 < frames.length ? frames[index + 1].at - frame.at : 0.05;
+ lines.push(`file '${name}'`, `duration ${Math.max(duration, 0.01).toFixed(4)}`);
+ });
+ lines.push(`file 'frame-${String(frames.length - 1).padStart(5, '0')}.jpg'`);
+ writeFileSync(join(frameDirectory, 'list.txt'), lines.join('\n'));
+ const encoded = spawnSync('ffmpeg', ['-y', '-f', 'concat', '-safe', '0', '-i', join(frameDirectory, 'list.txt'), '-vf', 'scale=960:540,fps=30,format=yuv420p', '-c:v', 'libx264', '-crf', '30', '-preset', 'medium', '-movflags', '+faststart', options.video], { encoding: 'utf8' });
+ if (encoded.status !== 0) throw new Error(`ffmpeg failed: ${encoded.stderr.slice(-400)}`);
+ rmSync(frameDirectory, { recursive: true, force: true });
+ console.log(JSON.stringify({ video: options.video, frames: frames.length, bytes: statSync(options.video).size }));
+}
+
+const options = parseArguments(process.argv.slice(2));
+const { server, origin } = await startServer(options.dist);
+options.origin = origin;
+const browser = await chromium.launch({ executablePath: CHROME_PATH, headless: true, args: GPU_ARGUMENTS });
+try {
+ if (options.stills) await captureStills(browser, options);
+ else if (options.video) await recordVideo(browser, options);
+ else await runMatrix(browser, options);
+} finally {
+ await browser.close();
+ server.close();
+}
diff --git a/scripts/simplify-hero-glb.mjs b/scripts/simplify-hero-glb.mjs
index b109601d..90b342c6 100644
--- a/scripts/simplify-hero-glb.mjs
+++ b/scripts/simplify-hero-glb.mjs
@@ -5,10 +5,14 @@
//
// node scripts/simplify-hero-glb.mjs <scene id> <input.glb> <output.glb> [--pixels 1.2] [--levels 0.0008,0.005,0.025,0.1]
// [--max-texture 1024] [--prop-texture 512] [--prop-prefix m_] [--webp-quality 78] [--height 900]
+// [--world-error 0.3 --uv-weight 1]
//
// The route is read from src/hero-lab/loop/scenes.js, so rerun this after a flight changes.
// --levels relative errors (share of the mesh's own size) the copies are simplified to; the first is applied to every mesh
// --prop-prefix materials whose name starts with this text get --prop-texture instead of --max-texture
+// --world-error a first view instead of levels: every mesh gets one copy simplified until it moves by this many world units (metres)
+// at most, whatever its node scale (--levels is then ignored, and so is the route)
+// --uv-weight how much the simplifier keeps texture coordinates and normals in place (0 ignores them, 1 is a good start)
// Nothing is read from the network and no input script is run.
import { NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS, EXTMeshoptCompression, EXTMeshGPUInstancing } from '@gltf-transform/extensions';
@@ -18,7 +22,7 @@ import sharp from 'sharp';
import { SCENES } from '../src/hero-lab/loop/scenes.js';
import { createCameraSpline } from '../src/hero-lab/camera-spline.js';
-const DEFAULTS = { pixels: 1.2, levels: '0.0008,0.005,0.025,0.1', maxTexture: 1024, propTexture: 512, propPrefix: 'm_', webpQuality: 78, height: 900 };
+const DEFAULTS = { pixels: 1.2, levels: '0.0008,0.005,0.025,0.1', maxTexture: 1024, propTexture: 512, propPrefix: 'm_', webpQuality: 78, height: 900, worldError: 0, uvWeight: 0 };
const ROUTE_SAMPLES = 240;
const MINIMUM_TRIANGLES = 12;
const UNUSED = 0xffffffff;
@@ -95,11 +99,35 @@ function pickElements(document, source, slots, count) {
}
// A copy of one primitive simplified until its error reaches relativeError of its own size; the same primitive when nothing is gained.
-function simplifiedPrimitive(document, primitive, relativeError) {
+// Per-vertex texture coordinates and normals, so the simplifier does not smear a texture atlas or fold a surface it must keep.
+function attributesFor(primitive, weight) {
+ const coordinates = primitive.getAttribute('TEXCOORD_0');
+ const normals = primitive.getAttribute('NORMAL');
+ if (!weight || !coordinates || !normals) return null;
+ const count = coordinates.getCount();
+ const values = new Float32Array(count * 5);
+ const element = [0, 0, 0];
+ for (let index = 0; index < count; index++) {
+ coordinates.getElement(index, element);
+ values[index * 5] = element[0];
+ values[index * 5 + 1] = element[1];
+ normals.getElement(index, element);
+ values.set(element, index * 5 + 2);
+ }
+ return { values, weights: [weight, weight, weight * 0.5, weight * 0.5, weight * 0.5] };
+}
+
+function simplifiedPrimitive(document, primitive, relativeError, { absolute = false, uvWeight = 0 } = {}) {
const indices = primitive.getIndices();
if (!indices || (primitive.getMode() ?? 4) !== 4 || primitive.listTargets().length) return primitive;
const source = Uint32Array.from(indices.getArray());
- const [reduced] = MeshoptSimplifier.simplify(source, positionsOf(primitive.getAttribute('POSITION')), 3, Math.min(MINIMUM_TRIANGLES * 3, source.length), relativeError, ['Permissive']);
+ const positions = positionsOf(primitive.getAttribute('POSITION'));
+ const targetIndexCount = Math.min(MINIMUM_TRIANGLES * 3, source.length);
+ const flags = absolute ? ['Permissive', 'ErrorAbsolute', 'Prune'] : ['Permissive'];
+ const attributes = attributesFor(primitive, uvWeight);
+ const [reduced] = attributes
+ ? MeshoptSimplifier.simplifyWithAttributes(source, positions, 3, attributes.values, 5, attributes.weights, null, targetIndexCount, relativeError, flags)
+ : MeshoptSimplifier.simplify(source, positions, 3, targetIndexCount, relativeError, flags);
if (reduced.length > source.length * 0.97) return primitive;
const [remap, unique] = MeshoptSimplifier.compactMesh(reduced);
const copy = document.createPrimitive().setMaterial(primitive.getMaterial()).setMode(4);
@@ -173,6 +201,7 @@ function sliceInstancing(document, extension, instancing, members) {
}
function splitNodeByLevel(document, node, chosen, variantOf, extension) {
+ const nodeScale = largestScale(node.getWorldMatrix());
const mesh = node.getMesh();
const instancing = node.getExtension('EXT_mesh_gpu_instancing');
const groups = new Map();
@@ -184,7 +213,7 @@ function splitNodeByLevel(document, node, chosen, variantOf, extension) {
for (const [level, members] of groups) {
const replacement = document.createNode(`${node.getName()}-level${level}`)
.setTranslation(node.getTranslation()).setRotation(node.getRotation()).setScale(node.getScale())
- .setMesh(variantOf(mesh, level));
+ .setMesh(variantOf(mesh, level, nodeScale));
if (instancing) replacement.setExtension('EXT_mesh_gpu_instancing', sliceInstancing(document, extension, instancing, members));
for (const parent of node.listParents()) if (typeof parent.addChild === 'function') parent.addChild(replacement);
created.push(replacement);
@@ -235,11 +264,11 @@ const pixelsPerRadian = options.height / (2 * Math.tan((narrowestFov * Math.PI)
const extension = document.createExtension(EXTMeshGPUInstancing);
const variants = new Map();
const meshIndex = new Map(document.getRoot().listMeshes().map((mesh, index) => [mesh, index]));
-function variantOf(mesh, level) {
- const key = `${meshIndex.get(mesh)}:${level}`;
+function variantOf(mesh, level, nodeScale = 1) {
+ const key = `${meshIndex.get(mesh)}:${level}:${options.worldError ? nodeScale : 1}`;
if (!variants.has(key)) {
const variant = document.createMesh(`${mesh.getName()}-level${level}`);
- mesh.listPrimitives().forEach(primitive => variant.addPrimitive(simplifiedPrimitive(document, primitive, levels[level])));
+ mesh.listPrimitives().forEach(primitive => variant.addPrimitive(simplifiedPrimitive(document, primitive, options.worldError ? options.worldError / nodeScale : levels[level], { absolute: options.worldError > 0, uvWeight: options.uvWeight })));
variants.set(key, variant);
}
return variants.get(key);
diff --git a/tests/hero-first-view-crop.test.js b/tests/hero-first-view-crop.test.js
new file mode 100644
index 00000000..933cec30
--- /dev/null
+++ b/tests/hero-first-view-crop.test.js
@@ -0,0 +1,29 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { CROP_DEFAULTS, nearestVisibleDistance, sampleFlightPoses } from '../scripts/hero-first-view-crop.mjs';
+import { parseArguments } from '../scripts/build-hero-first-view.mjs';
+
+const lookingDownZ = [{ eye: [0, 0, 0], forward: [0, 0, 1], right: [-1, 0, 0], up: [0, 1, 0], tanVertical: Math.tan(Math.PI / 8) }];
+
+test('a sphere in front of the camera is seen at its surface distance', () => {
+ const distance = nearestVisibleDistance(lookingDownZ, [0, 0, 100], 10, CROP_DEFAULTS);
+ assert.equal(distance, 90);
+});
+
+test('spheres behind the camera or far outside the frustum are dropped', () => {
+ assert.equal(nearestVisibleDistance(lookingDownZ, [0, 0, -100], 10, CROP_DEFAULTS), Infinity);
+ assert.equal(nearestVisibleDistance(lookingDownZ, [5000, 0, 100], 10, CROP_DEFAULTS), Infinity);
+ assert.equal(nearestVisibleDistance(lookingDownZ, [0, 5000, 100], 10, CROP_DEFAULTS), Infinity);
+});
+
+test('the port flight yields poses that start at the first key', () => {
+ const poses = sampleFlightPoses('port', 5);
+ assert.deepEqual(poses[0].eye.map(Math.round), [-20, 130, -300]);
+ assert.throws(() => sampleFlightPoses('nowhere', 5), /No scene nowhere/);
+});
+
+test('build options reject unknown names and non-positive numbers', () => {
+ assert.equal(parseArguments(['--near-pixels', '2']).nearPixels, 2);
+ assert.throws(() => parseArguments(['--bogus', '1']), /Unknown option/);
+ assert.throws(() => parseArguments(['--far-pixels', '0']), /positive number/);
+});