donmccurdy / three-gltf-viewer

Drag-and-drop preview for glTF 2.0 models in WebGL using three.js.
https://gltf-viewer.donmccurdy.com/
MIT License
2.1k stars 534 forks source link

import gltf model via query string #236

Closed LorenzWieseke closed 3 years ago

LorenzWieseke commented 3 years ago

Would it be possible to import a gltf file (zip) like the simple dropzone does but using the url #model=

https://gltf-viewer.donmccurdy.com/#model=https://3dit-model-storage.s3.eu-central-1.amazonaws.com/DamagedHelmet.zip

I am trying to convert the simple dropzone to do this but it's a bit tricky for me to understand all the blob stuff happening

Thanks in advance

donmccurdy commented 3 years ago

The #model= parameter is supported in three-gltf-viewer, but doesn't allow ZIP files - you'll need to unpack the hosted file as a .glb or .gltf first. See this example:

https://gltf-viewer.donmccurdy.com/#model=https://storage.googleapis.com/donmccurdy-public/universal-textures/BarramundiFish.original.glb

LorenzWieseke commented 3 years ago

Hey Don, thanks for the reply.

I thought I could use your simple dropdown to unzip a file I downloaded and bring it into the viewer.

I had a look at the object that you pass to the _loadZip

image

So I tried just handing over a file I download

image

Apperently it doesn't work that easy ;) I need to convert what I get to the an object like the one in the first screenshot (of type "application/x-zip-compressed") and if I understand it right, thats the type of object you get from the drop event (e.dataTransfer.files)

Don't know if this is the right place to ask stuff like this but as you can see I'm not realy a pro ;)

It would be really nice to get this to work because then I could use the viewer to import sketchfab files directly.

Best regards, Lorenz

donmccurdy commented 3 years ago

You've found the source code for _loadZip here, I assume?

https://github.com/donmccurdy/simple-dropzone/blob/a5c8b6a7306a560dadbed42ba000768202198930/index.js#L183-L213

The simple-dropzone package doesn't handle anything related to downloading files from the web, so you may just want to throw that out and reuse its unzip implementation.

LorenzWieseke commented 3 years ago

Yes I used the _loadZip from your repo and I know simple-dropzone has nothing to do with downloading files, the name kind of suggests what its used for ;)

I just thought I could use that pipeline to get my downloaded zip unpacked and into the viewer. The problem is to format of the file. You get your file from the event and then pass it to the _loadZip that calls importBlob to read it in and unzip it.

When I try to get my zip using a request I get a string with the binary in it or, using encoding:null, a buffer. So I first tried to use the importText method and pass that string but that gives me an error.

Next I tried to convert it to a blob and don't change your _loadZip method

image

but that just gives me an error as well :

image

I was fiddeling the hole day but don't get how to convert what I get from the request to something that matches the file you get from the drop event.

donmccurdy commented 3 years ago

zip.js needs to be given a Blob or a File instance to decompress. The easiest way to get that from a URL would be to use the fetch API as shown here: https://developer.mozilla.org/en-US/docs/Web/API/Body/blob

If that isn't working I think you may need to share a demo I can reproduce, it is hard to say exactly what is happening from screenshots.

LorenzWieseke commented 3 years ago

Thanks a lot ! Finally found some time to try your solution and it worked !

donmccurdy commented 3 years ago

Glad to hear! :)