mikaxyz/elm-gltf/5.0.0

Gltf

Load glTF files (3D scenes/models with animations and materials) into Elm WebGL

Types

type alias Asset =
{ version : String
, copyright : Maybe String
, generator : Maybe String
}

Information about the file

type Error
= HttpError Error
| TextureError Error
| SceneNotFound
| NodeNotFound

All possible failures

type Query

Type returned by queries.

Results from a query which can be used to retrieve content.

type Option
= GenerateFlatNormals

Options for configuring the behaviour of the importer. Provide them to initWithOptions.

GenerateFlatNormals

When a triangle primitive has no normals triangles/vertices will be separated and assigned one normal per triangle. This results in "flat shading".

The spec is a bit ambiguous (for webgl v1) here so that is why this is an option.

When normals are not specified, client implementations MUST calculate flat normals

Wiring

type Msg

Internal Msg type. Handle these in your update function. Your application Msg type should contain:

type Msg
    = GltfMsg Gltf.Msg
type Gltf

Internal state. Your application model should contain one:

type alias Model =
    { gltf : Gltf
    }

Initialize a Gltf and store it in your model:

init : Model
init =
    { gltf = Gltf.init
    }

Same as init but with a list of Option:

init : Model
init =
    { gltf = Gltf.initWithOptions [ Gltf.GenerateFlatNormals ]
    }
{ toMsg : Msg -> msg
, onComplete : Result Error QueryResult -> msg
}
-> Msg
-> Gltf
-> ( Gltf, Cmd msg )

Internal update function. Call it in your update something like this:

GltfMsg msg_ ->
    Gltf.update
        { toMsg = GltfMsg
        , onComplete = GltfOnComplete
        }
        msg_
        model.gltf
        |> Tuple.mapFirst (\gltf -> { model | gltf = gltf })

The onComplete function will be called when the query is ready. Your application Msg type should handle these:

type Msg
    = GltfMsg Gltf.Msg
    | GltfOnComplete (Result Gltf.Error Gltf.QueryResult)

In the onComplete handler you can use the content functions to get a tree of nodes, animations, cameras etc.

Load content

getBinary : String -> (Msg -> msg) -> Cmd msg

Get default scene from a file of type .glb

Note: If file does not specify a default scene this returns the first in list

getGltf : String -> (Msg -> msg) -> Cmd msg

Get default scene from a file of type .gltf

Note: If file does not specify a default scene this returns the first in list

getBinaryWithQuery : String -> Query -> (Msg -> msg) -> Cmd msg

Get content from a file of type .glb by supplying one of following queries:

getGltfWithQuery : String -> Query -> (Msg -> msg) -> Cmd msg

Get content from a file of type .gltf by supplying one of following queries:

Queries

Queries the glTF file for the default scene. If no default scene is defined this will query the first scene.

sceneQuery : Int -> Query

Queries the glTF file for scene by index.

query : Query -> (Msg -> msg) -> QueryResult -> Cmd msg

Query an already loaded file

Content

animations : QueryResult -> List Animation

Get all animations

cameras : QueryResult -> List Camera

Get all cameras

nodeTrees : QueryResult -> List (Tree Node)

Get node trees returned by query

scenes : QueryResult -> List Scene

Get information of all scenes

Get all skins

cameraByIndex : Index -> QueryResult -> Maybe Camera

Get camera by index

textureWithIndex : QueryResult -> Index -> Maybe Texture

Get texture by index.