3D engine that AI agents drive
Show animated WALL-E with pausing assembly slider on homepage
Nguyen Xuan Ane9e797f11921
diff --git a/index.html b/index.htmlindex 7043a9da..9bbe92c4 100644--- a/index.html+++ b/index.html@@ -23,8 +23,13 @@<header class="introduction"><div><h1>Your 3D scene,<br />from browser to film</h1><p>67.js brings browser 3D, animation and image/video export into one workflow. Create interactive experiences and export the same scene as images or video</p><div class="hero-actions"><a href="/downloads.html" data-icon="download">Download 67.js</a></div></div></header><section class="feature-showcase live-showcase" aria-label="Interactive 3D preview">- <canvas id="home-preview" tabindex="0" aria-label="Clockwork 3D model. Drag or use arrow keys to rotate. Use plus and minus to zoom. Press Space to pause or resume animation."></canvas>+ <canvas id="home-preview" tabindex="0" aria-label="WALL·E 3D model. Drag or use arrow keys to rotate. Use plus and minus to zoom. Press Space to pause or resume animation."></canvas><p id="home-preview-status" role="status">Loading 3D preview…</p>+ <div class="preview-assembly">+ <div class="preview-assembly-label"><label for="preview-assembly">Explore the machinery</label><output id="preview-assembly-value" for="preview-assembly">Assembled</output></div>+ <input id="preview-assembly" type="range" min="0" max="100" step="1" value="0" aria-valuetext="Assembled" aria-describedby="preview-assembly-hint" />+ <p id="preview-assembly-hint">Slide to take WALL·E apart. Reassemble to start rolling.</p>+ </div></section></section><section class="workflow" aria-label="What you can build"><div><span data-icon="cube"></span><h3>Build and inspect</h3><p>Shared meshes, materials and cameras. Pull a machine apart, explore a world, or bring your own GLB.</p></div><div><span data-icon="film"></span><h3>Make it move</h3><p>Sample keyframes at any time. Give visitors controls and keep the same timeline for your film.</p></div><div><span data-icon="image"></span><h3>Export the result</h3><p>Produce images and videos on GPU or CPU with explicit render budgets and one capture frame at a time.</p></div></section>diff --git a/scripts/smoke-site.mjs b/scripts/smoke-site.mjsindex 4188ff4b..e8f423e6 100644--- a/scripts/smoke-site.mjs+++ b/scripts/smoke-site.mjs@@ -73,7 +73,28 @@ try {assert.equal(await page.locator('.preview-tools, .showcase-caption').count(), 0);assert.equal(await page.evaluate(() => window.__homePreview.state.playing), false);await page.emulateMedia({ reducedMotion:'no-preference' });- await page.waitForFunction(() => window.__homePreview.state.time > 3.1);+ await page.waitForFunction(() => window.__homePreview.state.time > 0.1);+ await page.locator('#preview-assembly').evaluate(slider => {+ slider.value = '100';+ slider.dispatchEvent(new Event('input', { bubbles:true }));+ slider.dispatchEvent(new Event('change', { bubbles:true }));+ });+ assert.equal(await page.evaluate(() => window.__homePreview.state.assembly), 1);+ assert.equal(await page.evaluate(() => window.__homePreview.state.playing), false);+ const frozenTime = await page.evaluate(() => window.__homePreview.state.time);+ await page.waitForTimeout(150);+ assert.equal(await page.evaluate(() => window.__homePreview.state.time), frozenTime);+ await page.locator('#home-preview').focus();+ await page.keyboard.press('Space');+ assert.equal(await page.evaluate(() => window.__homePreview.state.playing), false, 'Exploded model resumes from Space');+ await page.locator('#preview-assembly').evaluate(slider => {+ slider.value = '0';+ slider.dispatchEvent(new Event('input', { bubbles:true }));+ slider.dispatchEvent(new Event('change', { bubbles:true }));+ });+ assert.equal(await page.evaluate(() => window.__homePreview.state.playing), true);+ assert.ok(await page.evaluate(() => window.__homePreview.state.speed) < 0.4, 'Drive restarts at full speed');+ await page.waitForFunction(() => window.__homePreview.state.speed === 1);await page.emulateMedia({ reducedMotion:'reduce' });await page.waitForFunction(() => !window.__homePreview.state.playing);assert.equal(await page.evaluate(() => window.__homePreview.state.playing), false);@@ -88,7 +109,7 @@ try {assert.equal(await page.evaluate(() => window.__homePreview.renderer.context.getError()), 0);assert.ok(await page.evaluate(() => window.__homePreview.renderer.memoryPlan.estimatedBytes) <= 192 * 1048576);await page.screenshot({ path:'artifacts/home-live-preview.png' });- report.interactions.push('Live homepage 3D, keyboard rotation, zoom, reset and animation');+ report.interactions.push('Live WALL·E, keyboard orbit, transparent render and paused assembly with gradual drive restart');}if (route === '/downloads.html') {await page.waitForFunction(() => window.__home.state.status === 'ready');diff --git a/src/home-preview.js b/src/home-preview.jsindex ae6c0160..9c452405 100644--- a/src/home-preview.js+++ b/src/home-preview.js@@ -1,10 +1,12 @@import { Renderer67 } from './renderer.js';-import { createMotionScene } from './motion.js';+import { createWallEPreviewScene } from './wall-e-preview-scene.js';const canvas = document.querySelector('#home-preview');const status = document.querySelector('#home-preview-status');+const assemblySlider = document.querySelector('#preview-assembly');+const assemblyValue = document.querySelector('#preview-assembly-value');const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');-const state = { yaw:0.6, pitch:0.28, distance:8, time:3, playing:!reducedMotion.matches, visible:false };+const state = { yaw:0.6, pitch:0.28, distance:7.5, time:0, playing:!reducedMotion.matches, visible:false, assembly:0, speed:0, interacting:false };let renderer;let scene;let scheduledFrame = 0;@@ -25,10 +27,11 @@ function draw() {try {const frame = scene.sample(state.time);renderer.updateParts(scene.parts);- const distance = state.distance * (canvas.clientWidth < 500 ? 1.25 : 1);+ const distance = state.distance * (canvas.clientWidth < 500 ? 1.05 : 1) + state.assembly * 10;+ const targetHeight = 1.5 + state.assembly * 0.35;renderer.render({- eye:[Math.sin(state.yaw) * Math.cos(state.pitch) * distance, Math.sin(state.pitch) * distance, Math.cos(state.yaw) * Math.cos(state.pitch) * distance],- target:[0,0.1,0], fov:40, time:state.time, exposure:1, effects:{ ...frame.effects, vignette:0, grain:0 },+ eye:[Math.sin(state.yaw) * Math.cos(state.pitch) * distance, targetHeight + Math.sin(state.pitch) * distance, Math.cos(state.yaw) * Math.cos(state.pitch) * distance],+ target:[0,targetHeight,0], fov:40, time:state.time, exposure:1, armorColor:[0.73,0.43,0.065], armorSurface:[0.62,0.43], assemblyAmount:state.assembly, effects:{ ...frame.effects, vignette:0, grain:0 },});} catch (error) {showError(error);@@ -39,7 +42,8 @@ function animate(timestamp) {scheduledFrame = 0;if (!state.playing || !state.visible || document.hidden || disposed) return;const elapsedSeconds = previousTime ? Math.min(0.1, (timestamp - previousTime) / 1000) : 0;- state.time = (state.time + elapsedSeconds) % scene.duration;+ state.speed = Math.min(1, state.speed + elapsedSeconds / 1.2);+ state.time += elapsedSeconds * state.speed;previousTime = timestamp;draw();if (state.playing) scheduledFrame = requestAnimationFrame(animate);@@ -57,7 +61,7 @@ function synchronizePlayback() {function initialize() {if (renderer || disposed) return;try {- scene = createMotionScene();+ scene = createWallEPreviewScene();renderer = new Renderer67(canvas, scene.parts, {ground:false, transparent:true, hdr:true, samples:4, pixelRatio:1.25, memoryBudgetMiB:192,});@@ -75,10 +79,47 @@ function zoom(multiplier) {}function reset() {- Object.assign(state, { yaw:0.6, pitch:0.28, distance:8 });+ Object.assign(state, { yaw:0.6, pitch:0.28, distance:7.5 });draw();}+function pauseForAssembly() {+ state.playing = false;+ state.speed = 0;+ synchronizePlayback();+}++function finishAssemblyInteraction() {+ state.interacting = false;+ state.playing = state.assembly === 0 && !reducedMotion.matches;+ synchronizePlayback();+}++assemblySlider.addEventListener('pointerdown', () => {+ state.interacting = true;+ pauseForAssembly();+});+assemblySlider.addEventListener('input', () => {+ pauseForAssembly();+ state.assembly = Number(assemblySlider.value) / 100;+ const description = state.assembly === 0 ? 'Assembled' : Math.round(state.assembly * 100) + '% exploded';+ assemblyValue.textContent = description;+ assemblySlider.setAttribute('aria-valuetext', description);+ draw();+});+assemblySlider.addEventListener('change', () => {+ if (!state.interacting) finishAssemblyInteraction();+});+window.addEventListener('pointerup', () => {+ if (state.interacting) finishAssemblyInteraction();+});+window.addEventListener('pointercancel', () => {+ if (state.interacting) finishAssemblyInteraction();+});+assemblySlider.addEventListener('blur', () => {+ if (state.interacting) finishAssemblyInteraction();+});+canvas.addEventListener('pointerdown', event => {if (event.button !== 0 || pointer) return;pointer = { id:event.pointerId, x:event.clientX, y:event.clientY };@@ -108,8 +149,11 @@ canvas.addEventListener('wheel', event => {canvas.addEventListener('keydown', event => {if (event.code === 'Space') {event.preventDefault();- state.playing = !state.playing;- synchronizePlayback();+ if (state.assembly === 0 && !state.interacting) {+ state.playing = !state.playing;+ state.speed = 0;+ synchronizePlayback();+ }return;}if (!['ArrowLeft','ArrowRight','ArrowUp','ArrowDown','+','=','-','Home'].includes(event.key)) return;@@ -157,7 +201,8 @@ document.addEventListener('visibilitychange', () => {synchronizePlayback();});reducedMotion.addEventListener('change', () => {- state.playing = !reducedMotion.matches;+ state.playing = !reducedMotion.matches && state.assembly === 0 && !state.interacting;+ state.speed = 0;synchronizePlayback();});window.addEventListener('pagehide', event => {diff --git a/src/home.css b/src/home.cssindex 7c76e8b1..343e0295 100644--- a/src/home.css+++ b/src/home.css@@ -193,3 +193,9 @@ td, th { text-align:left; padding:12px; border-bottom:0; vertical-align:top; }.hero-actions a:hover, .hero-actions a:focus-visible { box-shadow:0 0 28px color-mix(in srgb,var(--text) 24%,transparent); }.navigation nav a:hover .icon, .navigation nav a:focus-visible .icon { filter:drop-shadow(0 0 5px currentColor); }++.preview-assembly { width:min(100%,440px); margin:12px auto 0; padding:0 16px; }+.preview-assembly-label { display:flex; align-items:center; justify-content:space-between; gap:12px; font-size:13px; }+.preview-assembly-label output { color:var(--muted); font-size:12px; }+.preview-assembly input { display:block; width:100%; min-height:44px; margin:6px 0; cursor:ew-resize; }+.preview-assembly p { font-size:12px; text-align:center; margin:0; }diff --git a/src/wall-e-preview-scene.js b/src/wall-e-preview-scene.jsnew file mode 100644index 00000000..7cdb10b9--- /dev/null+++ b/src/wall-e-preview-scene.js@@ -0,0 +1,126 @@+import { mat4 } from 'gl-matrix';+import { createModel } from './model.js';+import { EFFECT_PRESETS } from './effects.js';++const DURATION = 12;+const TRAVEL = 0.28;+// Keep this shoe centerline aligned with the polygon in createModel().+const TRACK_OUTLINE = [[0.095, -0.59], [0.095, 0.6], [0.32, 0.73], [0.66, 0.48], [0.7, -0.37], [0.38, -0.73]];+const WHEEL_PART_NAMES = new Set([+ 'Track sprocket drum', 'Track exposed wheel disc', 'Track wheel rim',+ 'Track wheel hub', 'Track hub retaining bolt', 'Track wheel bolt',+]);++function createTrackPath() {+ let perimeter = 0;+ const edges = TRACK_OUTLINE.map((start, index) => {+ const end = TRACK_OUTLINE[(index + 1) % TRACK_OUTLINE.length];+ const heightDelta = end[0] - start[0];+ const depthDelta = end[1] - start[1];+ const length = Math.hypot(heightDelta, depthDelta);+ const edge = { start, heightDelta, depthDelta, length, distance: perimeter, angle: -Math.atan2(heightDelta, depthDelta) };+ perimeter += length;+ return edge;+ });+ return { edges, perimeter };+}++function trackDistance(matrix, edges) {+ let closestDistance = Infinity;+ let distance = 0;+ for (const edge of edges) {+ const height = matrix[13] - edge.start[0];+ const depth = matrix[14] - edge.start[1];+ const fraction = Math.max(0, Math.min(1, (height * edge.heightDelta + depth * edge.depthDelta) / edge.length ** 2));+ const separation = Math.hypot(height - edge.heightDelta * fraction, depth - edge.depthDelta * fraction);+ if (separation < closestDistance) {+ closestDistance = separation;+ distance = edge.distance + edge.length * fraction;+ }+ }+ return distance;+}++function blendAngle(start, end, fraction) {+ const difference = Math.atan2(Math.sin(end - start), Math.cos(end - start));+ const easing = fraction * fraction * (3 - 2 * fraction);+ return start + difference * easing;+}++function sampleTrack(path, distance, result) {+ const wrappedDistance = ((distance % path.perimeter) + path.perimeter) % path.perimeter;+ const index = path.edges.findIndex(edge => wrappedDistance < edge.distance + edge.length);+ const edge = path.edges[index];+ const along = wrappedDistance - edge.distance;+ const fraction = along / edge.length;+ let angle = edge.angle;+ // Shoes hinge gradually at corners instead of snapping to a new edge angle.+ const hingeDistance = 0.025;+ if (along < hingeDistance) {+ const previous = path.edges[(index + path.edges.length - 1) % path.edges.length];+ angle = blendAngle(previous.angle, angle, 0.5 + along / (2 * hingeDistance));+ } else if (edge.length - along < hingeDistance) {+ const next = path.edges[(index + 1) % path.edges.length];+ angle = blendAngle(angle, next.angle, 0.5 + (along - edge.length) / (2 * hingeDistance));+ }+ result.height = edge.start[0] + edge.heightDelta * fraction;+ result.depth = edge.start[1] + edge.depthDelta * fraction;+ result.angle = angle;+}++export function createWallEPreviewScene() {+ const parts = createModel();+ const entries = parts.map(part => ({ part, base: mat4.clone(part.matrix) }));+ const path = createTrackPath();+ const shoes = entries.filter(({ part }) => part.name === 'Caterpillar steel shoe' || part.name === 'Caterpillar rubber traction pad');+ for (const shoe of shoes) {+ shoe.distance = trackDistance(shoe.base, path.edges);+ shoe.angle = Math.atan2(shoe.base[6], shoe.base[5]);+ }+ const wheelCenters = entries.filter(({ part }) => part.name === 'Track sprocket drum');+ const wheels = entries.filter(({ part }) => WHEEL_PART_NAMES.has(part.name));+ for (const wheel of wheels) {+ const sideCenters = wheelCenters.filter(center => Math.sign(center.base[12]) === Math.sign(wheel.base[12]));+ wheel.center = sideCenters.reduce((closest, center) => {+ const separation = candidate => Math.hypot(wheel.base[13] - candidate.base[13], wheel.base[14] - candidate.base[14]);+ return separation(center) < separation(closest) ? center : closest;+ });+ wheel.radius = Math.hypot(wheel.center.base[0], wheel.center.base[1], wheel.center.base[2]);+ }+ const rotation = mat4.create();+ const trackPosition = {};+ const frame = { target: [0, 1.5, 0], effects: EFFECT_PRESETS.neutral };++ function sample(time) {+ if (!Number.isFinite(time)) throw new TypeError('WALL·E preview time must be finite seconds.');+ const phase = (time % DURATION) / DURATION * Math.PI * 2;+ const travel = TRAVEL * Math.sin(phase);+ for (const { part, base } of entries) {+ part.matrix.set(base);+ part.matrix[14] += travel;+ }+ for (const shoe of shoes) {+ sampleTrack(path, shoe.distance - travel, trackPosition);+ mat4.fromXRotation(rotation, trackPosition.angle - shoe.angle);+ mat4.multiply(shoe.part.matrix, rotation, shoe.base);+ shoe.part.matrix[12] = shoe.base[12];+ shoe.part.matrix[13] = trackPosition.height;+ shoe.part.matrix[14] = trackPosition.depth + travel;+ }+ for (const wheel of wheels) {+ const angle = travel / wheel.radius;+ const cosine = Math.cos(angle);+ const sine = Math.sin(angle);+ const height = wheel.base[13] - wheel.center.base[13];+ const depth = wheel.base[14] - wheel.center.base[14];+ mat4.fromXRotation(rotation, angle);+ mat4.multiply(wheel.part.matrix, rotation, wheel.base);+ wheel.part.matrix[12] = wheel.base[12];+ wheel.part.matrix[13] = wheel.center.base[13] + height * cosine - depth * sine;+ wheel.part.matrix[14] = wheel.center.base[14] + height * sine + depth * cosine + travel;+ }+ return frame;+ }++ return { parts, duration: DURATION, sample };+}diff --git a/tests/wall-e-preview-scene.test.js b/tests/wall-e-preview-scene.test.jsnew file mode 100644index 00000000..35cfdb60--- /dev/null+++ b/tests/wall-e-preview-scene.test.js@@ -0,0 +1,112 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { createWallEPreviewScene } from '../src/wall-e-preview-scene.js';+import { createModel } from '../src/model.js';+import { groupParts } from '../src/renderer.js';++function snapshot(scene) {+ return scene.parts.map(part => Array.from(part.matrix));+}++function close(actual, expected, tolerance = 0.000001) {+ assert.ok(Math.abs(actual - expected) < tolerance, `${actual} differs from ${expected}`);+}++test('preview preserves the assembled model, all instance identities and assembly data', () => {+ const scene = createWallEPreviewScene();+ const model = createModel();+ const identities = scene.parts.map(part => ({ part, matrix: part.matrix, geometry: part.geometry, material: part.material, assembly: part.assembly }));+ assert.equal(scene.parts.length, 574);+ assert.equal(new Set(scene.parts.map(part => part.geometry)).size, 7);+ scene.sample(0);+ for (let index = 0; index < model.length; index++) {+ assert.equal(scene.parts[index].name, model[index].name);+ assert.deepEqual(scene.parts[index].assembly, model[index].assembly);+ scene.parts[index].matrix.forEach((value, axis) => close(value, model[index].matrix[axis]));+ }+ for (const time of [3, 9, 1200, -7]) {+ scene.sample(time);+ for (const identity of identities) {+ assert.equal(scene.parts.includes(identity.part), true);+ for (const key of ['matrix', 'geometry', 'material', 'assembly']) assert.equal(identity.part[key], identity[key]);+ }+ assert.equal(groupParts(scene.parts).length, 7);+ }+});++test('absolute sampling is deterministic, periodic and rejects invalid seconds', () => {+ const scene = createWallEPreviewScene();+ const frame = scene.sample(1.25);+ const first = snapshot(scene);+ scene.sample(8);+ scene.sample(-100);+ assert.deepEqual(scene.sample(1.25), frame);+ assert.deepEqual(snapshot(scene), first);+ scene.sample(scene.duration + 1.25);+ assert.deepEqual(snapshot(scene), first);+ assert.deepEqual(frame.target, [0, 1.5, 0]);+ assert.equal(frame.effects.bloom.strength, 0);+ for (const invalid of [NaN, Infinity, -Infinity, undefined, '1']) assert.throws(() => scene.sample(invalid), TypeError);+});++test('both caterpillars circulate with coherent shoe/pad pairs and stationary ground contact', () => {+ const scene = createWallEPreviewScene();+ const base = snapshot(scene);+ scene.sample(1);+ for (const side of [-1, 1]) {+ const indices = scene.parts.flatMap((part, index) => part.name === 'Caterpillar steel shoe' && Math.sign(base[index][12]) === side ? [index] : []);+ assert.ok(indices.length > 25);+ let moved = 0;+ for (const index of indices) {+ const shoe = scene.parts[index];+ const pad = scene.parts[index + 1];+ assert.equal(pad.name, 'Caterpillar rubber traction pad');+ for (const axis of [12, 13, 14]) close(shoe.matrix[axis], pad.matrix[axis]);+ close(Math.atan2(shoe.matrix[6], shoe.matrix[5]), Math.atan2(pad.matrix[6], pad.matrix[5]));+ if (Math.abs(shoe.matrix[13] - base[index][13]) > 0.01) moved++;+ if (base[index][13] < 0.1 && base[index][14] > -0.3 && base[index][14] < 0.3) {+ close(shoe.matrix[13], base[index][13]);+ close(shoe.matrix[14], base[index][14]);+ }+ }+ assert.ok(moved > 5);+ }+});++test('wheel bolts orbit their own sprockets and retain their radius and x coordinate', () => {+ const scene = createWallEPreviewScene();+ const base = snapshot(scene);+ const centers = scene.parts.flatMap((part, index) => part.name === 'Track sprocket drum' ? [index] : []);+ scene.sample(3);+ for (let index = 0; index < scene.parts.length; index++) {+ const part = scene.parts[index];+ if (part.name !== 'Track wheel bolt') continue;+ const centerIndex = centers.filter(candidate => Math.sign(base[candidate][12]) === Math.sign(base[index][12])).reduce((closest, candidate) => {+ const distance = value => Math.hypot(base[index][13] - base[value][13], base[index][14] - base[value][14]);+ return distance(candidate) < distance(closest) ? candidate : closest;+ });+ const center = scene.parts[centerIndex];+ const radius = Math.hypot(base[index][13] - base[centerIndex][13], base[index][14] - base[centerIndex][14]);+ close(Math.hypot(part.matrix[13] - center.matrix[13], part.matrix[14] - center.matrix[14]), radius);+ close(part.matrix[12], base[index][12]);+ assert.ok(Math.hypot(part.matrix[13] - base[index][13], part.matrix[14] - base[index][14] - 0.28) > 0.02);+ }+});++test('travel eases through reversals and every transform stays continuous across the loop seam', () => {+ const scene = createWallEPreviewScene();+ for (const boundary of [3, 9, 12]) {+ scene.sample(boundary - 0.0001);+ const before = snapshot(scene);+ scene.sample(boundary + 0.0001);+ scene.parts.forEach((part, index) => part.matrix.forEach((value, axis) => close(value, before[index][axis], 0.0002)));+ }+ scene.sample(3);+ close(scene.parts[0].matrix[14], 0.28);+ scene.sample(9);+ close(scene.parts[0].matrix[14], -0.28);+ for (let time = 0; time < scene.duration; time += 0.125) {+ scene.sample(time);+ for (const part of scene.parts) assert.ok(Array.from(part.matrix).every(Number.isFinite));+ }+});