3D engine that AI agents drive
Apply near and far given without a lens, so large models draw (#495)
Nguyen Xuan An6a2559c11ba8
diff --git a/changes/495.md b/changes/495.mdnew file mode 100644index 00000000..e8274501--- /dev/null+++ b/changes/495.md@@ -0,0 +1 @@+- `Renderer67.render` now applies `near` and `far` given without `fov` or `projection`, so a model hundreds of units wide frames and draws (issue #495); before, those two values were ignored and the fixed 0.1 to 100 range clipped everything.diff --git a/changes/495.size.md b/changes/495.size.mdnew file mode 100644index 00000000..b56048b0--- /dev/null+++ b/changes/495.size.md@@ -0,0 +1,2 @@+- goal:lit-box: the fix for #495 adds 54 bytes (near and far alone now reach the projection); a model wider than 60 units drew nothing before.+- goal:agent-scene: the same #495 fix adds 46 bytes.diff --git a/src/camera.js b/src/camera.jsindex c39c2944..e8ffcfbd 100644--- a/src/camera.js+++ b/src/camera.js@@ -2,10 +2,20 @@ import { mat4, vec3 } from 'gl-matrix';export function applyProjection(matrix, frame, aspect, { world = false } = {}) {if (!Number.isFinite(aspect) || aspect <= 0) throw new RangeError('Camera aspect must be positive and finite');- if (frame.projection === undefined && frame.fov === undefined) return matrix;+ const hasLens = frame.projection !== undefined || frame.fov !== undefined;+ const hasClipping = frame.near !== undefined || frame.far !== undefined;+ if (!hasLens && !hasClipping) return matrix;const near = frame.near ?? 0.1;const far = frame.far ?? (world ? 600 : 100);if (!Number.isFinite(near) || !Number.isFinite(far) || near <= 0 || far <= near) throw new RangeError('Camera clipping planes must satisfy 0 < near < far');+ if (!hasLens) {+ // Only the clipping planes are given: keep the renderer's own lens (its field of view and off-centre shift) and replace the planes.+ const fieldOfView = 2 * Math.atan(1 / matrix[5]);+ const shift = matrix[8];+ mat4.perspective(matrix, fieldOfView, aspect, near, far);+ matrix[8] = shift;+ return matrix;+ }if (frame.projection === 'orthographic') {const height = frame.orthographicHeight ?? 4;if (!Number.isFinite(height) || height <= 0) throw new RangeError('Orthographic height must be positive and finite');diff --git a/tests/large-bounds.test.js b/tests/large-bounds.test.jsnew file mode 100644index 00000000..4ed0606d--- /dev/null+++ b/tests/large-bounds.test.js@@ -0,0 +1,89 @@+// A model hundreds of units wide, framed with explicit near and far, must still draw (issue 495).+// It draws in headless Chrome (SwiftShader, no GPU) from the Vite dev server, and skips when Chrome cannot start.+import test from 'node:test';+import assert from 'node:assert/strict';+import { spawn } from 'node:child_process';+import { freePort } from './helpers/free-port.js';++const MODEL_SIZES = [4, 26, 65, 400, 4000];+const BACKGROUND = [37, 41, 45];++async function startServer(port) {+ const server = spawn(process.execPath, ['node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });+ await new Promise((resolve, reject) => {+ const timeout = setTimeout(() => reject(new Error('The dev server did not start.')), 30000);+ server.stdout.on('data', (data) => {+ if (/ready in/.test(data.toString())) {+ clearTimeout(timeout);+ resolve();+ }+ });+ server.on('exit', (code) => {+ clearTimeout(timeout);+ reject(new Error(`The dev server exited ${code}.`));+ });+ });+ return server;+}++async function drawnPixelCounts(page, sizes) {+ const task = async (options) => {+ const { box } = await import('/src/geometry.js');+ const { Renderer67 } = await import('/src/renderer.js');+ const canvas = document.createElement('canvas');+ canvas.width = 128;+ canvas.height = 128;+ document.body.append(canvas);+ const copy = document.createElement('canvas');+ copy.width = 128;+ copy.height = 128;+ const copyContext = copy.getContext('2d', { willReadFrequently: true });+ const counts = [];+ for (const size of options.sizes) {+ const matrix = new Float32Array([size, 0, 0, 0, 0, size, 0, 0, 0, 0, size, 0, -size * 0.1, size * 0.2, size * 0.3, 1]);+ const part = { name: 'model', geometry: box(0, 1), matrix, material: { tag: 'surface', color: [0.8, 0.3, 0.2], metallic: 0, roughness: 0.6, emission: 0 } };+ const renderer = new Renderer67(canvas, [part], { ground: false, samples: 1, pixelRatio: 1 });+ const distance = size * 3;+ renderer.render({ eye: [distance, distance * 0.6, distance], target: [-size * 0.1, size * 0.2, size * 0.3], near: distance / 100, far: distance * 10 });+ copyContext.drawImage(canvas, 0, 0);+ const pixels = copyContext.getImageData(0, 0, 128, 128).data;+ let drawn = 0;+ for (let index = 0; index < pixels.length; index += 4) {+ const difference = Math.abs(pixels[index] - options.background[0]) + Math.abs(pixels[index + 1] - options.background[1]) + Math.abs(pixels[index + 2] - options.background[2]);+ if (difference > 12) drawn++;+ }+ counts.push(drawn);+ renderer.dispose?.();+ }+ return counts;+ };+ return page.evaluate(task, { sizes, background: BACKGROUND });+}++test('a model from 4 to 4000 units wide draws with explicit near and far', async (context) => {+ let chromium;+ try {+ ({ chromium } = await import('playwright'));+ } catch {+ context.skip('playwright is not installed');+ return;+ }+ const port = await freePort();+ const server = await startServer(port);+ let browser;+ try {+ try {+ browser = await chromium.launch({ headless: true, args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--enable-webgl', '--ignore-gpu-blocklist'] });+ } catch (error) {+ context.skip(`Chrome did not start: ${error.message.split('\n')[0]}`);+ return;+ }+ const page = await browser.newPage({ viewport: { width: 128, height: 128 } });+ await page.goto(`http://127.0.0.1:${port}/`);+ const counts = await drawnPixelCounts(page, MODEL_SIZES);+ counts.forEach((count, index) => assert.ok(count > 500, `a model ${MODEL_SIZES[index]} units wide drew ${count} pixels`));+ } finally {+ await browser?.close();+ server.kill();+ }+});