vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

Ability to get path of images #47

Closed garrynewman closed 4 years ago

garrynewman commented 4 years ago

When loading a model I want to be able to get the raw path of the image and load it myself, manually. With the current implementation I can't see a way to do that.

vpenades commented 4 years ago

This is by design, to simplify textures loading. this issue has also been discussed here.

Keep in mind that textures can be stored in three different ways, so if you want to load the textures on your own, you must implement the three ways.

In particular, embedded and buffered textures would require you to write quite a lot of code to load the textures.

With the current API, if you want to open the IO.Stream of a given image, you just need to do:

System.IO.Stream stream = model.LogicalImages[ index ].MemoryImage.Open();

And you don't need to worry about how the image is encoded.

In the future, in order to use glTFs for runtime visualization, I am planning to add a load hook that will allow you to receive the images as they're loaded, so you can instantiate the GPU textures, without using additional memory, but I have no time estimation for this.

garrynewman commented 4 years ago

I can understand this, and it's nice and clean like the rest of the API.

From my POV I'm loading the model into another engine. So I want to load the model then create my own materials if they're not already on disk. To do this I need to be able to get the texture and either save it to disk if it's not already a satellite, or just write the name of the texture if it is.

I get that it adds complication and you don't want to muck the API up, but maybe it could be an extension method that accesses internal fields, hidden behind a .Advanced namespace or something?

As it is I have to use reflection to access _uri on the Image.

vpenades commented 4 years ago

Just to be clear....

You just want to get the name of the texture file in case it's an external/satellite file but you'll load the embedded and buffered textures using the API...

Or you want to load and decode all the textures on your own, which includes finding the bufferview and decoding the Mime64 text for embedded textures?

Right now after loading the texture into memory, I clear the _uri to avoid mis management, but I could do some tricks and expose it in another way. That would include checking that the _uri is not an embedded texture (in which case it would be cleared)

garrynewman commented 4 years ago

My engine process would be this

To write the material I want to know whether the texture is on disk. That way I can make my material load the textures straight from the disk instead of re-routing through the model.

This is just so it works in my generic model/material/shader/texture system, which also supports other model formats.

vpenades commented 4 years ago

Okey, so if I understood correctly, you only need to handle the case where the texture is an external file, and when it's not, you'll load it through the proper API.

If that's the case, and given that's a feature that's been already requested, I'll figure out a way of exposing it in the API.

garrynewman commented 4 years ago

<3

vpenades commented 4 years ago

Okey, in the latest update, I've redesigned some of the Image APIs.... now most of the workload is inside MemoryImage.

You have a property called SourcePath that will point to the texture in the file system.

Notice this value will be Null if the image has been loaded from any source other than the file system.

garrynewman commented 4 years ago

<3 thank you!