Skip to content

Commit 6ac5583

Browse files
committed
fix: extract node material textures only when exporting scene
#763
1 parent b197b6e commit 6ac5583

4 files changed

Lines changed: 70 additions & 21 deletions

File tree

editor/src/editor/layout/assets-browser/events/material.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { join } from "path/posix";
21
import { ipcRenderer } from "electron";
32

4-
import { createDirectoryIfNotExist } from "../../../../tools/fs";
53
import { isNodeMaterial } from "../../../../tools/guards/material";
6-
import { extractNodeMaterialTextures } from "../../../../tools/material/extract";
74
import { normalizeNodeMaterialUniqueIds } from "../../../../tools/material/material";
85

96
import { getProjectAssetsRootUrl } from "../../../../project/configuration";
@@ -22,20 +19,8 @@ export function listenMaterialAssetsEvents(editor: Editor) {
2219
}
2320

2421
if (isNodeMaterial(material)) {
25-
const rootUrl = getProjectAssetsRootUrl();
26-
27-
if (rootUrl) {
28-
const outputPath = join(rootUrl, "assets", "editor-generated_extracted-textures");
29-
30-
await createDirectoryIfNotExist(outputPath);
31-
await extractNodeMaterialTextures(editor, {
32-
materialData,
33-
assetsDirectory: outputPath,
34-
});
35-
}
36-
3722
material.clear();
38-
material.parseSerializedObject(materialData, rootUrl ?? undefined);
23+
material.parseSerializedObject(materialData, getProjectAssetsRootUrl() ?? undefined);
3924
material.build(false);
4025

4126
normalizeNodeMaterialUniqueIds(material, materialData);

editor/src/project/export/assets.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Editor } from "../../editor/main";
77

88
import { compressFileToKtx } from "./ktx";
99
import { processExportedTexture } from "./texture";
10+
import { processExportedMaterial } from "./materials";
1011

1112
const supportedImagesExtensions: string[] = [".jpg", ".jpeg", ".webp", ".png", ".bmp"];
1213

@@ -85,10 +86,18 @@ export async function processAssetFile(editor: Editor, file: string, options: Pr
8586
});
8687
}
8788

88-
if (options.optimize && supportedImagesExtensions.includes(extension)) {
89-
await processExportedTexture(editor, finalPath, {
90-
force: isNewFile,
91-
exportedAssets: options.exportedAssets,
92-
});
89+
if (options.optimize) {
90+
if (supportedImagesExtensions.includes(extension)) {
91+
await processExportedTexture(editor, finalPath, {
92+
force: isNewFile,
93+
exportedAssets: options.exportedAssets,
94+
});
95+
} else if (extension === ".material") {
96+
await processExportedMaterial(editor, finalPath, {
97+
force: isNewFile,
98+
scenePath: options.scenePath,
99+
exportedAssets: options.exportedAssets,
100+
});
101+
}
93102
}
94103
}

editor/src/project/export/materials.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import { join } from "path/posix";
2+
import { readJSON, writeJSON } from "fs-extra";
3+
4+
import { createDirectoryIfNotExist } from "../../tools/fs";
5+
import { extractNodeMaterialTextures } from "../../tools/material/extract";
6+
7+
import { Editor } from "../../editor/main";
8+
9+
import { compressFileToKtx } from "./ktx";
10+
111
export function configureMaterials(data: any) {
212
if (!data.materials) {
313
return;
@@ -11,3 +21,42 @@ export function configureMaterials(data: any) {
1121
return true;
1222
});
1323
}
24+
25+
export type ComputeExportedMaterialOptions = {
26+
force: boolean;
27+
scenePath: string;
28+
exportedAssets: string[];
29+
};
30+
31+
export async function processExportedMaterial(editor: Editor, absolutePath: string, options: ComputeExportedMaterialOptions) {
32+
const materialData = await readJSON(absolutePath);
33+
if (materialData.customType !== "BABYLON.NodeMaterial") {
34+
return;
35+
}
36+
37+
const extractedTexturesOutputPath = join(options.scenePath, "assets", "editor-generated_extracted-textures");
38+
39+
await createDirectoryIfNotExist(extractedTexturesOutputPath);
40+
41+
const relativePaths = await extractNodeMaterialTextures(editor, {
42+
materialData,
43+
assetsDirectory: join(options.scenePath, "assets", "editor-generated_extracted-textures"),
44+
});
45+
46+
await writeJSON(absolutePath, materialData, {
47+
encoding: "utf-8",
48+
});
49+
50+
await Promise.all(
51+
relativePaths.map(async (relativePath) => {
52+
const finalPath = join(options.scenePath, relativePath);
53+
54+
options.exportedAssets.push(finalPath);
55+
56+
await compressFileToKtx(editor, finalPath, {
57+
force: options.force,
58+
exportedAssets: options.exportedAssets,
59+
});
60+
})
61+
);
62+
}

editor/src/tools/material/extract.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export async function extractNodeMaterialTextures(editor: Editor, options: IExtr
1212
(block: any) => (block.customType === "BABYLON.TextureBlock" || block.customType === "BABYLON.ImageSourceBlock") && block.texture?.name
1313
);
1414

15+
const relativePaths: string[] = [];
16+
1517
await Promise.all(
1618
blocks.map(async (block: any) => {
1719
if (block.texture?.name?.startsWith("http://") || block.texture.name.startsWith("https://")) {
@@ -21,6 +23,7 @@ export async function extractNodeMaterialTextures(editor: Editor, options: IExtr
2123
});
2224

2325
if (relativePath) {
26+
relativePaths.push(relativePath);
2427
block.texture.name = block.texture.url = relativePath;
2528
}
2629
}
@@ -32,9 +35,12 @@ export async function extractNodeMaterialTextures(editor: Editor, options: IExtr
3235
});
3336

3437
if (relativePath) {
38+
relativePaths.push(relativePath);
3539
block.texture.name = block.texture.url = relativePath;
3640
}
3741
}
3842
})
3943
);
44+
45+
return relativePaths;
4046
}

0 commit comments

Comments
 (0)