justgook/webgl-playground/2.0.0

NOTE: the latest version is 5.0.0

Playground.Advanced

Advanced user section, for:

  1. packages authors (if you like create addons)
  2. custom Shape creation
  3. embedding into other app

Shape

custom : Number -> Number -> Render -> Shape

Create your own shape

redRect w h =
    (\uP uT opacity ->
        WebGL.entity
            rectVertexShader
            fillFragment
            clipPlate
            { color = Math.Vector4.vec4 1 0 0 opacity
            , uP = uP
            , uT = uT
            }
    )
        |> custom w h
useTexture : String -> (Texture -> Shape) -> Shape

Get texture for your custom Shape

useTexture "image.png" <|
    \t ->
        custom 32 32 <|
            myCustomRender t

Types

type alias Render =
Translate -> ScaleRotateSkew -> Opacity -> Entity

Create Your own Render and use it in Advanced.custom to create Shape

redRect : Render
redRect uP uT opacity =
    WebGL.entityWith
        defaultEntitySettings
        vertNone
        fragFill
        mesh
        { color = setAlpha color opacity
        , uP = uP
        , uT = uT
        }
type alias Opacity =
Float
type alias ScaleRotateSkew =
Vec4

Vec4 representing part of transform matrix for Advanced.custom

| x y 0 |
| z w 0 |
| 0 0 1 |
type alias Translate =
Vec2

Vec2 representing part of transform matrix for Advanced.custom

| 1 0 x |
| 0 1 y |
| 0 0 1 |

Embedding

(Computer -> memory -> List Shape)
-> (Computer -> memory -> memory)
-> memory
-> { init : ( Game memory, Cmd Msg )
, view : Game memory -> Html a
, update : Msg -> Game memory -> ( Game memory, Cmd Msg )
}

When you need advanced application or just need to playground be part of your app

{ keys : Sub Msg
, time : Sub Msg
, click : Sub Msg
, mouse : Sub Msg
, resize : Sub Msg
, all : Sub Msg
}

Playground have different subscriptions:

  • picture uses only subscriptions.resize
  • animation uses only subscriptions.resize and subscriptions.time
  • game uses all (shortcut subscriptions.all)

When embedding playground you can choose what to use:

  • keys - listen to keyboard events (keyUp and keyDown) and updates
  • time - updates Computer.time each frame
  • click - listen on mouse movement Computer.mouse.down/click
  • mouse - listen on mouse movement Computer.mouse.x/y
  • resize - listen on window resize and updates Computer.screen
resize : Int -> Int -> Msg

Embedded target is be part of your application, most of time it will not take whole screen, so it is up to you decide on dimensions, and pass it to update function as Msg

cacheTextures : ( String, Texture ) -> Msg

All textures start loading only after first time request in view, with slow network that can take some time, but for more advanced games and more smooth transition you can cache textures for later use, so when you need show new image - texture already there.