atteneder / KtxUnity

Load KTX and Basis Universal textures at runtime
Apache License 2.0
221 stars 42 forks source link

How to load a basis file at on demand #23

Closed sarangborude closed 3 years ago

sarangborude commented 4 years ago

Currently the textures is loaded in the Start function, Is it possible that I pass a path to a .basis file and the texture gets loaded on demand in the middle of an execution of a script?

sarangborude commented 4 years ago

Would be nice to have a function Texture 2D texture = LoadBasisTexture()

hybridherbst commented 4 years ago

I think you should be able to do this inside a MonoBehaviour:

var basis = new BasisUniversalTexture();
basis.onTextureLoaded += (tex, orientation) => texture = tex;
basis.LoadFromUrl(absPath, this);

however there seems to currently be a bug with that in the latest version (#21).

atteneder commented 4 years ago

Thanks for your inputs!

Texture 2D texture = LoadBasisTexture()

This implies not just explicit loading, but also synchronous (which can cause frame stalls, so I'd discourage you to do that during game play)

@hybridherbst 's solution is explicit, but async, so that's great.

We should probably extend the Usage section in the README.md to cover this use case.

sarangborude commented 4 years ago

@atteneder My use case requires me to download the textures from an API at run time and then I have to decompress it. I might just decompress in a background thread and that might resolve the stalling issue.

@hybridherbst Thanks, I will try that out.

atteneder commented 3 years ago

My use case requires me to download the textures from an API at run time and then I have to decompress it. I might just decompress in a background thread and that might resolve the stalling issue.

If you load it like suggested, it already IS decompressed in a background thread. That's why I endorsed this solution :)

edit: To be accurate, transcoding is done in a thread. Creating the texture and uploading to GPU is still on main thread, but that's a Unity restriction.

I'd have to expand the 3-line example into context to be used as docs...Then it becomes no more simple than what is there. I'll rethink the docs once other features like #26 come in.

hybridherbst commented 3 years ago

@atteneder maybe a good future thing is to implement background texture GPU upload. It certainly works on Android and iOS, and I think I've seen Windows Desktop implementations, but not sure about the latter. That would definitely speed things up quite a bit on top of the already great gains that glTFast brings.

atteneder commented 3 years ago

@atteneder maybe a good future thing is to implement background texture GPU upload. It certainly works on Android and iOS, and I think I've seen Windows Desktop implementations, but not sure about the latter. That would definitely speed things up quite a bit on top of the already great gains that glTFast brings.

Absolutely!

I presume you mean by using native Graphics APIs (OpenGL,Vulkan,etc.) instead of Unity's API, right? We would then create the Unity Texture object via Texture2D.CreateExternalTexture.

This would probably be faster for two reasons:

Only disadvantage: Needs implementations per Graphics API (Vulkan, Metal, D3D, etc.). KTX-Software comes with native uploads for OpenGL and Vulkan already, so that might be a great starting point.

@hybridherbst Let me know your thoughts and if you meant something different.

Thanks!