优化第一阶段

This commit is contained in:
yinsx
2025-12-22 12:07:12 +08:00
parent dd99e932b4
commit 1df41ac4ab
38 changed files with 340 additions and 300 deletions

View File

@ -0,0 +1,27 @@
import { listGltfFiles } from "../../utils/gltf.js";
const extensionStep = {
name: "扩展选项",
type: "multiselect",
message: "请选择要添加的glTF扩展",
options: [
{ value: "textureBasisu", label: "KHR_texture_basisu纹理自动扩展", hint: "自动添加KTX2纹理扩展支持" },
{ value: "placeholder1", label: "预留选项1", hint: "功能开发中..." },
{ value: "placeholder2", label: "预留选项2", hint: "功能开发中..." },
{ value: "placeholder3", label: "预留选项3", hint: "功能开发中..." }
],
default: ["textureBasisu"]
};
export function getSteps() {
const files = listGltfFiles();
const fileStep = {
name: "文件选择",
type: "multiselect",
message: files.length ? "请选择要处理的glTF文件" : "当前目录未找到glTF文件",
options: files.map(file => ({ value: file, label: file })),
default: [...files]
};
return [fileStep, extensionStep];
}

80
lib/plugins/gltf/index.js Normal file
View File

@ -0,0 +1,80 @@
import fs from "fs";
import path from "path";
import color from "picocolors";
import { checkRequiredFiles, modifyGltfContent, listGltfFiles, BACKUP_SUFFIX } from "../../utils/gltf.js";
import { runInteractive, showSummary } from "./ui.js";
import { stopKeypress, waitForKey } from "../../keyboard.js";
export function runGltfExtension(config = { files: [], extensions: ["textureBasisu"] }) {
const cwd = process.cwd();
const { files = [], extensions = ["textureBasisu"] } = config;
const selectedExtensions = extensions.length ? extensions : ["textureBasisu"];
const { ok, missing } = checkRequiredFiles();
if (!ok) {
console.log(color.red("\n✖ 缺少必要文件: " + missing.join(", ")));
console.log(color.dim("请确保当前目录包含 .ktx2、.gltf 和 .bin 文件\n"));
return { success: false, count: 0 };
}
const fallbackFiles = listGltfFiles();
const gltfFiles = (files.length ? files : fallbackFiles).filter(f => f.toLowerCase().endsWith(".gltf"));
if (!gltfFiles.length) {
console.log(color.yellow("未选择可处理的 glTF 文件"));
return { success: false, count: 0 };
}
let count = 0;
for (const file of gltfFiles) {
const fullPath = path.join(cwd, file);
if (!fs.existsSync(fullPath)) {
console.log(color.yellow("跳过缺失的文件: " + file));
continue;
}
const baseName = file.replace(/\.gltf$/i, "");
const backupName = baseName + BACKUP_SUFFIX + ".gltf";
const backupPath = path.join(cwd, backupName);
fs.copyFileSync(fullPath, backupPath);
const modified = modifyGltfContent(fullPath, selectedExtensions);
fs.writeFileSync(fullPath, JSON.stringify(modified, null, 2), "utf-8");
console.log(color.green("✓ " + file + " (备份: " + backupName + ")"));
count++;
}
return { success: count > 0, count };
}
async function run() {
const result = await runInteractive();
if (!result) return "back";
stopKeypress();
const { results } = result;
const config = {
files: results[0] || [],
extensions: results[1] || []
};
showSummary([
"处理文件: " + (config.files.length ? config.files.join(", ") : "未选择"),
"扩展选项: " + (config.extensions.length ? config.extensions.join(", ") : "未选择")
]);
const { success, count } = runGltfExtension(config);
if (success) {
console.log(color.green("\n✓ 已修改 " + count + " 个 glTF 文件"));
}
await waitForKey();
return "back";
}
export default {
id: "gltf",
name: "glTF扩展",
desc: "添加KTX2纹理扩展",
run,
};

9
lib/plugins/gltf/ui.js Normal file
View File

@ -0,0 +1,9 @@
import { createStepUI } from "../../utils/stepui.js";
import { getSteps } from "./config.js";
const ui = createStepUI({
title: "glTF 扩展工具",
getSteps
});
export const { runInteractive, showSummary } = ui;