module Codec.GlTF.Mesh
  ( MeshIx(..)
  , Mesh(..)

  , MeshPrimitive(..)
  , MeshPrimitiveMode(..)
  , pattern POINTS
  , pattern LINES
  , pattern LINE_LOOP
  , pattern LINE_STRIP
  , pattern TRIANGLES
  , pattern TRIANGLE_STRIP
  , pattern TRIANGLE_FAN
 ) where

import Codec.GlTF.Prelude

import Codec.GlTF.Accessor (AccessorIx)
import Codec.GlTF.Material (MaterialIx)

newtype MeshIx = MeshIx { MeshIx -> Int
unMeshIx :: Int }
  deriving (MeshIx -> MeshIx -> Bool
(MeshIx -> MeshIx -> Bool)
-> (MeshIx -> MeshIx -> Bool) -> Eq MeshIx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MeshIx -> MeshIx -> Bool
$c/= :: MeshIx -> MeshIx -> Bool
== :: MeshIx -> MeshIx -> Bool
$c== :: MeshIx -> MeshIx -> Bool
Eq, Eq MeshIx
Eq MeshIx
-> (MeshIx -> MeshIx -> Ordering)
-> (MeshIx -> MeshIx -> Bool)
-> (MeshIx -> MeshIx -> Bool)
-> (MeshIx -> MeshIx -> Bool)
-> (MeshIx -> MeshIx -> Bool)
-> (MeshIx -> MeshIx -> MeshIx)
-> (MeshIx -> MeshIx -> MeshIx)
-> Ord MeshIx
MeshIx -> MeshIx -> Bool
MeshIx -> MeshIx -> Ordering
MeshIx -> MeshIx -> MeshIx
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MeshIx -> MeshIx -> MeshIx
$cmin :: MeshIx -> MeshIx -> MeshIx
max :: MeshIx -> MeshIx -> MeshIx
$cmax :: MeshIx -> MeshIx -> MeshIx
>= :: MeshIx -> MeshIx -> Bool
$c>= :: MeshIx -> MeshIx -> Bool
> :: MeshIx -> MeshIx -> Bool
$c> :: MeshIx -> MeshIx -> Bool
<= :: MeshIx -> MeshIx -> Bool
$c<= :: MeshIx -> MeshIx -> Bool
< :: MeshIx -> MeshIx -> Bool
$c< :: MeshIx -> MeshIx -> Bool
compare :: MeshIx -> MeshIx -> Ordering
$ccompare :: MeshIx -> MeshIx -> Ordering
$cp1Ord :: Eq MeshIx
Ord, Int -> MeshIx -> ShowS
[MeshIx] -> ShowS
MeshIx -> String
(Int -> MeshIx -> ShowS)
-> (MeshIx -> String) -> ([MeshIx] -> ShowS) -> Show MeshIx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MeshIx] -> ShowS
$cshowList :: [MeshIx] -> ShowS
show :: MeshIx -> String
$cshow :: MeshIx -> String
showsPrec :: Int -> MeshIx -> ShowS
$cshowsPrec :: Int -> MeshIx -> ShowS
Show, Value -> Parser [MeshIx]
Value -> Parser MeshIx
(Value -> Parser MeshIx)
-> (Value -> Parser [MeshIx]) -> FromJSON MeshIx
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [MeshIx]
$cparseJSONList :: Value -> Parser [MeshIx]
parseJSON :: Value -> Parser MeshIx
$cparseJSON :: Value -> Parser MeshIx
FromJSON, [MeshIx] -> Encoding
[MeshIx] -> Value
MeshIx -> Encoding
MeshIx -> Value
(MeshIx -> Value)
-> (MeshIx -> Encoding)
-> ([MeshIx] -> Value)
-> ([MeshIx] -> Encoding)
-> ToJSON MeshIx
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [MeshIx] -> Encoding
$ctoEncodingList :: [MeshIx] -> Encoding
toJSONList :: [MeshIx] -> Value
$ctoJSONList :: [MeshIx] -> Value
toEncoding :: MeshIx -> Encoding
$ctoEncoding :: MeshIx -> Encoding
toJSON :: MeshIx -> Value
$ctoJSON :: MeshIx -> Value
ToJSON, (forall x. MeshIx -> Rep MeshIx x)
-> (forall x. Rep MeshIx x -> MeshIx) -> Generic MeshIx
forall x. Rep MeshIx x -> MeshIx
forall x. MeshIx -> Rep MeshIx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MeshIx x -> MeshIx
$cfrom :: forall x. MeshIx -> Rep MeshIx x
Generic)

