Example #1
0
        public void ProcessOnAnyThread(glTF gltf, IStorage storage)
        {
            var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
            var segments   = gltf.GetImageBytes(storage, imageIndex, out m_textureName);

            m_imageBytes = ToArray(segments);
        }
Example #2
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);
        }
Example #3
0
        public void GetImageBytes(glTF gltf, IStorage storage)
        {
            if (IsAsset)
            {
                return;
            }

            var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);

            m_imageBytes = ToArray(gltf.GetImageBytes(storage, imageIndex, out m_textureName));
        }
        /// <summary>
        /// Extract images from glb or gltf out of Assets folder.
        /// </summary>
        /// <param name="prefabPath"></param>
        public void ExtranctImages(UnityPath prefabPath)
        {
            var prefabParentDir = prefabPath.Parent;

            // glb buffer
            var folder = prefabPath.GetAssetFolder(".Textures");

            //
            // https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
            //
            int created = 0;

            //for (int i = 0; i < GLTF.textures.Count; ++i)
            for (int i = 0; i < GLTF.images.Count; ++i)
            {
                folder.EnsureFolder();

                //var x = GLTF.textures[i];
                var image = GLTF.images[i];
                var src   = Storage.GetPath(image.uri);
                if (UnityPath.FromFullpath(src).IsUnderAssetsFolder)
                {
                    // asset is exists.
                }
                else
                {
                    string textureName;
                    var    byteSegment = GLTF.GetImageBytes(Storage, i, out textureName);

                    // path
                    var dst = folder.Child(textureName + image.GetExt());
                    File.WriteAllBytes(dst.FullPath, byteSegment.ToArray());
                    dst.ImportAsset();

                    // make relative path from PrefabParentDir
                    image.uri = dst.Value.Substring(prefabParentDir.Value.Length + 1);
                    ++created;
                }
            }

            if (created > 0)
            {
                AssetDatabase.Refresh();
            }

            CreateTextureItems(prefabParentDir);
        }
        public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler)
        {
            var gltfTexture = gltf.textures[m_textureIndex];
            var bytes       = gltf.GetImageBytes(storage, gltfTexture.source);

            // tmp file
            var tmp = Path.GetTempFileName();

            using (var f = new FileStream(tmp, FileMode.Create))
            {
                f.Write(bytes.Array, bytes.Offset, bytes.Count);
            }

            using (var d = new Deleter(tmp))
            {
                var url = "file:///" + tmp.Replace("\\", "/");
                Debug.LogFormat("UnityWebRequest: {0}", url);
#if UNITY_2017_1_OR_NEWER
                using (var m_uwr = UnityWebRequestTexture.GetTexture(url, true))
                {
                    yield return(m_uwr.SendWebRequest());

                    if (m_uwr.isNetworkError || m_uwr.isHttpError)
                    {
                        Debug.LogWarning(m_uwr.error);
                    }
                    else
                    {
                        // Get downloaded asset bundle
                        Texture      = ((DownloadHandlerTexture)m_uwr.downloadHandler).texture;
                        Texture.name = m_textureName;
                    }
                }
#elif UNITY_5
                using (var m_uwr = new WWW(url))
                {
                    yield return(m_uwr);

                    // wait for request
                    while (!m_uwr.isDone)
                    {
                        yield return(null);
                    }

                    if (!string.IsNullOrEmpty(m_uwr.error))
                    {
                        Debug.Log(m_uwr.error);
                        yield break;
                    }

                    // Get downloaded asset bundle
                    Texture      = m_uwr.textureNonReadable;
                    Texture.name = m_textureName;
                }
#else
#error Unsupported Unity version
#endif
            }
            if (sampler != null)
            {
                TextureSamplerUtil.SetSampler(Texture, sampler);
            }
        }
Example #6
0
        public static ArraySegment <Byte> GetImageBytesFromTextureIndex(this glTF self, IStorage storage, int textureIndex)
        {
            var imageIndex = self.textures[textureIndex].source;

            return(self.GetImageBytes(storage, imageIndex));
        }