'Unable to parse STL file in Azure Blob Storage with Three.js

I am trying to use the @azure/storage-blob package to download a STL file blob for use in Three.JS as follows:

const downloadResponse = await containerClient.getBlobClient('models/Large Knob.STL').download();
const downloaded = (
  await streamToBuffer(downloadResponse.readableStreamBody)
).toString(); 
const loader = new STLLoader();
const obj = loader.parse(downloaded);

and I load it in a mesh like this:

const mesh = new Mesh(model, material);
scene.add(mesh);
const renderer = new WebGLRenderer();
renderer.render(scene, camera);

I get the following stack trace:

three.module.js?ffb8:5319 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'center')
    at Sphere.copy (webpack-internal:///./node_modules/three/build/three.module.js:5759:28)
    at Frustum.intersectsObject (webpack-internal:///./node_modules/three/build/three.module.js:12767:13)
    at projectObject (webpack-internal:///./node_modules/three/build/three.module.js:27383:45)
    at projectObject (webpack-internal:///./node_modules/three/build/three.module.js:27428:4)
    at WebGLRenderer.render (webpack-internal:///./node_modules/three/build/three.module.js:27212:3)
    at _callee$ (webpack-internal:///./pages/index.tsx:130:30)
    at tryCatch (webpack-internal:///./node_modules/next/dist/compiled/regenerator-runtime/runtime.js:45:40)
    at Generator.invoke [as _invoke] (webpack-internal:///./node_modules/next/dist/compiled/regenerator-runtime/runtime.js:274:22)
    at Generator.prototype.<computed> [as next] (webpack-internal:///./node_modules/next/dist/compiled/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (webpack-internal:///./pages/index.tsx:35:28)
    at _next (webpack-internal:///./pages/index.tsx:53:17)
    at eval (webpack-internal:///./pages/index.tsx:58:13)
    at new Promise (<anonymous>)
    at eval (webpack-internal:///./pages/index.tsx:50:16)
    at createScene (webpack-internal:///./pages/index.tsx:89:25)
    at Home (webpack-internal:///./pages/index.tsx:209:9)
    at renderWithHooks (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:14985:18)

I am able to successfully load and view the mesh if I use loader.load(<filepath, but as my file is hosted in Azure blob storage, I don't think this is a viable option. Any advice would be greatly appreciated



Solution 1:[1]

I am not sure what STLLoader supports, but if you are running on NodeJS, BlobClient also has downloadToFile() and downloadToBuffer() methods that might be more convenient. Please check the documentation site for more information:

https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/blobclient?view=azure-node-latest#@azure-storage-blob-blobclient-downloadtofile

https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/blobclient?view=azure-node-latest#@azure-storage-blob-blobclient-downloadtobuffer-1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Meng