async void Start()
    {
        // Create KTX texture instance
        var texture = new KtxTexture();

        // Linear color sampling. Needed for non-color value textures (e.g. normal maps)
        bool linearColor = true;

        // Load file from Streaming Assets folder (relative path)
        var result = await texture.LoadFromStreamingAssets("trout.ktx", linearColor);

        // Alternative: Load from URL
        // var result = await texture.LoadFromUrl("https://myserver.com/trout.ktx", linearColor);

        // Alternative: Load from memory
        // var result = await texture.LoadFromBytes(nativeArray, linearColor);

        if (result != null)
        {
            // Use texture. For example, apply texture to a material
            targetMaterial.mainTexture = result.texture;

            // Optional: Support arbitrary texture orientation by flipping the texture if necessary
            var scale = targetMaterial.mainTextureScale;
            scale.x = result.orientation.IsXFlipped() ? -1 : 1;
            scale.y = result.orientation.IsYFlipped() ? -1 : 1;
            targetMaterial.mainTextureScale = scale;
        }
    }
Exemple #2
0
    async void NeverEndingStoryLoop()
    {
        start_time = Time.realtimeSinceStartup;
        if (currentType == ImageType.KTX)
        {
            while (!cancellationTokenSource.IsCancellationRequested)
            {
                var bt     = new KtxTexture();
                var result = await bt.LoadFromBytes(data, this);

                if (cancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
                ApplyTexture(result);
                batch_time = Time.realtimeSinceStartup - start_time;
                await Task.Yield();
            }
        }
        else
        {
            while (!cancellationTokenSource.IsCancellationRequested)
            {
                var texture = new Texture2D(2, 2);
                texture.LoadImage(data.ToArray(), true);
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    break;
                }
                ApplyTexture(new TextureResult(texture, TextureOrientation.UNITY_DEFAULT));
                batch_time = Time.realtimeSinceStartup - start_time;
                await Task.Yield();
            }
        }
    }
 public KtxLoadNativeContext(int index, NativeSlice <byte> slice)
 {
     this.imageIndex = index;
     this.slice      = slice;
     ktxTexture      = new KtxTexture();
     texture         = null;
 }
Exemple #4
0
 public KtxLoadContext(int index, byte[] data)
 {
     this.imageIndex = index;
     this.data       = data;
     ktxTexture      = new KtxTexture();
     texture         = null;
 }
Exemple #5
0
 IEnumerator NeverEndingStory()
 {
     start_time = Time.realtimeSinceStartup;
     if (currentType == ImageType.KTX)
     {
         while (true)
         {
             var bt = new KtxTexture();
             bt.onTextureLoaded += ApplyTexture;
             bt.LoadFromBytes(data, this);
             batch_time = Time.realtimeSinceStartup - start_time;
             yield return(null);
         }
     }
     else
     {
         while (true)
         {
             var texture = new Texture2D(2, 2);
             texture.LoadImage(data.ToArray(), true);
             ApplyTexture(texture);
             batch_time = Time.realtimeSinceStartup - start_time;
             yield return(null);
         }
     }
 }
Exemple #6
0
 void LoadBatch(int count)
 {
     Profiler.BeginSample("LoadBatch");
     start_time  = Time.realtimeSinceStartup;
     batch_count = count;
     batch_time  = -1;
     for (int i = 0; i < count; i++)
     {
         if (currentType == ImageType.KTX)
         {
             var bt = new KtxTexture();
             bt.onTextureLoaded += ApplyTexture;
             bt.LoadFromBytes(data, this);
         }
         else
         {
             var texture = new Texture2D(2, 2);
             texture.LoadImage(data.ToArray(), true);
             ApplyTexture(texture);
         }
     }
     Profiler.EndSample();
 }
Exemple #7
0
    async void LoadBatch(int count)
    {
        Profiler.BeginSample("LoadBatch");
        start_time  = Time.realtimeSinceStartup;
        batch_count = count;
        batch_time  = -1;
        for (int i = 0; i < count; i++)
        {
            if (currentType == ImageType.KTX)
            {
                var bt     = new KtxTexture();
                var result = await bt.LoadFromBytes(data, this);

                ApplyTexture(result);
            }
            else
            {
                var texture = new Texture2D(2, 2);
                texture.LoadImage(data.ToArray(), true);
                ApplyTexture(new TextureResult(texture, TextureOrientation.UNITY_DEFAULT));
            }
        }
        Profiler.EndSample();
    }