-- | A set of primitives to be rendered.
--
-- A node can contain one mesh. A node's transform places the mesh in the scene.
data Mesh = Mesh
  { Mesh -> Vector MeshPrimitive
primitives :: Vector MeshPrimitive
  , Mesh -> Maybe (Vector Float)
weights    :: Maybe (Vector Float)
  , Mesh -> Maybe Text
name       :: Maybe Text
  , Mesh -> Maybe Object
extensions :: Maybe Object
  , Mesh -> Maybe Value
extras     :: Maybe Value
  } deriving (Mesh -> Mesh -> Bool
(Mesh -> Mesh -> Bool) -> (Mesh -> Mesh -> Bool) -> Eq Mesh
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Mesh -> Mesh -> Bool
$c/= :: Mesh -> Mesh -> Bool
== :: Mesh -> Mesh -> Bool
$c== :: Mesh -> Mesh -> Bool
Eq, Int -> Mesh -> ShowS
[Mesh] -> ShowS
Mesh -> String
(Int -> Mesh -> ShowS)
-> (Mesh -> String) -> ([Mesh] -> ShowS) -> Show Mesh
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Mesh] -> ShowS
$cshowList :: [Mesh] -> ShowS
show :: Mesh -> String
$cshow :: Mesh -> String
showsPrec :: Int -> Mesh -> ShowS
$cshowsPrec :: Int -> Mesh -> ShowS
Show, (forall x. Mesh -> Rep Mesh x)
-> (forall x. Rep Mesh x -> Mesh) -> Generic Mesh
forall x. Rep Mesh x -> Mesh
forall x. Mesh -> Rep Mesh x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Mesh x -> Mesh
$cfrom :: forall x. Mesh -> Rep Mesh x
Generic)

instance FromJSON Mesh where
  parseJSON :: Value -> Parser Mesh
parseJSON = String -> (Object -> Parser Mesh) -> Value -> Parser Mesh
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Mesh" \Object
o -> do
    Vector MeshPrimitive
primitives <- Object
o Object -> Key -> Parser (Vector MeshPrimitive)
forall a. FromJSON a => Object -> Key -> Parser a
.:  Key
"primitives"
    Maybe (Vector Float)
weights    <- Object
o Object -> Key -> Parser (Maybe (Vector Float))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"weights"
    Maybe Text
name       <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"name"
    Maybe Object
extensions <- Object
o Object -> Key -> Parser (Maybe Object)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"extensions"
    Maybe Value
extras     <- Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"extras"
    pure Mesh :: Vector MeshPrimitive
-> Maybe (Vector Float)
-> Maybe Text
-> Maybe Object
-> Maybe Value
-> Mesh
Mesh{Maybe Text
Maybe Value
Maybe Object
Maybe (Vector Float)
Vector MeshPrimitive
extras :: Maybe Value
extensions :: Maybe Object
name :: Maybe Text
weights :: Maybe (Vector Float)
primitives :: Vector MeshPrimitive
$sel:extras:Mesh :: Maybe Value
$sel:extensions:Mesh :: Maybe Object
$sel:name:Mesh :: Maybe Text
$sel:weights:Mesh :: Maybe (Vector Float)
$sel:primitives:Mesh :: Vector MeshPrimitive
..}

instance ToJSON Mesh

-- | Geometry to be rendered with the given material.
data MeshPrimitive = MeshPrimitive
  { MeshPrimitive -> HashMap Text AccessorIx
attributes   :: HashMap Text AccessorIx
  , MeshPrimitive -> MeshPrimitiveMode
mode         :: MeshPrimitiveMode
  , MeshPrimitive -> Maybe AccessorIx
indices      :: Maybe AccessorIx
  , MeshPrimitive -> Maybe MaterialIx
material     :: Maybe MaterialIx
  , MeshPrimitive -> Maybe (Vector (HashMap Text AccessorIx))
targets      :: Maybe (Vector (HashMap Text AccessorIx))
  , MeshPrimitive -> Maybe Object
extensions   :: Maybe Object
  , MeshPrimitive -> Maybe Value
extras       :: Maybe Value
  } deriving (MeshPrimitive -> MeshPrimitive -> Bool
(MeshPrimitive -> MeshPrimitive -> Bool)
-> (MeshPrimitive -> MeshPrimitive -> Bool) -> Eq MeshPrimitive
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MeshPrimitive -> MeshPrimitive -> Bool
$c/= :: MeshPrimitive -> MeshPrimitive -> Bool
== :: MeshPrimitive -> MeshPrimitive -> Bool
$c== :: MeshPrimitive -> MeshPrimitive -> Bool
Eq, Int -> MeshPrimitive -> ShowS
[MeshPrimitive] -> ShowS
MeshPrimitive -> String
(Int -> MeshPrimitive -> ShowS)
-> (MeshPrimitive -> String)
-> ([MeshPrimitive] -> ShowS)
-> Show MeshPrimitive
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MeshPrimitive] -> ShowS
$cshowList :: [MeshPrimitive] -> ShowS
show :: MeshPrimitive -> String
$cshow :: MeshPrimitive -> String
showsPrec :: Int -> MeshPrimitive -> ShowS
$cshowsPrec :: Int -> MeshPrimitive -> ShowS
Show, (forall x. MeshPrimitive -> Rep MeshPrimitive x)
-> (forall x. Rep MeshPrimitive x -> MeshPrimitive)
-> Generic MeshPrimitive
forall x. Rep MeshPrimitive x -> MeshPrimitive
forall x. MeshPrimitive -> Rep MeshPrimitive x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MeshPrimitive x -> MeshPrimitive
$cfrom :: forall x. MeshPrimitive -> Rep MeshPrimitive x
Generic)

