From 3d85986f2ec20f684aefeeb7976a2c3237de9001 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Mar 2021 18:57:23 +0900 Subject: [PATCH] MetallicFactor and RoughnessFactor --- .../IO/MaterialLoader/PBRMaterialItem.cs | 3 +- .../OcclusionMetallicRoughnessConverter.cs | 34 ++++++++++--------- .../IO/TextureLoader/GetTextureParam.cs | 16 +++++---- .../IO/TextureLoader/TextureFactory.cs | 2 +- Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 2 +- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs index e79c9997c..c2fc530fd 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs @@ -52,7 +52,8 @@ namespace UniGLTF { return GetTextureParam.CreateStandard(gltf, src.pbrMetallicRoughness.metallicRoughnessTexture.index, - src.pbrMetallicRoughness.metallicFactor); + src.pbrMetallicRoughness.metallicFactor, + src.pbrMetallicRoughness.roughnessFactor); } public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs index c13377de0..e8749cdfb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs @@ -4,14 +4,18 @@ namespace UniGLTF { /// /// - /// * https://github.com/dwango/UniVRM/issues/212. - /// * https://blogs.unity3d.com/jp/2016/01/25/ggx-in-unity-5-3/ - /// * https://github.com/vrm-c/UniVRM/issues/388 + /// * https://github.com/vrm-c/UniVRM/issues/781 + /// + /// Unity = glTF + /// Occlusion: unity.g = glTF.r + /// Roughness: unity.a = 1 - glTF.g * roughnessFactor + /// Metallic : unity.r = glTF.b * metallicFactor /// /// glTF = Unity - /// Occlusion: src.r -> dst.g - /// Roughness: src.g -> dst.a (bake smoothnessOrRoughness) - /// Metallic : src.b -> dst.r + /// Occlusion: glTF.r = unity.g + /// Roughness: glTF.g = 1 - unity.a * smoothness + /// Metallic : glTF.b = unity.r + /// /// public class OcclusionMetallicRoughnessConverter : ITextureConverter { @@ -22,11 +26,11 @@ namespace UniGLTF _smoothnessOrRoughness = smoothnessOrRoughness; } - public static Texture2D GetImportTexture(Texture2D texture, float smoothnessOrRoughness) + public static Texture2D GetImportTexture(Texture2D texture, float metallicFactor, float roughnessFactor) { TextureConverter.ColorConversion convert = src => { - return Import(src, smoothnessOrRoughness); + return Import(src, metallicFactor, roughnessFactor); }; var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, convert, null); return converted; @@ -38,15 +42,14 @@ namespace UniGLTF return converted; } - public static Color32 Import(Color32 src, float _smoothnessOrRoughness) + public static Color32 Import(Color32 src, float metallicFactor, float roughnessFactor) { var dst = new Color32 { - r = src.b, // Metallic + r = (byte)(src.b * metallicFactor), // Metallic g = src.r, // Occlusion - b = 0, // not used - // Roughness to Smoothness. Bake _smoothnessOrRoughness into a texture. - a = (byte)(255 - src.g * _smoothnessOrRoughness), + b = 0, // not used + a = (byte)(255 - src.g * roughnessFactor), // Roughness to Smoothness }; return dst; @@ -56,9 +59,8 @@ namespace UniGLTF { var dst = new Color32 { - r = src.g, // Occlusion - // Roughness from Smoothness. Bake divide _smoothnessOrRoughness from a texture. - g = (byte)(255 - src.a), + r = src.g, // Occlusion + g = (byte)(255 - src.a), // Roughness from Smoothness b = src.r, // Metallic a = 255, // not used }; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs index 931d75522..c888366fa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/GetTextureParam.cs @@ -57,6 +57,7 @@ namespace UniGLTF public readonly TextureTypes TextureType; public readonly float MetallicFactor; + public readonly float RoughnessFactor; public readonly ushort? Index0; public readonly ushort? Index1; public readonly ushort? Index2; @@ -69,7 +70,7 @@ namespace UniGLTF /// public bool ExtractConverted => TextureType == TextureTypes.StandardMap; - public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, int i0, int i1, int i2, int i3, int i4, int i5) + public GetTextureParam(string name, TextureTypes textureType, float metallicFactor, float roughnessFactor, int i0, int i1, int i2, int i3, int i4, int i5) { if (string.IsNullOrEmpty(name)) { @@ -79,6 +80,7 @@ namespace UniGLTF TextureType = textureType; MetallicFactor = metallicFactor; + RoughnessFactor = roughnessFactor; Index0 = (ushort)i0; Index1 = (ushort)i1; Index2 = (ushort)i2; @@ -90,10 +92,10 @@ namespace UniGLTF public static GetTextureParam CreateSRGB(glTF gltf, int textureIndex) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.sRGB, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.sRGB, default, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam Create(glTF gltf, int index, string prop) + public static GetTextureParam Create(glTF gltf, int index, string prop, float metallicFactor, float roughnessFactor) { switch (prop) { @@ -102,7 +104,7 @@ namespace UniGLTF case OCCLUSION_PROP: case METALLIC_GLOSS_PROP: - return CreateStandard(gltf, index, 1); + return CreateStandard(gltf, index, metallicFactor, roughnessFactor); default: return CreateSRGB(gltf, index); @@ -112,13 +114,13 @@ namespace UniGLTF public static GetTextureParam CreateNormal(glTF gltf, int textureIndex) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.NormalMap, default, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.NormalMap, default, default, textureIndex, default, default, default, default, default); } - public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor) + public static GetTextureParam CreateStandard(glTF gltf, int textureIndex, float metallicFactor, float roughnessFactor) { var name = gltf.textures[textureIndex].name; - return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, textureIndex, default, default, default, default, default); + return new GetTextureParam(name, TextureTypes.StandardMap, metallicFactor, roughnessFactor, textureIndex, default, default, default, default, default); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index d90a6a4af..ff666a8dc 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -178,7 +178,7 @@ namespace UniGLTF case GetTextureParam.TextureTypes.StandardMap: { var baseTexture = await GetOrCreateBaseTexture(awaitCaller, gltf, param.Index0.Value, false); - var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor); + var converted = OcclusionMetallicRoughnessConverter.GetImportTexture(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor); converted.name = param.ConvertedName; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(converted.name, info); diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index 28cacb2e9..6ea9985bf 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -73,7 +73,7 @@ namespace VRM } foreach (var kv in item.textureProperties) { - var param = GetTextureParam.Create(gltf, kv.Value, kv.Key); + var param = GetTextureParam.Create(gltf, kv.Value, kv.Key, 1, 1); var texture = await getTexture(awaitCaller, gltf, param); if (texture != null) {