Esempio n. 1
0
        public static async Task <Texture2D> LoadTextureAsync(IAwaitCaller awaitCaller, glTF gltf, IStorage storage, int textureIndex)
        {
            var imageBytes = await awaitCaller.Run(() =>
            {
                var imageIndex = gltf.textures[textureIndex].source;
                var segments   = gltf.GetImageBytes(storage, imageIndex);
                return(ToArray(segments));
            });

            //
            // texture from image(png etc) bytes
            //
            var colorSpace = gltf.GetColorSpace(textureIndex);
            var texture    = new Texture2D(2, 2, TextureFormat.ARGB32, false, colorSpace == RenderTextureReadWrite.Linear);

            texture.name = gltf.textures[textureIndex].name;
            if (imageBytes != null)
            {
                texture.LoadImage(imageBytes);
            }

            var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);

            if (sampler != null)
            {
                TextureSamplerUtil.SetSampler(texture, sampler);
            }
            return(texture);
        }
Esempio n. 2
0
        public static Task <Texture2D> LoadTaskAsync(UnityPath m_assetPath,
                                                     glTF gltf, int textureIndex)
        {
            var colorSpace = gltf.GetColorSpace(textureIndex);

            //
            // texture from assets
            //
            m_assetPath.ImportAsset();
            var importer = m_assetPath.GetImporter <UnityEditor.TextureImporter>();

            if (importer == null)
            {
                Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
            }
            else
            {
                importer.maxTextureSize = 8192;
                importer.sRGBTexture    = colorSpace == RenderTextureReadWrite.sRGB;
                importer.SaveAndReimport();
            }

            var Texture = m_assetPath.LoadAsset <Texture2D>();

            if (Texture == null)
            {
                Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath);
            }

            else
            {
                var maxSize = Mathf.Max(Texture.width, Texture.height);

                importer.maxTextureSize
                    = maxSize > 4096 ? 8192 :
                      maxSize > 2048 ? 4096 :
                      maxSize > 1024 ? 2048 :
                      maxSize > 512 ? 1024 :
                      512;

                importer.SaveAndReimport();
            }

            var sampler = gltf.GetSamplerFromTextureIndex(textureIndex);

            if (sampler != null)
            {
                TextureSamplerUtil.SetSampler(Texture, sampler);
            }

            return(Task.FromResult(Texture));
        }