instance FromJSON MeshPrimitive where
  parseJSON :: Value -> Parser MeshPrimitive
parseJSON = String
-> (Object -> Parser MeshPrimitive)
-> Value
-> Parser MeshPrimitive
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"MeshPrimitive" \Object
o -> do
    HashMap Text AccessorIx
attributes <- Object
o Object -> Key -> Parser (HashMap Text AccessorIx)
forall a. FromJSON a => Object -> Key -> Parser a
.:  Key
"attributes"
    MeshPrimitiveMode
mode       <- Object
o Object -> Key -> Parser (Maybe MeshPrimitiveMode)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"mode" Parser (Maybe MeshPrimitiveMode)
-> MeshPrimitiveMode -> Parser MeshPrimitiveMode
forall a. Parser (Maybe a) -> a -> Parser a
.!= MeshPrimitiveMode
TRIANGLES
    Maybe AccessorIx
indices    <- Object
o Object -> Key -> Parser (Maybe AccessorIx)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"indices"
    Maybe MaterialIx
material   <- Object
o Object -> Key -> Parser (Maybe MaterialIx)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"material"
    Maybe (Vector (HashMap Text AccessorIx))
targets    <- Object
o Object -> Key -> Parser (Maybe (Vector (HashMap Text AccessorIx)))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"targets"
    Maybe Object
extensions <- Object
o Object -> Key -> Parser (Maybe Object)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"extensions"
    Maybe Value
extras     <- Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"extras"
    pure MeshPrimitive :: HashMap Text AccessorIx
-> MeshPrimitiveMode
-> Maybe AccessorIx
-> Maybe MaterialIx
-> Maybe (Vector (HashMap Text AccessorIx))
-> Maybe Object
-> Maybe Value
-> MeshPrimitive
MeshPrimitive{Maybe Value
Maybe Object
Maybe (Vector (HashMap Text AccessorIx))
Maybe MaterialIx
Maybe AccessorIx
HashMap Text AccessorIx
MeshPrimitiveMode
extras :: Maybe Value
extensions :: Maybe Object
targets :: Maybe (Vector (HashMap Text AccessorIx))
material :: Maybe MaterialIx
indices :: Maybe AccessorIx
mode :: MeshPrimitiveMode
attributes :: HashMap Text AccessorIx
$sel:extras:MeshPrimitive :: Maybe Value
$sel:extensions:MeshPrimitive :: Maybe Object
$sel:targets:MeshPrimitive :: Maybe (Vector (HashMap Text AccessorIx))
$sel:material:MeshPrimitive :: Maybe MaterialIx
$sel:indices:MeshPrimitive :: Maybe AccessorIx
$sel:mode:MeshPrimitive :: MeshPrimitiveMode
$sel:attributes:MeshPrimitive :: HashMap Text AccessorIx
..}

instance ToJSON MeshPrimitive

