使用GLTF转换合并命令-node js脚本



我想使用gltf转换命令"合并";在我的脚本中,并写了这样的东西来合并两个或多个gltf文件。

const { merge } = require('@gltf-transform/cli');
const fileA = '/model_path/fileA.gltf';
const fileB = '/model_path/fileB.gltf';
merge(fileA, fileB, output.gltf, false);

但什么也没发生。没有输出文件或控制台日志。所以我不知道在哪里继续我的小旅程。

如果有人有线索就太好了。

另一种选择是。。。

const command = "gltf-transform merge("+fileA+", "+fileB+", output.gltf, false)";
exec(command, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});

但也没用,看起来没必要。

您正在导入的@gltf-transform/cli包并不是为编程用途而设计的;它只是一个CLI。用于编程的代码位于/functions/core/extensions包中。

如果你想在JavaScript中实现类似的东西,我会尝试一下:

import { Document, NodeIO } from '@gltf-transform/core';
const io = new NodeIO();
const inputDocument1 = io.read('./model_path/fileA.gltf');
const inputDocument2 = io.read('./model_path/fileB.gltf');
const outputDocument = new Document()
.merge(inputDocument1)
.merge(inputDocument2);
// Optional: Merge binary resources to a single buffer.
const buffer = doc.getRoot().listBuffers()[0];
doc.getRoot().listAccessors().forEach((a) => a.setBuffer(buffer));
doc.getRoot().listBuffers().forEach((b, index) => index > 0 ? b.dispose() : null);
io.write('./model_path/output.gltf', outputDocument);

这将创建包含两个场景的单个glTF文件。如果要将两个文件的内容合并到单个场景中,则需要使用scene API移动一些节点。

您可以通过使用spawn来使用gltf transform/cli。

// you should run `node @gltf-transform/cli/bin/cli.js`
const gltf_transform_cli_path = `your project's node_modules dir path${path.sep}@gltf-transform${path.sep}cli${path.sep}bin${path.sep}cli.js`
const normPath = path.normalize(gltf_transform_cli_path);
const command = `node ${normPath} merge ${fileA} ${fileB} output.gltf false`
const result = spawn(command)

此外,如果要使用ktx(etc1s,usatc(进行纹理压缩,你必须在本地安装ktx。

最新更新