예제 #1
0
        public void TextureExportTest()
        {
            // Dummy texture
            var tex0 = new Texture2D(128, 128)
            {
                wrapMode   = TextureWrapMode.Clamp,
                filterMode = FilterMode.Trilinear,
            };
            var textureExporter = new TextureExporter(new EditorTextureSerializer());

            var material = new Material(Shader.Find("Standard"));

            material.mainTexture = tex0;

            var materialExporter = new MaterialExporter();

            materialExporter.ExportMaterial(material, textureExporter, new GltfExportSettings());

            var exported = textureExporter.Export();

            var(convTex0, colorSpace) = exported[0];
            var sampler = TextureSamplerUtil.Export(convTex0);

            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);

            Assert.AreEqual(FilterMode.Trilinear, convTex0.filterMode);
            Assert.IsTrue(convTex0.mipmapCount > 1);
            // Tirilinear => LINEAR_MIPMAP_LINEAR
            Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter);
            Assert.AreEqual(glFilter.LINEAR, sampler.magFilter);
        }
예제 #2
0
        /// <summary>
        /// gltf に texture を足す
        ///
        /// * textures
        /// * samplers
        /// * images
        /// * bufferViews
        ///
        /// を更新し、textures の index を返す
        ///
        /// </summary>
        /// <param name="gltf"></param>
        /// <param name="bufferIndex"></param>
        /// <param name="texture"></param>
        /// <returns>gltf texture index</returns>
        public static int PushGltfTexture(this glTF gltf, int bufferIndex, Texture2D texture)
        {
            var bytesWithMime = GetBytesWithMime(texture);

            // add view
            var view      = gltf.buffers[bufferIndex].Append(bytesWithMime.bytes, glBufferTarget.NONE);
            var viewIndex = gltf.AddBufferView(view);

            // add image
            var imageIndex = gltf.images.Count;

            gltf.images.Add(new glTFImage
            {
                name       = GetTextureParam.RemoveSuffix(texture.name),
                bufferView = viewIndex,
                mimeType   = bytesWithMime.mine,
            });

            // add sampler
            var samplerIndex = gltf.samplers.Count;
            var sampler      = TextureSamplerUtil.Export(texture);

            gltf.samplers.Add(sampler);

            // add texture
            var textureIndex = gltf.textures.Count;

            gltf.textures.Add(new glTFTexture
            {
                sampler = samplerIndex,
                source  = imageIndex,
            });

            return(textureIndex);
        }
        public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
        {
            // tmp file
            var tmp = Path.GetTempFileName();

            using (var f = new FileStream(tmp, FileMode.Create))
            {
                f.Write(m_segments.Array, m_segments.Offset, m_segments.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);
            }
        }
예제 #4
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);
        }
예제 #5
0
        /// <summary>
        /// gltf に texture を足す
        ///
        /// * textures
        /// * samplers
        /// * images
        /// * bufferViews
        ///
        /// を更新し、textures の index を返す
        ///
        /// もっとも根本の Exporter クラスのみが呼び出すべきである。
        /// 他の拡張機能などが呼び出すべきではない。
        ///
        /// </summary>
        /// <returns>gltf texture index</returns>
        public static int PushGltfTexture(ExportingGltfData data, Texture2D texture, ColorSpace textureColorSpace, ITextureSerializer textureSerializer)
        {
            var bytesWithMime = textureSerializer.ExportBytesWithMime(texture, textureColorSpace);

            // add view
            var viewIndex = data.ExtendBufferAndGetViewIndex(bytesWithMime.bytes);

            // add image
            var imageIndex = data.GLTF.images.Count;

            data.GLTF.images.Add(new glTFImage
            {
                name       = TextureImportName.RemoveSuffix(texture.name),
                bufferView = viewIndex,
                mimeType   = bytesWithMime.mime,
            });

            // add sampler
            var samplerIndex = data.GLTF.samplers.Count;
            var sampler      = TextureSamplerUtil.Export(texture);

            data.GLTF.samplers.Add(sampler);

            // add texture
            var textureIndex = data.GLTF.textures.Count;

            data.GLTF.textures.Add(new glTFTexture
            {
                sampler = samplerIndex,
                source  = imageIndex,
            });

            return(textureIndex);
        }
