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

时间:2021-03-23 10:22:48

标签: node.js merge transform gltf

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

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

1 个答案:

答案 0 :(得分:1)

您导入的 @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 移动一些节点的单个场景中。