Skip to content

Code-first video & image, rendered natively on WebGPU.

Only load texture formats the renderer can decode

Claude756aa81010c4
diff --git a/packages/framefields/src/extended-models-render.test.ts b/packages/framefields/src/extended-models-render.test.ts
index 060d8692..29249174 100644
--- a/packages/framefields/src/extended-models-render.test.ts
+++ b/packages/framefields/src/extended-models-render.test.ts
@@ -619,7 +619,7 @@ f 1/1 2/2 3/3 4/4
await fs.rm(tmpDir, { recursive: true, force: true });
});
- it("still renders when a texture cannot be decoded", async () => {
+ it("skips textures in formats the renderer cannot decode", async () => {
const tmpDir = await fs.mkdtemp(
path.join(os.tmpdir(), "framefields-bad-tex-"),
);
@@ -649,8 +649,9 @@ f 1/1 2/2 3/3 4/4
const center = getPixel(img, 200, 150);
expect(center.g).toBeGreaterThan(150);
expect(warn).toHaveBeenCalledWith(
- expect.stringContaining("Could not decode texture"),
- expect.anything(),
+ expect.stringContaining(
+ '"paint.tga" for material "Paint" is in an unsupported format',
+ ),
);
warn.mockRestore();
await fs.rm(tmpDir, { recursive: true, force: true });
diff --git a/packages/webgpu-renderers/src/loaders/fbx-loader.ts b/packages/webgpu-renderers/src/loaders/fbx-loader.ts
index 369fca30..36391218 100644
--- a/packages/webgpu-renderers/src/loaders/fbx-loader.ts
+++ b/packages/webgpu-renderers/src/loaders/fbx-loader.ts
@@ -3,6 +3,7 @@ import path from "node:path";
import { inflateSync } from "node:zlib";
import { type Vec3, Vector3Math } from "../math3d/index.js";
import {
+ isSupportedTexture,
readTextureFile,
textureMime,
warnMissingTexture,
@@ -369,6 +370,10 @@ export class FbxLoader {
stringChild(video, "RelativeFilename") ??
stringChild(video, "Filename");
+ if (ref && !isSupportedTexture(ref)) {
+ warnMissingTexture("FbxLoader", material.name, ref);
+ continue;
+ }
let bytes: Uint8Array | undefined;
const content = video?.children.find((n) => n.name === "Content")
?.props[0];
diff --git a/packages/webgpu-renderers/src/loaders/texture-files.test.ts b/packages/webgpu-renderers/src/loaders/texture-files.test.ts
index 370f287d..a92e0773 100644
--- a/packages/webgpu-renderers/src/loaders/texture-files.test.ts
+++ b/packages/webgpu-renderers/src/loaders/texture-files.test.ts
@@ -4,7 +4,7 @@ import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { FbxLoader, loadFBX } from "./fbx-loader.js";
import { loadOBJ, ObjLoader } from "./obj-loader.js";
-import { readTextureFile } from "./texture-files.js";
+import { isSupportedTexture, readTextureFile } from "./texture-files.js";
const PNG_BYTES = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 1, 2, 3, 4]);
@@ -141,3 +141,14 @@ map_Bump maps/height.png
expect(model.materials.Painted?.diffuseTextureBuffer).toEqual(embedded);
});
});
+
+describe("texture formats", () => {
+ it("skips formats the renderer cannot decode, even when the file exists", () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "framefields-tga-"));
+ fs.writeFileSync(path.join(dir, "paint.tga"), PNG_BYTES);
+ expect(isSupportedTexture("paint.tga")).toBe(false);
+ expect(isSupportedTexture("C:\\maps\\paint.PNG")).toBe(true);
+ expect(readTextureFile("paint.tga", dir)).toBeUndefined();
+ fs.rmSync(dir, { recursive: true, force: true });
+ });
+});
diff --git a/packages/webgpu-renderers/src/loaders/texture-files.ts b/packages/webgpu-renderers/src/loaders/texture-files.ts
index ab871ed7..ea0aad16 100644
--- a/packages/webgpu-renderers/src/loaders/texture-files.ts
+++ b/packages/webgpu-renderers/src/loaders/texture-files.ts
@@ -1,6 +1,7 @@
import fs from "node:fs";
import path from "node:path";
+/** The texture formats the renderer decodes (sharp); others (TGA, BMP, PSD, DDS, …) are skipped. */
const MIME_BY_EXT: Record<string, string> = {
".png": "image/png",
".jpg": "image/jpeg",
@@ -9,8 +10,7 @@ const MIME_BY_EXT: Record<string, string> = {
".gif": "image/gif",
".tif": "image/tiff",
".tiff": "image/tiff",
- ".bmp": "image/bmp",
- ".tga": "image/x-tga",
+ ".avif": "image/avif",
};
export interface TextureFile {
@@ -23,6 +23,12 @@ export function textureMime(name: string): string {
return MIME_BY_EXT[path.extname(name).toLowerCase()] ?? "image/png";
}
+/** Whether the renderer can decode a texture with this file name (no extension: assume yes). */
+export function isSupportedTexture(name: string): boolean {
+ const ext = path.extname(name.replace(/\\/g, "/")).toLowerCase();
+ return !ext || ext in MIME_BY_EXT;
+}
+
/**
* Reads a texture a model file refers to. Exporters often write absolute
* paths from the author's machine (`C:\Users\…\wood.png`), so after the
@@ -34,6 +40,7 @@ export function readTextureFile(
basePath: string | undefined,
): TextureFile | undefined {
if (!ref || typeof fs === "undefined" || !fs.existsSync) return undefined;
+ if (!isSupportedTexture(ref)) return undefined;
let cleaned = ref.trim().replace(/^file:\/\//, "");
try {
cleaned = decodeURIComponent(cleaned);
@@ -68,13 +75,16 @@ export function readTextureFile(
return undefined;
}
-/** Warns that a model's texture could not be found (the mesh renders untextured). */
+/** Warns that a model's texture is missing or in an unsupported format (the mesh renders untextured). */
export function warnMissingTexture(
loader: string,
material: string,
ref: string,
): void {
+ const reason = isSupportedTexture(ref)
+ ? "was not found"
+ : `is in an unsupported format (use ${Object.keys(MIME_BY_EXT).join(", ")})`;
console.warn(
- `[${loader}] Texture "${ref}" for material "${material}" was not found; the material renders without it.`,
+ `[${loader}] Texture "${ref}" for material "${material}" ${reason}; the material renders without it.`,
);
}