Exemple #8
0
            public IEnumerator CreateTextureAsync(bool linear, Action <Texture2D, TextureOrientation> onFinish, string mimeType, Action <float> onProgress = null)
            {
                Texture2D          tex         = new Texture2D(2, 2, TextureFormat.ARGB32, true, linear);
                bool               loaded      = false;
                TextureOrientation orientation = new TextureOrientation();

                //With GLTF, the mimeType stores the path, let's correct that mistake
                if (!string.IsNullOrEmpty(mimeType))
                {
                    if (File.Exists(mimeType))
                    {
                        path     = mimeType;
                        mimeType = "image/" + Path.GetExtension(path).Remove(0, 1);
                    }
                }

                //Use KtxUnity plugin to load ktx/ktx2/basis textures
                if (mimeType == "image/ktx" || mimeType == "image/ktx2" || mimeType == "image/basis")
                {
#if !KTX
                    Debug.LogError("GLTFImage.cs CreateTextureAsync() KTX and basis texture support is not enabled, try enabling 'KTX' scripting define symbol in project settings and make sure KtxUnity plugin is in your project");
                    yield break;
#else
                    NativeArray <byte> data = new NativeArray <byte>(bytes, KtxNativeInstance.defaultAllocator);

                    TextureBase textureBase = null;

                    if (mimeType == "image/ktx" || mimeType == "image/ktx2")
                    {
                        textureBase = new KtxTexture();
                    }
                    else if (mimeType == "image/basis")
                    {
                        textureBase = new BasisUniversalTexture();
                    }

                    textureBase.onTextureLoaded += (Texture2D texture, KtxUnity.TextureOrientation ktxOrientation) =>
                    {
                        orientation.IsXFlipped = ktxOrientation.IsXFlipped();
                        orientation.IsYFlipped = ktxOrientation.IsYFlipped();
                        tex = texture;

                        //Rename the texture if we have a valid path variable (not available with .glb)
                        if (!string.IsNullOrEmpty(path))
                        {
                            tex.name = Path.GetFileNameWithoutExtension(path);
                        }
                        //
                        if (tex.name == "material1_normal")
                        {
                            Debug.Log("OnTextureLoaded() " + tex.name + "[ " + tex.width + " x " + tex.height + " ] Flipped[ " + orientation.IsXFlipped + " : " + orientation.IsYFlipped + " ]");
                        }
                        //
                        loaded = true;
                    };

                    yield return(StaticCoroutine.Start(textureBase.LoadBytesRoutine(data, true)));

                    data.Dispose();
#endif
                }
                else                 //Load .jpg, .jpeg, .png textures
                {
                    orientation.IsXFlipped = false;
                    orientation.IsYFlipped = false;
                    tex.LoadImage(bytes);
                    loaded = true;
                }

                yield return(new WaitUntil(() =>
                {
                    /*
                     * if( tex.name == "material1_basecolor" )
                     *      Debug.Log( "WaitUntil() " + tex.name + "[ loaded = " + loaded + " ][ " + tex.width + " x " + tex.height + " ]" );
                     */
                    return loaded;
                }));

                if (tex != null)
                {
                    //Rename the texture if we have a valid path variable (not available with .glb)
                    if (!string.IsNullOrEmpty(path))
                    {
                        tex.name = Path.GetFileNameWithoutExtension(path);
                    }

                    /*
                     * if( tex.name == "material1_basecolor" )
                     *      Debug.Log( "OnFinish() Transcoded " + tex.name + "[" + mimeType + "][ " + bytes.Length + " ][ " + tex.width + " x " + tex.height + " ]" );
                     */
                    onFinish(tex, orientation);
                }
                else
                {
                    Debug.Log("OnFinish() Unable To Transcode " + tex.name + "[" + mimeType + "][ " + bytes.Length + " ]");
                }
            }     //END CreateTextureAsync()