83 lines
2.4 KiB
JavaScript
83 lines
2.4 KiB
JavaScript
import fs from "fs";
|
|
|
|
export const BACKUP_SUFFIX = "_备份";
|
|
|
|
// 检查当前目录是否有 gltf 文件
|
|
export function hasGltfFile() {
|
|
return listGltfFiles().length > 0;
|
|
}
|
|
|
|
// 获取当前目录下可用的 gltf 文件列表
|
|
export function listGltfFiles() {
|
|
const cwd = process.cwd();
|
|
return fs.readdirSync(cwd).filter(f => f.toLowerCase().endsWith(".gltf") && !f.includes(BACKUP_SUFFIX));
|
|
}
|
|
|
|
// 获取 glTF / GLB 模型文件
|
|
export function listModelFiles() {
|
|
const cwd = process.cwd();
|
|
return fs
|
|
.readdirSync(cwd)
|
|
.filter(file => /\.(gltf|glb)$/i.test(file) && !file.includes(BACKUP_SUFFIX));
|
|
}
|
|
|
|
// 获取所有支持的模型文件(包括需要转换的格式)
|
|
export function listAllModelFiles() {
|
|
const cwd = process.cwd();
|
|
return fs
|
|
.readdirSync(cwd)
|
|
.filter(file => /\.(gltf|glb|obj|fbx)$/i.test(file) && !file.includes(BACKUP_SUFFIX));
|
|
}
|
|
|
|
// 判断是否需要先转换
|
|
export function needsConversion(file) {
|
|
return /\.(obj|fbx)$/i.test(file);
|
|
}
|
|
|
|
// 检查是否满足执行条件(有 ktx2、gltf、bin 文件)
|
|
export function checkRequiredFiles() {
|
|
const cwd = process.cwd();
|
|
const files = fs.readdirSync(cwd);
|
|
const hasKtx2 = files.some(f => f.toLowerCase().endsWith(".ktx2"));
|
|
const hasGltf = files.some(f => f.toLowerCase().endsWith(".gltf"));
|
|
const hasBin = files.some(f => f.toLowerCase().endsWith(".bin"));
|
|
|
|
const missing = [];
|
|
if (!hasKtx2) missing.push("ktx2");
|
|
if (!hasGltf) missing.push("gltf");
|
|
if (!hasBin) missing.push("bin");
|
|
|
|
return { ok: missing.length === 0, missing };
|
|
}
|
|
|
|
// 修改 gltf 文件,根据选项添加扩展
|
|
export function modifyGltfContent(gltfPath, options = ["textureBasisu"]) {
|
|
const content = fs.readFileSync(gltfPath, "utf-8");
|
|
const gltf = JSON.parse(content);
|
|
|
|
if (options.includes("textureBasisu")) {
|
|
if (!gltf.extensionsUsed) gltf.extensionsUsed = [];
|
|
if (!gltf.extensionsUsed.includes("KHR_texture_basisu")) {
|
|
gltf.extensionsUsed.push("KHR_texture_basisu");
|
|
}
|
|
|
|
if (gltf.textures) {
|
|
gltf.textures = gltf.textures.map(tex => ({
|
|
extensions: { KHR_texture_basisu: { source: tex.source } }
|
|
}));
|
|
}
|
|
|
|
if (gltf.images) {
|
|
gltf.images = gltf.images.map(img => {
|
|
const uri = img.uri || "";
|
|
const newUri = uri.replace(/\.(png|jpg|jpeg|webp|tga)$/i, ".ktx2");
|
|
return { uri: newUri, mimeType: "image/ktx2" };
|
|
});
|
|
}
|
|
}
|
|
|
|
// placeholder1, placeholder2, placeholder3 预留扩展点
|
|
|
|
return gltf;
|
|
}
|