Search code examples
node.jsthree.jsgltf

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


I upload an obj file and convert it to a glb file on three.js. So I wanted to compress the file using the Draco encoder, but I only knew how to compress it using cmd. Is there any way to compress using node.js?

edited

I solved it by myself. The method was simple. I'm just install this. And, my code is here.

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);
        });
  });

I hope you can solve this problem as well. Go for it!


Solution

  • Use npm module

    or

    Convert DRACOExporter from /examples/js/exporters to a module