Esempio n. 1
0
        public void SaveTexturesAsPng(UnityPath prefabPath)
        {
            TextureBaseDir = prefabPath.Parent;
            TextureBaseDir.ImportAsset();

            //
            // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
            //
            for (int i = 0; i < GLTF.textures.Count; ++i)
            {
                var x     = GLTF.textures[i];
                var image = GLTF.images[x.source];
                if (string.IsNullOrEmpty(image.uri))
                {
                    // glb buffer
                    var folder = prefabPath.GetAssetFolder(".Textures");
                    folder.EnsureFolder();

                    // name & bytes
                    var textureName = !string.IsNullOrEmpty(image.name) ? image.name : string.Format("{0:00}#GLB", i);
                    var byteSegment = GLTF.GetViewBytes(image.bufferView);

                    // path
                    var png = folder.Child(textureName + ".png");
                    File.WriteAllBytes(png.FullPath, byteSegment.ToArray());
                    png.ImportAsset();

                    image.uri = png.Value.Substring(TextureBaseDir.Value.Length + 1);
                    //Debug.LogFormat("image.uri: {0}", image.uri);
                }
            }
            UnityEditor.AssetDatabase.Refresh();
        }
Esempio n. 2
0
        public void GetOrCreateTexture()
        {
#if UNITY_EDITOR
            if (IsAsset)
            {
                //
                // texture from assets
                //
                m_assetPath.ImportAsset();
                Texture = m_assetPath.LoadAsset <Texture2D>();
            }
            else
#endif
            {
                //
                // texture from image(png etc) bytes
                //
                Texture = new Texture2D(2, 2);
                if (m_imageBytes != null)
                {
                    Texture.LoadImage(m_imageBytes);
                }
            }
            Texture.name = m_textureName;
        }
Esempio n. 3
0
        public void GetOrCreateTexture(bool isLinear)
        {
#if UNITY_EDITOR
            if (IsAsset)
            {
                //
                // texture from assets
                //
                m_assetPath.ImportAsset();
                TextureImporter importer = m_assetPath.GetImporter <TextureImporter>();
                importer.sRGBTexture = !isLinear;
                importer.SaveAndReimport();
                m_texture = m_assetPath.LoadAsset <Texture2D>();
            }
            else
#endif
            {
                //
                // texture from image(png etc) bytes
                //
                m_texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
                if (m_imageBytes != null)
                {
                    m_texture.LoadImage(m_imageBytes);
                }
            }
            m_texture.name = m_textureName;
        }
Esempio n. 4
0
        public void Extract(GetTextureParam param, bool hasUri)
        {
            if (Textures.Values.Contains(param))
            {
                return;
            }

            var       subAsset   = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName);
            UnityPath targetPath = default;

            if (hasUri && !param.ExtractConverted)
            {
                var gltfTexture = GLTF.textures[param.Index0.Value];
                var gltfImage   = GLTF.images[gltfTexture.source];
                var ext         = GetExt(gltfImage.mimeType, gltfImage.uri);
                targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}");
            }
            else
            {
                switch (param.TextureType)
                {
                case GetTextureParam.TextureTypes.StandardMap:
                {
                    // write converted texture
                    targetPath = m_textureDirectory.Child($"{param.ConvertedName}.png");
                    File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray());
                    targetPath.ImportAsset();
                    break;
                }

                default:
                {
                    // write original bytes
                    var gltfTexture = GLTF.textures[param.Index0.Value];
                    var gltfImage   = GLTF.images[gltfTexture.source];
                    var ext         = GetExt(gltfImage.mimeType, gltfImage.uri);
                    targetPath = m_textureDirectory.Child($"{param.GltflName}{ext}");
                    File.WriteAllBytes(targetPath.FullPath, GLTF.GetImageBytes(Storage, gltfTexture.source).ToArray());
                    targetPath.ImportAsset();
                    break;
                }
                }
            }

            Textures.Add(targetPath, param);
        }
Esempio n. 5
0
        public static Task <Texture2D> LoadTaskAsync(UnityPath m_assetPath,
                                                     glTF gltf, int textureIndex)
        {
            var textureType = TextureIO.GetglTFTextureType(gltf, textureIndex);
            var colorSpace  = TextureIO.GetColorSpace(textureType);
            var isLinear    = colorSpace == RenderTextureReadWrite.Linear;
            var sampler     = gltf.GetSamplerFromTextureIndex(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    = !isLinear;
                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();
            }

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

            return(Task.FromResult(Texture));
        }
Esempio n. 6
0
        public void Extract(TextureImportParam param)
        {
            if (Textures.Values.Contains(param))
            {
                return;
            }

            UnityPath targetPath = default;

            if (!string.IsNullOrEmpty(param.Uri) && !param.ExtractConverted)
            {
                targetPath = m_textureDirectory.Child(param.GltfFileName);
            }
            else
            {
                switch (param.TextureType)
                {
                case TextureImportTypes.StandardMap:
                {
                    // write converted texture
                    var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName);
                    targetPath = m_textureDirectory.Child(param.ConvertedFileName);
                    File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray());
                    targetPath.ImportAsset();
                    break;
                }

                default:
                {
                    // write original bytes
                    targetPath = m_textureDirectory.Child(param.GltfFileName);
                    File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray());
                    targetPath.ImportAsset();
                    break;
                }
                }
            }

            Textures.Add(targetPath, param);
        }
        public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
        {
            //
            // texture from assets
            //
            m_assetPath.ImportAsset();
            var importer = m_assetPath.GetImporter <TextureImporter>();

            if (importer == null)
            {
                Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
            }
            importer.maxTextureSize = 8192;
            importer.sRGBTexture    = !isLinear;

            importer.SaveAndReimport();

            Texture = m_assetPath.LoadAsset <Texture2D>();

            //Texture.name = m_textureName;
            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();
            }

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

            yield break;
        }
Esempio n. 8
0
        public static Task <Texture2D> LoadTaskAsync(UnityPath m_assetPath,
                                                     bool isLinear, glTFTextureSampler sampler)
        {
            //
            // 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);
            }
            importer.maxTextureSize = 8192;
            importer.sRGBTexture    = !isLinear;

            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();
            }

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

            return(Task.FromResult(Texture));
        }
Esempio n. 9
0
        public IEnumerator ProcessOnMainThread(bool isLinear)
        {
            //
            // texture from assets
            //
            m_assetPath.ImportAsset();
            var importer = m_assetPath.GetImporter <TextureImporter>();

            if (importer == null)
            {
                Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
            }
            importer.sRGBTexture = !isLinear;
            importer.SaveAndReimport();

            Texture = m_assetPath.LoadAsset <Texture2D>();
            //Texture.name = m_textureName;
            if (Texture == null)
            {
                Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath);
            }

            yield break;
        }