From ecebd7b715046751ef835d8d4d46dc4d08b3c10d Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 16 Nov 2022 18:32:51 +0900 Subject: [PATCH 1/2] Add baseColorFactor migration of older vrm file --- .../BuiltInGltfPbrMaterialImporter.cs | 8 +++----- .../BuiltInGltfUnlitMaterialImporter.cs | 19 ++++++++----------- .../IO/MaterialIO/GltfMaterialImportUtils.cs | 16 ++++++++++++++++ .../Runtime/UniGLTF/IO/MigrationFlags.cs | 8 ++++++++ Assets/VRM/Runtime/IO/VRMData.cs | 11 +++++++++++ 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs index 2c04b419f..705084149 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs @@ -82,12 +82,10 @@ namespace UniGLTF } } - if (src.pbrMetallicRoughness.baseColorFactor != null && - src.pbrMetallicRoughness.baseColorFactor.Length == 4) + var baseColorFactor = GltfMaterialImportUtils.ImportLinearBaseColorFactor(data, src); + if (baseColorFactor.HasValue) { - colors.Add("_Color", - src.pbrMetallicRoughness.baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.sRGB) - ); + colors.Add("_Color", baseColorFactor.Value.gamma); } if (src.pbrMetallicRoughness.baseColorTexture != null && diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs index 84c1df03d..651df6d59 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs @@ -26,18 +26,15 @@ namespace UniGLTF return false; } + var colors = new Dictionary(); var textureSlots = new Dictionary(); - var colors = - src.pbrMetallicRoughness.baseColorFactor != null && - src.pbrMetallicRoughness.baseColorFactor.Length == 4 - ? new Dictionary - { - { - "_Color", - src.pbrMetallicRoughness.baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.sRGB) - } - } - : new Dictionary(); + + // color + var baseColorFactor = GltfMaterialImportUtils.ImportLinearBaseColorFactor(data, src); + if (baseColorFactor.HasValue) + { + colors.Add("_Color", baseColorFactor.Value.gamma); + } // texture if (src.pbrMetallicRoughness.baseColorTexture != null) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs index e12e42a24..f9d90eb8f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs @@ -14,6 +14,22 @@ namespace UniGLTF return $"material_{index:00}"; } + public static Color? ImportLinearBaseColorFactor(GltfData data, glTFMaterial src) + { + if (src.pbrMetallicRoughness == null) return null; + var baseColorFactor = src.pbrMetallicRoughness.baseColorFactor; + if (baseColorFactor == null || baseColorFactor.Length != 4) return null; + + if (data.MigrationFlags.IsBaseColorFactorGamma) + { + return baseColorFactor.ToColor4(ColorSpace.sRGB, ColorSpace.Linear); + } + else + { + return baseColorFactor.ToColor4(ColorSpace.Linear, ColorSpace.Linear); + } + } + public static Color? ImportLinearEmissiveFactorFromMaterial(GltfData data, glTFMaterial src) { if (src.emissiveFactor == null || src.emissiveFactor.Length != 3) return null; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs index fffb08419..10b39a7d6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs @@ -2,13 +2,21 @@ { public sealed class MigrationFlags { + /// + /// Before UniGLTF v0.54.0, Built-in RP Standard shader and Unlit shaders' albedo color is exported in gamma color space. + /// https://github.com/vrm-c/UniVRM/pull/339 + /// + public bool IsBaseColorFactorGamma { get; set; } + /// /// Before UniGLTF v0.69, roughness value in the texture was invalid squared value. + /// https://github.com/vrm-c/UniVRM/pull/780 /// public bool IsRoughnessTextureValueSquared { get; set; } = false; /// /// Before UniGLTF v0.107.0, Built-in RP Standard shader's emission color is exported in gamma color space. + /// https://github.com/vrm-c/UniVRM/pull/1909 /// public bool IsEmissiveFactorGamma { get; set; } = false; } diff --git a/Assets/VRM/Runtime/IO/VRMData.cs b/Assets/VRM/Runtime/IO/VRMData.cs index debd8c826..6e64cc997 100644 --- a/Assets/VRM/Runtime/IO/VRMData.cs +++ b/Assets/VRM/Runtime/IO/VRMData.cs @@ -24,6 +24,17 @@ namespace VRM { if (!VRMVersion.ParseVersion(exportedVrmVersionString, out var exportedVrmVersion)) return; + migrationFlags.IsBaseColorFactorGamma = VRMVersion.IsNewer( + new VRMVersion.Version + { + Major = 0, + Minor = 54, + Patch = 0, + Pre = "", + }, + exportedVrmVersion + ); + migrationFlags.IsRoughnessTextureValueSquared = VRMVersion.IsNewer( new VRMVersion.Version { From 7cdd1306a4e3d00b1c1c2bf360a66223d98c8f09 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 16 Nov 2022 18:33:10 +0900 Subject: [PATCH 2/2] rename --- .../Import/Materials/BuiltInGltfPbrMaterialImporter.cs | 2 +- .../Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs | 2 +- .../URP/Import/Materials/UrpGltfPbrMaterialImporter.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs index 705084149..bbc665005 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs @@ -139,7 +139,7 @@ namespace UniGLTF material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; }); - var emissiveFactor = GltfMaterialImportUtils.ImportLinearEmissiveFactorFromMaterial(data, src); + var emissiveFactor = GltfMaterialImportUtils.ImportLinearEmissiveFactor(data, src); if (emissiveFactor.HasValue) { // NOTE: Built-in RP Standard shader's emission color is in gamma color space. diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs index f9d90eb8f..27c676248 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImportUtils.cs @@ -30,7 +30,7 @@ namespace UniGLTF } } - public static Color? ImportLinearEmissiveFactorFromMaterial(GltfData data, glTFMaterial src) + public static Color? ImportLinearEmissiveFactor(GltfData data, glTFMaterial src) { if (src.emissiveFactor == null || src.emissiveFactor.Length != 3) return null; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs index eab137d2e..66d14310c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs @@ -117,7 +117,7 @@ namespace UniGLTF material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; }); - var emissiveFactor = GltfMaterialImportUtils.ImportLinearEmissiveFactorFromMaterial(data, src); + var emissiveFactor = GltfMaterialImportUtils.ImportLinearEmissiveFactor(data, src); if (emissiveFactor.HasValue) { colors.Add("_EmissionColor", emissiveFactor.Value);