diff --git a/Assets/VRM10/Runtime/IO/MToonAdapter.cs b/Assets/VRM10/Runtime/IO/MToonAdapter.cs index 4d4f94e00..15151d0e3 100644 --- a/Assets/VRM10/Runtime/IO/MToonAdapter.cs +++ b/Assets/VRM10/Runtime/IO/MToonAdapter.cs @@ -1,147 +1,15 @@ using System; using System.Collections.Generic; -using System.Numerics; using VrmLib.MToon; using VrmLib; using UniGLTF; using UniGLTF.Extensions.VRMC_materials_mtoon; -using UniJSON; + namespace UniVRM10 { public static class MToonAdapter { - // for debug - static readonly Vector4 Nan = new Vector4(float.NaN, float.NaN, float.NaN, float.NaN); - - static RenderMode GetRenderMode(string alphaMode, bool isTransparentWithZWrite) - { - switch (alphaMode) - { - case "OPAQUE": return RenderMode.Opaque; - case "MASK": return RenderMode.Cutout; - case "BLEND": - { - if (isTransparentWithZWrite) - { - return RenderMode.TransparentWithZWrite; - } - else - { - return RenderMode.Transparent; - } - } - } - - throw new NotImplementedException(); - } - - public static MToonMaterial MToonFromGltf(glTFMaterial material, List textures, VRMC_materials_mtoon extension) - { - var mtoon = new MToonMaterial(material.name); - - var Meta = new MetaDefinition - { - Implementation = "Santarh/MToon", - }; - var Color = new ColorDefinition - { - LitColor = material.pbrMetallicRoughness.baseColorFactor.ToLinearColor(Nan), - LitMultiplyTexture = material.pbrMetallicRoughness.baseColorTexture?.index.GetTexture(textures), - ShadeColor = extension.ShadeFactor.ToLinearColor(Nan), - ShadeMultiplyTexture = extension.ShadeMultiplyTexture.GetTexture(textures), - CutoutThresholdValue = material.alphaCutoff, - }; - var Outline = new OutlineDefinition - { - OutlineColorMode = (VrmLib.MToon.OutlineColorMode)extension.OutlineColorMode, - OutlineColor = extension.OutlineFactor.ToLinearColor(Nan), - OutlineLightingMixValue = extension.OutlineLightingMixFactor.Value, - OutlineScaledMaxDistanceValue = extension.OutlineScaledMaxDistanceFactor.Value, - OutlineWidthMode = (VrmLib.MToon.OutlineWidthMode)extension.OutlineWidthMode, - OutlineWidthValue = extension.OutlineWidthFactor.Value, - OutlineWidthMultiplyTexture = extension.OutlineWidthMultiplyTexture.GetTexture(textures), - }; - var Emission = new EmissionDefinition - { - EmissionColor = material.emissiveFactor.ToLinearColor(Nan), - }; - if (material.emissiveTexture != null) - { - Emission.EmissionMultiplyTexture = material.emissiveTexture.index.GetTexture(textures); - } - - var Lighting = new LightingDefinition - { - LightingInfluence = new LightingInfluenceDefinition - { - GiIntensityValue = extension.GiIntensityFactor.Value, - LightColorAttenuationValue = extension.LightColorAttenuationFactor.Value, - }, - LitAndShadeMixing = new LitAndShadeMixingDefinition - { - ShadingShiftValue = extension.ShadingShiftFactor.Value, - ShadingToonyValue = extension.ShadingToonyFactor.Value, - }, - Normal = new NormalDefinition - { - }, - }; - if (material.normalTexture != null) - { - Lighting.Normal.NormalScaleValue = material.normalTexture.scale; - Lighting.Normal.NormalTexture = material.normalTexture.index.GetTexture(textures); - } - - var MatCap = new MatCapDefinition - { - AdditiveTexture = extension.AdditiveTexture.GetTexture(textures) - }; - var Rendering = new RenderingDefinition - { - CullMode = material.doubleSided ? CullMode.Off : CullMode.Back, - RenderMode = GetRenderMode(material.alphaMode, extension.TransparentWithZWrite.Value), - RenderQueueOffsetNumber = extension.RenderQueueOffsetNumber.Value, - }; - var Rim = new RimDefinition - { - RimColor = extension.RimFactor.ToLinearColor(Nan), - RimMultiplyTexture = extension.RimMultiplyTexture.GetTexture(textures), - RimLiftValue = extension.RimLiftFactor.Value, - RimFresnelPowerValue = extension.RimFresnelPowerFactor.Value, - RimLightingMixValue = extension.RimLightingMixFactor.Value, - }; - - var TextureOption = new TextureUvCoordsDefinition - { - UvAnimationMaskTexture = extension.UvAnimationMaskTexture.GetTexture(textures), - UvAnimationRotationSpeedValue = extension.UvAnimationRotationSpeedFactor.Value, - UvAnimationScrollXSpeedValue = extension.UvAnimationScrollXSpeedFactor.Value, - UvAnimationScrollYSpeedValue = extension.UvAnimationScrollYSpeedFactor.Value, - }; - - if (glTF_KHR_texture_transform.TryGet(material.pbrMetallicRoughness.baseColorTexture, out glTF_KHR_texture_transform t)) - { - TextureOption.MainTextureLeftBottomOriginOffset = t.offset.ToVector2(); - TextureOption.MainTextureLeftBottomOriginScale = t.scale.ToVector2(); - } - - mtoon.Definition = new MToonDefinition - { - Meta = Meta, - Color = Color, - Outline = Outline, - Emission = Emission, - Lighting = Lighting, - MatCap = MatCap, - Rendering = Rendering, - Rim = Rim, - TextureOption = TextureOption, - }; - - return mtoon; - } - static (string, bool) GetRenderMode(RenderMode mode) { switch (mode) diff --git a/Assets/VRM10/Runtime/IO/MaterialAdapter.cs b/Assets/VRM10/Runtime/IO/MaterialAdapter.cs index d931fa903..ac618a1da 100644 --- a/Assets/VRM10/Runtime/IO/MaterialAdapter.cs +++ b/Assets/VRM10/Runtime/IO/MaterialAdapter.cs @@ -9,108 +9,6 @@ namespace UniVRM10 { public static class MaterialAdapter { - public static Material FromGltf(this glTFMaterial x, List textures) - { - if (UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(x.extensions, - out UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon mtoon)) - { - // mtoon - return MToonAdapter.MToonFromGltf(x, textures, mtoon); - } - - if (glTF_KHR_materials_unlit.IsEnable(x)) - { - // unlit - return UnlitFromGltf(x, textures); - } - - // PBR - return PBRFromGltf(x, textures); - } - - public static void LoadCommonParams(this Material self, glTFMaterial material, List textures) - { - var pbr = material.pbrMetallicRoughness; - if (pbr.baseColorFactor != null) - { - self.BaseColorFactor = LinearColor.FromLiner(pbr.baseColorFactor); - } - var baseColorTexture = pbr.baseColorTexture; - if (baseColorTexture != null && baseColorTexture.index.TryGetValidIndex(textures.Count, out int index)) - { - self.BaseColorTexture = new TextureInfo(textures[index]); - } - - self.AlphaMode = EnumUtil.Parse(material.alphaMode); - self.AlphaCutoff = material.alphaCutoff; - self.DoubleSided = material.doubleSided; - } - - public static PBRMaterial PBRFromGltf(glTFMaterial material, List textures) - { - var self = new PBRMaterial(material.name); - - self.LoadCommonParams(material, textures); - - // - // pbr - // - var pbr = material.pbrMetallicRoughness; - - // metallic roughness - self.MetallicFactor = pbr.metallicFactor; - self.RoughnessFactor = pbr.roughnessFactor; - var metallicRoughnessTexture = pbr.metallicRoughnessTexture; - if (metallicRoughnessTexture != null - && metallicRoughnessTexture.index.TryGetValidIndex(textures.Count, out int metallicRoughnessTextureIndex)) - { - self.MetallicRoughnessTexture = textures[metallicRoughnessTextureIndex]; - } - // - // emissive - // - if (material.emissiveFactor != null) - { - self.EmissiveFactor = new Vector3( - material.emissiveFactor[0], - material.emissiveFactor[1], - material.emissiveFactor[2]); - } - var emissiveTexture = material.emissiveTexture; - if (emissiveTexture != null - && emissiveTexture.index.TryGetValidIndex(textures.Count, out int emissiveTextureIndex)) - { - self.EmissiveTexture = textures[emissiveTextureIndex]; - } - // - // normal - // - var normalTexture = material.normalTexture; - if (normalTexture != null - && normalTexture.index.TryGetValidIndex(textures.Count, out int normalTextureIndex)) - { - self.NormalTexture = textures[normalTextureIndex]; - } - // - // occlusion - // - var occlusionTexture = material.occlusionTexture; - if (occlusionTexture != null - && occlusionTexture.index.TryGetValidIndex(textures.Count, out int occlusionTextureIndex)) - { - self.OcclusionTexture = textures[occlusionTextureIndex]; - } - - return self; - } - - public static UnlitMaterial UnlitFromGltf(glTFMaterial material, List textures) - { - var unlit = new UnlitMaterial(material.name); - unlit.LoadCommonParams(material, textures); - return unlit; - } - static string CastAlphaMode(VrmLib.AlphaModeType alphaMode) { if (alphaMode == AlphaModeType.BLEND_ZWRITE) diff --git a/Assets/VRM10/Runtime/IO/ModelLoader.cs b/Assets/VRM10/Runtime/IO/ModelLoader.cs index a7bf1c4ab..271be9fbe 100644 --- a/Assets/VRM10/Runtime/IO/ModelLoader.cs +++ b/Assets/VRM10/Runtime/IO/ModelLoader.cs @@ -45,15 +45,6 @@ namespace UniVRM10 } } - // image - model.Images.AddRange(Enumerable.Range(0, storage.ImageCount).Select(x => storage.CreateImage(x))); - - // texture - model.Textures.AddRange(Enumerable.Range(0, storage.TextureCount).Select(x => storage.CreateTexture(x, model.Images))); - - // material - model.Materials.AddRange(Enumerable.Range(0, storage.MaterialCount).Select(x => storage.CreateMaterial(x, model.Textures))); - // skin model.Skins.AddRange(Enumerable.Range(0, storage.SkinCount).Select(x => storage.CreateSkin(x, model.Nodes))); diff --git a/Assets/VRM10/Runtime/IO/TextureAdapter.cs b/Assets/VRM10/Runtime/IO/TextureAdapter.cs index d5c6f187c..ef09d36e2 100644 --- a/Assets/VRM10/Runtime/IO/TextureAdapter.cs +++ b/Assets/VRM10/Runtime/IO/TextureAdapter.cs @@ -7,24 +7,6 @@ namespace UniVRM10 { public static class TextureAdapter { - public static ImageTexture FromGltf(this glTFTexture x, glTFTextureSampler sampler, List images, Texture.ColorSpaceTypes colorSpace, Texture.TextureTypes textureType) - { - var image = images[x.source]; - var name = !string.IsNullOrEmpty(x.name) ? x.name : image.Name; - return new ImageTexture(name, sampler.FromGltf(), image, colorSpace, textureType); - } - - public static TextureSampler FromGltf(this glTFTextureSampler sampler) - { - return new TextureSampler - { - WrapS = (TextureWrapType)sampler.wrapS, - WrapT = (TextureWrapType)sampler.wrapT, - MinFilter = (TextureMinFilterType)sampler.minFilter, - MagFilter = (TextureMagFilterType)sampler.magFilter, - }; - } - public static glTFTextureSampler ToGltf(this TextureSampler src) { return new glTFTextureSampler @@ -36,4 +18,4 @@ namespace UniVRM10 }; } } -} \ No newline at end of file +} diff --git a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs index 1b547f323..2745d5f44 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Numerics; using System.Runtime.InteropServices; using VrmLib; -using UniJSON; namespace UniVRM10 @@ -319,14 +318,6 @@ namespace UniVRM10 } } - public void CreateBufferAccessorAndAdd(int? accessorIndex, VertexBuffer b, string key) - { - if (accessorIndex.HasValue) - { - CreateBufferAccessorAndAdd(accessorIndex.Value, b, key); - } - } - public void CreateBufferAccessorAndAdd(int accessorIndex, VertexBuffer b, string key) { var a = CreateAccessor(accessorIndex); @@ -373,12 +364,8 @@ namespace UniVRM10 public int NodeCount => Gltf.nodes.Count; - public int ImageCount => Gltf.images.Count; - public int TextureCount => Gltf.textures.Count; - public int MaterialCount => Gltf.materials.Count; - public int SkinCount => Gltf.skins.Count; public int MeshCount => Gltf.meshes.Count; @@ -432,114 +419,6 @@ namespace UniVRM10 } } - public Image CreateImage(int index) - { - return Gltf.images[index].FromGltf(this); - } - - /// - /// sRGB でないテクスチャーを検出する - /// - /// - /// - private (Texture.TextureTypes, UniGLTF.glTFMaterial) GetTextureType(int textureIndex) - { - foreach (var material in Gltf.materials) - { - if (UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(material.extensions, - out UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon mtoon)) - { - if (material.normalTexture?.index == textureIndex) return (Texture.TextureTypes.NormalMap, material); - } - else if (UniGLTF.glTF_KHR_materials_unlit.IsEnable(material)) - { - } - else - { - if (material.pbrMetallicRoughness?.baseColorTexture?.index == textureIndex) return (Texture.TextureTypes.Default, material); - if (material.pbrMetallicRoughness?.metallicRoughnessTexture?.index == textureIndex) return (Texture.TextureTypes.MetallicRoughness, material); - if (material.occlusionTexture?.index == textureIndex) return (Texture.TextureTypes.Occlusion, material); - if (material.emissiveTexture?.index == textureIndex) return (Texture.TextureTypes.Emissive, material); - if (material.normalTexture?.index == textureIndex) return (Texture.TextureTypes.NormalMap, material); - } - } - - return (Texture.TextureTypes.Default, null); - } - - private Texture.ColorSpaceTypes GetTextureColorSpaceType(int textureIndex) - { - foreach (var material in Gltf.materials) - { - if (UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(material.extensions, - out UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon mtoon)) - { - // mtoon - if (material.pbrMetallicRoughness.baseColorTexture.index == textureIndex) return Texture.ColorSpaceTypes.Srgb; - if (mtoon.ShadeMultiplyTexture == textureIndex) return Texture.ColorSpaceTypes.Srgb; - if (material.emissiveTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Srgb; - if (mtoon.RimMultiplyTexture == textureIndex) return Texture.ColorSpaceTypes.Srgb; - if (mtoon.AdditiveTexture == textureIndex) return Texture.ColorSpaceTypes.Srgb; - - if (mtoon.OutlineWidthMultiplyTexture == textureIndex) return Texture.ColorSpaceTypes.Linear; - if (mtoon.UvAnimationMaskTexture == textureIndex) return Texture.ColorSpaceTypes.Linear; - - if (material.normalTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Linear; - } - else if (UniGLTF.glTF_KHR_materials_unlit.IsEnable(material)) - { - // unlit - if (material.pbrMetallicRoughness.baseColorTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Srgb; - } - else - { - // Pbr - if (material.pbrMetallicRoughness?.baseColorTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Srgb; - if (material.pbrMetallicRoughness?.metallicRoughnessTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Linear; - if (material.occlusionTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Linear; - if (material.emissiveTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Srgb; - if (material.normalTexture?.index == textureIndex) return Texture.ColorSpaceTypes.Linear; - } - } - - return Texture.ColorSpaceTypes.Srgb; - } - - public Texture CreateTexture(int index, List images) - { - var texture = Gltf.textures[index]; - var textureType = GetTextureType(index); - var colorSpace = GetTextureColorSpaceType(index); - - var sampler = (texture.sampler >= 0 && texture.sampler < Gltf.samplers.Count) - ? Gltf.samplers[texture.sampler] - : new UniGLTF.glTFTextureSampler() - ; - - if (textureType.Item1 == Texture.TextureTypes.MetallicRoughness && textureType.Item2.pbrMetallicRoughness != null) - { - var roughnessFactor = textureType.Item2.pbrMetallicRoughness.roughnessFactor; - var name = !string.IsNullOrEmpty(texture.name) ? texture.name : images[texture.source].Name; - return new MetallicRoughnessImageTexture( - name, - sampler.FromGltf(), - images[texture.source], - roughnessFactor, - colorSpace, - textureType.Item1); - } - else - { - return texture.FromGltf(sampler, images, colorSpace, textureType.Item1); - } - } - - public Material CreateMaterial(int index, List textures) - { - var x = Gltf.materials[index]; - return x.FromGltf(textures); - } - public Skin CreateSkin(int index, List nodes) { var x = Gltf.skins[index]; @@ -586,11 +465,6 @@ namespace UniVRM10 return (meshIndex, skinIndex); } - public Meta CreateVrmMeta(List textures) - { - return gltfVrm.Meta.FromGltf(textures); - } - public ArraySegment GetBufferBytes(UniGLTF.glTFBufferView bufferView) { if (!bufferView.buffer.TryGetValidIndex(Gltf.buffers.Count, out int bufferViewBufferIndex)) diff --git a/Assets/VRM10/Tests/SerializationTests.cs b/Assets/VRM10/Tests/SerializationTests.cs index 09f15c95f..80ab2d612 100644 --- a/Assets/VRM10/Tests/SerializationTests.cs +++ b/Assets/VRM10/Tests/SerializationTests.cs @@ -204,28 +204,28 @@ namespace UniVRM10 // gltf <= json var deserialized = UniGLTF.GltfDeserializer.Deserialize_gltf_materials_LIST(jsonMaterial.ParseAsJson()); - // vrmlib <= gltf - var loaded = deserialized.FromGltf(textures); - // var context = ModelDiffContext.Create(); - // ModelDiffExtensions.MaterialEquals(context, vrmLibMaterial, loaded); - // var diff = context.List - // .Where(x => !s_ignoreKeys.Contains(x.Context)) - // .ToArray(); - // if (diff.Length > 0) + // // vrmlib <= gltf + // var loaded = deserialized.FromGltf(textures); + // // var context = ModelDiffContext.Create(); + // // ModelDiffExtensions.MaterialEquals(context, vrmLibMaterial, loaded); + // // var diff = context.List + // // .Where(x => !s_ignoreKeys.Contains(x.Context)) + // // .ToArray(); + // // if (diff.Length > 0) + // // { + // // Debug.LogWarning(string.Join("\n", diff.Select(x => $"{x.Context}: {x.Message}"))); + // // } + // // Assert.AreEqual(0, diff.Length); + + // // <= vrmlib + // var map = new Dictionary(); + // var dst = UniVRM10.RuntimeUnityMaterialBuilder.CreateMaterialAsset(loaded, hasVertexColor: false, map); + // dst.name = src.name; + + // if (sameShader) // { - // Debug.LogWarning(string.Join("\n", diff.Select(x => $"{x.Context}: {x.Message}"))); + // CompareUnityMaterial(src, dst); // } - // Assert.AreEqual(0, diff.Length); - - // <= vrmlib - var map = new Dictionary(); - var dst = UniVRM10.RuntimeUnityMaterialBuilder.CreateMaterialAsset(loaded, hasVertexColor: false, map); - dst.name = src.name; - - if (sameShader) - { - CompareUnityMaterial(src, dst); - } } [Test]