I need to load .glb or .gltf files on node. I’ve read all related topics here, but seems that there is no good answer. I have a glb file that should be processed by node, then attached to another code generated 3d-object and then send to client for rendering. Anybody has a working solution?
I thought that this will work:
const loadGLTFModel = (fileName) => {
const loader = new THREE.GLTFLoader();
const data = fs.readFileSync(fileName);
const buffer = new Buffer.from( data ).toString();
loader.parse(buffer, './', (gltf) => console.log(gltf));
}
But it doesn’t work with glb files at all. And it fails with gltf files as well.
For those who are looking for the answer to the question about load GLB models on node.js with three.js
Here’s a working solution for models WITHOUT textures:
const toArrayBuffer = (buf) => {
const arrayBuffer = new ArrayBuffer(buf.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return arrayBuffer;
}
const loadGLTFModel = (fileName) => {
const loader = new THREE.GLTFLoader();
return new Promise((resolve, reject) => {
if (fs.existsSync(fileName)) {
const data = fs.readFileSync(fileName);
const arrayBuffer = toArrayBuffer(data);
loader.parse(arrayBuffer, '',
(object3D) => {
resolve(object3D);
},
(error) => {
console.log(error);
reject('Loader failed')
});
} else reject(`Cannot find ${fileName}`);
});
}
console.log(0); // <--- this is logged
loader.parse(arrayBuffer, '',
(object3D) => {
console.log(1); // <--- this is not
resolve(object3D);
},
(error) => {
console.log(error); // <--- neither is this
reject('Loader failed')
});
what was your three version ? since r119 was released on Jul 30, 2020 it must have been r118 or earlier - let me try that… nope, same behavior in r118