GLTFKit2 icon indicating copy to clipboard operation
GLTFKit2 copied to clipboard

Unable to load KTX2 texture files into iOS Project

Open penchaldev opened this issue 1 year ago • 9 comments

  • Followed the documentation steps mentioned

  • ktx.xcframework added to the GLTFKit2 framework target

  • Trying to load KTX2 file using below code snippet but not able to load it

  • Please provide us any steps are missed here or code need to be updated! Thanks in Advance.

  • `

      private func loadAsset() {
      guard let assetURL = Bundle.main.url(forResource: "array_rgba8_linear",
                                           withExtension: ".ktx2")
      else {
          print("Failed to find asset for URL")
          return
      }
    
      GLTFAsset.load(with: assetURL, options: [:]) { (progress, status, maybeAsset, maybeError, _) in
          DispatchQueue.main.async {
              if status == .complete {
                  self.asset = maybeAsset
              } else if let error = maybeError {
                  print("Failed to load glTF asset: \(error)")
              }
          }
      }
    

    } `

Getting this error : Failed to load glTF asset: Error Domain=com.metalbyexample.gltfkit2 Code=1003 "The asset contains invalid JSON data." UserInfo={NSLocalizedDescription=The asset contains invalid JSON data.}

KTX2 file(Under ZIP file - array_etc1s.ktx2.ZIP) and code : array_etc1s.ktx2.zip KTX2-Samples-main.zip

penchaldev avatar Sep 19 '24 10:09 penchaldev

Getting this error : Failed to load glTF asset: Error Domain=com.metalbyexample.gltfkit2 Code=1003 "The asset contains invalid JSON data." UserInfo={NSLocalizedDescription=The asset contains invalid JSON data.}

penchaldev avatar Sep 19 '24 10:09 penchaldev

The GLTFKit2 framework doesn’t support loading standalone KTX2 files. It only supports loading glTF/GLB files. If you want to load a standalone KTX file, use libktx or some other KTX loader directly.

warrenm avatar Sep 19 '24 16:09 warrenm

Thanks for the quick response warrenm. Do you have any example for iOS libktx or KTX loader? It will help me to proceed. I have tried few iOS KTX loaders but they are not able to load KTX2 formats. Thanks in advance.

penchaldev avatar Sep 20 '24 10:09 penchaldev

I'm not sure what your use case is, but the code in GLTFKTX2Support.m is pretty straightforward and has no additional dependencies besides libktx. If you're using libktx from Swift (with or without a wrapper), I highly recommend writing a C or Objective-C interface to abstract over the libktx API, as using it from Swift gets really gnarly.

warrenm avatar Sep 20 '24 18:09 warrenm

@penchaldev MTKTextureLoader supports KTX2 textures if you are using Metal. Though I'm not sure if there are restrictions on compression formats.

MattGuerrette avatar Sep 20 '24 18:09 MattGuerrette

@MattGuerrette You sure about that? Even if Image I/O notionally knows what KTX2 is, MTKTextureLoader on iOS 18 fails to load every single KTX2 file in this sample repo, so I'm skeptical that it's usable for practical use cases.

warrenm avatar Sep 20 '24 19:09 warrenm

@warrenm Pretty sure, I have a textures sample in this repo: https://github.com/MattGuerrette/Metal which uses MTKTextureLoader to load KTX2 textures.

They use: VK_FORMAT_ASTC_8x8_SRGB_BLOCK as the compression format

Here is the exact source file: https://github.com/MattGuerrette/Metal/blob/main/source/textures/main.cpp

MattGuerrette avatar Sep 20 '24 20:09 MattGuerrette

Fair enough, though I notice that when I use MTKTextureLoader to load your files, they wind up as textures with a pixel format of MTLPixelFormatBGRA8Unorm_sRGB, which seems odd. This sort of spotty support and unpredictable behavior is why I normally steer people away from MTKTextureLoader.

warrenm avatar Sep 20 '24 20:09 warrenm

@warrenm Hmm, interesting. Yeah I only recently added that code to use MTKTextureLoader and made the libktx path optional. I'll probably continue using libktx for the reasons you mention.

MattGuerrette avatar Sep 20 '24 20:09 MattGuerrette

Closing due to lack of activity and being off-topic.

warrenm avatar Sep 25 '24 17:09 warrenm