From 13f7ab9254119460cbaf5010ccb6381e4a458566 Mon Sep 17 00:00:00 2001 From: Matthew Hayden Date: Wed, 28 Feb 2024 16:25:25 +0000 Subject: [PATCH] Additional textures & tangent spec tests --- test/Text/GLTF/Loader/Internal/AdapterSpec.hs | 41 ++++++- .../Loader/Internal/BufferAccessorSpec.hs | 2 +- test/Text/GLTF/Loader/Test/MkGltf.hs | 113 ++++++++++++++++-- 3 files changed, 141 insertions(+), 15 deletions(-) diff --git a/test/Text/GLTF/Loader/Internal/AdapterSpec.hs b/test/Text/GLTF/Loader/Internal/AdapterSpec.hs index 8460b20..6ededf0 100644 --- a/test/Text/GLTF/Loader/Internal/AdapterSpec.hs +++ b/test/Text/GLTF/Loader/Internal/AdapterSpec.hs @@ -106,7 +106,7 @@ spec = do Image.Image { uri = Nothing, mimeType = Just "text/jpg", - bufferView = Just $ BufferView.BufferViewIx 4, + bufferView = Just $ BufferView.BufferViewIx 6, name = Just "Image", extensions = Nothing, extras = Nothing @@ -114,7 +114,7 @@ spec = do it "Adapts a BufferView image" $ do env' <- env - let image = ImageBufferView (BufferView.BufferViewIx 5) + let image = ImageBufferView (BufferView.BufferViewIx 6) runReader (adaptImage image codecImage) env' `shouldBe` Image @@ -277,7 +277,7 @@ spec = do { Mesh.attributes = HashMap.insert attributeColors - (Accessor.AccessorIx 4) + (Accessor.AccessorIx 5) (Mesh.attributes mkCodecMeshPrimitive) } loaderMeshPrimitive' = loaderMeshPrimitive & _meshPrimitiveColors .~ [0, 0.2, 0.6, 1] @@ -347,7 +347,10 @@ loaderMaterial = materialAlphaMode = Opaque, materialDoubleSided = True, materialEmissiveFactor = V3 1.0 2.0 3.0, + materialEmissiveTexture = Just loaderEmissiveTexture, materialName = Just "Material", + materialNormalTexture = Just loaderNormalTexture, + materialOcclusionTexture = Just loaderOcclusionTexture, materialPbrMetallicRoughness = Just loaderPbrMetallicRoughness } @@ -402,6 +405,7 @@ loaderPbrMetallicRoughness = { pbrBaseColorFactor = V4 1.0 2.0 3.0 4.0, pbrBaseColorTexture = Just loaderBaseColorTexture, pbrMetallicFactor = 1.0, + pbrMetallicRoughnessTexture = Just loaderMetallicRoughnessTexture, pbrRoughnessFactor = 2.0 } @@ -413,6 +417,7 @@ loaderMeshPrimitive = meshPrimitiveMode = Triangles, meshPrimitiveNormals = fmap (\x -> V3 x x x) [5 .. 8], meshPrimitivePositions = fmap (\x -> V3 x x x) [1 .. 4], + meshPrimitiveTangents = fmap (\x -> V4 x x x x) [13 .. 16], meshPrimitiveTexCoords = fmap (\x -> V2 x x) [9 .. 12], meshPrimitiveColors = [] } @@ -423,3 +428,33 @@ loaderBaseColorTexture = { textureId = 15, textureTexCoord = 10 } + +loaderMetallicRoughnessTexture :: TextureInfo +loaderMetallicRoughnessTexture = + TextureInfo + { textureId = 16, + textureTexCoord = 11 + } + +loaderEmissiveTexture :: TextureInfo +loaderEmissiveTexture = + TextureInfo + { textureId = 17, + textureTexCoord = 12 + } + +loaderNormalTexture :: NormalTextureInfo +loaderNormalTexture = + NormalTextureInfo + { normalTextureId = 18, + normalTextureTexCoord = 13, + normalTextureScale = 1 + } + +loaderOcclusionTexture :: OcclusionTextureInfo +loaderOcclusionTexture = + OcclusionTextureInfo + { occlusionTextureId = 19, + occlusionTextureTexCoord = 14, + occlusionTextureStrength = 2 + } diff --git a/test/Text/GLTF/Loader/Internal/BufferAccessorSpec.hs b/test/Text/GLTF/Loader/Internal/BufferAccessorSpec.hs index 3e0c71c..9f7585e 100644 --- a/test/Text/GLTF/Loader/Internal/BufferAccessorSpec.hs +++ b/test/Text/GLTF/Loader/Internal/BufferAccessorSpec.hs @@ -98,7 +98,7 @@ spec = do } images <- loadImages gltf' basePath - images `shouldBe` [ImageBufferView (BufferView.BufferViewIx 5)] + images `shouldBe` [ImageBufferView (BufferView.BufferViewIx 6)] it "Returns NoImage when no data specificed" $ do let gltf' = diff --git a/test/Text/GLTF/Loader/Test/MkGltf.hs b/test/Text/GLTF/Loader/Test/MkGltf.hs index c539a4e..429a073 100644 --- a/test/Text/GLTF/Loader/Test/MkGltf.hs +++ b/test/Text/GLTF/Loader/Test/MkGltf.hs @@ -39,6 +39,7 @@ mkCodecGltf = [ mkCodecAccessorIndices, mkCodecAccessorPositions, mkCodecAccessorNormals, + mkCodecAccessorTangents, mkCodecAccessorTexCoords, mkCodecAccessorColors ], @@ -48,6 +49,7 @@ mkCodecGltf = [ mkCodecBufferIndices, mkCodecBufferPositions, mkCodecBufferNormals, + mkCodecBufferTangents, mkCodecBufferTexCoords, mkCodecBufferColors, mkCodecBufferImage @@ -57,6 +59,7 @@ mkCodecGltf = [ mkCodecBufferViewIndices, mkCodecBufferViewPositions, mkCodecBufferViewNormals, + mkCodecBufferViewTangents, mkCodecBufferViewTexCoords, mkCodecBufferViewColors, mkCodecBufferViewImage @@ -153,10 +156,27 @@ mkCodecAccessorPositions = type' = Accessor.AttributeType "VEC2" } +mkCodecAccessorTangents :: Accessor.Accessor +mkCodecAccessorTangents = + Accessor.Accessor + { bufferView = Just $ BufferView.BufferViewIx 3, + byteOffset = 0, + componentType = Accessor.ComponentType 5126, + count = 4, + extensions = Nothing, + extras = Nothing, + max = Nothing, + min = Nothing, + name = Just "Accessor Tangents", + normalized = False, + sparse = Nothing, + type' = Accessor.AttributeType "VEC4" + } + mkCodecAccessorTexCoords :: Accessor.Accessor mkCodecAccessorTexCoords = Accessor.Accessor - { bufferView = Just $ BufferView.BufferViewIx 3, + { bufferView = Just $ BufferView.BufferViewIx 4, byteOffset = 0, componentType = Accessor.ComponentType 5126, count = 4, @@ -173,7 +193,7 @@ mkCodecAccessorTexCoords = mkCodecAccessorColors :: Accessor.Accessor mkCodecAccessorColors = Accessor.Accessor - { bufferView = Just $ BufferView.BufferViewIx 4, + { bufferView = Just $ BufferView.BufferViewIx 5, byteOffset = 0, componentType = Accessor.ComponentType 5123, count = 4, @@ -226,10 +246,23 @@ mkCodecBufferViewPositions = extras = Nothing } +mkCodecBufferViewTangents :: BufferView.BufferView +mkCodecBufferViewTangents = + BufferView.BufferView + { buffer = Buffer.BufferIx 3, + byteOffset = 0, + byteLength = sizeOf (undefined :: V4 Float) * 4, + byteStride = Nothing, + target = Nothing, + name = Just "BufferView Tangents", + extensions = Nothing, + extras = Nothing + } + mkCodecBufferViewTexCoords :: BufferView.BufferView mkCodecBufferViewTexCoords = BufferView.BufferView - { buffer = Buffer.BufferIx 3, + { buffer = Buffer.BufferIx 4, byteOffset = 0, byteLength = sizeOf (undefined :: V2 Float) * 4, byteStride = Nothing, @@ -242,7 +275,7 @@ mkCodecBufferViewTexCoords = mkCodecBufferViewColors :: BufferView.BufferView mkCodecBufferViewColors = BufferView.BufferView - { buffer = Buffer.BufferIx 4, + { buffer = Buffer.BufferIx 5, byteOffset = 0, byteLength = sizeOf (undefined :: V4 Float) * 4, byteStride = Nothing, @@ -255,7 +288,7 @@ mkCodecBufferViewColors = mkCodecBufferViewImage :: BufferView.BufferView mkCodecBufferViewImage = BufferView.BufferView - { buffer = Buffer.BufferIx 5, + { buffer = Buffer.BufferIx 6, byteOffset = 0, byteLength = length "imageData" * 4, byteStride = Nothing, @@ -270,7 +303,7 @@ mkCodecImage = Image.Image { uri = Just (URI.URI "data:image/jpg;base64,aW1hZ2VQYXlsb2Fk"), mimeType = Just "image/png", - bufferView = Just $ BufferView.BufferViewIx 5, + bufferView = Just $ BufferView.BufferViewIx 6, name = Just "Image", extensions = Nothing, extras = Nothing @@ -306,6 +339,16 @@ mkCodecBufferPositions = name = Just "Buffer Positions" } +mkCodecBufferTangents :: Buffer.Buffer +mkCodecBufferTangents = + Buffer.Buffer + { byteLength = sizeOf (undefined :: V4 Float) * 4, + uri = Just mkCodecBufferUriTangents, + extensions = Nothing, + extras = Nothing, + name = Just "Buffer Tangents" + } + mkCodecBufferTexCoords :: Buffer.Buffer mkCodecBufferTexCoords = Buffer.Buffer @@ -344,9 +387,9 @@ mkCodecMaterial = alphaMode = Material.OPAQUE, doubleSided = True, pbrMetallicRoughness = Just mkCodecPbrMetallicRoughness, - normalTexture = Nothing, - occlusionTexture = Nothing, - emissiveTexture = Nothing, + normalTexture = Just mkCodecNormalTexture, + occlusionTexture = Just mkCodecOcclusionTexture, + emissiveTexture = Just mkCodecEmissiveTexture, name = Just "Material", extensions = Nothing, extras = Nothing @@ -368,7 +411,7 @@ mkCodecPbrMetallicRoughness = { baseColorFactor = (1.0, 2.0, 3.0, 4.0), metallicFactor = 1.0, roughnessFactor = 2.0, - metallicRoughnessTexture = Nothing, + metallicRoughnessTexture = Just mkCodecMetallicRoughnessTexture, baseColorTexture = Just mkCodecBaseColorTexture, extensions = Nothing, extras = Nothing @@ -395,6 +438,13 @@ mkCodecBufferUriPositions = URI.URI uriText encodedText = encodeBase64 . toStrict . runPut $ putPositions putPositions = mapM_ (replicateM_ 3 . putFloatle) ([1 .. 4] :: [Float]) +mkCodecBufferUriTangents:: URI.URI +mkCodecBufferUriTangents = URI.URI uriText + where + uriText = "data:application/octet-stream;base64," <> encodedText + encodedText = encodeBase64 . toStrict . runPut $ putPositions + putPositions = mapM_ (replicateM_ 4 . putFloatle) ([13 .. 16] :: [Float]) + mkCodecBufferUriTexCoords :: URI.URI mkCodecBufferUriTexCoords = URI.URI uriText where @@ -434,7 +484,8 @@ mkCodecMeshPrimitive = HashMap.fromList [ (attributePosition, Accessor.AccessorIx 1), (attributeNormal, Accessor.AccessorIx 2), - (attributeTexCoord, Accessor.AccessorIx 3) + (attributeTangent, Accessor.AccessorIx 3), + (attributeTexCoord, Accessor.AccessorIx 4) ], mode = Mesh.MeshPrimitiveMode 4, indices = Just $ Accessor.AccessorIx 0, @@ -454,6 +505,46 @@ mkCodecBaseColorTexture = texCoord = 10 } +mkCodecMetallicRoughnessTexture :: TextureInfo.TextureInfo_ +mkCodecMetallicRoughnessTexture = + TextureInfo.TextureInfo + { extensions = Nothing, + extras = Nothing, + index = 16, + subtype = TextureInfo.Basic, + texCoord = 11 + } + +mkCodecEmissiveTexture :: TextureInfo.TextureInfo_ +mkCodecEmissiveTexture = + TextureInfo.TextureInfo + { extensions = Nothing, + extras = Nothing, + index = 17, + subtype = TextureInfo.Basic, + texCoord = 12 + } + +mkCodecNormalTexture :: TextureInfo.TextureInfo Material.MaterialNormal +mkCodecNormalTexture = + TextureInfo.TextureInfo + { extensions = Nothing, + extras = Nothing, + index = 18, + subtype = Material.MaterialNormal 1, + texCoord = 13 + } + +mkCodecOcclusionTexture :: TextureInfo.TextureInfo Material.MaterialOcclusion +mkCodecOcclusionTexture = + TextureInfo.TextureInfo + { extensions = Nothing, + extras = Nothing, + index = 19, + subtype = Material.MaterialOcclusion 2, + texCoord = 14 + } + mkCodecNode :: Node.Node mkCodecNode = Node.Node