Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Load glTF textures that use EXT_texture_webp

Nguyen Xuan Andb07edc23e67
diff --git a/bench/size/budgets.json b/bench/size/budgets.json
index 6fc09b5e..347d4ab3 100644
--- a/bench/size/budgets.json
+++ b/bench/size/budgets.json
@@ -11,8 +11,8 @@
"entry:nature": 13200,
"entry:motion": 13600,
"entry:canvas2d": 9400,
- "entry:gltf": 54590,
- "entry:gltf+lazy": 57920,
+ "entry:gltf": 55020,
+ "entry:gltf+lazy": 58350,
"entry:modeling": 8700,
"entry:sculpt": 2500,
"entry:surface": 10100,
@@ -46,8 +46,8 @@
"entry:light-linking": 19680,
"entry:cascaded-shadows": 28480,
"scene:lit-box": 64890,
- "scene:gltf-box": 86430,
- "scene:gltf-box+lazy": 89760,
+ "scene:gltf-box": 86880,
+ "scene:gltf-box+lazy": 90200,
"scene:agent-scene": 106710,
"scene:agent-scene+lazy": 289120
}
diff --git a/changes/274.md b/changes/274.md
new file mode 100644
index 00000000..25e704ef
--- /dev/null
+++ b/changes/274.md
@@ -0,0 +1 @@
+- Load glTF textures that use `EXT_texture_webp` (closes #274). The WebP source decodes like PNG and JPEG, counts toward the texture budgets from its header size, and falls back to the PNG or JPEG `source` when the browser cannot decode WebP.
diff --git a/docs/gltf.md b/docs/gltf.md
index 54a1b68e..597cd6e5 100644
--- a/docs/gltf.md
+++ b/docs/gltf.md
@@ -46,7 +46,7 @@ The optional `signal` aborts those fetches.
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.
+It checks PNG, JPEG and WebP 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.
@@ -141,6 +141,7 @@ Line and point primitives are unsupported.
| `KHR_draco_mesh_compression` | Needs `options.decoders.draco`. |
| `EXT_meshopt_compression` | Needs `options.decoders.meshopt`. |
| `KHR_texture_basisu` | Needs `options.decoders.ktx2`. |
+| `EXT_texture_webp` | The WebP source is decoded by the browser like PNG and JPEG. When the texture also has a PNG or JPEG `source` and the browser cannot decode WebP, that source is used instead. |
| `KHR_materials_clearcoat`, `_transmission`, `_ior`, `_specular`, `_sheen`, `_volume`, `_anisotropy`, `_iridescence` | Read as factors and rendered as material layers, see `docs/material-maps.md`. The textures `clearcoatTexture`, `clearcoatRoughnessTexture`, `clearcoatNormalTexture`, `anisotropyTexture`, `sheenColorTexture` and `sheenRoughnessTexture` are read too, into `material.layerMaps` (their GLSL loads with `material-layer-shaders`); a renderer draws at most three different ones, and none beside splat layers. The transmission, iridescence and specular textures are not read: their factors are used. `occlusionTexture`, `emissiveTexture` and `metallicRoughnessTexture` may use TEXCOORD_1 (the mesh's `lightUvs`, which a light bake also uses); base color and normal textures must use TEXCOORD_0, and a texture on TEXCOORD_1 cannot carry `KHR_texture_transform`. Their textures are ignored, and an iridescent film takes the thickness maximum. Export writes them back. |
`KHR_materials_transmission` with a factor above zero sets `material.transmission` to that factor.
diff --git a/src/gltf-decode.js b/src/gltf-decode.js
index f73251a2..739110c1 100644
--- a/src/gltf-decode.js
+++ b/src/gltf-decode.js
@@ -22,11 +22,13 @@ const TEXTURE_SLOTS = [
['emissiveMap', 'emissiveTexture', false],
];
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47];
+// "RIFF", four size bytes that vary, then "WEBP".
+const WEBP_SIGNATURE = [0x52, 0x49, 0x46, 0x46, null, null, null, null, 0x57, 0x45, 0x42, 0x50];
const LINEAR_SRGB = 'srgb-linear';
const COMPONENT_CODES = new Map(Object.entries(COMPONENT_TYPES).map(([code, ArrayType]) => [ArrayType, Number(code)]));
export function textureSourceIndex(texture) {
- return texture.extensions?.KHR_texture_basisu?.source ?? texture.source;
+ return texture.extensions?.KHR_texture_basisu?.source ?? texture.extensions?.EXT_texture_webp?.source ?? texture.source;
}
function sanitizeNodeName(name) {
@@ -35,7 +37,9 @@ function sanitizeNodeName(name) {
function mimeTypeOf(imageDefinition, bytes) {
if (imageDefinition.mimeType) return imageDefinition.mimeType;
- return PNG_SIGNATURE.every((value, index) => bytes[index] === value) ? 'image/png' : 'image/jpeg';
+ if (PNG_SIGNATURE.every((value, index) => bytes[index] === value)) return 'image/png';
+ if (WEBP_SIGNATURE.every((value, index) => value === null || bytes[index] === value)) return 'image/webp';
+ return 'image/jpeg';
}
async function decodeImage(bytes, mimeType, target) {
@@ -149,11 +153,25 @@ export async function decodeGltf({ document, glbBinary, resourceBytes, decoders,
return loaded;
}
+ // A WebP source with a PNG or JPEG fallback falls back when this browser cannot decode WebP.
+ async function loadTextureImage(definition) {
+ const isKtx2 = Boolean(definition.extensions?.KHR_texture_basisu);
+ const sourceIndex = textureSourceIndex(definition);
+ const fallbackIndex = definition.source;
+ const hasWebpFallback = !isKtx2 && definition.extensions?.EXT_texture_webp && Number.isInteger(fallbackIndex) && fallbackIndex !== sourceIndex;
+ if (!hasWebpFallback) return loadImage(sourceIndex, isKtx2);
+ try {
+ return await loadImage(sourceIndex, false);
+ } catch {
+ return loadImage(fallbackIndex, false);
+ }
+ }
+
async function loadTexture(textureIndex) {
if (textures.has(textureIndex)) return textures.get(textureIndex);
const definition = document.textures[textureIndex];
const sampler = document.samplers?.[definition.sampler] ?? {};
- const { image, isCompressedTexture } = await loadImage(textureSourceIndex(definition), Boolean(definition.extensions?.KHR_texture_basisu));
+ const { image, isCompressedTexture } = await loadTextureImage(definition);
const texture = {
image,
isCompressedTexture,
@@ -185,7 +203,7 @@ export async function decodeGltf({ document, glbBinary, resourceBytes, decoders,
try {
material[field] = await loadTexture(reference.index);
} catch (error) {
- throw new Error(`${sourceField} failed to decode. Use a valid PNG or JPEG image. ${error.message}`);
+ throw new Error(`${sourceField} failed to decode. Use a valid PNG, JPEG or WebP image. ${error.message}`);
}
}
const layerTextures = {};
@@ -193,7 +211,7 @@ export async function decodeGltf({ document, glbBinary, resourceBytes, decoders,
try {
layerTextures[name] = await loadTexture(reference.index);
} catch (error) {
- throw new Error(`${name} failed to decode. Use a valid PNG or JPEG image. ${error.message}`);
+ throw new Error(`${name} failed to decode. Use a valid PNG, JPEG or WebP image. ${error.message}`);
}
}
if (Object.keys(layerTextures).length > 0) material.layerTextures = layerTextures;
diff --git a/src/gltf.js b/src/gltf.js
index 1fe7039e..1b79c568 100644
--- a/src/gltf.js
+++ b/src/gltf.js
@@ -9,7 +9,7 @@ import { MAX_MORPH_TARGETS, createMorphPose, validateMorphGeometry, validateMorp
export 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', 'TEXCOORD_1 for occlusion, emissive and metallic-roughness textures', 'clearcoat, clearcoat roughness, clearcoat normal, anisotropy, sheen color and sheen roughness textures', 'linear RGB vertex colors', 'metallic roughness factors', 'emissive factors', 'alpha mask and blend modes', 'double-sided materials', 'KHR_mesh_quantization', 'KHR_materials_emissive_strength', 'KHR_materials_clearcoat, transmission, volume, ior, specular, sheen, anisotropy and iridescence factors', 'KHR_texture_transform baked into UVs, with a normal map that differs by scale and offset applied in the shader', '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'],
+ 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/WebP base color textures', 'normal/occlusion/emissive/metallic-roughness textures', 'TEXCOORD_0', 'TEXCOORD_1 for occlusion, emissive and metallic-roughness textures', 'clearcoat, clearcoat roughness, clearcoat normal, anisotropy, sheen color and sheen roughness textures', 'linear RGB vertex colors', 'metallic roughness factors', 'emissive factors', 'alpha mask and blend modes', 'double-sided materials', 'KHR_mesh_quantization', 'KHR_materials_emissive_strength', 'KHR_materials_clearcoat, transmission, volume, ior, specular, sheen, anisotropy and iridescence factors', 'KHR_texture_transform baked into UVs, with a normal map that differs by scale and offset applied in the shader', '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', 'EXT_texture_webp, with the PNG or JPEG fallback source when the browser cannot decode WebP'],
// The primitive's own material is drawn; the other variants are not selectable.
ignoredExtensions: ['KHR_materials_variants'],
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', 'different texture transforms on one material, except a normal map that differs by scale and offset', 'skinned or morphed GPU instancing', 'transmission, iridescence and specular textures (their factors are used)', 'texture transforms on TEXCOORD_1'],
@@ -30,7 +30,7 @@ const DEFAULT_MAX_INSTANCES = 4096;
// KHR_xmp_json_ld and its earlier name KHR_xmp only carry metadata (authors, licences) and change nothing drawn: the
// Khronos sample assets list them, and a file must not be refused for them.
const METADATA_EXTENSIONS = ['KHR_xmp_json_ld', 'KHR_xmp'];
-const ALLOWED_EXTENSIONS = new Set(['KHR_mesh_quantization', 'KHR_materials_emissive_strength', 'KHR_texture_transform', 'EXT_mesh_gpu_instancing', ...METADATA_EXTENSIONS, ...GLTF_LAYER_EXTENSIONS]);
+const ALLOWED_EXTENSIONS = new Set(['KHR_mesh_quantization', 'KHR_materials_emissive_strength', 'KHR_texture_transform', 'EXT_mesh_gpu_instancing', 'EXT_texture_webp', ...METADATA_EXTENSIONS, ...GLTF_LAYER_EXTENSIONS]);
const DECODER_BY_EXTENSION = { KHR_draco_mesh_compression: 'draco', EXT_meshopt_compression: 'meshopt', KHR_texture_basisu: 'ktx2' };
// Accepted when optional, because 67js does not render them. Rejected when listed in extensionsRequired.
const IGNORED_EXTENSIONS = new Set(GLTF_CAPABILITIES.ignoredExtensions);
@@ -489,6 +489,28 @@ function dataUriBytes(uri) {
return Uint8Array.from(binary, character => character.charCodeAt(0));
}
+const RIFF_TAG = 0x52494646;
+const WEBP_TAG = 0x57454250;
+
+/** Read the size from a WebP header: the extended (VP8X), lossy (VP8) or lossless (VP8L) layout. */
+function webpImageSize(bytes, header) {
+ if (bytes.length < 30 || header.getUint32(0) !== RIFF_TAG || header.getUint32(8) !== WEBP_TAG) return null;
+ const chunk = String.fromCharCode(bytes[12], bytes[13], bytes[14], bytes[15]);
+ if (chunk === 'VP8X') {
+ const width = 1 + (bytes[24] | (bytes[25] << 8) | (bytes[26] << 16));
+ const height = 1 + (bytes[27] | (bytes[28] << 8) | (bytes[29] << 16));
+ return { width, height };
+ }
+ if (chunk === 'VP8 ' && bytes[23] === 0x9d && bytes[24] === 0x01 && bytes[25] === 0x2a) {
+ return { width: header.getUint16(26, true) & 0x3fff, height: header.getUint16(28, true) & 0x3fff };
+ }
+ if (chunk === 'VP8L' && bytes[20] === 0x2f) {
+ const bits = header.getUint32(21, true);
+ return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
+ }
+ return null;
+}
+
function validateImage(bytes, maxImagePixels) {
const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
let width;
@@ -515,13 +537,18 @@ function validateImage(bytes, maxImagePixels) {
offset += length;
}
}
+ if (!width) {
+ const webpSize = webpImageSize(bytes, header);
+ width = webpSize?.width;
+ height = webpSize?.height;
+ }
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);
scalable = false;
}
- if (!width || !height) throw new Error('Only valid PNG, JPEG and KTX2 texture images are supported.');
+ if (!width || !height) throw new Error('Only valid PNG, JPEG, WebP and KTX2 texture images are supported.');
if (width * height > maxImagePixels) throw new Error('Texture image exceeds maxTextureImagePixels before decoding.');
return { width, height, scalable };
}
diff --git a/tests/gltf.test.js b/tests/gltf.test.js
index 5ea0da3e..cf2f1e55 100644
--- a/tests/gltf.test.js
+++ b/tests/gltf.test.js
@@ -561,3 +561,85 @@ test('normalized integer skin weights import correctly and skinned mesh node tra
assert.ok(skinPosition(part, 4).x < 0.1);
asset.dispose();
});
+
+function webpLosslessUri(width, height) {
+ const bytes = new Uint8Array(30);
+ const view = new DataView(bytes.buffer);
+ bytes.set([0x52, 0x49, 0x46, 0x46], 0);
+ view.setUint32(4, 22, true);
+ bytes.set([0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4c], 8);
+ view.setUint32(16, 10, true);
+ bytes[20] = 0x2f;
+ view.setUint32(21, (width - 1) | ((height - 1) << 14), true);
+ return `data:image/webp;base64,${Buffer.from(bytes).toString('base64')}`;
+}
+
+function webpExtendedUri(width, height) {
+ const bytes = new Uint8Array(30);
+ const view = new DataView(bytes.buffer);
+ bytes.set([0x52, 0x49, 0x46, 0x46], 0);
+ view.setUint32(4, 22, true);
+ bytes.set([0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x58], 8);
+ view.setUint32(16, 10, true);
+ for (const [offset, value] of [[24, width - 1], [27, height - 1]]) {
+ bytes[offset] = value & 0xff;
+ bytes[offset + 1] = (value >> 8) & 0xff;
+ bytes[offset + 2] = (value >> 16) & 0xff;
+ }
+ return `data:image/webp;base64,${Buffer.from(bytes).toString('base64')}`;
+}
+
+function mockImageBitmapByType(context, decodable) {
+ const previousBitmap = globalThis.createImageBitmap;
+ const previousSelf = globalThis.self;
+ globalThis.self = globalThis;
+ const decodedTypes = [];
+ globalThis.createImageBitmap = async blob => {
+ if (!decodable(blob.type)) throw new Error(`cannot decode ${blob.type}`);
+ decodedTypes.push(blob.type);
+ return { width: 4, height: 4, close() {} };
+ };
+ context.after(() => {
+ if (previousBitmap) globalThis.createImageBitmap = previousBitmap;
+ else delete globalThis.createImageBitmap;
+ if (previousSelf) globalThis.self = previousSelf;
+ else delete globalThis.self;
+ });
+ return decodedTypes;
+}
+
+function useWebpTexture(document, images, texture) {
+ document.images = images;
+ document.textures = [texture];
+ document.extensionsUsed = ['EXT_texture_webp'];
+ document.materials[0].pbrMetallicRoughness.baseColorTexture = { index: 0 };
+}
+
+test('EXT_texture_webp textures decode as WebP', async context => {
+ const decodedTypes = mockImageBitmapByType(context, () => true);
+ const asset = await parseGltf(triangleAsset(document => {
+ useWebpTexture(document, [{ uri: webpLosslessUri(4, 4) }], { extensions: { EXT_texture_webp: { source: 0 } } });
+ document.extensionsRequired = ['EXT_texture_webp'];
+ }));
+ assert.ok(asset.parts[0].material.map);
+ assert.deepEqual(decodedTypes, ['image/webp']);
+ assert.ok(GLTF_CAPABILITIES.supported.some(entry => entry.includes('EXT_texture_webp')));
+ asset.dispose();
+});
+
+test('a WebP texture falls back to its PNG source when the browser cannot decode WebP', async context => {
+ const decodedTypes = mockImageBitmapByType(context, type => type !== 'image/webp');
+ const asset = await parseGltf(triangleAsset(document => {
+ useWebpTexture(document, [{ uri: webpLosslessUri(4, 4) }, { uri: PNG_IMAGE }], { source: 1, extensions: { EXT_texture_webp: { source: 0 } } });
+ }));
+ assert.ok(asset.parts[0].material.map);
+ assert.deepEqual(decodedTypes, ['image/png']);
+ asset.dispose();
+});
+
+test('WebP image sizes count toward the texture budgets before decoding', async () => {
+ const oversized = triangleAsset(document => {
+ useWebpTexture(document, [{ uri: webpExtendedUri(10000, 10000) }], { extensions: { EXT_texture_webp: { source: 0 } } });
+ });
+ await assert.rejects(parseGltf(oversized), /maxTextureImagePixels before decoding/);
+});