Ktx Unity by atteneder - 4

Games & ProjectsSDK

Integration of Basis Universal transcoder into the Unity game engine

Unknown VersionApache License 2.0Updated 17 hours agoCreated on May 27th, 2019
Go to source

KtxUnity

Unity package that allows users to load KTX 2.0 files with Basis Universal super compression or Basis Universal texture files directly.

Following build targets are supported

  • WebGL
  • iOS
  • Android (arm64 and armv7a)
  • Windows (32 and 64 bit)
  • macOS (64 bit)
  • Linux (32 and 64 bit)

Screenshot of loaded fish textures

Thanks to Khronos, Binomial and everyone involved in making KTX and Basis Universal available!

Installing

You have to manually add the package’s URL into your project manifest

Inside your Unity project there’s the folder Packages containing a file called manifest.json. You have to open it and add the following line inside the dependencies category:

"com.atteneder.ktx": "https://gitlab.com/atteneder/ktxunity.git",

It should look something like this:

{
  "dependencies": {
    "com.atteneder.ktx": "https://gitlab.com/atteneder/ktxunity.git",
    "com.unity.package-manager-ui": "2.1.2",
    "com.unity.modules.unitywebrequest": "1.0.0"
    ...
  }
}

Next time you open your project in Unity, it will download the package automatically. You have to have a GIT LFS client (large file support) installed on your system. Otherwise you will get an error that the native library file (dll on Windows) is corrupt. There’s more detail about how to add packages via GIT URLs in the Unity documentation.

Using

There’s a simple demo project that shows how you can use it:

https://github.com/atteneder/KtxUnityDemo

Excerpt how to load a file from StreamingAssets:

using UnityEngine;
using KtxUnity;

public class CustomKtxFileLoader : TextureFileLoader<KtxTexture>
{
    protected override void ApplyTexture(Texture2D texture) {
        var renderer = GetComponent<Renderer>();
        if(renderer!=null && renderer.sharedMaterial!=null) {
            renderer.material.mainTexture = texture;
        }
    }
}

In this simple case the base MonoBehaviour TextureFileLoader has a public filePath member, starts loading in Start and already takes care of all things. You only need to implement the ApplyTexture method and do something with your texture.

TextureFileLoader is generic and can load KTX or Basis Universal files. Depending on what you need, pass KtxTexture or BasisUniversalTexture into its type parameter.

Loading from URLs is similarly easy, just use TextureUrlLoader, which has the exact same interface. In this example we load a Basis Universal texture via URL:

using UnityEngine;
using KtxUnity;

public class CustomBasisUniversalUrlLoader : TextureUrlLoader<BasisUniversalTexture>
{
   protected override void ApplyTexture(Texture2D texture) {
        var renderer = GetComponent<Renderer>();
        if(renderer!=null && renderer.sharedMaterial!=null) {
            renderer.material.mainTexture = texture;
        }
    }
}

Developers who want to create advanced loading code should look into classes KtxTexture/BasisUniversalTexture and TextureBase directly.

Support

Like this demo? You can show your appreciation and …

Buy me a coffee

License

Copyright © 2019 Andreas Atteneder, All Rights Reserved. Licensed under the Apache License, Version 2.0 (the “License”); you may not use files in this repository except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Show all projects by atteneder