Skip to content

Commit

Permalink
Additional textures & tangent spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehayden1 authored and sgillespie committed May 1, 2024
1 parent cb50e5d commit 13f7ab9
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 15 deletions.
41 changes: 38 additions & 3 deletions test/Text/GLTF/Loader/Internal/AdapterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ 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
}

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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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 = []
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion test/Text/GLTF/Loader/Internal/BufferAccessorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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' =
Expand Down
113 changes: 102 additions & 11 deletions test/Text/GLTF/Loader/Test/MkGltf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mkCodecGltf =
[ mkCodecAccessorIndices,
mkCodecAccessorPositions,
mkCodecAccessorNormals,
mkCodecAccessorTangents,
mkCodecAccessorTexCoords,
mkCodecAccessorColors
],
Expand All @@ -48,6 +49,7 @@ mkCodecGltf =
[ mkCodecBufferIndices,
mkCodecBufferPositions,
mkCodecBufferNormals,
mkCodecBufferTangents,
mkCodecBufferTexCoords,
mkCodecBufferColors,
mkCodecBufferImage
Expand All @@ -57,6 +59,7 @@ mkCodecGltf =
[ mkCodecBufferViewIndices,
mkCodecBufferViewPositions,
mkCodecBufferViewNormals,
mkCodecBufferViewTangents,
mkCodecBufferViewTexCoords,
mkCodecBufferViewColors,
mkCodecBufferViewImage
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 13f7ab9

Please sign in to comment.