Loading GLB/GLTF model from String

Hi, I am trying to load a GLTF model into the script.js file. I was able to achieve this by using loader.load by feeding it a URL. However, I don’t want to put my model online and my project won’t run on a server so I can’t import objects from files inside the project folder. How can I achieve this? I know that there’s a parse function in GLTFLoader but since the model has armatures and textures, GLTF file is generated with binary and texture images, so I don’t know if I can put the GLTF file’s values into a string, parse it into JSON and loaded it with the loader since the texture and the binary file won’t be found. Can I manually put binary values of GLB into my script and parse it somehow?

Out of curiosity. In what runtime does your code run?

parse takes a binary array buffer, generally you should not use gltf but glb which contains everything as a binary. you need a bundler, pretty much all bundlers can simply import a file (your glb) and transform it via arraybuffer loader. feed that to parse, hit npm run build, and it creates a ready-made dist.

btw, you always need a server, local or remote, there is no way around it, unless you hack chrome with some flags that switch off security features. or you’re running electron, which to me sounds like what you’re after. you won’t be able to import any file otherwise, that’s just not how html works.

This thread may be helpful:

1 Like