GLTFUtility icon indicating copy to clipboard operation
GLTFUtility copied to clipboard

Direct .basisu image support

Open daverin opened this issue 3 years ago • 9 comments

Support for direct (non-KTX container) .basis format.

Uses atteneder/KtxUnity (needs to be included in manifest.json since unity does not resolve git dependencies.

My team needed this for a proof-of-concept glTF pipeline in Unity. Without it, loading glTF/glb files with large textures creates enormous stalls on the main thread and causes excessive RAM usage due to png/jpg expansion.

glb/gltf files with web encoded images (png/jpg) can be converted to ones with embedded basis images using one of the following tools:

daverin avatar Oct 05 '20 17:10 daverin

Thanks for your submission @daverin, our team is testing out your pull request with our own model and ran into an issue with KtxUnity failing to load the ktx2 textures from a glb. (returns a null texture reference)

Would you mind sharing a model your team can confirm works? I'm double-checking this with @atteneder from KtxUnity as well, but I saw your pull request and figured you might have a confirmed working .glb with ktx2 textures.

DerrickBarra avatar Nov 11 '20 18:11 DerrickBarra

Hi @DerrickBarra

I couldn't get KTX2 to work. I received a reply from @atteneder on an issue from KtxUnity confirming that it's resolved but I haven't had a chance to test it yet.

For now, my team is using .basis textures directly and we're happy.

here is a sample project. Checkout SampleSceneGltf. The sample assets have their basis images compressed with different flags. There is also a sample with basis and draco.

daverin avatar Nov 12 '20 12:11 daverin

@daverin Thanks for the links! I just tested loading .glb files with .basis textures from your repo (hosted on our AWS), with the same result (returned texture is null).

Avocado - GLB & Basis Gladiator - GLB & Basis

We're using KtxUnity 0.9.1 (latest release). Interesting that the older version you are using is working (the newest version has a TextureOrientation value returned as well as the final texture). I suppose it's possible that these files were made with an outdated version of the basis specification, but there's also a chance that something new in KtxUnity broke support.

If it's not too much of a bother, do you think you could see if the latest KtxUnity works with the files on your machine? Debugging what plugin or file has issues is pretty hard, so to hear that someone else has it working is exciting. If it breaks for you against the newest version of KtxUnity then I'll let @atteneder know.

DerrickBarra avatar Nov 12 '20 15:11 DerrickBarra

@daverin So I found out why KtxUnity 0.9.1 doesn't work with the code from this branch. For some reason the use of TextureBase.LoadBytesRoutine() causes the resulting texture to be null.

If I change the code to use TextureBase.LoadFromBytes(), the textures load as expected (.ktx2 and .basis work).

Basically I made a script called CoroutineHolder in the GLTFUtility plugin folder, and I spawn that or look it up so I can pass it in before I call TextureBase.LoadFromBytes()

Also btw the code in this branch could be updated to support .ktx2, since that also works in the latest version of KtxUnity.

DerrickBarra avatar Nov 12 '20 21:11 DerrickBarra

Hi @DerrickBarra

I can relate. Trying to pinpoint where these errors are located is pretty hard. I've encountered this issue before. On our GitLab fork of the repo, I managed to solve it.

The error you're seeing is the result of this enumerator not being run. It seems co-routines can't be directly yielded in the ImportTask if it is run asynchronously.

I created two potential fixes for it. I'm not knowledgable enough in co-routines to understand which one is better:

  • https://github.com/Beamm-Incorporated/KtxUnity/tree/fix/enumerator-logic
  • https://github.com/Beamm-Incorporated/KtxUnity/tree/fix/merge-enumerators

I also updated the sample project and my fork Beamm-Incorporated/KtxUnity to use the latest release of KtxUnity.

daverin avatar Nov 12 '20 21:11 daverin

@daverin Thanks! I let @atteneder know about this bug. Glad to see there are some possible fixes.

DerrickBarra avatar Nov 12 '20 21:11 DerrickBarra

Hi folks,

First of all thanks for putting KtxUnity to good use!

@daverin both your solutions seem workaroundish.

Make sure you understand how to synchronously call nested coroutines. I recommend this article.

That's why I always keep a monobehaviour reference around to call StartCoroutine on.

The long-term solution for KtxUnity is to replace Coroutines by async methods (since that should be DOTS compatible), but there's no timeline for it yet.

atteneder avatar Nov 13 '20 00:11 atteneder

Thanks @atteneder 🙌🏾

daverin avatar Nov 13 '20 16:11 daverin

@daverin Check out our forked version of GLTFUtility.

It has a dependency on KtxUnity and StaticCoroutine (along with the 'KTX' scripting define symbol). But I confirmed that it works with .KTX2 and .Basis texture loading withing unzipped .gltf archives and .glb files. It also properly flips the textures via the material UV settings.

The use of StaticCoroutine could be removed if we required that the user passes in a monobehaviour when they call the initial loading logic. Our team uses StaticCoroutine for other stuff, but that wouldn't be a hard change to make if you care to.

DerrickBarra avatar Nov 17 '20 13:11 DerrickBarra