gpvigano / AsImpL

Asynchronous Importer and run-time Loader for Unity
MIT License
212 stars 37 forks source link

Load file from URL via UnityWebRequest #40

Closed eldamir closed 4 years ago

eldamir commented 4 years ago

New features and suggestions

Context: I am writing an app for displaying building models. The app will be built for the WebGL target. In WebGL, I have two limitations that this library does not handle, even as it works exceedingly well on desktop!

  1. I don't have access to a file system. I want to read the obj and mtl file from HTTP request.
  2. I don't have a lot of available memory. I need to try to be conservative with memory usage, or the WebGL build will break. I will load the model via UnityWebRequest with a custom DownloadHandlerScript, which has a pre-allocated buffer for handling the response content in chunks, in order to not take up too much memory and not deal with reallocating many arrays and trigger GC all the time (similar to what you'll find at the bottom of this page)

From what I can tell from the code at the moment, a filepath is always expected, and so this behaviour is not supported. I would love some pointers for anything else that might impede my progress here. Are you using namespaces from .NET, which are not supported in WebGL build, etc.?

If the path forward here is relatively clear, I wouldn't mind writing the extension myself and submitting a pull request. I just need as many pointers and hints as you can give, so I don't head down a dead end

eldamir commented 4 years ago

Oh, I forgot. As I don't want to keep the entire file in memory at a time, but only load in chunks, it also affects how you can parse the file. For instance, you cannot count the amount of objects to load. You can only count how many bytes you have processed compared to the ContentLength that the server has yielded.

As far as I've been able to see and test from dealing with the OBJ format, it is possible to process objects/groups ("o" and "g") one at a time. Only problem is that faces ("f") and other structures use vertex indices to determine locations, so we would need to keep a running tally of what index we're at. For instance:

We basically just need to have a counter running to make sure each chunk knows where the indices start.

It also means that chunks must be processed in sequence in order. That is no problem though.

eldamir commented 4 years ago

Too quick to comment maybe. I see there is functionality to load from server. So maybe I just need to put in some options for pre-allocated buffers and memory control here and there.

If you have comments for me, please let me hear it. Otherwise, this can probably be closed. The ReadMe could feature some more text on how different sources are supported 😄

gpvigano commented 4 years ago

Maybe the sentence "3D models can be loaded both from files and from URLs" in the README is too cryptic, I guess some more documentation or even specific tutorials could be useful. Thank you for your suggestion, unfortunately I cannot work on it at the moment. I created the issue #41 as a reminder...