-- | The type of primitives to render.
newtype MeshPrimitiveMode = MeshPrimitiveMode { MeshPrimitiveMode -> Int
unMeshPrimitiveMode :: Int }
  deriving (MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
(MeshPrimitiveMode -> MeshPrimitiveMode -> Bool)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> Bool)
-> Eq MeshPrimitiveMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
$c/= :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
== :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
$c== :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
Eq, Eq MeshPrimitiveMode
Eq MeshPrimitiveMode
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> Ordering)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> Bool)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> Bool)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> Bool)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> Bool)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode)
-> (MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode)
-> Ord MeshPrimitiveMode
MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
MeshPrimitiveMode -> MeshPrimitiveMode -> Ordering
MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode
$cmin :: MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode
max :: MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode
$cmax :: MeshPrimitiveMode -> MeshPrimitiveMode -> MeshPrimitiveMode
>= :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
$c>= :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
> :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
$c> :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
<= :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
$c<= :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
< :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
$c< :: MeshPrimitiveMode -> MeshPrimitiveMode -> Bool
compare :: MeshPrimitiveMode -> MeshPrimitiveMode -> Ordering
$ccompare :: MeshPrimitiveMode -> MeshPrimitiveMode -> Ordering
$cp1Ord :: Eq MeshPrimitiveMode
Ord, Int -> MeshPrimitiveMode -> ShowS
[MeshPrimitiveMode] -> ShowS
MeshPrimitiveMode -> String
(Int -> MeshPrimitiveMode -> ShowS)
-> (MeshPrimitiveMode -> String)
-> ([MeshPrimitiveMode] -> ShowS)
-> Show MeshPrimitiveMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MeshPrimitiveMode] -> ShowS
$cshowList :: [MeshPrimitiveMode] -> ShowS
show :: MeshPrimitiveMode -> String
$cshow :: MeshPrimitiveMode -> String
showsPrec :: Int -> MeshPrimitiveMode -> ShowS
$cshowsPrec :: Int -> MeshPrimitiveMode -> ShowS
Show, Value -> Parser [MeshPrimitiveMode]
Value -> Parser MeshPrimitiveMode
(Value -> Parser MeshPrimitiveMode)
-> (Value -> Parser [MeshPrimitiveMode])
-> FromJSON MeshPrimitiveMode
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [MeshPrimitiveMode]
$cparseJSONList :: Value -> Parser [MeshPrimitiveMode]
parseJSON :: Value -> Parser MeshPrimitiveMode
$cparseJSON :: Value -> Parser MeshPrimitiveMode
FromJSON, [MeshPrimitiveMode] -> Encoding
[MeshPrimitiveMode] -> Value
MeshPrimitiveMode -> Encoding
MeshPrimitiveMode -> Value
(MeshPrimitiveMode -> Value)
-> (MeshPrimitiveMode -> Encoding)
-> ([MeshPrimitiveMode] -> Value)
-> ([MeshPrimitiveMode] -> Encoding)
-> ToJSON MeshPrimitiveMode
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [MeshPrimitiveMode] -> Encoding
$ctoEncodingList :: [MeshPrimitiveMode] -> Encoding
toJSONList :: [MeshPrimitiveMode] -> Value
$ctoJSONList :: [MeshPrimitiveMode] -> Value
toEncoding :: MeshPrimitiveMode -> Encoding
$ctoEncoding :: MeshPrimitiveMode -> Encoding
toJSON :: MeshPrimitiveMode -> Value
$ctoJSON :: MeshPrimitiveMode -> Value
ToJSON, (forall x. MeshPrimitiveMode -> Rep MeshPrimitiveMode x)
-> (forall x. Rep MeshPrimitiveMode x -> MeshPrimitiveMode)
-> Generic MeshPrimitiveMode
forall x. Rep MeshPrimitiveMode x -> MeshPrimitiveMode
forall x. MeshPrimitiveMode -> Rep MeshPrimitiveMode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MeshPrimitiveMode x -> MeshPrimitiveMode
$cfrom :: forall x. MeshPrimitiveMode -> Rep MeshPrimitiveMode x
Generic)

pattern POINTS :: MeshPrimitiveMode
pattern $bPOINTS :: MeshPrimitiveMode
$mPOINTS :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
POINTS = MeshPrimitiveMode 0

pattern LINES :: MeshPrimitiveMode
pattern $bLINES :: MeshPrimitiveMode
$mLINES :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
LINES = MeshPrimitiveMode 1

pattern LINE_LOOP :: MeshPrimitiveMode
pattern $bLINE_LOOP :: MeshPrimitiveMode
$mLINE_LOOP :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
LINE_LOOP = MeshPrimitiveMode 2

pattern LINE_STRIP :: MeshPrimitiveMode
pattern $bLINE_STRIP :: MeshPrimitiveMode
$mLINE_STRIP :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
LINE_STRIP = MeshPrimitiveMode 3

pattern TRIANGLES :: MeshPrimitiveMode
pattern $bTRIANGLES :: MeshPrimitiveMode
$mTRIANGLES :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
TRIANGLES = MeshPrimitiveMode 4

pattern TRIANGLE_STRIP :: MeshPrimitiveMode
pattern $bTRIANGLE_STRIP :: MeshPrimitiveMode
$mTRIANGLE_STRIP :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
TRIANGLE_STRIP = MeshPrimitiveMode 5

pattern TRIANGLE_FAN :: MeshPrimitiveMode
pattern $bTRIANGLE_FAN :: MeshPrimitiveMode
$mTRIANGLE_FAN :: forall r. MeshPrimitiveMode -> (Void# -> r) -> (Void# -> r) -> r
TRIANGLE_FAN = MeshPrimitiveMode 6