atteneder / KtxUnity

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

Model import crashes the Unity Editor #52

Closed seyfeb closed 2 years ago

seyfeb commented 2 years ago

When importing a glTF model in Unity using glTFast and the KtxUnity package the Unity editor crashes. This is observed when KtxUnity is added via the file system (either the current main branch or the commit tagged 2.0.1. If I add the package via git URL glTFast claims that the KtxUnity package is not even available (see the issue in the glTFast repo https://github.com/atteneder/glTFast/issues/381).

Expected behavior

The model is imported correctly. Addtitionally, a model import should never crash the editor or a runtime application, even for completely broken models.

OS: macOS Unity editor version: 2021.2.19f1 KtxUnity version: main / 2.0.1 Test model: https://github.com/KhronosGroup/glTF/files/4132437/Avocado.zip

package.json

    "com.atteneder.gltfast": "4.6.0",
    "com.atteneder.ktx": "file:../mypackages/KtxUnity",

editor.log

Start importing Assets/Avocado/Avocado.gltf using Guid(a7299cc32fc4d43a3856f49f0bc3efcf) Importer(-1,00000000000000000000000000000000) Assertion failed: (colorModel == KHR_DF_MODEL_UASTC), function ktxTexture2_TranscodeBasis, file /Users/runner/work/KTX-Software-Unity/KTX-Software-Unity/KTX-Software/lib/basis_transcode.cpp, line 177.
Obtained 0 stack frames.
Launching bug reporter
Assertion failed: (colorModel == KHR_DF_MODEL_UASTC), function ktxTexture2_TranscodeBasis, file /Users/runner/work/KTX-Software-Unity/KTX-Software-Unity/KTX-Software/lib/basis_transcode.cpp, line 177.
Obtained 0 stack frames.
Launching bug reporter
Assertion failed: (colorModel == KHR_DF_MODEL_UASTC), function ktxTexture2_TranscodeBasis, file /Users/runner/work/KTX-Software-Unity/KTX-Software-Unity/KTX-Software/lib/basis_transcode.cpp, line 177.
Obtained 0 stack frames.
atteneder commented 2 years ago

@seyfeb Thanks for reporting!

I can reproduce the issue. I tried to view this file in two alternative glTF viewers and they also threw errors.

Looking at the KTX file (via ktxinfo) I can see that it apparently has an incorrect model (KHR_DF_MODEL_RGBSDA). They were created with gltfpack 0.13, which is rather old. Have you tried re-encoding the original glTF with an up-to-date version of gltfpack ?

Where does this file come from? I presume it's an attachment of an older GH thread, right? Is there a link to its context?

atteneder commented 2 years ago

Nevermind, I figured it's from here.

Don't use this asset. It was created during development of KTX-Software and is probably outdated.

seyfeb commented 2 years ago

Thanks for looking into this. Reconverting this with a current version indeed fixes the error, however the result is somewhat strange:

Before conversion (right), and after conversion (left) Screen Shot 2022-04-08 at 12 40 20

Converted using gltfpack -i Avocado.gltf -o gltfpacked/Avocado.gltf -tc

The textures here in seem okay

Screen Shot 2022-04-08 at 12 42 01

Anyway, I would still be worried using KtxUnity if it crashes the editor (and also any app at Runtime?) only because the input was malformed. Is there a way to prevent this from happening?

atteneder commented 2 years ago

This is a known Unity issue. Use gltfpack's -noq parameter to avoid the troublesome quantization.

seyfeb commented 2 years ago

Good to know it’s a different issue. While having the other one I thought it might be related...

So the only thing that remains here is the question how to avoid the crash at all (even for malformed data)!

Btw, thanks for the work you put into these open-source libraries!

atteneder commented 2 years ago

@seyfeb You're right about crashes are not a reasonable reaction to corrupt data. Since the assertion is inside the third party code, I forwarded the request in a discussion. Let's see what their answer is.

MarkCallow commented 2 years ago

@atteneder how come you are using a debug version of libktx in Unity? The encoders, especially ETC1S, are much slower when compiled in debug mode.

atteneder commented 2 years ago

@MarkCallow That's a great question! I assumed asserts are active in release builds as well, but that's not true. Turns out my macOS build was misconfigured. After re-building the lib, libktx returns a proper error and does not crash anymore.

Thanks for pointing it out!