Nodejs threejs GLTFExporter (server side) Blob issue

I’ve used the following hack to run GLTFExporter in Node.js —

const THREE = require('three');
const Canvas = require('canvas');
const { Blob, FileReader } = require('vblob');

// Patch global scope to imitate browser environment.
global.window = global;
global.Blob = Blob;
global.FileReader = FileReader;
global.THREE = THREE;
global.document = {
  createElement: (nodeName) => {
    if (nodeName !== 'canvas') throw new Error(`Cannot create node ${nodeName}`);
    const canvas = new Canvas(256, 256);
    // This isn't working — currently need to avoid toBlob(), so export to embedded .gltf not .glb.
    // canvas.toBlob = function () {
    //   return new Blob([this.toBuffer()]);
    // };
    return canvas;
  }
};

// https://github.com/mrdoob/three.js/issues/9562
require('three/examples/js/exporters/GLTFExporter');

At the time I didn’t find a fix for canvas.toBlob(), so you cannot go directly to .glb, but export to .gltf should work. There are various tools (e.g. https://glb-packer.glitch.me/) for packing glTF to GLB if needed.

2 Likes