From ce9129749f23a640ba551a6dccbad1e64ee027a3 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 19 May 2021 18:08:24 +0900 Subject: [PATCH] Add principle texture colorspace tests. --- .../IO/Tests/4x4_gray_import_as_linear.png | Bin 0 -> 79 bytes .../Tests/4x4_gray_import_as_linear.png.meta | 116 ++++++++++++++++++ .../GLTF/IO/Tests/4x4_gray_import_as_srgb.png | Bin 0 -> 79 bytes .../IO/Tests/4x4_gray_import_as_srgb.png.meta | 116 ++++++++++++++++++ .../TextureColorSpaceSpecificationTests.cs | 74 +++++++++++ ...extureColorSpaceSpecificationTests.cs.meta | 3 + 6 files changed, 309 insertions(+) create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/4x4_gray_import_as_linear.png create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/4x4_gray_import_as_linear.png.meta create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/4x4_gray_import_as_srgb.png create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/4x4_gray_import_as_srgb.png.meta create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/TextureColorSpaceSpecificationTests.cs create mode 100644 Assets/VRMShaders/GLTF/IO/Tests/TextureColorSpaceSpecificationTests.cs.meta diff --git a/Assets/VRMShaders/GLTF/IO/Tests/4x4_gray_import_as_linear.png b/Assets/VRMShaders/GLTF/IO/Tests/4x4_gray_import_as_linear.png new file mode 100644 index 0000000000000000000000000000000000000000..9716d31247798700b498a7e3f9c095b80b5cafb9 GIT binary patch literal 79 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1SHiab7}%9F;5rA5RRG2Yu2w%Z(vlj;5Kh> bY-D8c&SPwCGqAJ-s$lSR^>bP0l+XkKBM1 bY-D8c&SPwCGqAJ-s$lSR^>bP0l+XkKBM1($"{AssetPath}/4x4_gray_import_as_srgb.png"); + private static readonly Texture2D LinearGrayTex = AssetDatabase.LoadAssetAtPath($"{AssetPath}/4x4_gray_import_as_linear.png"); + private static readonly Color32 JustGray = new Color32(127, 127, 127, 255); + private static readonly Color32 SrgbGrayInSrgb = JustGray; + private static readonly Color32 SrgbGrayInLinear = ((Color) SrgbGrayInSrgb).linear; + private static readonly Color32 LinearGrayInLinear = JustGray; + private static readonly Color32 LinearGrayInSrgb = ((Color) LinearGrayInLinear).gamma; + + [Test] + public void InputAssetsRawImage() + { + Assert.AreEqual(SrgbGrayInSrgb, GetFirstPixelInTexture2D(SrgbGrayTex)); + + // Image color space is sRGB even through Texture color space was Linear. + Assert.AreEqual(SrgbGrayInSrgb, GetFirstPixelInTexture2D(LinearGrayTex)); + } + + [Test] + public void CopyToSrgbRenderTexture() + { + var srgbRt = RenderTexture.GetTemporary(4, 4, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); + + Graphics.Blit(SrgbGrayTex, srgbRt); + Assert.AreEqual(SrgbGrayInSrgb, GetFirstPixelInRenderTexture(srgbRt)); + + Graphics.Blit(LinearGrayTex, srgbRt); + Assert.AreEqual(LinearGrayInSrgb, GetFirstPixelInRenderTexture(srgbRt)); + + RenderTexture.ReleaseTemporary(srgbRt); + } + + [Test] + public void CopyToLinearRenderTexture() + { + var linearRt = RenderTexture.GetTemporary(4, 4, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); + + Graphics.Blit(SrgbGrayTex, linearRt); + Assert.AreEqual(SrgbGrayInLinear, GetFirstPixelInRenderTexture(linearRt)); + + Graphics.Blit(LinearGrayTex, linearRt); + Assert.AreEqual(LinearGrayInLinear, GetFirstPixelInRenderTexture(linearRt)); + + RenderTexture.ReleaseTemporary(linearRt); + } + + private Color32 GetFirstPixelInTexture2D(Texture2D tex) + { + return tex.GetPixels32()[0]; + } + + private Color32 GetFirstPixelInRenderTexture(RenderTexture rt) + { + // This function copies memory values and just read, so Texture2D's color space is not important. + var srgbTex = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, mipChain: false, linear: false); + RenderTexture.active = rt; + srgbTex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); + srgbTex.Apply(); + RenderTexture.active = null; + var pixel = GetFirstPixelInTexture2D(srgbTex); + UnityEngine.Object.DestroyImmediate(srgbTex); + return pixel; + } + } +} diff --git a/Assets/VRMShaders/GLTF/IO/Tests/TextureColorSpaceSpecificationTests.cs.meta b/Assets/VRMShaders/GLTF/IO/Tests/TextureColorSpaceSpecificationTests.cs.meta new file mode 100644 index 000000000..a430c5519 --- /dev/null +++ b/Assets/VRMShaders/GLTF/IO/Tests/TextureColorSpaceSpecificationTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 19082be49cd24881a2f53c13de83ac70 +timeCreated: 1621413009 \ No newline at end of file