예제 #6
0
        public void TextureExportTest()
        {
            // Dummy texture
            var tex0 = new Texture2D(128, 128)
            {
                wrapMode   = TextureWrapMode.Clamp,
                filterMode = FilterMode.Trilinear,
            };
            var textureManager = new TextureExporter(AssetTextureUtil.IsTextureEditorAsset);

            var material = new Material(Shader.Find("Standard"));

            material.mainTexture = tex0;

            var materialExporter = new MaterialExporter();

            materialExporter.ExportMaterial(material, textureManager);

            var(convTex0, colorSpace) = textureManager.Exported[0];
            var sampler = TextureSamplerUtil.Export(convTex0);

            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
            Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);
            Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter);
            Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter);
        }
예제 #7
0
파일: TextureIO.cs 프로젝트: artv93/UniVRM
        public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType)
        {
            var bytesWithMime = GetBytesWithMime(texture, textureType);;

            // add view
            var view      = gltf.buffers[bufferIndex].Append(bytesWithMime.Bytes, glBufferTarget.NONE);
            var viewIndex = gltf.AddBufferView(view);

            // add image
            var imageIndex = gltf.images.Count;

            gltf.images.Add(new glTFImage
            {
                name       = texture.name,
                bufferView = viewIndex,
                mimeType   = bytesWithMime.Mime,
            });

            // add sampler
            var samplerIndex = gltf.samplers.Count;
            var sampler      = TextureSamplerUtil.Export(texture);

            gltf.samplers.Add(sampler);

            // add texture
            gltf.textures.Add(new glTFTexture
            {
                sampler = samplerIndex,
                source  = imageIndex,
            });

            return(imageIndex);
        }
 public void TextureSamplerExportTest_for_Unity_2017_1()
 {
     {
         var texture = Resources.Load <Texture2D>("Samplers/wrap_clamp");
         var sampler = TextureSamplerUtil.Export(texture);
         Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
         Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);
     }
     {
         var texture = Resources.Load <Texture2D>("Samplers/wrap_mirror");
         var sampler = TextureSamplerUtil.Export(texture);
         Assert.AreEqual(glWrap.MIRRORED_REPEAT, sampler.wrapS);
         Assert.AreEqual(glWrap.MIRRORED_REPEAT, sampler.wrapT);
     }
     {
         // ?
         var texture = Resources.Load <Texture2D>("Samplers/wrap_mirroronce");
         var sampler = TextureSamplerUtil.Export(texture);
         Assert.AreEqual(glWrap.MIRRORED_REPEAT, sampler.wrapS);
         Assert.AreEqual(glWrap.MIRRORED_REPEAT, sampler.wrapT);
     }
     {
         var texture = Resources.Load <Texture2D>("Samplers/wrap_repeat");
         var sampler = TextureSamplerUtil.Export(texture);
         Assert.AreEqual(glWrap.REPEAT, sampler.wrapS);
         Assert.AreEqual(glWrap.REPEAT, sampler.wrapT);
     }
     {
         var texture = Resources.Load <Texture2D>("Samplers/wrap_repeat_clamp");
         var sampler = TextureSamplerUtil.Export(texture);
         Assert.AreEqual(glWrap.REPEAT, sampler.wrapS);
         Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);
     }
 }
        public void TextureSamplerImportTest_for_Unity_5_6()
        {
            Assert.AreEqual(
                new[] { TextureSamplerUtil.TypeWithMode(TextureSamplerUtil.TextureWrapType.All, TextureWrapMode.Repeat) },
                TextureSamplerUtil.GetUnityWrapMode(new glTFTextureSampler
            {
            })
                );

            Assert.AreEqual(
                new[] { TextureSamplerUtil.TypeWithMode(TextureSamplerUtil.TextureWrapType.All, TextureWrapMode.Clamp) },
                TextureSamplerUtil.GetUnityWrapMode(new glTFTextureSampler
            {
                wrapS = glWrap.CLAMP_TO_EDGE
            })
                );

            Assert.AreEqual(
                new[] { TextureSamplerUtil.TypeWithMode(TextureSamplerUtil.TextureWrapType.All, TextureWrapMode.Repeat) },
                TextureSamplerUtil.GetUnityWrapMode(new glTFTextureSampler
            {
                wrapS = glWrap.REPEAT
            })
                );
        }
