Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

KTX2 & Basis Textures

Discussion in 'Web' started by DerrickBarra, Nov 4, 2020.

  1. DerrickBarra

    DerrickBarra

    Joined:
    Nov 19, 2013
    Posts:
    210
    Our team is using @atteneder's awesome KtxUnity plugin to load up .basis textures at runtime. This new file type is a supercompressed texture, which means you only need to build it once, and then an importer can convert it into the GPU texture for any platform with almost no performance cost (perfect for WebGL).

    You can create .basis textures relatively easily using basisu. Just make sure to flip the texture in their export settings, otherwise, you'll have to flip it in Unity (@atteneder gives examples of this in their repo).

    .basis textures can be used independently or inside of the new .ktx2 container format. The original .ktx format supported multiple GPU textures inside of it at once, and the new .ktx2 format supports .basis texture as well. Usually, if you see a .ktx2 in the wild with no other explanation, it will contain a .basis texture inside of it.

    KtxUnity does not support loading of GPU ready textures from a .ktx/.ktx2 container, it's intended for .basis payloads only or direct .basis file loading.

    Currently, in Unity WebGL, we have access to DXT1/5 compression for desktop, but on mobile, these textures convert to uncompressed RGBA32, so they're non-performant for mobile devices.

    If you're running out of RAM in WebGL Mobile, your texture memory is a likely culprit.

    Note that KtxUnity doesn't bring full editor integration, so you won't be able to link to these files directly in the editor, you'll have to load them via code.

    If you have a specific question about KtxUnity or find a bug, report it to @atteneder at the repo directly.

    But for anything else, I've been researching the subject so ask away.
     
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Great writeup!

    One thing I would add, is

    This may not be the case. In our experience, long music clips are the most reported cause of running out of memory in WebGL mobile builds. This is because of these unresolved issues in Web Audio API:

    https://github.com/WebAudio/web-audio-api-v2/issues/18
    https://github.com/WebAudio/web-audio-api-v2/issues/11
    https://github.com/WebAudio/web-audio-api/issues/1305

    These add up, and can cause a surprising several hundred megabytes of excess memory usage in a web build. Whoops. We are looking towards AudioWorklets and WebCodecs spec areas, but unfortunately those are not widespread enough yet.
     
    gtk2k, De-Panther and DerrickBarra like this.
  3. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    I'm the author of KtxUnity

    @DerrickBarra Thanks for the promo!

    KtxUnity tries to find the best available option when it comes to target transcode format. On mobile this usually is ETC or PVRTC. Uncompressed is only a last resort fallback. If you end up with uncompressed formats where you should not, report an issue and give details about the hardware/software/graphic API you used.
     
    gtk2k and De-Panther like this.
  4. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    If this is supported by the editor, wouldn't it take longer to switch platforms?
     
  5. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    Not sure if I get the question right. Please elaborate what you mean.

    The package handles run-time import of KTX 2.0 textures with supercompression, which are target platform independent. This means switching platforms doesn‘t require re-encoding ktx2 or basis files.
    Depending on platform and device, the final texture format that the ktx2/basis file is transcoded into can vary, but that‘s happening at runtime and is very quick.

    Hth
     
  6. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    Thanx.

    When switching the platform of the editor, the conversion process of resources including textures is executed, which takes a lot of time, isn't it?
    Is it possible to switch without this conversion process with Basis Universal? I thought.
     
  7. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    Yes.

    ktx2 and basis files are not imported by the Editor (like regular image formats). In fact Unity doesn't know those file types and will ignore them. You will have to prepare the ktx2/basis file yourself in advance though (but just once).

    It would be a nice feature to be able to encode/compress any image texture into ktx2/basis from within the Editor (see this issue).
     
    liudgervr and gtk2k like this.
  8. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    I want a method-style transcoder like this.
    Code (CSharp):
    1. var loader = new BasisUniversalLoader();
    2. var tex = loader.loadFromUrl(url);
    3. material.mainTexture = tex;
     
  9. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    Sounds like a good idea!
    Place feature requests here:
    https://github.com/atteneder/KtxUnity/issues/new
     
    gtk2k likes this.
  10. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    tteneder likes this.
  11. miko_unity

    miko_unity

    Joined:
    May 31, 2021
    Posts:
    2
    Hello everyone,

    I'm currently interested in using ktx2/basis texture format for our WebGL mobile build. I have tried using both (compressed using basisu with & without -ktx2 option) and I still get
    Warning: RGBA Compressed DXT5 UNorm Format is not supported, decompressing texture.

    Does this mean that ktx2/basis texture format will be transcoded into DXT by default for WebGL regardless of the GPU support?
    In memory consumption point of view, is this the same as if we are using DXT texture from the start or will ktx2/basis use less memory?
     
    Last edited: May 31, 2021
  12. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    The supported texture formats are(should be) detected at runtime and the best match is chosen (with fallback to uncompressed ones). See `TextureBase.GetFormat`.

    If you think the format is incorrect, you could debug this method or raise an issue including the texture and full details about the runtime (hardware/GPU, OS, browser + version).
     
  13. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Current Unity versions do not recognize compressed texture formats for mobile. This should improve for Unity 2021.2, there the different mobile compressed formats should be possible to activate.
     
    atteneder likes this.
  14. miko_unity

    miko_unity

    Joined:
    May 31, 2021
    Posts:
    2
  15. DonCornholio

    DonCornholio

    Joined:
    Feb 27, 2017
    Posts:
    92
    So should i switch to the current Unity 2021.2 Version to have ktx Textures loading properly for mobiles in a Unity WebGL Build, or is this yet to come ?
    Is it possible to just replace the current textures in the materials with ktx/basisu ones, or how else could i go about it?
     
    Last edited: Jun 1, 2021
  16. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    The support for mobile textures is not yet in Unity 2021.2 alpha, but it should land in to Unity 2021.2 beta. After that Unity WebGL builds should advertise ETC1, ETC2, PVRTC and ASTC textures as being supported, so if the Ktx plugin attempts to create textures in those formats, that should work out ok.
     
    OceanX000 and DerrickBarra like this.
  17. DonCornholio

    DonCornholio

    Joined:
    Feb 27, 2017
    Posts:
    92
    Thanks for the reply ! Will ktx textures also be supported like regular textures in 2021.2 beta ? Will i be able to just slap them onto my materials?
     
  18. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Unfortunately there won't be a built-in Ktx support just yet in Unity 2021.2, but that is something that is being looked into.
     
  19. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    114
    in the release notes for 2021.2a19 it says:
    "WebGL: Enabled ETC/ETC2/ASTC/BC4/BC5/BC6/BC7 compressed texture formats for WebGL in editor, build and runtime."
    Does that mean Ktx compressed textures picking the right format for mobile might be ...working now?
     
    tteneder likes this.
  20. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    It (KtxUnity) should, but I'd have to test first. Feel free to go ahead and test though. I'd be glad for feedback.
     
  21. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    114
    Can ktx textures be generated from and used as lightmaps?
     
  22. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    KTX encoding is not supported in Unity (via KtxUnity) at the moment. There's a chance it'll get added, as it would be useful for other use-cases as well. There's no timeline though.

    I hardly have experience with lightmaps so I wouldn't know if KTX could be used for it. I'm optimistic that it can be done though.
     
  23. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    114
    So I tried ktx textures as lightmaps, the problem I'm having is the alpha channel is blocky (in 4x4 blocks) which isn't playing well with the RGBM encoding on the lightmaps. Think that's just a limitation of the compression?

     
    Last edited: Jul 6, 2021
  24. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    Basis Universal super-compressed KTX2 files support two different compressions. ETC1s and UASTC. I have the suspicion that you used ETC1s (which is lower size and quality). Please try the (higher quality and size) UASTC mode and see if it works for you.
     
  25. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    @tteneder Congrats on getting hired by unity!

    We also use your plugin in our enterprise webgl application and its awesome, please continue supporting it and working on v3.0.0 as its a god send !

    Especially for people who cant stay on bleeding edge unity!
     
    d1favero and tteneder like this.
  26. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    174
    Thanks for the kind words! Always great to know people put the packages to good use :)
     
    MadeFromPolygons likes this.