【问题标题】:Use GLTF-transform merge command - node js script使用 GLTF-transform 合并命令 - 节点 js 脚本
【发布时间】:2021-06-19 23:55:26
【问题描述】:

我想在我的脚本中使用 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}`);
});

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

【问题讨论】:

    标签: node.js merge transform gltf


    【解决方案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 移动一些节点。

    【讨论】:

    • 非常感谢,不仅感谢您的回答,也感谢您的所有工作!
    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多