3D engine that AI agents drive
Add bounded glTF asset bridge with rigid animation and textures
Nguyen Xuan And88984e46286
diff --git a/docs/gltf.md b/docs/gltf.mdnew file mode 100644index 00000000..52cdee01--- /dev/null+++ b/docs/gltf.md@@ -0,0 +1,134 @@+# glTF asset bridge++`src/gltf.js` converts glTF 2.0 assets into Renderer67 parts.+It uses the optional Three.js `GLTFLoader` for decoding, without creating a Three.js renderer.+The core renderer does not import this addon.++```js+import { loadGltf } from './src/gltf.js';+import { Renderer67 } from './src/renderer.js';++const asset = await loadGltf('/models/object.glb');+const renderer = new Renderer67(canvas, asset.parts, {+ ground: false,+ samples: 4,+ memoryBudgetMiB: 256,+});+renderer.render({ eye: [0, 2, 5], target: [0, 1, 0] });+```++## Input++`loadGltf(source, options)` accepts a URL string, a `URL`, a local `File`, or a `Blob`.+`parseGltf(data, options)` accepts an `ArrayBuffer` or a glTF JSON string.+Both functions return a promise for the decoded asset.++URL loading derives the resource directory from the asset URL.+An explicit `baseUrl` overrides that directory.+Relative external resources in a local glTF file require `baseUrl`.+GLB files can contain their buffers and images without external resources.++HTTP, HTTPS, blob URLs, and base64 data URIs are supported.+External requests use bounded streaming fetches.+The optional `signal` aborts those fetches.++| Option | Default | Limit |+| --- | ---: | --- |+| `maxBytes` | 33,554,432 | Total encoded input and fetched external resources. |+| `maxVertices` | 2,000,000 | Declared and decoded geometry vertices. Indices have a separate ceiling of twelve times this value. |+| `maxParts` | 20,000 | Declared nodes and decoded mesh parts. |+| `maxResources` | 64 | Unique resource URIs. |+| `maxTexturePixels` | 16,777,216 | Combined texture image pixels, checked before decoding. |++Every numeric limit must be a positive safe integer.+The importer rejects cyclic hierarchies, multiple parents, and hierarchies deeper than 256 levels.+It checks PNG and JPEG dimensions before browser image decoding.+Decoded accessor data has a separate ceiling of four times `maxBytes`.+These limits bound asset inputs and geometry, rather than the renderer's total GPU allocation.+The renderer retains its separate memory budget.++## Output++The result contains `parts`, `animations`, `applyAnimation`, `dispose`, and `capabilities`.+Each part contains `name`, `geometry`, `matrix`, and `material`.+The column-major matrix contains the complete world transform, including parent transforms.+Repeated primitives share geometry objects.+Static mirrored instances reverse triangle winding while preserving their world matrices.++Geometry contains `positions` and `normals` as `Float32Array` values, plus `indices` as a `Uint32Array`.+The importer computes missing normals and normalizes supplied normals.+It rejects non-finite attributes, invalid indices, singular transforms, and unusable normals.+`geometry.uvs` preserves `TEXCOORD_0` as two floats per vertex.++Materials preserve linear `color`, `metallic`, `roughness`, and `emissiveColor` factors.+`emission` contains the emissive intensity.+The `gltf` tag uses standard PBR shading without studio finish overrides.+An emissive-strength extension preserves its intensity.++A base color texture adds this descriptor:++```js+material.map = {+ image, // Decoded browser image or ImageBitmap.+ uvChannel: 0,+ wrapS: 10497, // Native WebGL sampler enum.+ wrapT: 33071,+ minFilter: 9729,+ magFilter: 9729,+ flipY: false,+ colorSpace: 'srgb',+};+```++Texture descriptors share identity when materials reuse the same texture.+Texture rendering requires batches grouped by geometry and texture identity.+The image descriptor requires sRGB sampling with conversion to linear RGB before lighting.+The importer rejects a requested texture that fails to decode, rather than returning an untextured material.++## Animation and ownership++`animations` contains the decoded Three.js animation clips, including their names, durations, and tracks.+`applyAnimation(time, clipIndex = 0)` evaluates one rigid node animation at an absolute time in seconds.+Time clamps to the selected clip's duration.+The hook updates the existing part matrices and returns the same `parts` array.+Seeking in a different order produces the same matrices.++```js+const changedParts = asset.applyAnimation(1.25, 0);+renderer.updateParts(changedParts);+renderer.render(camera);+```++Translation, rotation, and scale tracks are supported.+The importer rejects animation poses that become singular or change triangle winding.+It does not support skinning or morph targets.++The caller owns the Renderer67 instance.+`asset.dispose()` releases importer textures, image bitmaps, parser resources, and temporary object URLs.+It does not dispose the renderer.+Disposal is idempotent, and subsequent animation calls fail explicitly.++1. Call `renderer.dispose()` before replacing its imported asset.+2. Call `asset.dispose()` after the renderer finishes using the asset's images.++## Supported subset++`GLTF_CAPABILITIES` lists the supported and unsupported features.+The bridge supports triangle meshes, PNG/JPEG base color textures, rigid animations, and metallic-roughness material factors.+It also supports `KHR_mesh_quantization` and `KHR_materials_emissive_strength`.++The importer rejects alpha blending, alpha masking, double-sided materials, vertex colors, and additional material texture channels.+It rejects texture transforms, DRACO, KTX2, GPU instancing, and unsupported extensions, including optional extensions.+Line and point primitives are unsupported.+The bridge does not import cameras, lights, scene environments, or skin data into Renderer67.+The caller supplies its own camera and lighting.++## Verification++`node --test tests/gltf.test.js` covers generated GLB triangles and the existing `exportGlb` roundtrip.+The tests cover parent transforms, missing normals, UVs, shared geometry, PBR factors, mirrored winding, and animation seeks.+The tests also cover input limits, unsupported features, compressed image dimensions, and oversized fetch cancellation.++`tests/fixtures/textured-triangle.gltf` contains an original triangle and a generated two-pixel-square PNG.+Chrome loading preserved its UVs, linear material factors, sampler values, and decoded image dimensions.+Renderer integration owns the final textured rendering check.diff --git a/src/gltf.js b/src/gltf.jsnew file mode 100644index 00000000..d984f95f--- /dev/null+++ b/src/gltf.js@@ -0,0 +1,515 @@+import { AnimationMixer, LoadingManager, LoopOnce } from 'three';+import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';++export const GLTF_CAPABILITIES = Object.freeze({+ formats: ['glb', 'gltf'],+ supported: ['triangle meshes', 'world transforms', 'rigid node animations', 'PNG/JPEG base color textures', 'TEXCOORD_0', 'metallic roughness factors', 'emissive factors', 'KHR_mesh_quantization', 'KHR_materials_emissive_strength'],+ unsupported: ['skinning', 'morph targets', 'vertex colors', 'alpha blending or masking', 'double-sided materials', 'normal/occlusion/emissive/metallic-roughness textures', 'texture transforms', 'DRACO', 'KTX2', 'GPU instancing'],+});++const DEFAULT_MAX_BYTES = 32 * 1024 * 1024;+const DEFAULT_MAX_VERTICES = 2_000_000;+const ALLOWED_EXTENSIONS = new Set(['KHR_mesh_quantization', 'KHR_materials_emissive_strength']);+const WRAPPING = { 1000: 10497, 1001: 33071, 1002: 33648 };+const FILTERING = { 1003: 9728, 1004: 9984, 1005: 9986, 1006: 9729, 1007: 9985, 1008: 9987 };++function limitsFor(options) {+ const limits = {+ maxBytes: options.maxBytes ?? DEFAULT_MAX_BYTES,+ maxVertices: options.maxVertices ?? DEFAULT_MAX_VERTICES,+ maxParts: options.maxParts ?? 20_000,+ maxResources: options.maxResources ?? 64,+ maxTexturePixels: options.maxTexturePixels ?? 16_777_216,+ };+ for (const [name, value] of Object.entries(limits)) {+ if (!Number.isSafeInteger(value) || value <= 0) throw new TypeError(`${name} must be a positive safe integer.`);+ }+ return limits;+}++function documentFrom(data, maxBytes) {+ if (typeof data === 'string') {+ if (new TextEncoder().encode(data).byteLength > maxBytes) throw new Error('glTF exceeds maxBytes.');+ return JSON.parse(data);+ }+ if (!(data instanceof ArrayBuffer)) throw new TypeError('parseGltf expects an ArrayBuffer or glTF JSON string.');+ if (data.byteLength > maxBytes) throw new Error('glTF exceeds maxBytes.');+ if (data.byteLength < 4) throw new Error('glTF input is truncated.');+ const header = new DataView(data);+ if (header.getUint32(0, true) !== 0x46546c67) return JSON.parse(new TextDecoder().decode(data));+ if (data.byteLength < 20 || header.getUint32(4, true) !== 2 || header.getUint32(8, true) !== data.byteLength) {+ throw new Error('Invalid GLB version or declared length.');+ }+ const jsonLength = header.getUint32(12, true);+ if (header.getUint32(16, true) !== 0x4e4f534a || jsonLength % 4 !== 0 || 20 + jsonLength > data.byteLength) {+ throw new Error('Invalid GLB JSON chunk.');+ }+ let offset = 20 + jsonLength;+ while (offset < data.byteLength) {+ if (offset + 8 > data.byteLength) throw new Error('Truncated GLB chunk.');+ const length = header.getUint32(offset, true);+ if (length % 4 !== 0 || offset + 8 + length > data.byteLength) throw new Error('Invalid GLB chunk length.');+ offset += 8 + length;+ }+ return JSON.parse(new TextDecoder().decode(new Uint8Array(data, 20, jsonLength)));+}++function validateDocument(document, limits) {+ if (document.asset?.version !== '2.0') throw new Error('Only glTF 2.0 assets are supported.');+ const pending = [document];+ while (pending.length) {+ const value = pending.pop();+ if (!value || typeof value !== 'object') continue;+ for (const extension of Object.keys(value.extensions ?? {})) {+ if (!ALLOWED_EXTENSIONS.has(extension)) throw new Error(`Unsupported glTF extension: ${extension}.`);+ }+ pending.push(...Object.values(value).filter(child => child && typeof child === 'object'));+ }+ for (const extension of [...(document.extensionsUsed ?? []), ...(document.extensionsRequired ?? [])]) {+ if (!ALLOWED_EXTENSIONS.has(extension)) throw new Error(`Unsupported glTF extension: ${extension}.`);+ }+ let declaredBytes = 0;+ for (const buffer of document.buffers ?? []) {+ if (!Number.isSafeInteger(buffer.byteLength) || buffer.byteLength < 0) throw new Error('Invalid glTF buffer length.');+ declaredBytes += buffer.byteLength;+ }+ if (declaredBytes > limits.maxBytes) throw new Error('glTF buffers exceed maxBytes.');+ let vertices = 0;+ let indices = 0;+ for (const mesh of document.meshes ?? []) {+ for (const primitive of mesh.primitives ?? []) {+ if ((primitive.mode ?? 4) !== 4) throw new Error('Only glTF TRIANGLES primitives are supported.');+ if (primitive.targets?.length) throw new Error('Morph targets are unsupported.');+ if (primitive.attributes?.COLOR_0 !== undefined) throw new Error('Vertex colors are unsupported.');+ const position = document.accessors?.[primitive.attributes?.POSITION];+ if (!position || position.type !== 'VEC3' || !Number.isSafeInteger(position.count) || position.count < 3) {+ throw new Error('Each mesh primitive requires a valid POSITION accessor.');+ }+ vertices += position.count;+ for (const [name, size] of [['NORMAL', 'VEC3'], ['TEXCOORD_0', 'VEC2']]) {+ const accessorIndex = primitive.attributes?.[name];+ const accessor = document.accessors?.[accessorIndex];+ if (accessorIndex !== undefined && (!accessor || accessor.type !== size || accessor.count !== position.count)) {+ throw new Error(`${name} must match the POSITION vertex count and component shape.`);+ }+ }+ const index = primitive.indices === undefined ? null : document.accessors?.[primitive.indices];+ if (primitive.indices !== undefined && (!index || index.type !== 'SCALAR' || !Number.isSafeInteger(index.count) || index.count < 3)) {+ throw new Error('Invalid mesh index accessor.');+ }+ const indexCount = index?.count ?? position.count;+ if (indexCount % 3 !== 0) throw new Error('Triangle index count must be divisible by three.');+ indices += indexCount;+ }+ }+ if (vertices > limits.maxVertices || indices > limits.maxVertices * 12) throw new Error('glTF geometry exceeds maxVertices or its index budget.');+ if ((document.nodes?.length ?? 0) > limits.maxParts) throw new Error('glTF nodes exceed maxParts.');+ for (const node of document.nodes ?? []) {+ if (node.skin !== undefined) throw new Error('Skinned meshes are unsupported.');+ if (node.extensions && Object.keys(node.extensions).length) throw new Error('Node extensions are unsupported.');+ }+ validateHierarchy(document.nodes ?? []);+ for (const animation of document.animations ?? []) {+ for (const channel of animation.channels ?? []) {+ if (!['translation', 'rotation', 'scale'].includes(channel.target?.path)) throw new Error('Only rigid node translation, rotation and scale animations are supported.');+ }+ }+ for (const material of document.materials ?? []) {+ const pbr = material.pbrMetallicRoughness ?? {};+ const factors = [pbr.baseColorFactor ?? [1, 1, 1, 1], material.emissiveFactor ?? [0, 0, 0]];+ for (let index = 0; index < factors.length; index++) {+ if (!Array.isArray(factors[index]) || factors[index].length !== (index === 0 ? 4 : 3) || !factors[index].every(value => Number.isFinite(value) && value >= 0 && value <= 1)) {+ throw new Error('Invalid glTF material color factor.');+ }+ }+ for (const factor of [pbr.metallicFactor ?? 1, pbr.roughnessFactor ?? 1]) {+ if (!Number.isFinite(factor) || factor < 0 || factor > 1) throw new Error('Invalid glTF metallic or roughness factor.');+ }+ if ((material.alphaMode ?? 'OPAQUE') !== 'OPAQUE') throw new Error(`Unsupported alpha mode: ${material.alphaMode}.`);+ if (material.doubleSided) throw new Error('Double-sided materials are unsupported.');+ if (material.normalTexture || material.occlusionTexture || material.emissiveTexture || material.pbrMetallicRoughness?.metallicRoughnessTexture) {+ throw new Error('Only base color textures are supported. Other material texture channels are unsupported.');+ }+ const texture = material.pbrMetallicRoughness?.baseColorTexture;+ if (texture && ((texture.texCoord ?? 0) !== 0 || texture.extensions && Object.keys(texture.extensions).length)) {+ throw new Error('Base color textures require TEXCOORD_0 without texture transforms.');+ }+ }+ let decodedBytes = 0;+ const components = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 };+ for (const accessor of document.accessors ?? []) {+ if (!Number.isSafeInteger(accessor.count) || accessor.count < 0 || accessor.count > limits.maxVertices * 12) {+ throw new Error('glTF accessor exceeds its element budget.');+ }+ if (!components[accessor.type]) throw new Error('Unsupported glTF accessor shape.');+ decodedBytes += accessor.count * components[accessor.type] * 4;+ }+ if (decodedBytes > limits.maxBytes * 4) throw new Error('Decoded glTF accessors exceed four times maxBytes.');+ return vertices;+}++function validateHierarchy(nodes) {+ const parents = new Set();+ for (const node of nodes) {+ for (const child of node.children ?? []) {+ if (!Number.isInteger(child) || !nodes[child] || parents.has(child)) throw new Error('Invalid glTF hierarchy child or multiple parents.');+ parents.add(child);+ }+ }+ const states = new Uint8Array(nodes.length);+ for (let root = 0; root < nodes.length; root++) {+ const pending = [{ index: root, exit: false, depth: 0 }];+ while (pending.length) {+ const current = pending.pop();+ if (current.exit) {+ states[current.index] = 2;+ continue;+ }+ if (states[current.index] === 1) throw new Error('Cyclic glTF node hierarchy.');+ if (states[current.index] === 2) continue;+ if (current.depth > 256) throw new Error('glTF hierarchy exceeds 256 levels.');+ states[current.index] = 1;+ pending.push({ ...current, exit: true });+ for (const child of nodes[current.index].children ?? []) pending.push({ index: child, exit: false, depth: current.depth + 1 });+ }+ }+}++function dataUriBytes(uri) {+ const comma = uri.indexOf(',');+ if (comma < 0 || !uri.slice(0, comma).endsWith(';base64')) throw new Error('Embedded glTF resources must use base64 data URIs.');+ const binary = atob(uri.slice(comma + 1));+ return Uint8Array.from(binary, character => character.charCodeAt(0));+}++function validateImage(bytes, maxPixels) {+ const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);+ let width;+ let height;+ if (bytes.length >= 24 && header.getUint32(0) === 0x89504e47 && header.getUint32(4) === 0x0d0a1a0a && header.getUint32(12) === 0x49484452) {+ width = header.getUint32(16);+ height = header.getUint32(20);+ } else if (bytes.length >= 4 && header.getUint16(0) === 0xffd8) {+ let offset = 2;+ while (offset + 4 <= bytes.length) {+ if (bytes[offset++] !== 0xff) break;+ while (bytes[offset] === 0xff) offset++;+ const marker = bytes[offset++];+ if (marker === 0xd9 || marker === 0xda) break;+ if (offset + 2 > bytes.length) break;+ const length = header.getUint16(offset);+ if (length < 2 || offset + length > bytes.length) break;+ if ([0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf].includes(marker) && length >= 7) {+ height = header.getUint16(offset + 3);+ width = header.getUint16(offset + 5);+ break;+ }+ offset += length;+ }+ }+ if (!width || !height) throw new Error('Only valid PNG and JPEG texture images are supported.');+ if (width * height > maxPixels) throw new Error('Texture image exceeds maxTexturePixels before decoding.');+ return width * height;+}++function binaryChunk(data) {+ if (!(data instanceof ArrayBuffer) || data.byteLength < 20) return null;+ const header = new DataView(data);+ if (header.getUint32(0, true) !== 0x46546c67) return null;+ let offset = 12;+ while (offset + 8 <= data.byteLength) {+ const length = header.getUint32(offset, true);+ if (header.getUint32(offset + 4, true) === 0x004e4942) return new Uint8Array(data, offset + 8, length);+ offset += 8 + length;+ }+ return null;+}++async function boundedFetch(url, maxBytes, signal) {+ const response = await fetch(url, { signal });+ if (!response.ok) throw new Error(`glTF fetch failed (${response.status}): ${url}.`);+ const length = response.headers.get('content-length');+ if (length !== null && Number(length) > maxBytes) {+ await response.body?.cancel();+ throw new Error('glTF resource exceeds maxBytes.');+ }+ if (!response.body?.getReader) throw new Error('Bounded glTF loading requires a streaming fetch response.');+ const reader = response.body.getReader();+ const chunks = [];+ let total = 0;+ try {+ while (true) {+ const { done, value } = await reader.read();+ if (done) break;+ total += value.byteLength;+ if (total > maxBytes) throw new Error('glTF resource exceeds maxBytes.');+ chunks.push(value);+ }+ } catch (error) {+ await reader.cancel().catch(() => {});+ throw error;+ } finally {+ reader.releaseLock();+ }+ const bytes = new Uint8Array(total);+ let offset = 0;+ for (const chunk of chunks) {+ bytes.set(chunk, offset);+ offset += chunk.byteLength;+ }+ return { bytes, mimeType: response.headers.get('content-type') ?? 'application/octet-stream' };+}++async function prepareResources(document, data, dataBytes, options, limits) {+ const resources = new Map();+ const resourceBytes = new Map();+ const objectUrls = [];+ const definitions = [...(document.buffers ?? []), ...(document.images ?? [])];+ const uris = new Set(definitions.map(resource => resource.uri).filter(uri => uri !== undefined));+ if (uris.size > limits.maxResources) throw new Error('glTF resources exceed maxResources.');+ let usedBytes = dataBytes;+ try {+ for (const uri of uris) {+ if (typeof uri !== 'string') throw new Error('Invalid glTF resource URI.');+ if (uri.startsWith('data:')) {+ resourceBytes.set(uri, dataUriBytes(uri));+ resources.set(uri, uri);+ continue;+ }+ if (!options.baseUrl && !/^(https?:|blob:)/.test(uri)) throw new Error('External glTF resources require baseUrl. Use GLB for a self-contained local file.');+ const resolved = new URL(uri, options.baseUrl || undefined).href;+ if (!/^(https?:|blob:)/.test(resolved)) throw new Error('Only HTTP, HTTPS, blob and embedded data resources are supported.');+ const resource = await boundedFetch(resolved, limits.maxBytes - usedBytes, options.signal);+ usedBytes += resource.bytes.byteLength;+ resourceBytes.set(uri, resource.bytes);+ const objectUrl = URL.createObjectURL(new Blob([resource.bytes], { type: resource.mimeType }));+ objectUrls.push(objectUrl);+ resources.set(resolved, objectUrl);+ }+ let texturePixels = 0;+ for (const image of document.images ?? []) {+ let bytes = resourceBytes.get(image.uri);+ if (!bytes && image.bufferView !== undefined) {+ const view = document.bufferViews?.[image.bufferView];+ const buffer = view && document.buffers?.[view.buffer];+ const source = buffer?.uri ? resourceBytes.get(buffer.uri) : view?.buffer === 0 ? binaryChunk(data) : null;+ const offset = view?.byteOffset ?? 0;+ if (!source || !Number.isSafeInteger(view.byteLength) || view.byteLength < 0 || offset < 0 || offset + view.byteLength > source.length) {+ throw new Error('Invalid embedded glTF image buffer view.');+ }+ bytes = source.subarray(offset, offset + view.byteLength);+ }+ if (!bytes) throw new Error('glTF image has no supported source.');+ texturePixels += validateImage(bytes, limits.maxTexturePixels);+ if (texturePixels > limits.maxTexturePixels) throw new Error('Combined texture images exceed maxTexturePixels before decoding.');+ }+ return { resources, objectUrls };+ } catch (error) {+ objectUrls.forEach(url => URL.revokeObjectURL(url));+ throw error;+ }+}++function copyAttribute(attribute, size) {+ if (attribute.itemSize !== size) throw new Error(`Expected a ${size}-component mesh attribute.`);+ const result = new Float32Array(attribute.count * size);+ const getters = ['getX', 'getY', 'getZ', 'getW'];+ for (let vertex = 0; vertex < attribute.count; vertex++) {+ for (let component = 0; component < size; component++) {+ const value = attribute[getters[component]](vertex);+ if (!Number.isFinite(value)) throw new Error('Mesh attributes must contain finite values.');+ result[vertex * size + component] = value;+ }+ }+ return result;+}++function convertGeometry(source, mirrored) {+ const position = source.getAttribute('position');+ if (!source.getAttribute('normal')) source.computeVertexNormals();+ if (source.getAttribute('normal').count !== position.count) throw new Error('NORMAL and POSITION counts must match.');+ const geometry = { positions: copyAttribute(position, 3), normals: copyAttribute(source.getAttribute('normal'), 3) };+ for (let offset = 0; offset < geometry.normals.length; offset += 3) {+ const length = Math.hypot(...geometry.normals.subarray(offset, offset + 3));+ if (length < 1e-8) throw new Error('Degenerate geometry has no usable vertex normal.');+ for (let axis = 0; axis < 3; axis++) geometry.normals[offset + axis] /= length;+ }+ const sourceIndex = source.getIndex();+ geometry.indices = new Uint32Array(sourceIndex?.count ?? position.count);+ for (let index = 0; index < geometry.indices.length; index++) {+ const value = sourceIndex ? sourceIndex.getX(index) : index;+ if (!Number.isInteger(value) || value < 0 || value >= position.count) throw new Error('Mesh index is out of range.');+ geometry.indices[index] = value;+ }+ if (mirrored) {+ for (let index = 0; index < geometry.indices.length; index += 3) {+ const second = geometry.indices[index + 1];+ geometry.indices[index + 1] = geometry.indices[index + 2];+ geometry.indices[index + 2] = second;+ }+ }+ const uv = source.getAttribute('uv');+ if (uv && uv.count !== position.count) throw new Error('TEXCOORD_0 and POSITION counts must match.');+ if (uv) geometry.uvs = copyAttribute(uv, 2);+ return geometry;+}++function convertMaterial(source, limits, maps) {+ const material = {+ tag: 'gltf',+ color: source.color.toArray(),+ metallic: source.metalness,+ roughness: source.roughness,+ emission: source.emissiveIntensity ?? 1,+ emissiveColor: source.emissive.toArray(),+ };+ if (!material.emissiveColor.some(value => value > 0)) material.emission = 0;+ if (!Number.isFinite(Math.fround(material.emission)) || material.emission < 0) throw new Error('Emissive intensity must be a finite nonnegative float.');+ if (source.map) {+ if (!maps.has(source.map)) {+ const image = source.map.image;+ if (!image || image.width * image.height > limits.maxTexturePixels) throw new Error('Base color image is missing or exceeds maxTexturePixels.');+ maps.set(source.map, {+ image,+ uvChannel: 0,+ wrapS: WRAPPING[source.map.wrapS],+ wrapT: WRAPPING[source.map.wrapT],+ minFilter: FILTERING[source.map.minFilter],+ magFilter: FILTERING[source.map.magFilter],+ flipY: false,+ colorSpace: 'srgb',+ });+ }+ material.map = maps.get(source.map);+ }+ return material;+}++function releaseParsed(gltf) {+ const geometries = new Set();+ const materials = new Set();+ const textures = new Set();+ for (const scene of gltf.scenes) {+ scene.traverse(node => {+ if (node.geometry) geometries.add(node.geometry);+ for (const material of Array.isArray(node.material) ? node.material : node.material ? [node.material] : []) {+ materials.add(material);+ for (const value of Object.values(material)) if (value?.isTexture) textures.add(value);+ }+ });+ }+ geometries.forEach(geometry => geometry.dispose());+ materials.forEach(material => material.dispose());+ textures.forEach(texture => {+ texture.image?.close?.();+ texture.dispose();+ });+}++/** Parse a bounded glTF 2.0 asset into Renderer67 parts. Three.js only decodes the asset. */+export async function parseGltf(data, options = {}) {+ const limits = limitsFor(options);+ const document = documentFrom(data, limits.maxBytes);+ validateDocument(document, limits);+ const dataBytes = typeof data === 'string' ? new TextEncoder().encode(data).byteLength : data.byteLength;+ const { resources, objectUrls } = await prepareResources(document, data, dataBytes, options, limits);+ const manager = new LoadingManager();+ manager.setURLModifier(url => {+ if (resources.has(url)) return resources.get(url);+ if (url.startsWith('blob:')) return url;+ throw new Error(`Unregistered glTF resource: ${url}.`);+ });+ let gltf;+ try {+ gltf = await new GLTFLoader(manager).parseAsync(data, options.baseUrl ?? '');+ gltf.scene.updateMatrixWorld(true);+ const parts = [];+ const bindings = [];+ const geometries = new Map();+ const materials = new Map();+ const maps = new Map();+ let actualVertices = 0;+ gltf.scene.traverse(node => {+ if (!node.isMesh) return;+ if (node.isSkinnedMesh || node.morphTargetInfluences?.length) throw new Error('Skinning and morph targets are unsupported.');+ if (Array.isArray(node.material)) throw new Error('Multiple materials on one decoded primitive are unsupported.');+ const materialIndex = gltf.parser.associations.get(node.material)?.materials;+ if (document.materials?.[materialIndex]?.pbrMetallicRoughness?.baseColorTexture && !node.material.map) {+ throw new Error('Base color texture failed to decode. Use a valid PNG or JPEG image.');+ }+ const determinant = node.matrixWorld.determinant();+ if (!Number.isFinite(determinant) || Math.abs(determinant) < 1e-12) throw new Error('Mesh world transform is singular or non-finite.');+ const mirrored = determinant < 0;+ const geometryKey = `${node.geometry.uuid}:${mirrored}`;+ if (!geometries.has(geometryKey)) {+ actualVertices += node.geometry.getAttribute('position').count;+ if (actualVertices > limits.maxVertices) throw new Error('Decoded geometry exceeds maxVertices.');+ geometries.set(geometryKey, convertGeometry(node.geometry, mirrored));+ }+ if (!materials.has(node.material)) materials.set(node.material, convertMaterial(node.material, limits, maps));+ const geometry = geometries.get(geometryKey);+ const material = materials.get(node.material);+ if (material.map && !geometry.uvs) throw new Error('A base color texture requires TEXCOORD_0.');+ const part = { name: node.name || `glTF mesh ${parts.length + 1}`, geometry, matrix: new Float32Array(node.matrixWorld.elements), material };+ if (!Array.from(part.matrix).every(Number.isFinite)) throw new Error('Mesh world matrix must be finite.');+ parts.push(part);+ bindings.push({ node, part, mirrored });+ if (parts.length > limits.maxParts) throw new Error('Decoded parts exceed maxParts.');+ });+ if (!parts.length) throw new Error('glTF scene contains no supported triangle meshes.');+ const mixer = new AnimationMixer(gltf.scene);+ let disposed = false;+ function dispose() {+ if (disposed) return;+ disposed = true;+ mixer.stopAllAction();+ mixer.uncacheRoot(gltf.scene);+ releaseParsed(gltf);+ objectUrls.forEach(url => URL.revokeObjectURL(url));+ }+ function applyAnimation(time, clipIndex = 0) {+ if (disposed) throw new Error('The glTF asset was disposed.');+ if (!Number.isFinite(time)) throw new TypeError('Animation time must be finite.');+ if (!Number.isInteger(clipIndex) || !gltf.animations[clipIndex]) throw new RangeError('Animation clip index is out of range.');+ const clip = gltf.animations[clipIndex];+ mixer.stopAllAction();+ const action = mixer.clipAction(clip);+ action.reset();+ action.setLoop(LoopOnce, 1);+ action.clampWhenFinished = true;+ action.play();+ mixer.setTime(Math.max(0, Math.min(time, clip.duration)));+ gltf.scene.updateMatrixWorld(true);+ for (const { node, mirrored } of bindings) {+ const determinant = node.matrixWorld.determinant();+ if (!Number.isFinite(determinant) || Math.abs(determinant) < 1e-12 || (determinant < 0) !== mirrored) {+ throw new Error('Animation creates a singular transform or changes triangle winding.');+ }+ }+ for (const { node, part } of bindings) part.matrix.set(node.matrixWorld.elements);+ return parts;+ }+ return { parts, animations: gltf.animations, applyAnimation, dispose, capabilities: GLTF_CAPABILITIES };+ } catch (error) {+ if (gltf) releaseParsed(gltf);+ objectUrls.forEach(url => URL.revokeObjectURL(url));+ throw error;+ }+}++/** Fetch a URL or read a local File/Blob, then parse it with the same limits. */+export async function loadGltf(source, options = {}) {+ const limits = limitsFor(options);+ if (typeof source === 'string' || source instanceof URL) {+ const url = new URL(String(source), globalThis.location?.href).href;+ if (!/^(https?:|blob:|data:)/.test(url)) throw new Error('Unsupported glTF source URL protocol.');+ const resource = await boundedFetch(url, limits.maxBytes, options.signal);+ const baseUrl = options.baseUrl ?? (/^https?:/.test(url) ? new URL('.', url).href : '');+ return parseGltf(resource.bytes.buffer, { ...options, baseUrl });+ }+ if (!source || typeof source.arrayBuffer !== 'function' || !Number.isSafeInteger(source.size)) {+ throw new TypeError('loadGltf expects a URL or a File/Blob.');+ }+ if (source.size > limits.maxBytes) throw new Error('Local glTF file exceeds maxBytes.');+ return parseGltf(await source.arrayBuffer(), options);+}diff --git a/tests/fixtures/textured-triangle.gltf b/tests/fixtures/textured-triangle.gltfnew file mode 100644index 00000000..13af8d9d--- /dev/null+++ b/tests/fixtures/textured-triangle.gltf@@ -0,0 +1,110 @@+{+ "asset": {+ "version": "2.0"+ },+ "scene": 0,+ "scenes": [+ {+ "nodes": [+ 0+ ]+ }+ ],+ "nodes": [+ {+ "name": "Textured triangle",+ "mesh": 0+ }+ ],+ "meshes": [+ {+ "primitives": [+ {+ "attributes": {+ "POSITION": 0,+ "TEXCOORD_0": 1+ },+ "material": 0+ }+ ]+ }+ ],+ "materials": [+ {+ "pbrMetallicRoughness": {+ "baseColorFactor": [+ 0.8,+ 0.6,+ 0.4,+ 1+ ],+ "metallicFactor": 0,+ "roughnessFactor": 0.7,+ "baseColorTexture": {+ "index": 0+ }+ }+ }+ ],+ "textures": [+ {+ "source": 0,+ "sampler": 0+ }+ ],+ "samplers": [+ {+ "wrapS": 10497,+ "wrapT": 33071,+ "minFilter": 9729,+ "magFilter": 9729+ }+ ],+ "images": [+ {+ "uri": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR4AWI6k2b8H4SZGKAAAAAA//8VS4tGAAAABklEQVQDAEBuBM3uhq5wAAAAAElFTkSuQmCC"+ }+ ],+ "accessors": [+ {+ "bufferView": 0,+ "componentType": 5126,+ "count": 3,+ "type": "VEC3",+ "min": [+ -1,+ 0,+ 0+ ],+ "max": [+ 1,+ 1.5,+ 0+ ]+ },+ {+ "bufferView": 1,+ "componentType": 5126,+ "count": 3,+ "type": "VEC2"+ }+ ],+ "bufferViews": [+ {+ "buffer": 0,+ "byteOffset": 0,+ "byteLength": 36+ },+ {+ "buffer": 0,+ "byteOffset": 36,+ "byteLength": 24+ }+ ],+ "buffers": [+ {+ "byteLength": 60,+ "uri": "data:application/octet-stream;base64,AACAvwAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAwD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAD8AAIA/"+ }+ ]+}diff --git a/tests/gltf.test.js b/tests/gltf.test.jsnew file mode 100644index 00000000..716d3d35--- /dev/null+++ b/tests/gltf.test.js@@ -0,0 +1,195 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { mat4 } from 'gl-matrix';+import { exportGlb } from '../src/export.js';+import { GLTF_CAPABILITIES, loadGltf, parseGltf } from '../src/gltf.js';++function triangleAsset(edit = () => {}) {+ const positions = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);+ const uvs = new Float32Array([0, 0, 1, 0, 0, 1]);+ const times = new Float32Array([0, 2]);+ const translations = new Float32Array([1, 2, 3, 5, 2, 3]);+ const alternate = new Float32Array([1, 2, 3, 1, 6, 3]);+ const chunks = [positions, uvs, times, translations, alternate];+ let offset = 0;+ const bufferViews = chunks.map(chunk => {+ const view = { buffer: 0, byteOffset: offset, byteLength: chunk.byteLength };+ offset += chunk.byteLength;+ return view;+ });+ const document = {+ asset: { version: '2.0' }, scene: 0,+ scenes: [{ nodes: [0] }],+ nodes: [{ name: 'Parent', translation: [10, 0, 0], children: [1, 2] }, { name: 'First', mesh: 0, translation: [1, 2, 3] }, { name: 'Second', mesh: 0, translation: [0, 4, 0] }],+ meshes: [{ primitives: [{ attributes: { POSITION: 0, TEXCOORD_0: 1 }, material: 0 }] }],+ materials: [{ pbrMetallicRoughness: { baseColorFactor: [0.2, 0.4, 0.6, 1], metallicFactor: 0.35, roughnessFactor: 0.7 }, emissiveFactor: [0.1, 0.2, 0.3] }],+ accessors: [+ { bufferView: 0, componentType: 5126, count: 3, type: 'VEC3', min: [0, 0, 0], max: [1, 1, 0] },+ { bufferView: 1, componentType: 5126, count: 3, type: 'VEC2' },+ { bufferView: 2, componentType: 5126, count: 2, type: 'SCALAR', min: [0], max: [2] },+ { bufferView: 3, componentType: 5126, count: 2, type: 'VEC3' },+ { bufferView: 4, componentType: 5126, count: 2, type: 'VEC3' },+ ],+ bufferViews, buffers: [{ byteLength: offset }],+ };+ edit(document);+ const json = new TextEncoder().encode(JSON.stringify(document));+ const jsonLength = Math.ceil(json.length / 4) * 4;+ const result = new ArrayBuffer(28 + jsonLength + offset);+ const header = new DataView(result);+ header.setUint32(0, 0x46546c67, true);+ header.setUint32(4, 2, true);+ header.setUint32(8, result.byteLength, true);+ header.setUint32(12, jsonLength, true);+ header.setUint32(16, 0x4e4f534a, true);+ const bytes = new Uint8Array(result);+ bytes.fill(32, 20, 20 + jsonLength);+ bytes.set(json, 20);+ header.setUint32(20 + jsonLength, offset, true);+ header.setUint32(24 + jsonLength, 0x004e4942, true);+ for (let index = 0; index < chunks.length; index++) {+ bytes.set(new Uint8Array(chunks[index].buffer), 28 + jsonLength + bufferViews[index].byteOffset);+ }+ return result;+}++test('GLB bridge preserves hierarchy, shared geometry, missing normals, UVs and linear PBR factors', async () => {+ const asset = await parseGltf(triangleAsset());+ assert.equal(asset.parts.length, 2);+ const [first, second] = asset.parts;+ assert.equal(first.geometry, second.geometry);+ assert.deepEqual(Array.from(first.matrix.subarray(12, 15)), [11, 2, 3]);+ assert.deepEqual(Array.from(second.matrix.subarray(12, 15)), [10, 4, 0]);+ assert.deepEqual(Array.from(first.geometry.indices), [0, 1, 2]);+ assert.deepEqual(Array.from(first.geometry.normals), [0, 0, 1, 0, 0, 1, 0, 0, 1]);+ assert.deepEqual(Array.from(first.geometry.uvs), [0, 0, 1, 0, 0, 1]);+ assert.deepEqual(first.material.color, [0.2, 0.4, 0.6]);+ assert.deepEqual(first.material.emissiveColor, [0.1, 0.2, 0.3]);+ assert.equal(first.material.metallic, 0.35);+ assert.equal(first.material.roughness, 0.7);+ assert.equal(asset.capabilities, GLTF_CAPABILITIES);+ asset.dispose();+ asset.dispose();+});++test('67.js exported GLB roundtrip preserves transforms and repeated primitive identity', async () => {+ const geometry = {+ positions: new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]),+ normals: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1]),+ indices: new Uint32Array([0, 1, 2]),+ };+ const material = { color: [0.2, 0.4, 0.6], metallic: 0.25, roughness: 0.6, emission: 0 };+ const matrix = mat4.fromTranslation(mat4.create(), [4, 5, 6]);+ const input = [{ name: 'Roundtrip', geometry, matrix, material }, { name: 'Instance', geometry, matrix: mat4.create(), material }];+ const asset = await parseGltf(exportGlb(input));+ assert.deepEqual(Array.from(asset.parts[0].matrix), Array.from(matrix));+ assert.deepEqual(Array.from(asset.parts[0].geometry.positions), Array.from(geometry.positions));+ assert.equal(asset.parts[0].geometry, asset.parts[1].geometry);+ assert.equal(asset.parts[0].material, asset.parts[1].material);+ asset.dispose();+});++test('rigid animation seeks are absolute, order independent, clip selectable and clamped', async () => {+ const asset = await parseGltf(triangleAsset(document => {+ document.animations = [3, 4].map((output, index) => ({+ name: `Clip ${index}`,+ samplers: [{ input: 2, output, interpolation: 'LINEAR' }],+ channels: [{ sampler: 0, target: { node: 1, path: 'translation' } }],+ }));+ }));+ assert.equal(asset.animations.length, 2);+ assert.equal(asset.applyAnimation(1), asset.parts);+ assert.deepEqual(Array.from(asset.parts[0].matrix.subarray(12, 15)), [13, 2, 3]);+ const middle = Array.from(asset.parts[0].matrix);+ asset.applyAnimation(2);+ assert.deepEqual(Array.from(asset.parts[0].matrix.subarray(12, 15)), [15, 2, 3]);+ asset.applyAnimation(0.1);+ asset.applyAnimation(1);+ assert.deepEqual(Array.from(asset.parts[0].matrix), middle);+ asset.applyAnimation(1, 1);+ assert.deepEqual(Array.from(asset.parts[0].matrix.subarray(12, 15)), [11, 4, 3]);+ asset.applyAnimation(-1);+ assert.deepEqual(Array.from(asset.parts[0].matrix.subarray(12, 15)), [11, 2, 3]);+ assert.throws(() => asset.applyAnimation(Infinity), /finite/);+ assert.throws(() => asset.applyAnimation(1, 8), /clip index/);+ asset.dispose();+ assert.throws(() => asset.applyAnimation(1), /disposed/);+});++test('mirrored static transforms reverse triangle winding without altering world matrices', async () => {+ const asset = await parseGltf(triangleAsset(document => { document.nodes[2].scale = [-1, 1, 1]; }));+ assert.deepEqual(Array.from(asset.parts[0].geometry.indices), [0, 1, 2]);+ assert.deepEqual(Array.from(asset.parts[1].geometry.indices), [0, 2, 1]);+ assert.equal(asset.parts[1].matrix[0], -1);+ asset.dispose();+});++test('unsupported features fail explicitly before loading resources', async () => {+ const cases = [+ [document => { document.nodes[1].skin = 0; }, /Skinned/],+ [document => { document.meshes[0].primitives[0].targets = [{ POSITION: 0 }]; }, /Morph/],+ [document => { document.meshes[0].primitives[0].attributes.COLOR_0 = 0; }, /Vertex colors/],+ [document => { document.meshes[0].primitives[0].mode = 1; }, /TRIANGLES/],+ [document => { document.materials[0].alphaMode = 'BLEND'; }, /alpha mode/],+ [document => { document.materials[0].doubleSided = true; }, /Double-sided/],+ [document => { document.materials[0].normalTexture = { index: 0 }; }, /texture channels/],+ [document => { document.extensionsRequired = ['KHR_draco_mesh_compression']; }, /KHR_draco/],+ [document => { document.materials[0].extensions = { KHR_materials_clearcoat: {} }; }, /KHR_materials_clearcoat/],+ [document => { document.buffers[0].uri = 'external.bin'; }, /baseUrl/],+ [document => { document.nodes[2].children = [0]; }, /Cyclic/],+ [document => { document.materials[0].pbrMetallicRoughness.metallicFactor = 2; }, /metallic/],+ [document => { document.accessors[1].count = 4; }, /TEXCOORD_0/],+ ];+ for (const [edit, expected] of cases) await assert.rejects(parseGltf(triangleAsset(edit)), expected);+});++test('byte, vertex, part and input guards reject oversized or malformed assets', async () => {+ const data = triangleAsset();+ await assert.rejects(parseGltf(data, { maxBytes: 32 }), /maxBytes/);+ await assert.rejects(parseGltf(data, { maxVertices: 2 }), /maxVertices/);+ await assert.rejects(parseGltf(data, { maxParts: 1 }), /maxParts/);+ await assert.rejects(parseGltf(data, { maxVertices: NaN }), /positive safe integer/);+ await assert.rejects(parseGltf(new ArrayBuffer(2)), /truncated/);+ const malformed = data.slice(0);+ new DataView(malformed).setUint32(8, data.byteLength + 10, true);+ await assert.rejects(parseGltf(malformed), /declared length/);+ const oversizedAccessor = triangleAsset(document => { document.accessors.push({ componentType: 5126, count: 10_000_000, type: 'MAT4' }); });+ await assert.rejects(parseGltf(oversizedAccessor), /Decoded glTF accessors/);+ await assert.rejects(loadGltf(new Blob([data]), { maxBytes: 10 }), /maxBytes/);+ await assert.rejects(loadGltf({ arrayBuffer: () => Promise.resolve(data) }), /File\/Blob/);+ const asset = await loadGltf(new Blob([data]));+ assert.equal(asset.parts.length, 2);+ asset.dispose();+});++test('texture pixel budgets reject compressed images before browser decoding', async () => {+ const header = new Uint8Array(24);+ const view = new DataView(header.buffer);+ view.setUint32(0, 0x89504e47);+ view.setUint32(4, 0x0d0a1a0a);+ view.setUint32(12, 0x49484452);+ view.setUint32(16, 100000);+ view.setUint32(20, 100000);+ const oversized = triangleAsset(document => {+ document.images = [{ uri: `data:image/png;base64,${Buffer.from(header).toString('base64')}` }];+ });+ await assert.rejects(parseGltf(oversized), /maxTexturePixels before decoding/);+});++test('URL loading streams within maxBytes and cancels oversized responses', async context => {+ const data = triangleAsset();+ const url = `data:model/gltf-binary;base64,${Buffer.from(data).toString('base64')}`;+ const asset = await loadGltf(url);+ assert.equal(asset.parts.length, 2);+ asset.dispose();+ let cancelled = false;+ context.mock.method(globalThis, 'fetch', async () => new Response(new ReadableStream({+ start(controller) {+ controller.enqueue(new Uint8Array(64));+ controller.enqueue(new Uint8Array(64));+ },+ cancel() { cancelled = true; },+ })));+ await assert.rejects(loadGltf('https://example.invalid/asset.glb', { maxBytes: 80 }), /maxBytes/);+ assert.equal(cancelled, true);+});