Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public VrmLib.TextureInfo GetOrCreateTexture(Texture _texture, VrmLib.Texture.ColorSpaceTypes colorSpace, VrmLib.Texture.TextureTypes textureType)
        {
            var texture = _texture as Texture2D;

            if (texture is null)
            {
                return(null);
            }

            if (!Textures.TryGetValue(texture, out VrmLib.TextureInfo info))
            {
                Material normalConvertMaterial = null;
                if (textureType == VrmLib.Texture.TextureTypes.NormalMap)
                {
                    normalConvertMaterial = TextureConvertMaterial.GetNormalMapConvertUnityToGltf();
                }

                var(bytes, mime) = GetImageEncodedBytes(
                    texture,
                    (colorSpace == VrmLib.Texture.ColorSpaceTypes.Linear) ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB,
                    normalConvertMaterial
                    );

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

                var sampler = new VrmLib.TextureSampler
                {
                    MagFilter = texture.filterMode.ToVrmLibMagFilter(),
                    MinFilter = texture.filterMode.ToVrmLibMinFilter(),
                    WrapS     = texture.wrapMode.ToVrmLib(),
                    WrapT     = texture.wrapMode.ToVrmLib(),
                };
                var image = new VrmLib.Image(texture.name, mime, VrmLib.ImageUsage.None, bytes);
                info = new VrmLib.TextureInfo(new VrmLib.ImageTexture(texture.name, sampler, image, colorSpace, textureType));
                Textures.Add(texture, info);
                Model.Images.Add(image);
                Model.Textures.Add(info.Texture);
            }

            return(info);
        }
Ejemplo n.º 3
0
        public VrmLib.TextureInfo GetOrCreateTexture(Material material, Texture srcTexture, VrmLib.Texture.ColorSpaceTypes colorSpace, VrmLib.Texture.TextureTypes textureType)
        {
            var texture = srcTexture as Texture2D;

            if (texture is null)
            {
                return(null);
            }

            if (!Textures.TryGetValue(texture, out VrmLib.TextureInfo info))
            {
                Material converter = null;
                if (textureType == VrmLib.Texture.TextureTypes.NormalMap)
                {
                    converter = TextureConvertMaterial.GetNormalMapConvertUnityToGltf();
                }
                else if (textureType == VrmLib.Texture.TextureTypes.MetallicRoughness)
                {
                    float smoothness = 0.0f;
                    if (material.HasProperty("_GlossMapScale"))
                    {
                        smoothness = material.GetFloat("_GlossMapScale");
                    }

                    converter = TextureConvertMaterial.GetMetallicRoughnessUnityToGltf(smoothness);
                }
                else if (textureType == VrmLib.Texture.TextureTypes.Occlusion)
                {
                    converter = TextureConvertMaterial.GetOcclusionUnityToGltf();
                }

                var(bytes, mime) = GetImageEncodedBytes(
                    texture,
                    (colorSpace == VrmLib.Texture.ColorSpaceTypes.Linear) ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB,
                    converter
                    );

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

                var sampler = new VrmLib.TextureSampler
                {
                    MagFilter = texture.filterMode.ToVrmLibMagFilter(),
                    MinFilter = texture.filterMode.ToVrmLibMinFilter(),
                    WrapS     = texture.wrapMode.ToVrmLib(),
                    WrapT     = texture.wrapMode.ToVrmLib(),
                };
                var image = new VrmLib.Image(texture.name, mime, VrmLib.ImageUsage.None, bytes);
                info = new VrmLib.TextureInfo(new VrmLib.ImageTexture(texture.name, sampler, image, colorSpace, textureType));
                Textures.Add(texture, info);

                if (Model != null)
                {
                    Model.Images.Add(image);
                    Model.Textures.Add(info.Texture);
                }
            }

            return(info);
        }