Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Home hero loop: lighter warehouse and dark store, new flights, motion and haze

Nguyen Xuan Aneb6b2ffd3e61
diff --git a/public/showcase/darkstore/scene.glb b/public/showcase/darkstore/scene.glb
index 59dc6cd5..e3eca0c5 100644
Binary files a/public/showcase/darkstore/scene.glb and b/public/showcase/darkstore/scene.glb differ
diff --git a/public/showcase/warehouse/scene.glb b/public/showcase/warehouse/scene.glb
index b508e2cf..ff38475b 100644
Binary files a/public/showcase/warehouse/scene.glb and b/public/showcase/warehouse/scene.glb differ
diff --git a/scripts/simplify-hero-glb.mjs b/scripts/simplify-hero-glb.mjs
new file mode 100644
index 00000000..a10af9e3
--- /dev/null
+++ b/scripts/simplify-hero-glb.mjs
@@ -0,0 +1,257 @@
+// Lightens an already prepared hero GLB (meshopt compressed, instanced, WebP) for a camera that flies a known route.
+// Every instance gets the coarsest copy of its mesh whose error stays under `--pixels` on screen at the nearest the camera comes
+// to it, so a shelf the camera never nears is drawn with a few thousand triangles while one it passes keeps its detail.
+// Instanced nodes are split by level (one node per mesh and level), so the page needs no level-of-detail code.
+//
+// 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-pattern '^m_'] [--webp-quality 78] [--height 900]
+//
+// 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-pattern materials whose name matches get --prop-texture instead of --max-texture
+// 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';
+import { weld, prune } from '@gltf-transform/functions';
+import { MeshoptDecoder, MeshoptEncoder, 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 DEFAULTS = { pixels: 1.2, levels: '0.0008,0.005,0.025,0.1', maxTexture: 1024, propTexture: 512, propPattern: '^m_', webpQuality: 78, height: 900 };
+const ROUTE_SAMPLES = 240;
+const MINIMUM_TRIANGLES = 12;
+const UNUSED = 0xffffffff;
+
+function parseArguments(argv) {
+ const options = { ...DEFAULTS };
+ const positional = [];
+ for (let index = 0; index < argv.length; index++) {
+ const name = argv[index];
+ if (!name.startsWith('--')) {
+ positional.push(name);
+ continue;
+ }
+ 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 (positional.length !== 3) throw new Error('Pass <scene id>, <input.glb> and <output.glb>.');
+ const [sceneId, input, output] = positional;
+ return { sceneId, input, output, options, levels: options.levels.split(',').map(Number) };
+}
+
+// The camera's eye along the whole flight, plus the narrowest field of view (it asks the most of the meshes).
+function sampleRoute(sceneId) {
+ 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 eyes = [];
+ let narrowest = Infinity;
+ for (let index = 0; index <= ROUTE_SAMPLES; index++) {
+ const pose = route(index / ROUTE_SAMPLES);
+ eyes.push(pose.eye);
+ narrowest = Math.min(narrowest, pose.fov);
+ }
+ return { eyes, narrowestFov: narrowest };
+}
+
+function distanceToRoute(eyes, point) {
+ let nearest = Infinity;
+ for (const eye of eyes) nearest = Math.min(nearest, Math.hypot(eye[0] - point[0], eye[1] - point[1], eye[2] - point[2]));
+ return nearest;
+}
+
+function triangleTotal(document) {
+ let total = 0;
+ for (const node of document.getRoot().listNodes()) {
+ const mesh = node.getMesh();
+ if (!mesh) continue;
+ const instancing = node.getExtension('EXT_mesh_gpu_instancing');
+ const instances = instancing ? instancing.listAttributes()[0].getCount() : 1;
+ for (const primitive of mesh.listPrimitives()) {
+ const count = primitive.getIndices()?.getCount() ?? primitive.getAttribute('POSITION').getCount();
+ total += (count / 3) * instances;
+ }
+ }
+ return Math.round(total);
+}
+
+function positionsOf(accessor) {
+ if (!(accessor.getArray() instanceof Float32Array)) throw new Error('Simplifying needs float POSITION data.');
+ return Float32Array.from(accessor.getArray());
+}
+
+// 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);
+}
+
+// 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) {
+ 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']);
+ if (reduced.length > source.length * 0.97) return primitive;
+ const [remap, unique] = MeshoptSimplifier.compactMesh(reduced);
+ const copy = document.createPrimitive().setMaterial(primitive.getMaterial()).setMode(4);
+ for (const semantic of primitive.listSemantics()) copy.setAttribute(semantic, pickElements(document, primitive.getAttribute(semantic), remap, unique));
+ const indexArray = unique <= 65535 ? Uint16Array.from(reduced) : reduced;
+ return copy.setIndices(document.createAccessor().setType('SCALAR').setArray(indexArray));
+}
+
+function meshDiagonal(mesh) {
+ const low = [Infinity, Infinity, Infinity];
+ const high = [-Infinity, -Infinity, -Infinity];
+ for (const primitive of mesh.listPrimitives()) {
+ const accessor = primitive.getAttribute('POSITION');
+ const minimum = accessor.getMin([0, 0, 0]);
+ const maximum = accessor.getMax([0, 0, 0]);
+ for (let axis = 0; axis < 3; axis++) {
+ low[axis] = Math.min(low[axis], minimum[axis]);
+ high[axis] = Math.max(high[axis], maximum[axis]);
+ }
+ }
+ return Math.hypot(high[0] - low[0], high[1] - low[1], high[2] - low[2]);
+}
+
+function transformPoint(matrix, point) {
+ return [
+ matrix[0] * point[0] + matrix[4] * point[1] + matrix[8] * point[2] + matrix[12],
+ matrix[1] * point[0] + matrix[5] * point[1] + matrix[9] * point[2] + matrix[13],
+ matrix[2] * point[0] + matrix[6] * point[1] + matrix[10] * point[2] + 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]));
+}
+
+// The level each instance of a node needs, as one number per instance.
+function levelsForInstances(node, eyes, pixelsPerRadian, options, levels) {
+ const world = node.getWorldMatrix();
+ const instancing = node.getExtension('EXT_mesh_gpu_instancing');
+ const count = instancing ? instancing.listAttributes()[0].getCount() : 1;
+ const translation = instancing?.getAttribute('TRANSLATION');
+ const scaling = instancing?.getAttribute('SCALE');
+ const baseSize = meshDiagonal(node.getMesh()) * largestScale(world);
+ const chosen = new Uint8Array(count);
+ const local = [0, 0, 0];
+ const scale = [1, 1, 1];
+ for (let index = 0; index < count; index++) {
+ if (translation) translation.getElement(index, local);
+ if (scaling) scaling.getElement(index, scale);
+ const size = Math.max(baseSize * Math.max(scale[0], scale[1], scale[2]), 1e-4);
+ const distance = Math.max(0.05, distanceToRoute(eyes, transformPoint(world, local)) - size / 2);
+ const tolerable = (options.pixels * distance) / pixelsPerRadian / size;
+ let level = 0;
+ for (let candidate = 0; candidate < levels.length; candidate++) if (levels[candidate] <= tolerable) level = candidate;
+ chosen[index] = level;
+ }
+ return chosen;
+}
+
+function sliceInstancing(document, extension, instancing, members) {
+ const instanced = extension.createInstancedMesh();
+ for (const semantic of instancing.listSemantics()) {
+ const source = instancing.getAttribute(semantic);
+ const size = source.getElementSize();
+ const raw = source.getArray();
+ const array = new raw.constructor(members.length * size);
+ members.forEach((instance, slot) => array.set(raw.subarray(instance * size, (instance + 1) * size), slot * size));
+ instanced.setAttribute(semantic, document.createAccessor().setType(source.getType()).setNormalized(source.getNormalized()).setArray(array));
+ }
+ return instanced;
+}
+
+function splitNodeByLevel(document, node, chosen, variantOf, extension) {
+ const mesh = node.getMesh();
+ const instancing = node.getExtension('EXT_mesh_gpu_instancing');
+ const groups = new Map();
+ chosen.forEach((level, instance) => {
+ if (!groups.has(level)) groups.set(level, []);
+ groups.get(level).push(instance);
+ });
+ const created = [];
+ 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));
+ 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);
+ }
+ node.listChildren().forEach(child => created[0].addChild(child));
+ node.setExtension('EXT_mesh_gpu_instancing', null).setMesh(null);
+ node.dispose();
+}
+
+async function scaleTextures(document, options) {
+ const pattern = new RegExp(options.propPattern);
+ const propTextures = new Set();
+ const largeTextures = new Set();
+ for (const material of document.getRoot().listMaterials()) {
+ const bucket = pattern.test(material.getName()) ? propTextures : largeTextures;
+ for (const texture of [material.getBaseColorTexture(), material.getNormalTexture(), material.getOcclusionTexture(), material.getEmissiveTexture(), material.getMetallicRoughnessTexture()]) {
+ if (texture) bucket.add(texture);
+ }
+ }
+ for (const texture of propTextures) if (largeTextures.has(texture)) propTextures.delete(texture);
+ for (const [textures, limit] of [[propTextures, options.propTexture], [largeTextures, options.maxTexture]]) {
+ for (const texture of textures) {
+ 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 })
+ .toBuffer();
+ texture.setImage(new Uint8Array(resized));
+ texture.setMimeType('image/webp');
+ }
+ }
+}
+
+const { sceneId, input, output, options, levels } = parseArguments(process.argv.slice(2));
+await Promise.all([MeshoptDecoder.ready, MeshoptEncoder.ready, MeshoptSimplifier.ready]);
+const io = new NodeIO()
+ .registerExtensions([...ALL_EXTENSIONS, EXTMeshoptCompression])
+ .registerDependencies({ 'meshopt.decoder': MeshoptDecoder, 'meshopt.encoder': MeshoptEncoder });
+const document = await io.read(input);
+const before = triangleTotal(document);
+await document.transform(weld({ tolerance: 0 }));
+
+const { eyes, narrowestFov } = sampleRoute(sceneId);
+const pixelsPerRadian = options.height / (2 * Math.tan((narrowestFov * Math.PI) / 360));
+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}`;
+ if (!variants.has(key)) {
+ const variant = document.createMesh(`${mesh.getName()}-level${level}`);
+ mesh.listPrimitives().forEach(primitive => variant.addPrimitive(simplifiedPrimitive(document, primitive, levels[level])));
+ variants.set(key, variant);
+ }
+ return variants.get(key);
+}
+const levelCounts = new Array(levels.length).fill(0);
+for (const node of document.getRoot().listNodes().filter(candidate => candidate.getMesh())) {
+ const chosen = levelsForInstances(node, eyes, pixelsPerRadian, options, levels);
+ chosen.forEach(level => levelCounts[level]++);
+ splitNodeByLevel(document, node, chosen, variantOf, extension);
+}
+await scaleTextures(document, options);
+await document.transform(prune({ keepLeaves: false }));
+document.createExtension(EXTMeshoptCompression).setRequired(true).setEncoderOptions({ method: EXTMeshoptCompression.EncoderMethod.FILTER });
+await io.write(output, document);
+const after = triangleTotal(document);
+console.log(`${sceneId}: triangles ${before} -> ${after} (${((after / before) * 100).toFixed(1)}%); instances per level ${levelCounts.join('/')}; meshes ${variants.size}`);
diff --git a/src/hero-lab/hero-loop.js b/src/hero-lab/hero-loop.js
index 14803a44..b89a2edd 100644
--- a/src/hero-lab/hero-loop.js
+++ b/src/hero-lab/hero-loop.js
@@ -42,6 +42,7 @@ let loopRound = 0;
const loopState = window.__loop = {
events: [],
samples: [],
+ ticks: [],
worldsAlive: () => [active, incoming, pending?.world && pending].filter(Boolean).length,
peakWorlds: 0,
};
@@ -74,6 +75,7 @@ async function buildFrom(download) {
context.readPixels(0, 0, 1, 1, context.RGBA, context.UNSIGNED_BYTE, probe);
};
}
+ if (measureMode) (loopState.worlds ??= {})[world.id] = world;
return world;
}
@@ -234,7 +236,8 @@ function tick(seconds) {
const flightSeconds = prefersReducedMotion ? flights[active.index].duration * REDUCED_FLIGHT_POSITION : activeSeconds;
const sceneTime = prefersReducedMotion ? STILL_SCENE_TIME : clock;
const stillHeld = prefersReducedMotion && !incoming && active.drawn;
- if (!stillHeld) drawWorld(active, flightSeconds, sceneTime);
+ // While a fade runs the world that is leaving keeps its last frame: drawing two worlds per tick doubled the frame time of every fade.
+ if (!stillHeld && !incoming) drawWorld(active, flightSeconds, sceneTime);
active.drawn = true;
if (incoming) {
const amount = smoothstep(0, 1, (clock - incoming.startedAt) / fadeSeconds);
@@ -272,7 +275,10 @@ async function start() {
return;
}
const loop = startLoop(stage, seconds => {
+ const started = performance.now();
+ const blending = Boolean(incoming);
tick(seconds);
+ if (measureMode) loopState.ticks.push({ scene: active && SCENES[active.index].id, ms: performance.now() - started, blending });
return true;
});
loopState.wake = loop.wake;
@@ -293,6 +299,8 @@ loopState.scout = async id => {
const world = await buildFrom(await fetchWorld(SCENES[index].description));
world.canvas.style.opacity = '1';
stage.dataset.live = 'true';
- return (eye, target, fov) => world.render({ eye, target, fov }, 1);
+ const draw = (eye, target, fov, seconds = 1) => world.render({ eye, target, fov }, seconds);
+ draw.world = world;
+ return draw;
};
if (!parameters.has('scout')) start().catch(error => showStageError(stage, `Live view unavailable: ${error.message}`));
diff --git a/src/hero-lab/loop/effects.js b/src/hero-lab/loop/effects.js
new file mode 100644
index 00000000..e724312e
--- /dev/null
+++ b/src/hero-lab/loop/effects.js
@@ -0,0 +1,100 @@
+// Small, cheap motion for the hero worlds, all of it a pure function of the scene time so a recorder can render exact frames:
+// blink a set of emissive parts (signal lamps, status LEDs) pulses, each material variant on its own phase
+// drive machines (forklifts, picker robots) slide to and fro along an aisle; the parts of one machine share a spot, so they move as one
+// haze one drifting volume of fog (dust in the sun, cold air in a hall) through the 67js volume fog addon
+// glare the bloom strength breathes slowly
+// Each world lists what it wants in EFFECTS; a world without an entry draws exactly as before.
+// Parts keep their geometry and shadows: only matrices and emission change, and the shadow map is not redrawn for them.
+
+const TAU = Math.PI * 2;
+const BLINK_VARIANTS = 3;
+const MAXIMUM_MACHINE_PARTS = 40;
+
+const EFFECTS = {
+ port: {
+ blink: [{ materials: /^glow_/, period: 2.4, low: 0.25, high: 1 }],
+ glare: { amount: 0.1, period: 9 },
+ },
+ datacenter: {
+ blink: [{ materials: /status_green|status_blue/, period: 1.7, low: 0.1, high: 1.4 }],
+ haze: { minimum: [-16, 0.2, -14], maximum: [16, 5.5, 12], density: 0.012, color: [0.45, 0.62, 0.9], ambient: [0.012, 0.02, 0.04], steps: 16, maxDistance: 70, drift: [0.15, 0, 0.05], noiseScale: 0.18, noiseAmount: 0.85, keyScattering: 0.6 },
+ },
+ warehouse: {
+ blink: [{ names: /^doorLamp/, period: 2.2, low: 0.3, high: 1 }],
+ drive: [{ materials: /forklift/, axis: 2, amplitude: 2.2, period: 14 }],
+ haze: { minimum: [-27, 0.3, -22], maximum: [34, 11, 21], density: 0.006, color: [0.95, 0.88, 0.75], ambient: [0.02, 0.02, 0.02], steps: 16, maxDistance: 80, drift: [0.06, 0.01, 0.03], noiseScale: 0.25, noiseAmount: 0.9, keyScattering: 1.2 },
+ },
+ darkstore: {
+ drive: [{ materials: /robot/, axis: 2, amplitude: 3.2, period: 12 }],
+ },
+};
+
+function matchesPart(rule, part) {
+ const materialName = part.material?.name ?? '';
+ return (rule.materials ? rule.materials.test(materialName) : false) || (rule.names ? rule.names.test(part.name ?? '') : false);
+}
+
+// Gives every matched part one of a few copies of its material so the copies can pulse out of step.
+function prepareBlink(rule, parts) {
+ const matched = parts.filter(part => matchesPart(rule, part));
+ const variants = new Map();
+ matched.forEach((part, index) => {
+ const slot = index % BLINK_VARIANTS;
+ const key = `${part.material.name ?? ''}#${slot}`;
+ if (!variants.has(key)) variants.set(key, { material: { ...part.material }, base: part.material.emission || 1, slot, parts: [] });
+ const variant = variants.get(key);
+ part.material = variant.material;
+ variant.parts.push(part);
+ });
+ return { rule, variants: [...variants.values()], parts: matched };
+}
+
+// Machines are told apart by where they stand: all parts of one forklift share a translation.
+function prepareDrive(rule, parts) {
+ const groups = new Map();
+ for (const part of parts) {
+ const key = `${part.matrix[12].toFixed(3)},${part.matrix[14].toFixed(3)}`;
+ if (!groups.has(key)) groups.set(key, []);
+ groups.get(key).push(part);
+ }
+ const machines = [];
+ for (const members of groups.values()) {
+ // A spot shared by dozens of parts is stock or shell with no offset of its own, not one machine.
+ if (members.length > MAXIMUM_MACHINE_PARTS || !members.some(part => matchesPart(rule, part))) continue;
+ machines.push({ parts: members, base: members.map(part => part.matrix[12 + rule.axis]), phase: machines.length * 1.7 });
+ }
+ return { rule, machines, parts: machines.flatMap(machine => machine.parts) };
+}
+
+export function prepareEffects(worldId, parts) {
+ const config = EFFECTS[worldId];
+ if (!config) return null;
+ const blinks = (config.blink ?? []).map(rule => prepareBlink(rule, parts));
+ const drives = (config.drive ?? []).map(rule => prepareDrive(rule, parts));
+ const moving = [...blinks.flatMap(entry => entry.parts), ...drives.flatMap(entry => entry.parts)];
+ return {
+ // Added to the renderer options: the haze needs a depth buffer.
+ rendererOptions: config.haze ? { depth: true } : {},
+ volumeFog: config.haze ?? null,
+ needsFogAddon: Boolean(config.haze),
+ // Called before each draw with the scene time; returns the extra frame fields and updates the renderer's parts.
+ apply(renderer, seconds, frame) {
+ for (const { rule, variants } of blinks) {
+ for (const variant of variants) {
+ const wave = 0.5 + 0.5 * Math.sin(TAU * (seconds / rule.period + variant.slot / BLINK_VARIANTS));
+ variant.material.emission = variant.base * (rule.low + (rule.high - rule.low) * wave);
+ }
+ }
+ for (const { rule, machines } of drives) {
+ for (const machine of machines) {
+ const offset = rule.amplitude * Math.sin(TAU * seconds / rule.period + machine.phase);
+ machine.parts.forEach((part, index) => { part.matrix[12 + rule.axis] = machine.base[index] + offset; });
+ }
+ }
+ if (moving.length) renderer.updateParts(moving, { shadows: false, checks: false });
+ if (!config.glare || !frame.effects?.bloom) return frame;
+ const bloom = { ...frame.effects.bloom, strength: frame.effects.bloom.strength * (1 + config.glare.amount * Math.sin(TAU * seconds / config.glare.period)) };
+ return { ...frame, effects: { ...frame.effects, bloom } };
+ },
+ };
+}
diff --git a/src/hero-lab/loop/scenes.js b/src/hero-lab/loop/scenes.js
index d6ab09e0..a533f2ec 100644
--- a/src/hero-lab/loop/scenes.js
+++ b/src/hero-lab/loop/scenes.js
@@ -3,7 +3,7 @@
// Eye and target are in the world's own units (metres). Flights were scouted against the real scenes, see FLIGHT NOTES in each entry.
const NVIDIA_SIMREADY = { label: 'NVIDIA SimReady assets', licence: 'CC BY 4.0', href: 'https://docs.omniverse.nvidia.com/usd/latest/usd_content_samples/simready_assets.html' };
-export const SCENES = [
+const ALL_SCENES = [
{
id: 'port',
name: 'Container port',
@@ -65,10 +65,11 @@ export const SCENES = [
near: 0.3,
far: 400,
keys: [
- { at: 0, eye: [30, 22, 40], target: [4, 1, -1], fov: 32 },
- { at: 0.3, eye: [14, 16, 27], target: [0, 1.5, -4], fov: 38 },
- { at: 0.62, eye: [0, 9.5, 15], target: [-8, 1.5, -8], fov: 46 },
- { at: 1, eye: [-16, 4.5, 9], target: [-26, 2, -14], fov: 56 },
+ { at: 0, eye: [34, 1.8, 16], target: [14, 2.4, 2], fov: 52 },
+ { at: 0.28, eye: [22, 3.2, 9], target: [4, 2.4, -3], fov: 48 },
+ { at: 0.52, eye: [13, 4.6, 13], target: [0, 2.4, -5], fov: 46 },
+ { at: 0.76, eye: [2, 8.5, 13], target: [-10, 2, -6], fov: 44 },
+ { at: 1, eye: [-18, 12, 22], target: [2, 0, -8], fov: 41 },
],
credits: [NVIDIA_SIMREADY],
},
@@ -81,10 +82,16 @@ export const SCENES = [
near: 0.25,
far: 300,
keys: [
- { at: 0, eye: [22, 17, 22], target: [4, 0, -6], fov: 34 },
- { at: 0.5, eye: [6, 14.5, 18], target: [-2, 0, -10], fov: 38 },
- { at: 1, eye: [-10, 11, 9], target: [8, 0.5, -18], fov: 42 },
+ { at: 0, eye: [0, 2.2, 21], target: [0, 2, -8], fov: 54 },
+ { at: 0.28, eye: [-1.5, 3.2, 6], target: [1.5, 2, -12], fov: 52 },
+ { at: 0.55, eye: [2.5, 6.2, 1.5], target: [-4, 1.5, -10], fov: 48 },
+ { at: 0.8, eye: [8, 9, 3], target: [-3, 1, -12], fov: 44 },
+ { at: 1, eye: [14, 12, 15], target: [-3, 0.5, -10], fov: 42 },
],
credits: [NVIDIA_SIMREADY],
},
];
+
+// TEMPORARY while the chip fab is rebuilt: ?skip=chipfab (comma list) leaves scenes out of the loop. Remove with the chip fab swap.
+const skipped = new Set((new URLSearchParams(typeof location === 'undefined' ? '' : location.search).get('skip') ?? '').split(',').filter(Boolean));
+export const SCENES = ALL_SCENES.filter(scene => !skipped.has(scene.id));
diff --git a/src/hero-lab/loop/world.js b/src/hero-lab/loop/world.js
index 1f9c9488..56f6abf5 100644
--- a/src/hero-lab/loop/world.js
+++ b/src/hero-lab/loop/world.js
@@ -8,6 +8,7 @@ import { loadHdri } from '../../hdri.js';
import { createCascadedShadows } from '../../cascaded-shadows.js';
import { applyWaterReflection, attachWaterReflection } from '../../water-reflection.js';
import { readPartRanks } from '../../showcase-download.js';
+import { prepareEffects } from './effects.js';
// The water mirror is off in the loop: once the camera moves between two draws the port world drew only its sky (the same happens
// on hero-b.html?scene=port when the camera is set by hand; with the mirror off every pose draws). Turn it on again when that is fixed.
@@ -39,14 +40,25 @@ export async function fetchWorld(descriptionUrl) {
export async function buildWorld(download, canvas, overrides = {}) {
const { description, paths, models, sky } = download;
+ const timings = {};
+ let lapStart = performance.now();
+ const lap = name => {
+ const now = performance.now();
+ timings[name] = Math.round(now - lapStart);
+ lapStart = now;
+ };
const sharedTextures = createSharedTextures();
const files = [];
for (const [index, model] of models.entries()) {
const loaded = await loadGltf(model, { baseUrl: baseUrlOf(paths[index]), sharedTextures, ...description.gltfOptions });
files.push({ parts: loaded.parts, ranks: await readPartRanks(model) });
}
+ lap('parseMs');
const options = description.renderer;
const environmentMap = await loadHdri(description.hdri, { ...description.hdriOptions, fetchImplementation: async () => new Response(sky) });
+ lap('hdriMs');
+ const effects = prepareEffects(description.id, inWholeWorldOrder(files));
+ if (effects?.needsFogAddon) await import('../../fog-shader.js');
const surfaceOf = parts => (description.waterReflection && WATER_MIRROR_ENABLED ? applyWaterReflection(parts) : { parts, surface: null });
// The renderer starts on the first file's parts and takes the whole world through replaceParts, the order the showcase page
// uses: a water mirror attached to a renderer built on the whole world drew nothing after its first frame.
@@ -56,8 +68,9 @@ export async function buildWorld(download, canvas, overrides = {}) {
...overrides,
pixelRatio: Math.min(devicePixelRatio, 1.25),
memoryBudgetMiB: 512,
+ ...effects?.rendererOptions,
...(options.shadowCascades ? { shadowCascades: createCascadedShadows(options.shadowCascades) } : {}),
- environment: { ...options.environment, map: environmentMap },
+ environment: { ...options.environment, map: environmentMap, ...(effects?.volumeFog ? { volumeFog: effects.volumeFog } : {}) },
});
let detachReflection = attachWaterReflection(renderer, firstReflection.surface);
if (files.length > 1) {
@@ -67,14 +80,18 @@ export async function buildWorld(download, canvas, overrides = {}) {
detachReflection();
detachReflection = attachWaterReflection(renderer, whole.surface);
}
+ lap('rendererMs');
const frame = { ...description.frame, exposure: description.exposure ?? 1 };
return {
id: description.id,
title: description.title,
description,
canvas,
+ renderer,
+ timings,
render(camera, seconds) {
- renderer.render({ ...frame, eye: camera.eye, target: camera.target, fov: camera.fov, time: seconds });
+ const drawn = effects ? effects.apply(renderer, seconds, frame) : frame;
+ renderer.render({ ...drawn, eye: camera.eye, target: camera.target, fov: camera.fov, time: seconds });
},
resize: () => renderer.resize(),
dispose() {