-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathprocess.mts
More file actions
112 lines (91 loc) · 3.08 KB
/
process.mts
File metadata and controls
112 lines (91 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { basename, extname, join } from "node:path/posix";
import fs from "fs-extra";
import { compressFileToKtx } from "./ktx.mjs";
import { ICreateAssetsOptions } from "./assets.mjs";
import { processExportedTexture } from "./texture.mjs";
import { processExportedMaterial } from "./material.mjs";
import { processExportedNodeParticleSystemSet } from "./particle-system.mjs";
const supportedImagesExtensions: string[] = [".jpg", ".jpeg", ".webp", ".png", ".bmp"];
const supportedCubeTexturesExtensions: string[] = [".env", ".dds", ".hdr"];
const supportedAudioExtensions: string[] = [".mp3", ".wav", ".wave", ".ogg"];
const supportedJsonExtensions: string[] = [".material", ".gui", ".cinematic", ".npss", ".ragdoll", ".json"];
const supportedMiscExtensions: string[] = [".3dl", ".exr", ".hdr"];
const supportedExtensions: string[] = [
...supportedImagesExtensions,
...supportedCubeTexturesExtensions,
...supportedAudioExtensions,
...supportedJsonExtensions,
...supportedMiscExtensions,
];
export interface IProcessAssetFileOptions extends ICreateAssetsOptions {
outputAssetsDir: string;
exportedAssets: string[];
optimize: boolean;
cache: Record<string, string>;
compressedTexturesEnabled: boolean;
}
export async function processAssetFile(file: string, options: IProcessAssetFileOptions) {
const isNavMesh = file.includes(".navmesh");
const extension = extname(file).toLocaleLowerCase();
if (!isNavMesh && !supportedExtensions.includes(extension)) {
return;
}
if (basename(file).startsWith("editor_preview")) {
return;
}
const relativePath = file.replace(join(options.projectDir, "/"), "");
const split = relativePath.split("/");
let path = "";
for (let i = 0; i < split.length - 1; ++i) {
try {
await fs.ensureDir(join(options.publicDir, path, split[i]));
} catch (e) {
// Catch silently.
}
path = join(path, split[i]);
}
let isNewFile = false;
const fileStat = await fs.stat(file);
const hash = fileStat.mtimeMs.toString();
isNewFile = !options.cache[relativePath] || options.cache[relativePath] !== hash;
options.cache[relativePath] = hash;
const finalPath = join(options.publicDir, relativePath);
const finalPathExists = await fs.pathExists(finalPath);
if (isNewFile || !finalPathExists) {
if (supportedJsonExtensions.includes(extension)) {
await fs.writeJSON(finalPath, await fs.readJSON(file), {
encoding: "utf-8",
});
} else {
await fs.copyFile(file, finalPath);
}
}
options.onStepChanged?.("assets", {
message: `Processed asset: ${relativePath}`,
});
options.exportedAssets.push(finalPath);
if (options.optimize) {
await compressFileToKtx(finalPath, {
force: isNewFile,
...options,
});
}
if (options.optimize) {
if (supportedImagesExtensions.includes(extension)) {
await processExportedTexture(finalPath, {
...options,
force: isNewFile,
});
} else if (extension === ".material") {
await processExportedMaterial(finalPath, {
...options,
force: isNewFile,
});
} else if (extension === ".npss") {
await processExportedNodeParticleSystemSet(finalPath, {
...options,
force: isNewFile,
});
}
}
}