diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs index 5e37f544b..31db25670 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MaterialImporter.cs @@ -164,9 +164,8 @@ namespace UniGLTF if (texture != null) { var prop = "_MetallicGlossMap"; - var smoothness = 1.0f - x.pbrMetallicRoughness.roughnessFactor; - // Bake smoothness values into a texture. - material.SetTexture(prop, texture.ConvertTexture(prop, smoothness)); + // Bake roughnessFactor values into a texture. + material.SetTexture(prop, texture.ConvertTexture(prop, x.pbrMetallicRoughness.roughnessFactor)); } material.SetFloat("_Metallic", 1.0f); diff --git a/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs b/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs index 2c96649b2..43e0350ac 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/TextureConverter.cs @@ -51,11 +51,11 @@ namespace UniGLTF { private const string m_extension = ".metallicRoughness"; - private float _smoothness; + private float _smoothnessOrRoughness; - public MetallicRoughnessConverter(float smoothness) + public MetallicRoughnessConverter(float smoothnessOrRoughness) { - _smoothness = smoothness; + _smoothnessOrRoughness = smoothnessOrRoughness; } public Texture2D GetImportTexture(Texture2D texture) @@ -76,6 +76,10 @@ namespace UniGLTF { // Roughness(glTF): dst.g -> Smoothness(Unity): src.a (with conversion) // Metallic(glTF) : dst.b -> Metallic(Unity) : src.r + + var pixelRoughnessFactor = src.g * _smoothnessOrRoughness; // roughness + var pixelSmoothness = 1.0f - Mathf.Sqrt(pixelRoughnessFactor); + return new Color32 { r = src.b, @@ -83,7 +87,7 @@ namespace UniGLTF b = 0, // 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)), + a = (byte)Mathf.Clamp(pixelSmoothness * 255, 0, 255), }; } @@ -91,12 +95,17 @@ namespace UniGLTF { // Smoothness(Unity): src.a -> Roughness(glTF): dst.g (with conversion) // Metallic(Unity) : src.r -> Metallic(glTF) : dst.b + + var pixelSmoothness = src.a * _smoothnessOrRoughness; // smoothness + // https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ + var pixelRoughnessFactor = (1.0f - pixelSmoothness) * (1.0f - pixelSmoothness); + return new Color32 { r = 0, // Bake smoothness values into a texture. // See: https://github.com/dwango/UniVRM/issues/212. - g = (byte)(255 - Math.Min(src.a * _smoothness, 255)), + g = (byte)Mathf.Clamp(pixelRoughnessFactor * 255, 0, 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 421a87074..87f7fb26a 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/TextureItem.cs @@ -36,7 +36,7 @@ namespace UniGLTF /// /// used only when converting MetallicRoughness maps /// - public Texture2D ConvertTexture(string prop, float smoothness = 1.0f) + public Texture2D ConvertTexture(string prop, float smoothnessOrRoughness = 1.0f) { var convertedTexture = Converts.FirstOrDefault(x => x.Key == prop); if (convertedTexture.Value != null) @@ -69,7 +69,7 @@ namespace UniGLTF if (prop == "_MetallicGlossMap") { - var converted = new MetallicRoughnessConverter(smoothness).GetImportTexture(Texture); + var converted = new MetallicRoughnessConverter(smoothnessOrRoughness).GetImportTexture(Texture); m_converts.Add(prop, converted); return converted; }