3D engine that AI agents drive
Load Draco, meshopt, KTX2, texture transform and GPU instancing glTF assets
Nguyen Xuan An683e822c6496
diff --git a/docs/gltf.md b/docs/gltf.mdindex 88ce5ef5..6008f4e8 100644--- a/docs/gltf.md+++ b/docs/gltf.md@@ -40,6 +40,7 @@ The optional `signal` aborts those fetches.| `maxResources` | 64 | Unique resource URIs. || `maxTexturePixels` | 16,777,216 | Combined texture image pixels, checked before decoding. || `maxJoints` | 4,096 | Combined declared joints and joint palettes for instantiated meshes. |+| `maxInstances` | 4,096 | Instances of one `EXT_mesh_gpu_instancing` node. |Every numeric limit must be a positive safe integer.The importer rejects cyclic hierarchies, multiple parents, and hierarchies deeper than 256 levels.@@ -50,7 +51,7 @@ The renderer retains its separate memory budget.## Output-The result contains `parts`, `animations`, `applyAnimation`, `dispose`, and `capabilities`.+The result contains `parts`, `warnings`, `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.@@ -125,8 +126,51 @@ It also supports `KHR_mesh_quantization` and `KHR_materials_emissive_strength`.The importer rejects alpha blending, alpha masking, double-sided materials and vertex transparency.It supports base-color, normal, metallic-roughness, occlusion and emissive texture channels.-It rejects texture transforms, DRACO, KTX2, GPU instancing, and unsupported extensions, including optional extensions.+It rejects unknown extensions, including optional ones.Line and point primitives are unsupported.++## Extensions++| Extension | Behavior |+| --- | --- |+| `KHR_mesh_quantization`, `KHR_materials_emissive_strength` | Supported. |+| `KHR_texture_transform` | Offset, rotation and scale are baked into `geometry.uvs`. All maps of one material must share one transform, otherwise the import fails. `texCoord` overrides are rejected. |+| `EXT_mesh_gpu_instancing` | The node expands into one part per instance and primitive. Parts of one primitive share one geometry object, so the renderer batches them. Skinned or morphed instancing is rejected. |+| `KHR_draco_mesh_compression` | Needs `options.decoders.draco`. |+| `EXT_meshopt_compression` | Needs `options.decoders.meshopt`. |+| `KHR_texture_basisu` | Needs `options.decoders.ktx2`. |+| `KHR_materials_clearcoat`, `_transmission`, `_ior`, `_specular`, `_sheen`, `_volume` | Accepted but not rendered. Each one used adds a line to `asset.warnings`. Listing one in `extensionsRequired` fails the import. |++`KHR_materials_transmission` with a factor above zero sets `material.transmission` to that factor.+It is a hint only: the renderer does not draw transmission yet.++Without the matching decoder, a file that uses a compression extension fails with `Unsupported glTF extension` and the option name.++### Decoders++The decoders are the Three.js loaders. 67.js does not bundle or fetch their wasm files, so you choose where they are served.++```js+import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';+import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';+import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';++const draco = new DRACOLoader().setDecoderPath('/vendor/draco/'); // copy three/examples/jsm/libs/draco/+const ktx2 = new KTX2Loader().setTranscoderPath('/vendor/basis/'); // copy three/examples/jsm/libs/basis/+// Transcode to RGBA8 only: the renderer uploads plain pixels, not GPU-compressed formats.+ktx2.workerConfig = { astcSupported: false, astcHDRSupported: false, etc1Supported: false, etc2Supported: false, dxtSupported: false, bptcSupported: false, pvrtcSupported: false };++const asset = await loadGltf('/models/car.glb', { decoders: { draco, ktx2, meshopt: MeshoptDecoder } });+```++Any subset works. Draco and KTX2 decode in browser workers. Dispose the loaders yourself when done (`draco.dispose()`, `ktx2.dispose()`).+A KTX2 texture that decodes to a GPU-compressed format fails with a message that points back to this setting.+KTX2 dimensions count toward `maxTexturePixels` before decoding.++### Instancing limits++`maxInstances` (default 4,096) caps the instances of one node, and `maxParts` still caps the total number of parts.+Both are checked from the accessor counts before any data is decoded.The bridge does not import cameras, lights or scene environments into Renderer67.The caller supplies its own camera and lighting.@@ -136,6 +180,10 @@ The caller supplies its own camera and lighting.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.+`node --test tests/gltf-extensions.test.js` covers texture transform baking, instancing, ignored material extensions and the decoder options.+The meshopt test decodes a real encoded buffer with the Three.js decoder.+The Draco and KTX2 tests use stand-in loaders, because no encoder is installed: they prove the wiring, not the wasm decoders.+`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.jsindex ba2f7b28..8f832e46 100644--- a/src/gltf.js+++ b/src/gltf.js@@ -6,13 +6,19 @@ import { MAX_MORPH_TARGETS, createMorphPose, validateMorphGeometry, validateMorpexport const GLTF_CAPABILITIES = Object.freeze({formats: ['glb', 'gltf'],- supported: ['triangle meshes', 'world transforms', 'rigid node animations', 'four-influence skeletal skinning', 'joint node animations', 'relative POSITION/NORMAL morph targets', 'morph weight animations', 'PNG/JPEG base color textures', 'normal/occlusion/emissive/metallic-roughness textures', 'TEXCOORD_0', 'linear RGB vertex colors', 'metallic roughness factors', 'emissive factors', 'KHR_mesh_quantization', 'KHR_materials_emissive_strength'],- unsupported: ['more than four skin influences', 'singular weighted skin transforms', 'reflected skin transforms', 'TANGENT morph targets', 'morph targets without authored base normals', 'vertex alpha below one', 'alpha blending or masking', 'double-sided materials', 'texture transforms', 'DRACO', 'KTX2', 'GPU instancing'],+ supported: ['triangle meshes', 'world transforms', 'rigid node animations', 'four-influence skeletal skinning', 'joint node animations', 'relative POSITION/NORMAL morph targets', 'morph weight animations', 'PNG/JPEG base color textures', 'normal/occlusion/emissive/metallic-roughness textures', 'TEXCOORD_0', 'linear RGB vertex colors', 'metallic roughness factors', 'emissive factors', 'KHR_mesh_quantization', 'KHR_materials_emissive_strength', 'KHR_texture_transform baked into UVs', 'EXT_mesh_gpu_instancing expanded into parts', 'KHR_draco_mesh_compression with options.decoders.draco', 'EXT_meshopt_compression with options.decoders.meshopt', 'KHR_texture_basisu with options.decoders.ktx2'],+ ignoredExtensions: ['KHR_materials_clearcoat', 'KHR_materials_transmission', 'KHR_materials_ior', 'KHR_materials_specular', 'KHR_materials_sheen', 'KHR_materials_volume'],+ unsupported: ['more than four skin influences', 'singular weighted skin transforms', 'reflected skin transforms', 'TANGENT morph targets', 'morph targets without authored base normals', 'vertex alpha below one', 'alpha blending or masking', 'double-sided materials', 'different texture transforms on one material', 'skinned or morphed GPU instancing', 'rendering of ignored material extensions'],});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 DEFAULT_MAX_INSTANCES = 4096;+const ALLOWED_EXTENSIONS = new Set(['KHR_mesh_quantization', 'KHR_materials_emissive_strength', 'KHR_texture_transform', 'EXT_mesh_gpu_instancing']);+const DECODER_BY_EXTENSION = { KHR_draco_mesh_compression: 'draco', EXT_meshopt_compression: 'meshopt', KHR_texture_basisu: 'ktx2' };+// Accepted when optional, because 67.js does not render them. Rejected when listed in extensionsRequired.+const IGNORED_EXTENSIONS = new Set(GLTF_CAPABILITIES.ignoredExtensions);+const IDENTITY_UV_TRANSFORM = Object.freeze({ offset: [0, 0], rotation: 0, scale: [1, 1] });const WRAPPING = { 1000: 10497, 1001: 33071, 1002: 33648 };const FILTERING = { 1003: 9728, 1004: 9984, 1005: 9986, 1006: 9729, 1007: 9985, 1008: 9987 };const MATERIAL_TEXTURES = [@@ -35,6 +41,7 @@ function limitsFor(options) {maxResources: options.maxResources ?? 64,maxTexturePixels: options.maxTexturePixels ?? 16_777_216,maxJoints: options.maxJoints ?? 4096,+ maxInstances: options.maxInstances ?? DEFAULT_MAX_INSTANCES,};for (const [name, value] of Object.entries(limits)) {if (!Number.isSafeInteger(value) || value <= 0) throw new TypeError(`${name} must be a positive safe integer.`);@@ -112,20 +119,117 @@ function validateAnimationChannels(document) {}}-function validateDocument(document, limits) {- if (document.asset?.version !== '2.0') throw new Error('Only glTF 2.0 assets are supported.');+function allowedExtensionsFor(decoders) {+ const allowed = new Set(ALLOWED_EXTENSIONS);+ for (const [extension, decoderName] of Object.entries(DECODER_BY_EXTENSION)) {+ if (decoders[decoderName]) allowed.add(extension);+ }+ return allowed;+}++function unsupportedExtensionError(extension) {+ const decoderName = DECODER_BY_EXTENSION[extension];+ const hint = decoderName ? ` Pass options.decoders.${decoderName} to enable it.` : '';+ return new Error(`Unsupported glTF extension: ${extension}.${hint}`);+}++/** Rejects unknown extensions and returns the sorted names of accepted but unrendered ones. */+function scanExtensions(document, allowedExtensions) {+ const ignored = new Set();+ function checkOptional(extension) {+ if (allowedExtensions.has(extension)) return;+ if (!IGNORED_EXTENSIONS.has(extension)) throw unsupportedExtensionError(extension);+ ignored.add(extension);+ }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}.`);- }+ for (const extension of Object.keys(value.extensions ?? {})) checkOptional(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}.`);+ for (const extension of document.extensionsUsed ?? []) checkOptional(extension);+ for (const extension of document.extensionsRequired ?? []) {+ if (!allowedExtensions.has(extension)) throw unsupportedExtensionError(extension);+ }+ return [...ignored].sort();+}++function textureUvTransform(texture) {+ const extension = texture.extensions?.KHR_texture_transform;+ if (!extension) return IDENTITY_UV_TRANSFORM;+ const offset = extension.offset ?? [0, 0];+ const scale = extension.scale ?? [1, 1];+ const rotation = extension.rotation ?? 0;+ const vectorsAreFinite = [offset, scale].every(vector => Array.isArray(vector) && vector.length === 2 && vector.every(Number.isFinite));+ if (!vectorsAreFinite || !Number.isFinite(rotation)) throw new Error('KHR_texture_transform requires finite offset, rotation and scale.');+ if ((extension.texCoord ?? 0) !== 0) throw new Error('KHR_texture_transform texCoord overrides are unsupported.');+ return { offset, rotation, scale };+}++function uvTransformKey(transform) {+ return transform === IDENTITY_UV_TRANSFORM ? '' : JSON.stringify([transform.offset, transform.rotation, transform.scale]);+}++/** All maps of a material read one UV set, so one shared transform can be baked into it. */+function materialUvTransform(material) {+ let found = null;+ for (const [, field] of MATERIAL_TEXTURES) {+ const texture = materialTexture(material, field);+ if (!texture) continue;+ const transform = textureUvTransform(texture);+ if (found && uvTransformKey(found) !== uvTransformKey(transform)) {+ throw new Error('Maps of one material must share the same KHR_texture_transform. Different transforms per map are unsupported.');+ }+ found = transform;+ }+ return found ?? IDENTITY_UV_TRANSFORM;+}++function bakeUvTransform(uvs, transform) {+ const cosine = Math.cos(transform.rotation);+ const sine = Math.sin(transform.rotation);+ const [offsetU, offsetV] = transform.offset;+ const [scaleU, scaleV] = transform.scale;+ for (let index = 0; index < uvs.length; index += 2) {+ const u = uvs[index];+ const v = uvs[index + 1];+ uvs[index] = cosine * scaleU * u + sine * scaleV * v + offsetU;+ uvs[index + 1] = -sine * scaleU * u + cosine * scaleV * v + offsetV;+ }+}++function validateMaterialExtensions(material) {+ materialUvTransform(material);+ const transmission = material.extensions?.KHR_materials_transmission?.transmissionFactor ?? 0;+ if (!Number.isFinite(transmission) || transmission < 0 || transmission > 1) throw new Error('KHR_materials_transmission transmissionFactor must be within zero and one.');+}++/** Validates EXT_mesh_gpu_instancing on a node and returns how many instances it expands to. */+function nodeInstanceCount(document, node, limits) {+ const extensions = Object.keys(node.extensions ?? {});+ if (!extensions.length) return 1;+ if (extensions.length > 1 || extensions[0] !== 'EXT_mesh_gpu_instancing') throw new Error('Node extensions other than EXT_mesh_gpu_instancing are unsupported.');+ const attributes = node.extensions.EXT_mesh_gpu_instancing.attributes;+ if (node.mesh === undefined || node.skin !== undefined) throw new Error('EXT_mesh_gpu_instancing requires a static mesh node without a skin.');+ if (morphCount(document.meshes?.[node.mesh])) throw new Error('EXT_mesh_gpu_instancing with morph targets is unsupported.');+ const shapes = { TRANSLATION: 'VEC3', ROTATION: 'VEC4', SCALE: 'VEC3' };+ let count;+ for (const [name, accessorIndex] of Object.entries(attributes ?? {})) {+ if (!(name in shapes)) continue;+ const accessor = document.accessors?.[accessorIndex];+ if (!accessor || accessor.type !== shapes[name] || !Number.isSafeInteger(accessor.count)) throw new Error(`Instancing ${name} requires a ${shapes[name]} accessor.`);+ if (count !== undefined && accessor.count !== count) throw new Error('Instancing attributes must share one count.');+ count = accessor.count;}+ if (count === undefined || count < 1) throw new Error('EXT_mesh_gpu_instancing needs TRANSLATION, ROTATION or SCALE instances.');+ if (count > limits.maxInstances) throw new Error(`EXT_mesh_gpu_instancing node has ${count} instances, above maxInstances (${limits.maxInstances}).`);+ return count;+}++function validateDocument(document, limits, allowedExtensions) {+ if (document.asset?.version !== '2.0') throw new Error('Only glTF 2.0 assets are supported.');+ const ignoredExtensions = scanExtensions(document, allowedExtensions);let declaredBytes = 0;for (const buffer of document.buffers ?? []) {if (!Number.isSafeInteger(buffer.byteLength) || buffer.byteLength < 0) throw new Error('Invalid glTF buffer length.');@@ -214,7 +318,7 @@ function validateDocument(document, limits) {for (const node of document.nodes ?? []) {const mesh = document.meshes?.[node.mesh];if (node.mesh !== undefined && (!Number.isInteger(node.mesh) || !mesh)) throw new Error('Invalid mesh node index.');- declaredParts += mesh?.primitives?.length ?? 0;+ declaredParts += (mesh?.primitives?.length ?? 0) * nodeInstanceCount(document, node, limits);if (declaredParts > limits.maxParts) throw new Error('glTF primitive instances exceed maxParts.');validateDefaultWeights(node.weights, morphCount(mesh));morphBytes += morphCount(mesh) * (mesh?.primitives?.length ?? 0) * 16;@@ -226,7 +330,6 @@ function validateDocument(document, limits) {if (poseJointCount > limits.maxJoints) throw new Error('Instanced skin poses exceed maxJoints.');}}- if (node.extensions && Object.keys(node.extensions).length) throw new Error('Node extensions are unsupported.');}validateHierarchy(document.nodes ?? []);validateAnimationChannels(document);@@ -246,14 +349,16 @@ function validateDocument(document, limits) {for (const [, field] of MATERIAL_TEXTURES) {const texture = materialTexture(material, field);if (!texture) continue;- if ((texture.texCoord ?? 0) !== 0 || texture.extensions && Object.keys(texture.extensions).length) throw new Error(`${field} requires TEXCOORD_0 without texture transforms.`);+ if ((texture.texCoord ?? 0) !== 0 || Object.keys(texture.extensions ?? {}).some(name => name !== 'KHR_texture_transform')) throw new Error(`${field} requires TEXCOORD_0 and no texture extensions besides KHR_texture_transform.`);if (!Number.isInteger(texture.index) || !document.textures?.[texture.index]) throw new Error(`Invalid ${field} texture index.`);if (field === 'normalTexture' && (!Number.isFinite(texture.scale ?? 1) || !Number.isFinite(Math.fround(texture.scale ?? 1)))) throw new Error('Normal texture scale must be a finite float.');if (field === 'occlusionTexture' && (!Number.isFinite(texture.strength ?? 1) || (texture.strength ?? 1) < 0 || (texture.strength ?? 1) > 1)) throw new Error('Occlusion texture strength must be within zero and one.');}+ validateMaterialExtensions(material);}for (const texture of document.textures ?? []) {- if (!Number.isInteger(texture.source) || !document.images?.[texture.source]) throw new Error('Invalid texture image index.');+ const source = textureSourceIndex(texture);+ if (!Number.isInteger(source) || !document.images?.[source]) throw new Error('Invalid texture image index.');if (texture.sampler !== undefined && (!Number.isInteger(texture.sampler) || !document.samplers?.[texture.sampler])) throw new Error('Invalid texture sampler index.');const sampler = document.samplers?.[texture.sampler] ?? {};textureDescriptorInfo({ image: { width: 1, height: 1 }, ...sampler });@@ -274,7 +379,11 @@ function validateDocument(document, limits) {decodedBytes += accessor.count * components[accessor.type] * 4;}if (decodedBytes > limits.maxBytes * 4) throw new Error('Decoded glTF accessors exceed four times maxBytes.');- return vertices;+ return { vertices, ignoredExtensions };+}++function textureSourceIndex(texture) {+ return texture.extensions?.KHR_texture_basisu?.source ?? texture.source;}function validateHierarchy(nodes) {@@ -338,7 +447,12 @@ function validateImage(bytes, maxPixels) {offset += length;}}- if (!width || !height) throw new Error('Only valid PNG and JPEG texture images are supported.');+ const ktx2Identifier = [0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a];+ if (!width && bytes.length >= 28 && ktx2Identifier.every((value, index) => bytes[index] === value)) {+ width = header.getUint32(20, true);+ height = header.getUint32(24, true);+ }+ if (!width || !height) throw new Error('Only valid PNG, JPEG and KTX2 texture images are supported.');if (width * height > maxPixels) throw new Error('Texture image exceeds maxTexturePixels before decoding.');return width * height;}@@ -568,6 +682,32 @@ function copyPrimitiveAssociations(original, decoded, associations) {}}+/** Three.js builds instanced meshes after it records associations, so restore the mesh and primitive indices here. */+function completeInstancedAssociations(object, associations, document, inheritedNodeIndex) {+ const association = associations.get(object);+ const ownsNode = Number.isInteger(association?.nodes);+ const nodeIndex = ownsNode ? association.nodes : inheritedNodeIndex;+ if (object.isInstancedMesh && association?.primitives === undefined) {+ const siblingPrimitives = ownsNode || !object.parent ? [object] : object.parent.children.filter(child => child.isInstancedMesh && !Number.isInteger(associations.get(child)?.nodes));+ associations.set(object, { ...association, meshes: document.nodes?.[nodeIndex]?.mesh, primitives: siblingPrimitives.indexOf(object) });+ }+ for (const child of object.children) completeInstancedAssociations(child, associations, document, nodeIndex);+}++function instanceMatricesOf(node, maximumInstances) {+ if (!node.isInstancedMesh) return [null];+ if (node.count > maximumInstances) throw new Error(`Decoded instance count ${node.count} exceeds maxInstances (${maximumInstances}).`);+ return Array.from({ length: node.count }, (unused, index) => {+ const matrix = new Matrix4();+ node.getMatrixAt(index, matrix);+ return matrix;+ });+}++function worldMatrixOf(node, instanceMatrix, target) {+ return instanceMatrix ? target.multiplyMatrices(node.matrixWorld, instanceMatrix) : target.copy(node.matrixWorld);+}+function skinPreflightPlugin(parser, document, primitiveAssociations) {let originalRoots;return {@@ -617,6 +757,7 @@ function skinPreflightPlugin(parser, document, primitiveAssociations) {}const roots = await Promise.all(Array.from(rootIndices, async index => [index, await parser.getDependency('node', index)]));originalRoots = new Map(roots);+ for (const [, root] of roots) completeInstancedAssociations(root, parser.associations, document, undefined);for (const [node, association] of parser.associations) {if (node.isMesh || Number.isInteger(association?.nodes)) {primitiveAssociations.set(node, Object.freeze({ ...association }));@@ -656,6 +797,17 @@ function convertVertexColors(attribute, vertexCount) {return colors;}+/** KTX2 textures arrive as RGBA8 pixel data, which the renderer uploads as an ImageData. */+function decodedTextureImage(texture, field) {+ if (texture.isCompressedTexture) throw new Error(`${field} decoded to a GPU-compressed KTX2 format. Configure the KTX2 loader to transcode to RGBA8 (see docs/gltf.md).`);+ const image = texture.image;+ if (!image?.data) return image;+ const { width, height, data } = image;+ if (!(data instanceof Uint8Array) || data.length < width * height * 4) throw new Error(`${field} KTX2 image must decode to RGBA8 pixels.`);+ const pixels = new Uint8ClampedArray(data.buffer, data.byteOffset, width * height * 4);+ return typeof ImageData === 'function' ? new ImageData(pixels, width, height) : { width, height, data: pixels };+}+function convertMaterial(source, limits, maps, definition = {}) {const material = {tag: 'gltf',@@ -667,6 +819,9 @@ function convertMaterial(source, limits, maps, definition = {}) {normalScale: [definition.normalTexture?.scale ?? 1, definition.normalTexture?.scale ?? 1],occlusionStrength: source.aoMapIntensity,};+ // Hint only: transmission is not rendered yet.+ const transmission = definition.extensions?.KHR_materials_transmission?.transmissionFactor ?? 0;+ if (transmission > 0) material.transmission = transmission;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.');for (const [field, , colorSpace] of MATERIAL_TEXTURES) {@@ -675,7 +830,7 @@ function convertMaterial(source, limits, maps, definition = {}) {if (!maps.has(texture)) maps.set(texture, new Map());const descriptors = maps.get(texture);if (!descriptors.has(colorSpace)) {- const image = texture.image;+ const image = decodedTextureImage(texture, field);if (!image || image.width * image.height > limits.maxTexturePixels) throw new Error(`${field} image is missing or exceeds maxTexturePixels.`);const descriptor = {image,@@ -725,7 +880,9 @@ function releaseParsed(gltf) {export async function parseGltf(data, options = {}) {const limits = limitsFor(options);const document = documentFrom(data, limits.maxBytes);- validateDocument(document, limits);+ const decoders = options.decoders ?? {};+ const { ignoredExtensions } = validateDocument(document, limits, allowedExtensionsFor(decoders));+ const warnings = ignoredExtensions.map(extension => `${extension} is accepted but not rendered.`);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();@@ -740,6 +897,9 @@ export async function parseGltf(data, options = {}) {const primitiveAssociations = new WeakMap();try {const loader = new GLTFLoader(manager);+ if (decoders.draco) loader.setDRACOLoader(decoders.draco);+ if (decoders.meshopt) loader.setMeshoptDecoder(decoders.meshopt);+ if (decoders.ktx2) loader.setKTX2Loader(decoders.ktx2);loader.register(parser => skinPreflightPlugin(parser, document, primitiveAssociations));gltf = await loader.parseAsync(data, options.baseUrl ?? '');gltf.scene.updateMatrixWorld(true);@@ -767,43 +927,49 @@ export async function parseGltf(data, options = {}) {const decodedField = field === 'metallicRoughnessMap' ? 'metalnessMap' : field === 'occlusionMap' ? 'aoMap' : field;if (materialTexture(document.materials?.[materialIndex] ?? {}, sourceField) && !node.material[decodedField]) throw new Error(`${sourceField} 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 geometryMirrored = node.isSkinnedMesh ? false : mirrored;- const geometryKey = `${node.geometry.uuid}:${geometryMirrored}`;- if (!geometries.has(geometryKey)) {- actualVertices += node.geometry.getAttribute('position').count * (1 + (primitive.targets?.length ?? 0));- if (actualVertices > limits.maxVertices) throw new Error('Decoded geometry exceeds maxVertices.');- geometries.set(geometryKey, convertGeometry(node.geometry, geometryMirrored, primitive));- }+ const uvTransform = materialUvTransform(document.materials?.[materialIndex] ?? {});+ const uvKey = uvTransformKey(uvTransform);if (!materials.has(node.material)) materials.set(node.material, convertMaterial(node.material, limits, maps, document.materials?.[materialIndex]));- const geometry = geometries.get(geometryKey);const material = materials.get(node.material);- if (MATERIAL_TEXTURES.some(([field]) => material[field]) && !geometry.uvs) throw new Error('Material textures require TEXCOORD_0.');- const part = { name: node.name || `glTF mesh ${parts.length + 1}`, geometry, matrix: new Float32Array(node.matrixWorld.elements), material };- const transformBinding = { node, part, mirrored, matrix: part.matrix, pendingMatrix: new Float32Array(16) };- let pendingSkin;- if (node.isSkinnedMesh) {- const binding = makeSkinBinding(node);- prepareSkinBinding(binding, geometry);- commitSkinBinding(binding);- part.skin = binding.skin;- skinBindings.push({ ...binding, geometry });- pendingSkin = binding.pendingPose;- }- validateSkinGeometry(geometry, part.skin);- transformBinding.skin = part.skin;- if (geometry.morphTargets) {- const binding = makeMorphBinding(node, geometry, morphMetadata);- part.morph = binding.pose;- validateMorphTransform(binding.pose, binding.metadata, part.matrix, part.skin);- morphBindings.push({ ...binding, part, pendingMatrix: transformBinding.pendingMatrix, pendingSkin });+ for (const instanceMatrix of instanceMatricesOf(node, limits.maxInstances)) {+ const determinant = worldMatrixOf(node, instanceMatrix, pendingTransform).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 geometryMirrored = node.isSkinnedMesh ? false : mirrored;+ const geometryKey = `${node.geometry.uuid}:${geometryMirrored}:${uvKey}`;+ if (!geometries.has(geometryKey)) {+ actualVertices += node.geometry.getAttribute('position').count * (1 + (primitive.targets?.length ?? 0));+ if (actualVertices > limits.maxVertices) throw new Error('Decoded geometry exceeds maxVertices.');+ const converted = convertGeometry(node.geometry, geometryMirrored, primitive);+ if (converted.uvs) bakeUvTransform(converted.uvs, uvTransform);+ geometries.set(geometryKey, converted);+ }+ const geometry = geometries.get(geometryKey);+ if (MATERIAL_TEXTURES.some(([field]) => material[field]) && !geometry.uvs) throw new Error('Material textures require TEXCOORD_0.');+ const part = { name: node.name || `glTF mesh ${parts.length + 1}`, geometry, matrix: new Float32Array(worldMatrixOf(node, instanceMatrix, pendingTransform).elements), material };+ const transformBinding = { node, instanceMatrix, part, mirrored, matrix: part.matrix, pendingMatrix: new Float32Array(16) };+ let pendingSkin;+ if (node.isSkinnedMesh) {+ const binding = makeSkinBinding(node);+ prepareSkinBinding(binding, geometry);+ commitSkinBinding(binding);+ part.skin = binding.skin;+ skinBindings.push({ ...binding, geometry });+ pendingSkin = binding.pendingPose;+ }+ validateSkinGeometry(geometry, part.skin);+ transformBinding.skin = part.skin;+ if (geometry.morphTargets) {+ const binding = makeMorphBinding(node, geometry, morphMetadata);+ part.morph = binding.pose;+ validateMorphTransform(binding.pose, binding.metadata, part.matrix, part.skin);+ morphBindings.push({ ...binding, part, pendingMatrix: transformBinding.pendingMatrix, pendingSkin });+ }+ if (!Array.from(part.matrix).every(Number.isFinite)) throw new Error('Mesh world matrix must be finite.');+ parts.push(part);+ bindings.push(transformBinding);+ if (parts.length > limits.maxParts) throw new Error('Decoded parts exceed maxParts.');}- if (!Array.from(part.matrix).every(Number.isFinite)) throw new Error('Mesh world matrix must be finite.');- parts.push(part);- bindings.push(transformBinding);- if (parts.length > limits.maxParts) throw new Error('Decoded parts exceed maxParts.');});validateNodeTransforms(nodes, pendingNodeMatrix, pendingTransform);if (!parts.length) throw new Error('glTF scene contains no supported triangle meshes.');@@ -904,20 +1070,21 @@ export async function parseGltf(data, options = {}) {function preparePose() {gltf.scene.updateMatrixWorld(true);validateNodeTransforms(nodes, pendingNodeMatrix, pendingTransform);- for (const { node, part, mirrored, matrix, pendingMatrix, skin } of bindings) {+ const worldMatrix = new Matrix4();+ for (const { node, instanceMatrix, part, mirrored, matrix, pendingMatrix, skin } of bindings) {if (part.matrix !== matrix || matrix.length !== 16) {throw new Error('Imported part matrix identity must remain unchanged.');}if (part.skin !== skin) {throw new Error('Imported skin pose identity must remain unchanged.');}- pendingMatrix.set(node.matrixWorld.elements);+ pendingMatrix.set(worldMatrixOf(node, instanceMatrix, worldMatrix).elements);for (const value of pendingMatrix) {if (!Number.isFinite(value)) {throw new Error('Animation creates a non-finite Float32 world matrix.');}}- const determinant = node.matrixWorld.determinant();+ const determinant = worldMatrix.determinant();const roundedDeterminant = pendingTransform.fromArray(pendingMatrix).determinant();if (!Number.isFinite(determinant) || !Number.isFinite(roundedDeterminant) || Math.abs(roundedDeterminant) < 1e-12 || (determinant < 0) !== mirrored || (roundedDeterminant < 0) !== mirrored) {throw new Error('Animation creates a singular transform or changes triangle winding.');@@ -1125,7 +1292,7 @@ export async function parseGltf(data, options = {}) {}return stagePose({ animation: { clip: clipIndex, time: Math.max(0, Math.min(time, clip.duration)) } }).commit();}- return { parts, skins: skinBindings.map(binding => binding.skin), animations: gltf.animations,+ return { parts, warnings, skins: skinBindings.map(binding => binding.skin), animations: gltf.animations,get nodes() { return structuredClone(nodeMetadata); }, stagePose, applyAnimation, dispose, capabilities: GLTF_CAPABILITIES };} catch (error) {if (gltf) releaseParsed(gltf);diff --git a/tests/gltf-extensions.test.js b/tests/gltf-extensions.test.jsnew file mode 100644index 00000000..177f31aa--- /dev/null+++ b/tests/gltf-extensions.test.js@@ -0,0 +1,293 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { BufferGeometry, DataTexture, Float32BufferAttribute } from 'three';+import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';+import { parseGltf } from '../src/gltf.js';++const PNG_IMAGE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR4AWI6k2b8H4SZGKAAAAAA//8VS4tGAAAABklEQVQDAEBuBM3uhq5wAAAAAElFTkSuQmCC';+const TRIANGLE_POSITIONS = new Float32Array([0, 0, 0, 1, 0, 0, 0, 1, 0]);+const TRIANGLE_UVS = new Float32Array([0, 0, 1, 0, 0, 1]);++function padToFour(length) {+ return Math.ceil(length / 4) * 4;+}++function bytesOf(typedArray) {+ return new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);+}++function buildGlb(document, binary) {+ const json = new TextEncoder().encode(JSON.stringify(document));+ const jsonLength = padToFour(json.length);+ const binaryLength = padToFour(binary.length);+ const glb = new Uint8Array(28 + jsonLength + binaryLength);+ const header = new DataView(glb.buffer);+ header.setUint32(0, 0x46546c67, true);+ header.setUint32(4, 2, true);+ header.setUint32(8, glb.length, true);+ header.setUint32(12, jsonLength, true);+ header.setUint32(16, 0x4e4f534a, true);+ glb.fill(32, 20, 20 + jsonLength);+ glb.set(json, 20);+ header.setUint32(20 + jsonLength, binaryLength, true);+ header.setUint32(24 + jsonLength, 0x004e4942, true);+ glb.set(binary, 28 + jsonLength);+ return glb.buffer;+}++/** Packs chunks into one buffer and returns the matching bufferViews. */+function packChunks(chunks) {+ const bufferViews = [];+ const parts = [];+ let offset = 0;+ for (const chunk of chunks) {+ bufferViews.push({ buffer: 0, byteOffset: offset, byteLength: chunk.length });+ parts.push(chunk);+ offset += padToFour(chunk.length);+ }+ const binary = new Uint8Array(offset);+ let cursor = 0;+ for (const part of parts) {+ binary.set(part, cursor);+ cursor += padToFour(part.length);+ }+ return { binary, bufferViews };+}++function triangleDocument({ extraChunks = [], edit = () => {} } = {}) {+ const { binary, bufferViews } = packChunks([bytesOf(TRIANGLE_POSITIONS), bytesOf(TRIANGLE_UVS), ...extraChunks]);+ const document = {+ asset: { version: '2.0' },+ scene: 0,+ scenes: [{ nodes: [0] }],+ nodes: [{ name: 'Triangle', mesh: 0 }],+ meshes: [{ primitives: [{ attributes: { POSITION: 0, TEXCOORD_0: 1 }, material: 0 }] }],+ materials: [{ pbrMetallicRoughness: {} }],+ 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' },+ ],+ bufferViews,+ buffers: [{ byteLength: binary.length }],+ };+ edit(document);+ return buildGlb(document, binary);+}++function mockImageBitmap(context) {+ const previousBitmap = globalThis.createImageBitmap;+ const previousSelf = globalThis.self;+ globalThis.self = globalThis;+ globalThis.createImageBitmap = async () => ({ width: 2, height: 2, close() {} });+ context.after(() => {+ if (previousBitmap) globalThis.createImageBitmap = previousBitmap;+ else delete globalThis.createImageBitmap;+ if (previousSelf) globalThis.self = previousSelf;+ else delete globalThis.self;+ });+}++function assertClose(actual, expected) {+ assert.equal(actual.length, expected.length);+ for (let index = 0; index < expected.length; index++) {+ assert.ok(Math.abs(actual[index] - expected[index]) < 1e-6, `index ${index}: ${actual[index]} vs ${expected[index]}`);+ }+}++function addTexturedMaterial(document, transforms) {+ document.images = [{ uri: PNG_IMAGE }];+ document.textures = [{ source: 0 }];+ const withTransform = transform => ({ index: 0, ...(transform && { extensions: { KHR_texture_transform: transform } }) });+ document.materials[0].pbrMetallicRoughness.baseColorTexture = withTransform(transforms[0]);+ document.materials[0].normalTexture = withTransform(transforms[1]);+ document.extensionsUsed = ['KHR_texture_transform'];+}++test('KHR_texture_transform bakes one shared offset, rotation and scale into the UVs', async context => {+ mockImageBitmap(context);+ const shared = { offset: [0.5, 0], rotation: Math.PI / 2, scale: [1, 1] };+ const asset = await parseGltf(triangleDocument({ edit: document => addTexturedMaterial(document, [shared, shared]) }));+ assertClose(asset.parts[0].geometry.uvs, [0.5, 0, 0.5, -1, 1.5, 0]);+ const scaled = { offset: [0.25, 0.5], scale: [2, 4] };+ const scaledAsset = await parseGltf(triangleDocument({ edit: document => addTexturedMaterial(document, [scaled, scaled]) }));+ assertClose(scaledAsset.parts[0].geometry.uvs, [0.25, 0.5, 2.25, 0.5, 0.25, 4.5]);+});++test('KHR_texture_transform rejects different transforms and texCoord overrides on one material', async context => {+ mockImageBitmap(context);+ const moved = { offset: [0.5, 0] };+ await assert.rejects(parseGltf(triangleDocument({ edit: document => addTexturedMaterial(document, [moved, undefined]) })), /same KHR_texture_transform/);+ await assert.rejects(parseGltf(triangleDocument({ edit: document => addTexturedMaterial(document, [moved, { offset: [0, 0.5] }]) })), /same KHR_texture_transform/);+ await assert.rejects(parseGltf(triangleDocument({ edit: document => addTexturedMaterial(document, [{ texCoord: 1 }, { texCoord: 1 }]) })), /texCoord/);+});++function instancingDocument(translations, edit = () => {}) {+ return triangleDocument({+ extraChunks: [bytesOf(new Float32Array(translations))],+ edit: document => {+ document.accessors.push({ bufferView: 2, componentType: 5126, count: translations.length / 3, type: 'VEC3' });+ document.extensionsUsed = ['EXT_mesh_gpu_instancing'];+ document.nodes[0].translation = [0, 10, 0];+ document.nodes[0].extensions = { EXT_mesh_gpu_instancing: { attributes: { TRANSLATION: 2 } } };+ edit(document);+ },+ });+}++test('EXT_mesh_gpu_instancing expands into parts that share one geometry', async () => {+ const asset = await parseGltf(instancingDocument([1, 0, 0, 2, 0, 0, 3, 0, 0]));+ assert.equal(asset.parts.length, 3);+ assert.equal(new Set(asset.parts.map(part => part.geometry)).size, 1);+ assert.equal(new Set(asset.parts.map(part => part.material)).size, 1);+ assert.deepEqual(asset.parts.map(part => Array.from(part.matrix.slice(12, 15))), [[1, 10, 0], [2, 10, 0], [3, 10, 0]]);+});++test('EXT_mesh_gpu_instancing handles several primitives and a bounded instance count', async () => {+ const twoPrimitives = document => {+ document.meshes[0].primitives.push({ attributes: { POSITION: 0 } });+ };+ const asset = await parseGltf(instancingDocument([1, 0, 0, 2, 0, 0], twoPrimitives));+ assert.equal(asset.parts.length, 4);+ assert.equal(asset.nodes.length, 1);+ assert.equal(asset.nodes[0].meshIds.length, 4);+ await assert.rejects(parseGltf(instancingDocument([1, 0, 0, 2, 0, 0, 3, 0, 0]), { maxInstances: 2 }), /maxInstances/);+ await assert.rejects(parseGltf(instancingDocument([1, 0, 0, 2, 0, 0, 3, 0, 0]), { maxParts: 2 }), /maxParts/);+ await assert.rejects(parseGltf(instancingDocument([1, 0, 0], document => { document.nodes[0].extensions.EXT_mesh_gpu_instancing.attributes = {}; })), /needs TRANSLATION/);+});++const OPTIONAL_MATERIAL_EXTENSIONS = {+ KHR_materials_clearcoat: { clearcoatFactor: 1 },+ KHR_materials_transmission: { transmissionFactor: 0.6 },+ KHR_materials_ior: { ior: 1.4 },+ KHR_materials_specular: { specularFactor: 0.5 },+ KHR_materials_sheen: { sheenRoughnessFactor: 0.3 },+ KHR_materials_volume: { thicknessFactor: 0.1 },+};++test('optional KHR_materials extensions load with warnings and a transmission hint', async () => {+ const asset = await parseGltf(triangleDocument({+ edit: document => {+ document.extensionsUsed = Object.keys(OPTIONAL_MATERIAL_EXTENSIONS);+ document.materials[0].extensions = structuredClone(OPTIONAL_MATERIAL_EXTENSIONS);+ },+ }));+ assert.equal(asset.parts.length, 1);+ assert.equal(asset.parts[0].material.transmission, 0.6);+ assert.deepEqual(asset.warnings, Object.keys(OPTIONAL_MATERIAL_EXTENSIONS).sort().map(name => `${name} is accepted but not rendered.`));+ const plain = await parseGltf(triangleDocument());+ assert.deepEqual(plain.warnings, []);+ assert.equal('transmission' in plain.parts[0].material, false);+});++test('required material extensions, bad transmission and missing decoders are rejected', async () => {+ const required = triangleDocument({ edit: document => { document.extensionsRequired = ['KHR_materials_clearcoat']; } });+ await assert.rejects(parseGltf(required), /Unsupported glTF extension: KHR_materials_clearcoat/);+ const badTransmission = triangleDocument({ edit: document => { document.materials[0].extensions = { KHR_materials_transmission: { transmissionFactor: 2 } }; } });+ await assert.rejects(parseGltf(badTransmission), /transmissionFactor/);+ for (const [extension, decoder] of [['KHR_draco_mesh_compression', 'draco'], ['EXT_meshopt_compression', 'meshopt'], ['KHR_texture_basisu', 'ktx2']]) {+ const data = triangleDocument({ edit: document => { document.extensionsRequired = [extension]; } });+ await assert.rejects(parseGltf(data), new RegExp(`options\\.decoders\\.${decoder}`));+ }+});++/** Encodes bytes with the meshopt ATTRIBUTES codec using literal 8-bit delta groups only. */+function encodeMeshoptLiteral(bytes, count, stride) {+ assert.ok(count <= 16, 'the test encoder handles one group of sixteen elements');+ const header = 0xa0;+ const tailSize = Math.max(32, stride);+ const encoded = [header];+ for (let lane = 0; lane < stride; lane++) {+ encoded.push(0b11);+ let previous = 0;+ for (let element = 0; element < 16; element++) {+ const value = element < count ? bytes[element * stride + lane] : previous;+ const delta = (value - previous) << 24 >> 24;+ encoded.push(((delta << 1) ^ (delta >> 7)) & 0xff);+ previous = value;+ }+ }+ encoded.push(...new Array(tailSize).fill(0));+ return Uint8Array.from(encoded);+}++test('EXT_meshopt_compression decodes a real encoded vertex buffer', async () => {+ const compressed = encodeMeshoptLiteral(bytesOf(TRIANGLE_POSITIONS), 3, 12);+ const { binary, bufferViews } = packChunks([compressed, bytesOf(TRIANGLE_UVS)]);+ const document = {+ asset: { version: '2.0' },+ scenes: [{ nodes: [0] }],+ nodes: [{ mesh: 0 }],+ meshes: [{ primitives: [{ attributes: { POSITION: 0, TEXCOORD_0: 1 } }] }],+ 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' },+ ],+ bufferViews: [+ {+ buffer: 1, byteLength: 36, byteStride: 12,+ extensions: { EXT_meshopt_compression: { buffer: 0, byteOffset: 0, byteLength: compressed.length, byteStride: 12, mode: 'ATTRIBUTES', count: 3 } },+ },+ { ...bufferViews[1] },+ ],+ buffers: [{ byteLength: binary.length }, { byteLength: 36, extensions: { EXT_meshopt_compression: { fallback: true } } }],+ extensionsUsed: ['EXT_meshopt_compression'],+ extensionsRequired: ['EXT_meshopt_compression'],+ };+ const asset = await parseGltf(buildGlb(document, binary), { decoders: { meshopt: MeshoptDecoder } });+ assert.deepEqual(Array.from(asset.parts[0].geometry.positions), Array.from(TRIANGLE_POSITIONS));+});++test('KHR_draco_mesh_compression decodes through the supplied Draco loader', async () => {+ const decodedGeometry = new BufferGeometry();+ decodedGeometry.setAttribute('position', new Float32BufferAttribute(TRIANGLE_POSITIONS, 3));+ const requested = [];+ const dracoLoader = {+ preload() {},+ decodeDracoFile(buffer, onLoad, attributeIds) {+ requested.push(attributeIds);+ onLoad(decodedGeometry);+ },+ };+ const data = triangleDocument({+ extraChunks: [new Uint8Array(8)],+ edit: document => {+ document.accessors[0].bufferView = undefined;+ document.meshes[0].primitives[0] = { attributes: { POSITION: 0 }, extensions: { KHR_draco_mesh_compression: { bufferView: 2, attributes: { POSITION: 0 } } } };+ document.extensionsUsed = ['KHR_draco_mesh_compression'];+ document.extensionsRequired = ['KHR_draco_mesh_compression'];+ },+ });+ const asset = await parseGltf(data, { decoders: { draco: dracoLoader } });+ assert.deepEqual(requested, [{ position: 0 }]);+ assert.deepEqual(Array.from(asset.parts[0].geometry.positions), Array.from(TRIANGLE_POSITIONS));+});++test('KHR_texture_basisu textures arrive as RGBA8 pixels from the supplied KTX2 loader', async context => {+ mockImageBitmap(context);+ const ktx2Header = new Uint8Array(80);+ ktx2Header.set([0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a]);+ new DataView(ktx2Header.buffer).setUint32(20, 2, true);+ new DataView(ktx2Header.buffer).setUint32(24, 2, true);+ const pixels = Uint8Array.from({ length: 16 }, (unused, index) => index * 10);+ const ktx2Loader = {+ load(url, onLoad) {+ onLoad(new DataTexture(pixels, 2, 2));+ },+ };+ const data = triangleDocument({+ extraChunks: [ktx2Header],+ edit: document => {+ document.images = [{ bufferView: 2, mimeType: 'image/ktx2' }];+ document.textures = [{ extensions: { KHR_texture_basisu: { source: 0 } } }];+ document.materials[0].pbrMetallicRoughness.baseColorTexture = { index: 0 };+ document.extensionsUsed = ['KHR_texture_basisu'];+ document.extensionsRequired = ['KHR_texture_basisu'];+ },+ });+ const asset = await parseGltf(data, { decoders: { ktx2: ktx2Loader }, maxTexturePixels: 4 });+ const { image } = asset.parts[0].material.map;+ assert.equal(image.width, 2);+ assert.equal(image.height, 2);+ assert.deepEqual(Array.from(image.data), Array.from(pixels));+ await assert.rejects(parseGltf(data, { decoders: { ktx2: ktx2Loader }, maxTexturePixels: 3 }), /maxTexturePixels/);+});diff --git a/tests/gltf.test.js b/tests/gltf.test.jsindex 98d40291..b31b7374 100644--- a/tests/gltf.test.js+++ b/tests/gltf.test.js@@ -152,7 +152,7 @@ test('unsupported features fail explicitly before loading resources', async () =[document => { document.materials[0].doubleSided = true; }, /Double-sided/],[document => { document.materials[0].normalTexture = { index: 0 }; }, /normalTexture texture index/],[document => { document.extensionsRequired = ['KHR_draco_mesh_compression']; }, /KHR_draco/],- [document => { document.materials[0].extensions = { KHR_materials_clearcoat: {} }; }, /KHR_materials_clearcoat/],+ [document => { document.materials[0].extensions = { KHR_materials_unknown_vendor: {} }; }, /KHR_materials_unknown_vendor/],[document => { document.buffers[0].uri = 'external.bin'; }, /baseUrl/],[document => { document.nodes[2].children = [0]; }, /Cyclic/],[document => { document.materials[0].pbrMetallicRoughness.metallicFactor = 2; }, /metallic/],