diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MaterialExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MaterialExporter.cs index 613971d76..34570b527 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MaterialExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MaterialExporter.cs @@ -61,33 +61,44 @@ namespace UniGLTF int index = -1; if (m.HasProperty("_MetallicGlossMap")) { - index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), new MetallicRoughnessConverter()); + float smoothness = 0.0f; + if (m.HasProperty("_GlossMapScale")) + { + smoothness = m.GetFloat("_GlossMapScale"); + } + + // Bake smoothness values into a texture. + var converter = new MetallicRoughnessConverter(smoothness); + index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter); if (index != -1) { - material.pbrMetallicRoughness.metallicRoughnessTexture = new glTFMaterialMetallicRoughnessTextureInfo() - { - index = index, - }; + material.pbrMetallicRoughness.metallicRoughnessTexture = + new glTFMaterialMetallicRoughnessTextureInfo() + { + index = index, + }; } } - if (index != -1 && m.HasProperty("_GlossMapScale")) + if (index != -1) { material.pbrMetallicRoughness.metallicFactor = 1.0f; - material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_GlossMapScale"); + // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. + material.pbrMetallicRoughness.roughnessFactor = 1.0f; } - else - { + else { if (m.HasProperty("_Metallic")) { material.pbrMetallicRoughness.metallicFactor = m.GetFloat("_Metallic"); } + if (m.HasProperty("_Glossiness")) { material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_Glossiness"); } - } + + } static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs index 335b32397..5e37f544b 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs @@ -164,11 +164,14 @@ namespace UniGLTF if (texture != null) { var prop = "_MetallicGlossMap"; - material.SetTexture(prop, texture.ConvertTexture(prop)); + var smoothness = 1.0f - x.pbrMetallicRoughness.roughnessFactor; + // Bake smoothness values into a texture. + material.SetTexture(prop, texture.ConvertTexture(prop, smoothness)); } material.SetFloat("_Metallic", 1.0f); - material.SetFloat("_GlossMapScale", 1.0f - x.pbrMetallicRoughness.roughnessFactor); + // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. + material.SetFloat("_GlossMapScale", 1.0f); } else { diff --git a/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs b/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs index b10e27682..2c96649b2 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using UnityEngine; #if UNITY_EDITOR @@ -50,6 +51,13 @@ namespace UniGLTF { private const string m_extension = ".metallicRoughness"; + private float _smoothness; + + public MetallicRoughnessConverter(float smoothness) + { + _smoothness = smoothness; + } + public Texture2D GetImportTexture(Texture2D texture) { var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null); @@ -66,21 +74,29 @@ namespace UniGLTF public Color32 Import(Color32 src) { + // Roughness(glTF): dst.g -> Smoothness(Unity): src.a (with conversion) + // Metallic(glTF) : dst.b -> Metallic(Unity) : src.r return new Color32 { r = src.b, g = 0, b = 0, - a = (byte)(255 - src.g), + // Bake roughness values into a texture. + // See: https://github.com/dwango/UniVRM/issues/212. + a = (byte)(255 - Math.Min(src.g * (1.0f - _smoothness), 255)), }; } public Color32 Export(Color32 src) { + // Smoothness(Unity): src.a -> Roughness(glTF): dst.g (with conversion) + // Metallic(Unity) : src.r -> Metallic(glTF) : dst.b return new Color32 { r = 0, - g = (byte)(255 - src.a), + // Bake smoothness values into a texture. + // See: https://github.com/dwango/UniVRM/issues/212. + g = (byte)(255 - Math.Min(src.a * _smoothness, 255)), b = src.r, a = 255, }; diff --git a/Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs b/Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs index 1f79ca13f..421a87074 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs @@ -30,7 +30,13 @@ namespace UniGLTF get { return m_converts; } } - public Texture2D ConvertTexture(string prop) + /// + /// + /// + /// + /// used only when converting MetallicRoughness maps + /// + public Texture2D ConvertTexture(string prop, float smoothness = 1.0f) { var convertedTexture = Converts.FirstOrDefault(x => x.Key == prop); if (convertedTexture.Value != null) @@ -63,7 +69,7 @@ namespace UniGLTF if (prop == "_MetallicGlossMap") { - var converted = new MetallicRoughnessConverter().GetImportTexture(Texture); + var converted = new MetallicRoughnessConverter(smoothness).GetImportTexture(Texture); m_converts.Add(prop, converted); return converted; }