예제 #10
0
 public static (SubAssetKey, TextureDescriptor) CreateSRGB(GltfData data, int textureIndex, Vector2 offset, Vector2 scale)
 {
     var gltfTexture = data.GLTF.textures[textureIndex];
     var gltfImage   = data.GLTF.images[gltfTexture.source];
     var name        = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfTexture.name, gltfImage.uri);
     var sampler     = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex);
     GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GetImageBytesFromTextureIndex(textureIndex)));
     var param = new TextureDescriptor(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default);
 public void TextureSamplerImportFilterModeTest()
 {
     Assert.AreEqual(FilterMode.Bilinear, TextureSamplerUtil.ImportFilterMode(glFilter.NONE));
     Assert.AreEqual(FilterMode.Bilinear, TextureSamplerUtil.ImportFilterMode(glFilter.LINEAR));
     Assert.AreEqual(FilterMode.Trilinear, TextureSamplerUtil.ImportFilterMode(glFilter.LINEAR_MIPMAP_LINEAR));
     Assert.AreEqual(FilterMode.Bilinear, TextureSamplerUtil.ImportFilterMode(glFilter.LINEAR_MIPMAP_NEAREST));
     Assert.AreEqual(FilterMode.Point, TextureSamplerUtil.ImportFilterMode(glFilter.NEAREST));
     Assert.AreEqual(FilterMode.Point, TextureSamplerUtil.ImportFilterMode(glFilter.NEAREST_MIPMAP_LINEAR));
     Assert.AreEqual(FilterMode.Point, TextureSamplerUtil.ImportFilterMode(glFilter.NEAREST_MIPMAP_NEAREST));
     Assert.AreEqual(FilterMode.Bilinear, TextureSamplerUtil.ImportFilterMode(glFilter.NONE));
 }
예제 #12
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));
        }
예제 #13
0
        public IEnumerator ProcessOnMainThreadCoroutine(glTF gltf)
        {
            using (m_textureLoader)
            {
                var textureType = TextureIO.GetglTFTextureType(gltf, m_textureIndex);
                var colorSpace  = TextureIO.GetColorSpace(textureType);
                var isLinear    = colorSpace == RenderTextureReadWrite.Linear;
                yield return(m_textureLoader.ProcessOnMainThread(isLinear));

                TextureSamplerUtil.SetSampler(Texture, gltf.GetSamplerFromTextureIndex(m_textureIndex));
            }
        }
 public void TextureSamplerExportFilterModeTest()
 {
     {
         var texture = Resources.Load <Texture2D>("Samplers/filter_point");
         Assert.AreEqual(glFilter.NEAREST, TextureSamplerUtil.ExportFilterMode(texture));
     }
     {
         var texture = Resources.Load <Texture2D>("Samplers/filter_bilinear");
         Assert.AreEqual(glFilter.LINEAR, TextureSamplerUtil.ExportFilterMode(texture));
     }
     {
         var texture = Resources.Load <Texture2D>("Samplers/filter_trilinear");
         Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, TextureSamplerUtil.ExportFilterMode(texture));
     }
 }
 public IEnumerator ProcessOnMainThread(bool isLinear, glTFTextureSampler sampler)
 {
     //
     // texture from image(png etc) bytes
     //
     Texture      = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
     Texture.name = m_textureName;
     if (m_imageBytes != null)
     {
         Texture.LoadImage(m_imageBytes);
     }
     if (sampler != null)
     {
         TextureSamplerUtil.SetSampler(Texture, sampler);
     }
     yield break;
 }
예제 #16
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));
        }
        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;
        }
        public void TextureSamplerExportTest_for_Unity_5_6()
        {
            {
                var texture = Resources.Load <Texture2D>("Samplers/wrap_clamp");
                var sampler = TextureSamplerUtil.Export(texture);
                Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapS);
                Assert.AreEqual(glWrap.CLAMP_TO_EDGE, sampler.wrapT);
            }
            {
                var texture = Resources.Load <Texture2D>("Samplers/wrap_repeat");
                var sampler = TextureSamplerUtil.Export(texture);
                Assert.AreEqual(glWrap.REPEAT, sampler.wrapS);
                Assert.AreEqual(glWrap.REPEAT, sampler.wrapT);
            }

            /*
             * {
             *  var texture = Resources.Load<Texture2D>("Samplers/wrap_repeat_clamp");
             *  var sampler = TextureSamplerUtil.Export(texture);
             *  Assert.AreEqual(glWrap.REPEAT, sampler.wrapS);
             *  Assert.AreEqual(glWrap.REPEAT, sampler.wrapT);
             * }
             */
        }
예제 #19
0
 public void SetSampler(glTF gltf)
 {
     TextureSamplerUtil.SetSampler(m_texture, gltf.GetSamplerFromTextureIndex(m_textureIndex));
 }