vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
454 stars 72 forks source link

How to Load glb from stream? #178

Closed CeSun closed 1 year ago

CeSun commented 1 year ago

I use the SharpGLTF library in Android. The glb file is stored in the asstes directory of the apk. It needs to use the android API to open the stream, so the following code cannot work:

var model = SharpGLTF.Schema2.ModelRoot.Load("model.gltf");

I need an api for parsing glb files from a stream, does SharpGLTF support it?

The expected code is as follows:

var stream = Asstes.Open("abc.glb");   // android api
var model = SharpGLTF.Schema2.ModelRoot.LoadFromStream(stream);
vpenades commented 1 year ago
var model = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream);
CeSun commented 1 year ago
var model = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream);

It will throw exception:

System.NotSupportedException: 'Specified method is not supported.'
  at Android.Runtime.InputStreamInvoker.get_Length() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/InputStreamInvoker.cs:line 137
   at SharpGLTF.Schema2._BinarySerialization._ReadBinaryHeader(BinaryReader binaryReader)
   at SharpGLTF.Schema2._BinarySerialization.ReadBinaryFile(Stream stream)
   at SharpGLTF.Schema2.ReadContext._ReadGLB(Stream stream)
   at SharpGLTF.Schema2.ReadContext.ReadBinarySchema2(Stream stream)
   at SharpGLTF.Schema2.ModelRoot.ReadGLB(Stream stream, ReadSettings settings)
vpenades commented 1 year ago

I guess you're opening a stream from a network? the opened stream needs to be seekable, so it must be opened from a file or from memory.

CeSun commented 1 year ago

It's android platform's stream, open file from assets. It looks like I have to copy from the file stream to the memory stream first...

vpenades commented 1 year ago

the library needs to know the length of the stream; it's weird that being a local asset, the stream fails to give that information... anyway, I'll take a look to see if I can improve binary reading for these cases.