ianmackenzie/elm-3d-camera/1.0.2

NOTE: the latest version is 4.0.1

Camera3d

A Camera3d is a perspective or orthographic camera in 3D, encapsulating the camera's viewpoint and projection matrix as well as the dimensions of the screen the camera renders to. This module contains functions for:

  • Defining perspective and orthographic cameras
  • Obtaining view and projection matrices for a given camera, which can then be used for WebGL rendering
  • Using cameras to project 3D geometry to 2D screen space
type alias Camera3d =
Camera3d

Constructors

Cameras have some commmon properties regardless of how they are constructed:

  • viewpoint defines the position and orientation of the camera in 3D space.
  • nearClipDistance and farClipDistance specify the standard near and far clipping planes used when rendering.
  • screenWidth and screenHeight specify the dimensions in pixels of the screen that will be rendered to (the dimensions of an actual WebGl element, for example). This is used to determine aspect ratio when constructing the camera's projection matrix, but also by functions such as Camera3d.point2d which convert from 3D world space to 2D screen space for a given camera.
{ viewpoint : Viewpoint3d
, screenWidth : Float
, screenHeight : Float
, verticalFieldOfView : Float
, nearClipDistance : Float
, farClipDistance : Float
}
-> Camera3d

Create a perspective camera with the common camera properties plus vertical field of view given in radians. The horizontal field of view will be chosen to match the aspect ratio of the screen given by screenWidth and screenHeight.

perspectiveCamera =
    Camera3d.perspective
        { viewpoint = cameraViewpoint
        , verticalFieldOfView = degrees 30
        , nearClipDistance = 0.1
        , farClipDistance = 1000
        , screenWidth = 1024
        , screenHeight = 768
        }
{ viewpoint : Viewpoint3d
, screenWidth : Float
, screenHeight : Float
, viewportHeight : Float
, nearClipDistance : Float
, farClipDistance : Float
}
-> Camera3d

Create an orthographic camera with the common camera properties plus the height of the orthographic viewport: this is the height in 3D world units of the section of the model to be rendered. (The width will be chosen to match the aspect ratio of the screen given by screenWidth and screenHeight.)

orthographicCamera =
    Camera3d.orthographic
        { viewpoint = cameraViewpoint
        , viewportHeight = 5
        , nearClipDistance = 0.1
        , farClipDistance = 1000
        , screenWidth = 1024
        , screenHeight = 768
        }

Properties

viewpoint : Camera3d -> Viewpoint3d

Get the viewpoint defining the position and orientation of a camera.

screenWidth : Camera3d -> Float

Get the width of the screen rendered to by a camera.

screenHeight : Camera3d -> Float

Get the height of the screen rendered to by a camera.

Matrices

viewMatrix : Camera3d -> Mat4

Get the view matrix of a camera;

Camera3d.viewMatrix camera

is shorthand for

Viewpoint3d.viewMatrix (Camera3d.viewpoint camera)
modelViewMatrix : Frame3d -> Camera3d -> Mat4

Construct a WebGL model-view matrix given a camera and a Frame3d that defines the position and orientation of an object;

Camera3d.modelViewMatrix modelFrame camera

is shorthand for

Viewpoint3d.modelViewMatrix
    modelFrame
    (Camera3d.viewpoint camera)
projectionMatrix : Camera3d -> Mat4

Get the projection matrix of a camera. Multiplying by this matrix converts from eye coordinates to WebGL normalized device coordinates.

modelViewProjectionMatrix : Frame3d -> Camera3d -> Mat4

Get the full model-view-projection matrix given a camera and a Frame3d that defines the position and orientation of an object;

Camera3d.modelViewProjectionMatrix modelFrame camera

is equivalent to

Matrix4.mul (Camera3d.projectionMatrix camera)
    (Camera3d.modelViewMatrix modelFrame camera)