async void Start()
    {
        // Create a basis universal texture instance
        var texture = new BasisUniversalTexture();

        // Load file from Streaming Assets folder
        var result = await texture.LoadFromStreamingAssets("dachstein.basis");

        if (result != null)
        {
            // Calculate correct size
            var pos  = new Vector2(0, 0);
            var size = new Vector2(result.texture.width, result.texture.height);

            // Flip Sprite, if required
            if (result.orientation.IsXFlipped())
            {
                pos.x   = size.x;
                size.x *= -1;
            }

            if (result.orientation.IsYFlipped())
            {
                pos.y   = size.y;
                size.y *= -1;
            }

            // Create a Sprite and assign it to the Image
            GetComponent <Image>().sprite = Sprite.Create(result.texture, new Rect(pos, size), Vector2.zero);

            // Preserve aspect ratio:
            // Flipping the sprite by making the size x or y negative (above) breaks Image's `Preserve Aspect` feature
            // You can/have to calculate the RectTransform size yourself. Example:

            // Calculate correct size and assign it to the RectTransform
            const float scale = 0.5f; // Set this to whatever size you need it - best make it a serialized class field
            var         rt    = GetComponent <RectTransform>();
            rt.sizeDelta = new Vector2(result.texture.width * scale, result.texture.height * scale);
        }
    }
Esempio n. 2
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()
Esempio n. 3
0
            public IEnumerator CreateTextureAsync(bool linear, Action <Texture2D> onFinish, string mimeType, Action <float> onProgress = null)
            {
                if (!string.IsNullOrEmpty(path))
                {
#if UNITY_EDITOR
                    // Load textures from asset database if we can
                    Texture2D assetTexture = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
                    if (assetTexture != null)
                    {
                        onFinish(assetTexture);
                        if (onProgress != null)
                        {
                            onProgress(1f);
                        }
                        yield break;
                    }
#endif

#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
                    path = "File://" + path;
#endif
                    // TODO: Support linear/sRGB textures
                    using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path, true)) {
                        UnityWebRequestAsyncOperation operation = uwr.SendWebRequest();
                        float progress = 0;
                        while (!operation.isDone)
                        {
                            if (progress != uwr.downloadProgress)
                            {
                                if (onProgress != null)
                                {
                                    onProgress(uwr.downloadProgress);
                                }
                            }
                            yield return(null);
                        }

                        if (onProgress != null)
                        {
                            onProgress(1f);
                        }

                        if (uwr.isNetworkError || uwr.isHttpError)
                        {
                            Debug.LogError("GLTFImage.cs ToTexture2D() ERROR: " + uwr.error + "- " + path);
                        }
                        else
                        {
                            Texture2D tex = DownloadHandlerTexture.GetContent(uwr);
                            tex.name = Path.GetFileNameWithoutExtension(path);
                            onFinish(tex);
                        }
                        uwr.Dispose();
                    }
                }
                else
                {
                    if (mimeType == "image/png" || mimeType == "image/jpg" || mimeType == "image/jpeg")
                    {
                        Texture2D tex = new Texture2D(2, 2, TextureFormat.ARGB32, true, linear);
                        if (!tex.LoadImage(bytes))
                        {
                            yield break;
                        }
                        else
                        {
                            onFinish(tex);
                        }
                    }
                    else if (mimeType == "image/basis")
                    {
                        var nativeArrayBytes = new NativeArray <byte>(bytes, KtxNativeInstance.defaultAllocator);
                        var basisTex         = new BasisUniversalTexture();
                        basisTex.onTextureLoaded += delegate(Texture2D tex, TextureOrientation _) {
                            if (tex == null)
                            {
                                Debug.LogError("tex is null");
                            }
                            else
                            {
                                onFinish(tex);
                            }
                        };
                        IEnumerator en = basisTex.LoadBytesRoutine(nativeArrayBytes, linear);
                        while (en.MoveNext())
                        {
                            yield return(null);
                        }
                        ;
                        nativeArrayBytes.Dispose();
                    }
                    else
                    {
                        Debug.LogError("mimeType not supported");
                    }
                }
            }