예제 #1
0
        /// <summary>
        /// 画像のバイト列からテクスチャを作成する
        /// <summary>
        public static Texture2D CreateTexture(VrmLib.ImageTexture imageTexture)
        {
            Texture2D dstTexture = null;

            UnityEngine.Material convertMaterial = null;
            var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, imageTexture.ColorSpace == VrmLib.Texture.ColorSpaceTypes.Linear);

            texture.LoadImage(imageTexture.Image.Bytes.ToArray());

            // Convert Texture Gltf to Unity
            if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.NormalMap)
            {
                convertMaterial = TextureConvertMaterial.GetNormalMapConvertGltfToUnity();
                dstTexture      = UnityTextureUtil.CopyTexture(
                    texture,
                    GetRenderTextureReadWrite(imageTexture.ColorSpace),
                    convertMaterial);
            }
            else if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.MetallicRoughness)
            {
                var metallicRoughnessImage = imageTexture as VrmLib.MetallicRoughnessImageTexture;
                convertMaterial = TextureConvertMaterial.GetMetallicRoughnessGltfToUnity(metallicRoughnessImage.RoughnessFactor);
                dstTexture      = UnityTextureUtil.CopyTexture(
                    texture,
                    GetRenderTextureReadWrite(imageTexture.ColorSpace),
                    convertMaterial);
            }
            else if (imageTexture.TextureType == VrmLib.Texture.TextureTypes.Occlusion)
            {
                convertMaterial = TextureConvertMaterial.GetOcclusionGltfToUnity();
                dstTexture      = UnityTextureUtil.CopyTexture(
                    texture,
                    GetRenderTextureReadWrite(imageTexture.ColorSpace),
                    convertMaterial);
            }

            if (dstTexture != null)
            {
                if (texture != null)
                {
                    UnityEngine.Object.DestroyImmediate(texture);
                }
                texture = dstTexture;
            }

            if (convertMaterial != null)
            {
                UnityEngine.Object.DestroyImmediate(convertMaterial);
            }

            return(texture);
        }
예제 #2
0
        public void ImportingColorTest()
        {
            {
                var roughnessFactor = 1.0f;
                var src             = CreateMonoTexture(new UnityEngine.Color(1.0f, 1.0f, 1.0f, 1.0f), true);
                var material        = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessGltfToUnity(roughnessFactor);
                var dst             = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 255 : Same metallic (src.r)
                // g <- 0   : (Unused)
                // b <- 0   : (Unused)
                // a <- 0   : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8)
                EqualColor(GetColor(dst), new Color(1.0f, 0, 0, 0));
            }

            {
                var roughnessFactor = 1.0f;
                var src             = CreateMonoTexture(new UnityEngine.Color(1.0f, 0.25f, 1.0f, 1.0f), true);
                var material        = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessGltfToUnity(roughnessFactor);
                var dst             = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 255 : Same metallic (src.r)
                // g <- 0   : (Unused)
                // b <- 0   : (Unused)
                // a <- 128 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8)
                EqualColor(GetColor(dst), new Color(1.0f, 0, 0, 0.5f));
            }

            {
                var roughnessFactor = 0.5f;
                var src             = CreateMonoTexture(new UnityEngine.Color(1.0f, 1.0f, 1.0f, 1.0f), true);
                var material        = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessGltfToUnity(roughnessFactor);
                var dst             = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 255 : Same metallic (src.r)
                // g <- 0   : (Unused)
                // b <- 0   : (Unused)
                // a <- 74 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8)
                EqualColor(GetColor(dst), new Color(1.0f, 0, 0, 0.29289f));
            }

            {
                var roughnessFactor = 0.0f;
                var src             = CreateMonoTexture(new UnityEngine.Color(1.0f, 1.0f, 1.0f, 1.0f), true);
                var material        = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessGltfToUnity(roughnessFactor);
                var dst             = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 255 : Same metallic (src.r)
                // g <- 0   : (Unused)
                // b <- 0   : (Unused)
                // a <- 255 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8)
                EqualColor(GetColor(dst), new Color(1.0f, 0, 0, 1.0f));
            }
        }
예제 #3
0
        /// <summary>
        /// return (bytes, mime string)
        /// </summary>
        static (byte[], string) GetImageEncodedBytes(Texture src, RenderTextureReadWrite renderTextureReadWrite, Material renderMaterial = null)
        {
#if false
            /// 元になるアセットがあればそれを得る(png, jpgのみ)
            var assetPath = UnityEditor.AssetDatabase.GetAssetPath(src);
            if (!string.IsNullOrEmpty(assetPath))
            {
                var mime = GetSupportedMime(assetPath);
                if (!string.IsNullOrEmpty(mime))
                {
                    return(File.ReadAllBytes(assetPath), GetSupportedMime(assetPath));
                }
            }
#endif

            var copy = UnityTextureUtil.CopyTexture(src, renderTextureReadWrite, renderMaterial);
            return(copy.EncodeToPNG(), "image/png");
        }
예제 #4
0
        public void ExportingColorTest()
        {
            {
                var smoothness = 1.0f;
                var src        = CreateMonoTexture(new UnityEngine.Color(1.0f, 1.0f, 1.0f, 1.0f), true);
                var material   = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessUnityToGltf(smoothness);
                var dst        = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 0   : (Unused)
                // g <- 0   : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8)
                // b <- 255 : Same metallic (src.r)
                // a <- 255 : (Unused)
                EqualColor(GetColor(dst), new Color(0, 0, 1.0f, 1.0f));
            }

            {
                var smoothness = 0.5f;
                var src        = CreateMonoTexture(new UnityEngine.Color(1.0f, 1.0f, 1.0f, 1.0f), true);
                var material   = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessUnityToGltf(smoothness);
                var dst        = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 0   : (Unused)
                // g <- 63  : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8)
                // b <- 255 : Same metallic (src.r)
                // a <- 255 : (Unused)
                EqualColor(GetColor(dst), new Color(0, 0.25f, 1.0f, 1.0f));
            }

            {
                var smoothness = 0.0f;
                var src        = CreateMonoTexture(new UnityEngine.Color(1.0f, 1.0f, 1.0f, 1.0f), true);
                var material   = UniVRM10.TextureConvertMaterial.GetMetallicRoughnessUnityToGltf(smoothness);
                var dst        = UnityTextureUtil.CopyTexture(src, RenderTextureReadWrite.Linear, material);
                // r <- 0   : (Unused)
                // g <- 255 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8)
                // b <- 255 : Same metallic (src.r)
                // a <- 255 : (Unused)
                EqualColor(GetColor(dst), new Color(0, 1.0f, 1.0f, 1.0f));
            }
        }