From 388a7fbc6e283680f90d2b14a024e4d10fe13734 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 27 May 2021 22:45:21 +0900 Subject: [PATCH] Add complex test to MetallicRoughnessConverter --- .../Shaders/StandardMapImporter.shader | 16 +++---- .../Tests/MetallicRoughnessConverterTests.cs | 42 ++++++++++++------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/StandardMapImporter.shader b/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/StandardMapImporter.shader index 3115bd222..f42507579 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/StandardMapImporter.shader +++ b/Assets/UniGLTF/Runtime/UniGLTF/Resources/Shaders/StandardMapImporter.shader @@ -49,15 +49,17 @@ fixed4 frag (v2f i) : SV_Target { - half4 metallicRoughness = tex2D(_GltfMetallicRoughnessTexture, i.uv); - half metallic = metallicRoughness.b * _GltfMetallicFactor; - half roughness = metallicRoughness.g * _GltfRoughnessFactor; - half occlusion = tex2D(_GltfOcclusionTexture, i.uv).r; + half4 metallicRoughnessTex = tex2D(_GltfMetallicRoughnessTexture, i.uv); + half4 occlusionTex = tex2D(_GltfOcclusionTexture, i.uv); + + half occlusion = occlusionTex.r; // R: glTF Occlusion + half roughness = metallicRoughnessTex.g * _GltfRoughnessFactor; // G: glTF Roughness + half metallic = metallicRoughnessTex.b * _GltfMetallicFactor; // B: glTF Metallic fixed4 result; - result.r = metallic; // R: Unity Metallic - result.g = occlusion; // G: Unity Occlusion - result.b = 0; // B: Unity Heightmap (no use) + result.r = metallic; // R: Unity Metallic + result.g = occlusion; // G: Unity Occlusion + result.b = 0; // B: Unity Heightmap (no use) result.a = 1.0 - roughness; // A: Unity Smoothness return result; diff --git a/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs b/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs index e44377f01..4199cbd5c 100644 --- a/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs +++ b/Assets/VRMShaders/GLTF/IO/Tests/MetallicRoughnessConverterTests.cs @@ -76,45 +76,55 @@ namespace VRMShaders var roughnessFactor = 1.0f; Assert.That( ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), - // r <- 255 : Same metallic (src.r) - // g <- 0 : (Unused) + // r <- 255 : metallic (metallicRoughness.b * metallicFactor) + // g <- 0 : occlusion (occlusion.r) // b <- 0 : (Unused) - // a <- 0 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) + // a <- 0 : smoothness (1.0 - (metallicRoughness.g * roughnessFactor)) Is.EqualTo(new Color32(255, 0, 0, 0))); } { var roughnessFactor = 1.0f; Assert.That( - ImportPixel(new Color32(255, 128, 255, 255), 1.0f, roughnessFactor, default), - // r <- 255 : Same metallic (src.r) - // g <- 0 : (Unused) + ImportPixel(new Color32(255, 127, 255, 255), 1.0f, roughnessFactor, default), + // r <- 255 : metallic (metallicRoughness.b * metallicFactor) + // g <- 0 : occlusion (occlusion.r) // b <- 0 : (Unused) - // a <- 128 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) - Is.EqualTo(new Color32(255, 0, 0, 127))); // smoothness 0.5 * src.a 1.0 + // a <- 128 : smoothness (1.0 - (metallicRoughness.g * roughnessFactor)) + Is.EqualTo(new Color32(255, 0, 0, 128))); // A:smoothness = 1.0 - (0.5 * 1.0) = 0.5 } { var roughnessFactor = 0.5f; Assert.That( - ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), - // r <- 255 : Same metallic (src.r) - // g <- 0 : (Unused) + ImportPixel(new Color32(255, 127, 255, 255), 1.0f, roughnessFactor, default), + // r <- 255 : metallic (metallicRoughness.b * metallicFactor) + // g <- 0 : occlusion (occlusion.r) // b <- 0 : (Unused) - // a <- 74 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) - Is.EqualTo(new Color32(255, 0, 0, 127))); + // a <- 191 : smoothness (1.0 - (metallicRoughness.g * roughnessFactor)) + Is.EqualTo(new Color32(255, 0, 0, 191))); // A:smoothness = 1.0 - (0.5 * 0.5) = 0.75 } { var roughnessFactor = 0.0f; Assert.That( ImportPixel(new Color32(255, 255, 255, 255), 1.0f, roughnessFactor, default), - // r <- 255 : Same metallic (src.r) - // g <- 0 : (Unused) + // r <- 255 : metallic (metallicRoughness.b * metallicFactor) + // g <- 0 : occlusion (occlusion.r) // b <- 0 : (Unused) - // a <- 255 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) + // a <- 255 : smoothness (1.0 - (metallicRoughness.g * roughnessFactor)) Is.EqualTo(new Color32(255, 0, 0, 255))); } + + { + Assert.That( + ImportPixel(new Color32(222, 200, 100, 255), 0.5f, 0.25f, new Color32(127, 0, 0, 0)), + // r <- 50 : metallic (metallicRoughness.b * metallicFactor) + // g <- 127 : occlusion (occlusion.r) + // b <- 0 : (Unused) + // a <- 205 : smoothness (1.0 - (metallicRoughness.g * roughnessFactor)) + Is.EqualTo(new Color32(50, 127, 0, 205))); + } } [Test]