I have a simple GLB file compressed using “glTF-Transform”, which loads fine in GLTF Viewer. I’ve attached the model for reference.
But when I extract the contents into a string and attempt to parse it using “loader.parse”, I get the following error:
Error: Malformed buffer data: -1
The code I’m using is:
var enc = new TextEncoder().encode(glb_string);
loader.parse(enc.buffer, '', function(object){
console.log(object);
}, function(error){
console.log(error);
});
When loading the GLB model directly with the loader, it also loads fine.
So is it the case that the GLTF loader’s parse function is unable to load compressed GLB models?
The ArrayBuffer passed to GLTFLoader.parse cannot be an encoded string; the underlying data must be binary. When you “extract the contents into a string”, how are you doing that? If it’s Base64 encoding, you must decode it before use. If it’s any other string encoding then that’s probably a mistake.
If you want a string representation of a GLB then Base64 in a Data URI is probably the way to go – you can pass that Data URI directly to the load function. But be aware than Base64 is less efficient than a binary representation and will add ~33% size, and some additional parsing time.
At the moment I’m simply opening the GLB file in my code editor, copying the contents into my database and save it as a string, without any encoding just to test.
What would you recommend as the correct way to convert GLB files into Base64 in a Data URI in Node JS?