Learn HalogenGHC loads in the background

19. A scene in three dimensions

haskell-halogen-three renders three-dimensional scenes with Three.js. As with Pixi in lesson 11, elements and properties build a VDom tree. The reconciler updates Three.js objects instead of HTML elements.

Import Halogen.Three.Elements as TE and Halogen.Three.Properties as TP for meshes, lights, transforms and event handlers.

The scene

scene converts state into a Three.View containing a camera, interaction settings and nodes. Three.defaultCamera looks at the origin from above and to the right. Three.defaultInteraction lets you orbit by dragging and zoom by scrolling.

The y-axis points upward. To make the floor horizontal, rotate Three.Plane around x with TP.rotation (Three.Euler (-pi / 2) 0 0) and place it one unit below the origin.

A mesh combines geometry and material. TE.mesh (Three.Sphere 1 48 24) (Three.standard (hex st.colour)) [...] creates a sphere of radius 1 with a lit material. The scene uses TE.ambientLight to illuminate all sides and TE.directionalLight at Three.Vec3 3 5 4 for sunlight-like lighting toward the origin. Without lighting, the standard material would appear black.

Geometry and material are mesh properties. Clicking the sphere changes only the material's colour; its geometry and other scene objects stay in place.

Three.component receives the scene through the "scene" slot. TP.onClick (\_ -> Just ()) produces Three.Fired (), handled as Scene (Three.Fired ()). The renderer finds the clicked mesh by raycasting from the pointer. Camera drags do not count as clicks; they emit Three.CameraMoved, which this example ignores.

Three.js loads from a CDN on mounting, so the scene may appear shortly after compilation.

Exercise

Animate the scene:

  1. Add startFrames to H.onInitialize. Import Halogen.Query.Event.Window qualified as Window and subscribe with void (H.subscribe (Frame <$ Window.animationFrame)).
  2. Add an angle to state and increase it on each Frame. Rotate the cube with TP.rotation (Three.Euler 0 st.angle 0).
  3. Move the sphere with TP.position (sphereAt st), where sphereAt st = Three.Vec3 0 (0.4 * sin (2 * st.angle)) 0.
  4. Add a point light just above it: TE.pointLight 0xfff1c1 8 (above (sphereAt st)). Its intensity falls with squared distance, so it needs enough intensity to illuminate nearby objects.

The renderer draws after scene changes, once per frame. Your frame updates now animate the objects while camera dragging remains available.

Lesson.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Lesson (component) where

import Clay qualified as C
import Control.Monad.State (modify)
import Control.Monad.UUID (MonadUUID)
import Data.Row (type (.==))
import Data.Text qualified as T
import Halogen qualified as H
import Halogen.HTML qualified as HH
import Halogen.HTML.Properties qualified as HP
import Halogen.Three qualified as Three
import Halogen.Three.Elements qualified as TE
import Halogen.Three.Properties qualified as TP
import UnliftIO (MonadUnliftIO)

type Slots = "scene" .== H.Slot H.VoidF (Three.SceneEvent ()) ()

data Colour = Amber | Sky | Rose | Lime
  deriving (Show, Eq, Enum, Bounded)

-- The colour after this one, and after the last the first again.
next :: Colour -> Colour
next c = if c == maxBound then minBound else succ c

hex :: Colour -> Three.Color
hex Amber = 0xf59e0b
hex Sky = 0x38bdf8
hex Rose = 0xf43f5e
hex Lime = 0x84cc16

newtype State = State {colour :: Colour}

newtype Action = Scene (Three.SceneEvent ())

-- The nodes are a VDom tree, as a render's HTML is, of Three.js objects:
-- a floor, a cube and a sphere, lit by the room and by a sun.
scene :: State -> Three.View ()
scene st =
  Three.view
    Three.defaultCamera
    Three.defaultInteraction
    [ TE.ambientLight 0xffffff 0.6
    , TE.directionalLight 0xffffff 2.5 (Three.Vec3 3 5 4)
    , TE.mesh (Three.Plane 8 8) (Three.standard 0x334155) [TP.position (Three.Vec3 0 (-1) 0), TP.rotation (Three.Euler (-pi / 2) 0 0)]
    , TE.mesh (Three.Box 1.2 1.2 1.2) (Three.standard 0x6366f1) [TP.position (Three.Vec3 (-2.5) (-0.4) 0)]
    , TE.mesh (Three.Sphere 1 48 24) (Three.standard (hex st.colour)) [TP.onClick (\_ -> Just ())]
    ]

component :: forall q o m. (MonadUnliftIO m, MonadUUID m) => H.Component q () o m
component =
  H.component initialState render [H.onAction handleAction]
  where
    initialState _ = pure State {colour = Amber}
    render :: State -> H.ComponentHTML Action Slots m
    render st =
      HH.div_
        [ HH.div
            [HP.style (C.position C.relative >> C.width (C.px 360) >> C.height (C.px 240))]
            [HH.slot "scene" () Three.component (scene st) Scene]
        , HH.p_ [HH.text ("The sphere is " <> T.toLower (T.pack (show st.colour)) <> ". Click it, or drag to look around.")]
        ]
    handleAction (Scene (Three.Fired ())) = modify (\st -> st {colour = next st.colour})
    handleAction (Scene _) = pure ()

The compiler loads once the page has started.

Your component will appear here after compilation.