Skip to content

Commit

Permalink
Vertex tangents.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehayden1 committed Feb 25, 2024
1 parent 4224266 commit a5d82bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Text/GLTF/Loader/Gltf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ data MeshPrimitive = MeshPrimitive
meshPrimitiveNormals :: Vector (V3 Float),
-- | A Vector of vertex positions.
meshPrimitivePositions :: Vector (V3 Float),
-- | A Vector of vertex tangents
meshPrimitiveTangents :: Vector (V4 Float),
-- | A Vector of vertex texture coordinates
meshPrimitiveTexCoords :: Vector (V2 Float),
-- | A Vector of vertex colors.
Expand Down
5 changes: 5 additions & 0 deletions src/Text/GLTF/Loader/Internal/Adapter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ attributePosition = "POSITION"
attributeNormal :: Text
attributeNormal = "NORMAL"

attributeTangent :: Text
attributeTangent = "TANGENT"

attributeTexCoord :: Text
attributeTexCoord = "TEXCOORD_0"

Expand Down Expand Up @@ -296,13 +299,15 @@ adaptMeshPrimitive Mesh.MeshPrimitive{..} = do
meshPrimitiveMode = adaptMeshPrimitiveMode mode,
meshPrimitiveNormals = maybe mempty (vertexNormals gltf buffers') normals,
meshPrimitivePositions = maybe mempty (vertexPositions gltf buffers') positions,
meshPrimitiveTangents = maybe mempty (vertexTangents gltf buffers') tangents,
meshPrimitiveTexCoords = maybe mempty (vertexTexCoords gltf buffers') texCoords,
meshPrimitiveColors =
maybe mempty (fmap (mapV4 toRatio) . vertexColors gltf buffers') colors
}
where
positions = attributes HashMap.!? attributePosition
normals = attributes HashMap.!? attributeNormal
tangents = attributes HashMap.!? attributeTangent
texCoords = attributes HashMap.!? attributeTexCoord
colors = attributes HashMap.!? attributeColors
toRatio w = fromIntegral w / fromIntegral (maxBound :: Word16)
Expand Down
5 changes: 5 additions & 0 deletions src/Text/GLTF/Loader/Internal/BufferAccessor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Text.GLTF.Loader.Internal.BufferAccessor
vertexPositions,
vertexNormals,
vertexTexCoords,
vertexTangents,
vertexColors,
imageDataRaw,
) where
Expand Down Expand Up @@ -109,6 +110,10 @@ vertexPositions = readBufferWithGet getPositions
vertexNormals :: GlTF -> Vector GltfBuffer -> AccessorIx -> Vector (V3 Float)
vertexNormals = readBufferWithGet getNormals

-- | Decode vertex tangents
vertexTangents :: GlTF -> Vector GltfBuffer -> AccessorIx -> Vector (V4 Float)
vertexTangents = readBufferWithGet getTangents

-- | Decode texture coordinates. Note that we only use the first one.
vertexTexCoords :: GlTF -> Vector GltfBuffer -> AccessorIx -> Vector (V2 Float)
vertexTexCoords = readBufferWithGet getTexCoords
Expand Down
5 changes: 5 additions & 0 deletions src/Text/GLTF/Loader/Internal/Decoders.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Text.GLTF.Loader.Internal.Decoders
getIndices32,
getPositions,
getNormals,
getTangents,
getTexCoords,
getColors,

Expand Down Expand Up @@ -46,6 +47,10 @@ getPositions = getVec3 getFloat
getNormals :: Get (Vector (V3 Float))
getNormals = getVec3 getFloat

-- | Vertex tangents binary decoder
getTangents :: Get (Vector (V4 Float))
getTangents = getVec4 getFloat

-- | Texture coordinates binary decoder
getTexCoords :: Get (Vector (V2 Float))
getTexCoords = getVec2 getFloat
Expand Down

0 comments on commit a5d82bd

Please sign in to comment.