Пример #1
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);
        }
Пример #2
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);
        }