使用 GLTF-transform 合并命令 - 节点 js 脚本

Use GLTF-transform merge command - node js script

我想在我的脚本中使用 gltf-transform 命令“合并”并编写类似这样的东西来合并两个或多个 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