ianmackenzie/elm-3d-camera/2.0.0

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 units coordinates =
Camera3d units coordinates

Constructors

Cameras have a couple of commmon properties regardless of how they are constructed:

  • viewpoint defines the position and orientation of the camera in 3D space.
  • clipDepth specifies the standard near clipping plane used when rendering. The far clipping plane is assumed to be at infinity.
{ viewpoint : Viewpoint3d units coordinates
, clipDepth : Quantity Float units
, verticalFieldOfView : Angle
}
-> Camera3d units coordinates

Create a perspective camera from a viewpoint, a vertical field of view and a clip depth.

perspectiveCamera =
    Camera3d.perspective
        { viewpoint = cameraViewpoint
        , verticalFieldOfView = Angle.degrees 30
        , clipDepth = Length.meters 0.1
        }
{ viewpoint : Viewpoint3d units coordinates
, clipDepth : Quantity Float units
, viewportHeight : Quantity Float units
}
-> Camera3d units coordinates

Create an orthographic camera from a viewpoint, a clip depth and the height of the orthographic viewport: this is the height in 3D world units of the section of the model to be rendered.

orthographicCamera =
    Camera3d.orthographic
        { viewpoint = cameraViewpoint
        , viewportHeight = Length.meters 5
        , clipDepth = Length.meters 0.1
        }

Properties

viewpoint : Camera3d units coordinates -> Viewpoint3d units coordinates

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

clipDepth : Camera3d units coordinates -> Quantity Float units

Get the clip depth of a camera.

clipPlane : Camera3d units coordinates -> Plane3d units coordinates

Get the clip plane of a camera.

Ray casting

ray :
Camera3d units coordinates
-> Rectangle2d screenUnits screenCoordinates
-> Point2d screenUnits screenCoordinates
-> Axis3d units coordinates

Given a camera and a 2D screen point, calculate the corresponding 3D ray as an Axis3d. Conceptually, the ray will pass through the given point on the screen and will have direction equal to the viewing direction at that point.

For a perspective camera, the origin of the ray will be constant (always equal to the camera's eye point) and the direction will vary depending on the 2D screen point. For an orthographic camera, the direction of the ray will be constant (the view direction of the camera) but the origin will vary depending on the 2D screen point.

WebGL rendering

viewMatrix : Camera3d units coordinates -> Mat4

Get the view matrix of a camera;

Camera3d.viewMatrix camera

is shorthand for

Viewpoint3d.viewMatrix (Camera3d.viewpoint camera)
Frame3d units coordinates defines
-> Camera3d units coordinates
-> 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)
projectionParameters : { screenAspectRatio : Float } -> Camera3d units coordinates -> Vec4

Construct a special Vec4 containing values useful for settting up perspective and orthographic projection in WebGL. For a perspective camera, the entries are

clip depth
screen aspect ratio
1 / camera frustum slope
0

For an orthographic camera, the entries are

clip depth
screen aspect ratio
0
-2 / camera viewport height

Currently used internally by elm-3d-scene.