From c3775ca15fdbc5db1fb249986a67bfe43b9ae9fb Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 22 Jun 2021 19:59:59 +0900 Subject: [PATCH] Add migration code about legacy roughness texture behaviour. --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs | 7 ++++--- Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs | 3 ++- Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs | 10 ++++++++++ .../UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs.meta | 3 +++ .../IO/TextureIO/Shaders/StandardMapImporter.shader | 8 ++++++++ .../IO/Runtime/OcclusionMetallicRoughnessConverter.cs | 10 +++++++++- Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs | 9 +++++++-- .../GLTF/IO/Tests/MetallicRoughnessConverterTests.cs | 3 ++- 8 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs index cc086a741..bcab6200a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs @@ -25,6 +25,7 @@ namespace UniGLTF /// public IStorage Storage; + public MigrationFlags MigrationFlags = new MigrationFlags(); #region Parse public void ParsePath(string path) @@ -226,7 +227,7 @@ namespace UniGLTF var gltfImage = GLTF.images[gltfTexture.source]; if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:")) { - // from image uri + // from image uri gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri); } if (string.IsNullOrEmpty(gltfTexture.name)) @@ -284,7 +285,7 @@ namespace UniGLTF { if (used.Add(material.name)) { -#if VRM_DEVELOP +#if VRM_DEVELOP // Debug.Log($"Material: {material.name}"); #endif break; @@ -327,7 +328,7 @@ namespace UniGLTF { if (used.Add(animation.name)) { -#if VRM_DEVELOP +#if VRM_DEVELOP // Debug.Log($"Material: {material.name}"); #endif break; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 55b19c84f..f63b36422 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -32,7 +32,8 @@ namespace UniGLTF TextureFactory = new TextureFactory(textureDeserializer, _externalObjectMap .Where(x => x.Value is Texture) - .ToDictionary(x => x.Key, x => (Texture)x.Value)); + .ToDictionary(x => x.Key, x => (Texture)x.Value), + Parser.MigrationFlags.IsRoughnessTextureValueSquared); MaterialFactory = new MaterialFactory(_externalObjectMap .Where(x => x.Value is Material) .ToDictionary(x => x.Key, x => (Material)x.Value)); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs new file mode 100644 index 000000000..1dfa79e80 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs @@ -0,0 +1,10 @@ +namespace UniGLTF +{ + public sealed class MigrationFlags + { + /// + /// Before UniGLTF v0.69, roughness value in the texture was invalid squared value. + /// + public bool IsRoughnessTextureValueSquared { get; set; } = false; + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs.meta new file mode 100644 index 000000000..5594b4182 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MigrationFlags.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8a9174a8f8ee45dc948d93c5e8af5714 +timeCreated: 1624359107 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/Shaders/StandardMapImporter.shader b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/Shaders/StandardMapImporter.shader index f42507579..6c62b49bd 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/Shaders/StandardMapImporter.shader +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/Shaders/StandardMapImporter.shader @@ -7,6 +7,7 @@ _GltfRoughnessFactor ("glTF Roughness Factor", Float) = 0.0 _GltfMetallicRoughnessTexture ("glTF Metallic Roughness Texture", 2D) = "black" {} _GltfOcclusionTexture ("glTF Occlusion Texture", 2D) = "black" {} + _IsLegacySquaredRoughness ("Is UniGLTF Legacy Squared Roughness", Float) = 0.0 } SubShader { @@ -46,6 +47,7 @@ half _GltfRoughnessFactor; sampler2D _GltfMetallicRoughnessTexture; sampler2D _GltfOcclusionTexture; + half _IsLegacySquaredRoughness; fixed4 frag (v2f i) : SV_Target { @@ -56,6 +58,12 @@ half roughness = metallicRoughnessTex.g * _GltfRoughnessFactor; // G: glTF Roughness half metallic = metallicRoughnessTex.b * _GltfMetallicFactor; // B: glTF Metallic + // MIGRATION code: legacy behaviour + if (_IsLegacySquaredRoughness == 1.0) + { + roughness = sqrt(roughness); + } + fixed4 result; result.r = metallic; // R: Unity Metallic result.g = occlusion; // G: Unity Occlusion diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs b/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs index 72d4e8e8f..b4770ceac 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/OcclusionMetallicRoughnessConverter.cs @@ -34,8 +34,14 @@ namespace VRMShaders } } + /// + /// Import glTF Metallic-Roughness texture to Unity Metallic-Smoothness-Occlusion texture. + /// + /// isLegacySquaredRoughness: + /// Before UniGLTF v0.69, roughness value in the texture was invalid squared value. + /// public static Texture2D Import(Texture2D metallicRoughnessTexture, - float metallicFactor, float roughnessFactor, Texture2D occlusionTexture) + float metallicFactor, float roughnessFactor, Texture2D occlusionTexture, bool isLegacySquaredRoughness) { if (metallicRoughnessTexture == null && occlusionTexture == null) { @@ -49,6 +55,7 @@ namespace VRMShaders Exporter.SetTexture("_GltfOcclusionTexture", occlusionTexture); Exporter.SetFloat("_GltfMetallicFactor", metallicFactor); Exporter.SetFloat("_GltfRoughnessFactor", roughnessFactor); + Exporter.SetFloat("_IsLegacySquaredRoughness", isLegacySquaredRoughness ? 1 : 0); var dst = TextureConverter.CopyTexture(src, ColorSpace.Linear, true, Exporter); @@ -57,6 +64,7 @@ namespace VRMShaders Exporter.SetTexture("_GltfOcclusionTexture", null); Exporter.SetFloat("_GltfMetallicFactor", 0); Exporter.SetFloat("_GltfRoughnessFactor", 0); + Exporter.SetFloat("_IsLegacySquaredRoughness", 0); return dst; } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs index e2f8ee6f2..98d158581 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/TextureFactory.cs @@ -10,6 +10,7 @@ namespace VRMShaders { private readonly ITextureDeserializer _textureDeserializer; private readonly IReadOnlyDictionary _externalMap; + private readonly bool _isLegacySquaredRoughness; private readonly Dictionary _temporaryTextures = new Dictionary(); private readonly Dictionary _textureCache = new Dictionary(); @@ -23,10 +24,14 @@ namespace VRMShaders /// public IReadOnlyDictionary ExternalTextures => _externalMap; - public TextureFactory(ITextureDeserializer textureDeserializer, IReadOnlyDictionary externalTextures) + public TextureFactory( + ITextureDeserializer textureDeserializer, + IReadOnlyDictionary externalTextures, + bool isLegacySquaredRoughness) { _textureDeserializer = textureDeserializer; _externalMap = externalTextures; + _isLegacySquaredRoughness = isLegacySquaredRoughness; } public void Dispose() @@ -103,7 +108,7 @@ namespace VRMShaders } var combinedTexture = OcclusionMetallicRoughnessConverter.Import(metallicRoughnessTexture, - texDesc.MetallicFactor, texDesc.RoughnessFactor, occlusionTexture); + texDesc.MetallicFactor, texDesc.RoughnessFactor, occlusionTexture, _isLegacySquaredRoughness); combinedTexture.name = subAssetKey.Name; combinedTexture.SetSampler(texDesc.Sampler); _textureCache.Add(subAssetKey, combinedTexture); diff --git a/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs b/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs index 4199cbd5c..e6505fcba 100644 --- a/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs +++ b/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs @@ -20,7 +20,8 @@ namespace VRMShaders metallicRoughnessTexture, metallicFactor, roughnessFactor, - occlusionTexture + occlusionTexture, + false ); var result = converted.GetPixels32()[0];