From 8e74488061e13b8487db3d38374b9acc9e3d5634 Mon Sep 17 00:00:00 2001 From: MatsErdkamp <62242064+MatsErdkamp@users.noreply.github.com> Date: Sun, 19 May 2024 19:58:49 +0200 Subject: [PATCH] Added webp support (#204) should support webp extension now --- src/gltf/gltf-parser.ts | 64 +++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/src/gltf/gltf-parser.ts b/src/gltf/gltf-parser.ts index ebfb32c..b3b6c0a 100644 --- a/src/gltf/gltf-parser.ts +++ b/src/gltf/gltf-parser.ts @@ -212,28 +212,54 @@ export class glTFParser { * @param source The source object or index. */ parseTexture(index: number) { - const texture = this._descriptor.textures[index] - const image = this._asset.images[texture.source] - const result = new Texture(new BaseTexture(image.baseTexture.resource, { - wrapMode: WRAP_MODES.REPEAT, - // Went back and forth about NO_PREMULTIPLIED_ALPHA. The default in - // PixiJS is to have premultiplied alpha textures, but this may not work - // so well when rendering objects as opaque (which have alpha equal to 0). - // In that case it's impossible to retrieve the original RGB values, - // because they are all zero when using premultiplied alpha. Both the glTF - // Sample Viewer and Babylon.js uses NO_PREMULTIPLIED_ALPHA so decided to - // do the same. - alphaMode: ALPHA_MODES.NO_PREMULTIPLIED_ALPHA - })) + const texture = this._descriptor.textures[index]; + const image = this._asset.images[this.findTextureSource(texture)]; + const result = new Texture( + new BaseTexture(image.baseTexture.resource, { + wrapMode: WRAP_MODES.REPEAT, + // Went back and forth about NO_PREMULTIPLIED_ALPHA. The default in + // PixiJS is to have premultiplied alpha textures, but this may not work + // so well when rendering objects as opaque (which have alpha equal to 0). + // In that case it's impossible to retrieve the original RGB values, + // because they are all zero when using premultiplied alpha. Both the glTF + // Sample Viewer and Babylon.js uses NO_PREMULTIPLIED_ALPHA so decided to + // do the same. + alphaMode: ALPHA_MODES.NO_PREMULTIPLIED_ALPHA, + }) + ); if (this._descriptor.samplers && texture.sampler !== undefined) { - const sampler = this._descriptor.samplers[texture.sampler] + const sampler = this._descriptor.samplers[texture.sampler]; switch (sampler.wrapS) { - case 10497: result.baseTexture.wrapMode = WRAP_MODES.REPEAT; break - case 33648: result.baseTexture.wrapMode = WRAP_MODES.MIRRORED_REPEAT; break - case 33071: result.baseTexture.wrapMode = WRAP_MODES.CLAMP; break + case 10497: + result.baseTexture.wrapMode = WRAP_MODES.REPEAT; + break; + case 33648: + result.baseTexture.wrapMode = WRAP_MODES.MIRRORED_REPEAT; + break; + case 33071: + result.baseTexture.wrapMode = WRAP_MODES.CLAMP; + break; } } - return result + return result; + } + + findTextureSource(obj) { + // Base case: Check if the current object directly contains the `source` key + if (obj && typeof obj === "object" && "source" in obj) { + return obj.source; + } + + // Recursively check each key in the object + for (const key in obj) { + if (obj[key] && typeof obj[key] === "object") { + const result = this.findTextureSource(obj[key]); + if (result !== undefined) { + return result; + } + } + } + return undefined; } /** @@ -399,4 +425,4 @@ const componentCount: { [name: string]: number } = { const componentSize: { [name: number]: number } = { [5120]: 1, [5121]: 1, [5122]: 2, [5123]: 2, [5125]: 4, [5126]: 4 -} \ No newline at end of file +}