Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(assets): a model texture named by a path on the artist machine is found by its name beside the model, instead of left as a path that is not there

yhh4d8711dad909
diff --git a/pipeline/src/assets/importModelFile.ts b/pipeline/src/assets/importModelFile.ts
index 680477665..a4d1807c1 100644
--- a/pipeline/src/assets/importModelFile.ts
+++ b/pipeline/src/assets/importModelFile.ts
@@ -123,6 +123,29 @@ export async function importModelFile(opts: ImportModelOptions): Promise<Importe
},
);
+ // A model exported with the path its artist had on their machine (`E:/props/road/wall.png`) names an image that is not there, and which sits
+ // beside the model in the project it was brought into: the name alone finds it, as a DCC tool's own import does.
+ const slotNames = ['baseColorTexture', 'normalTexture', 'emissiveTexture', 'occlusionTexture', 'metallicRoughnessTexture', 'flowTexture', 'shiftTexture'] as const;
+ const beside = new Map<string, string>();
+ for (const [i, uri] of externalFiles.entries()) {
+ if (existsSync(path.join(sourceDir, uri))) continue;
+ const name = uri.split(/[\\/]/).pop() ?? '';
+ if (name === '' || name === uri || !existsSync(path.join(sourceDir, name))) continue;
+ beside.set(uri, name);
+ externalFiles[i] = name;
+ }
+ if (beside.size > 0) {
+ for (const mesh of meshes) {
+ const material = mesh.material as Record<string, unknown> | undefined;
+ if (!material) continue;
+ for (const slot of slotNames) {
+ const image = material[slot] as { external?: boolean; file?: string } | undefined;
+ const found = image?.external && image.file !== undefined ? beside.get(image.file) : undefined;
+ if (image && found !== undefined) image.file = found;
+ }
+ }
+ }
+
// Before anything is written, because it splits vertices: every product below is built from the mesh data this leaves behind.
if (lightmapUV) warnings.push(...applyLightmapUV(meshes));
// After it, because a level is a collapse of the mesh as it will be written.
diff --git a/pipeline/tests/model-missing-texture.test.ts b/pipeline/tests/model-missing-texture.test.ts
index d01c14546..3e25bbeb5 100644
--- a/pipeline/tests/model-missing-texture.test.ts
+++ b/pipeline/tests/model-missing-texture.test.ts
@@ -33,13 +33,13 @@ describe('a model that names a texture the project does not have', () => {
const roots: string[] = [];
afterAll(() => { for (const r of roots) rmSync(r, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); });
- async function run(image: string, withFile: boolean) {
+ async function run(image: string, withFile: boolean, besideAs = image) {
const root = mkdtempSync(path.join(tmpdir(), 'model-missing-'));
roots.push(root);
const src = path.join(root, 'assets', 'models');
mkdirSync(src, { recursive: true });
writeFileSync(path.join(src, 'tri.gltf'), triangleGltf(image));
- if (withFile) writeFileSync(path.join(src, image), Buffer.from('89504e470d0a1a0a', 'hex'));
+ if (withFile) writeFileSync(path.join(src, besideAs), Buffer.from('89504e470d0a1a0a', 'hex'));
const result = await importModelFile({ root, destDir: 'assets/models', absSource: path.join(src, 'tri.gltf') });
const text = readdirSync(src).map((f) => (/\.(esprefab|esmaterial)$/.test(f) ? readFileSync(path.join(src, f), 'utf8') : '')).join('\n');
return { result, text };
@@ -56,4 +56,17 @@ describe('a model that names a texture the project does not have', () => {
expect(result.warnings.some((w) => w.includes('wall.png'))).toBe(false);
expect(text).toContain('wall.png');
});
+
+ it('finds by its name a texture whose path is one on the artist machine, when the file lies beside the model', async () => {
+ const { result, text } = await run('E:/props/road/wall.png', true, 'wall.png');
+ expect(result.warnings.some((w) => w.includes('is not there'))).toBe(false);
+ expect(text).toContain('assets/models/wall.png');
+ expect(text).not.toContain('E:/');
+ });
+
+ it('still leaves such a texture off when no file by that name is beside the model', async () => {
+ const { result, text } = await run('E:/props/road/wall.png', false);
+ expect(result.warnings.some((w) => w.includes('wall.png'))).toBe(true);
+ expect(text).not.toContain('E:/');
+ });
});