我可以使用 node.js 的 draco 编码器压缩 glb 文件吗?

Can I compress the glb file using the draco encoder with node.js?

我上传了一个 obj 文件并在 three.js 上将其转换为 glb 文件。所以我想用Draco encoder压缩文件,但我只知道如何用cmd压缩它。有什么方法可以使用 node.js 进行压缩吗?

已编辑

我自己解决了。方法很简单。我只是安装 this。 而且,我的代码在这里。

const gltfPipeline = require('gltf-pipeline');
const fsExtra = require('fs-extra');
const path = require('path');
const glbToGltf = gltfPipeline.glbToGltf;
const processGltf = gltfPipeline.processGltf;

const glb = fsExtra.readFileSync(path.resolve(__dirname+'/../public/glbFiles/' + req.filename));
const options = {
  dracoOptions: {
    compressionLevel: 10
  }
};
glbToGltf(glb)
  .then(gltf => {
    processGltf(gltf.gltf, options)
        .then(results => {
          fsExtra.writeJsonSync(path.resolve(__dirname + '/../public/compressionFiles/' + req.filename.split('.')[0] + '.gltf'), results.gltf);
        });
  });

希望你也能解决这个问题。加油!

使用npm module

DRACOExporter from /examples/js/exporters 转换为模块