This package provides convenient ways to define and use perspective and
orthographic cameras in 3D. It is based on elm-geometry
and has two main goals:
elm-explorations/linear-algebra
directlyTo define a camera, you first create a viewpoint which represents the position and orientation of the camera:
import Viewpoint3d
import Point3d
import Direction3d
cameraViewpoint =
Viewpoint3d.lookAt
{ eyePoint = Point3d.meters 10 0 5
, focalPoint = Point3d.origin
, upDirection = Direction3d.positiveZ
}You can then create either a perspective or orthographic camera from the viewpoint:
import Camera3d
import Angle
import Length
perspectiveCamera =
Camera3d.perspective
{ viewpoint = cameraViewpoint
, verticalFieldOfView = Angle.degrees 30
}
orthographicCamera =
Camera3d.orthographic
{ viewpoint = cameraViewpoint
, viewportHeight = Length.meters 5
}Once you have a camera, you can use it to get WebGL view and projection matrices:
import WebGL.Matrices as WebGL
viewMatrix =
WebGL.viewMatrix camera
projectionMatrix =
WebGL.projectionMatrix camera
{ nearClipDepth = Length.meters 0.1
, farClipDepth = Length.meters 100
, aspectRatio = 16 / 9
}You can also use a Camera3d to project points, lines, triangles and polylines
from 3D to 2D:
import Point3d.Projection as Point3d
import LineSegment3d.Projection as LineSegment3d
screen =
Rectangle2d.from Point2d.origin
(Point2d.pixels 800 600)
point2d =
point3d |> Point3d.toScreenSpace camera screen
lineSegment2d =
lineSegment3d
|> LineSegment3d.toScreenSpace camera screenThis allows you to, for example, do a perspective projection of 3D points and lines into 2D so that those points and lines can be rendered with SVG (taking advantage of SVG features like perfect circles and dashed lines which are difficult to do with WebGL):

A few more features are planned:
Please open a new issue if you
run into a bug, if any documentation is missing/incorrect/confusing, or if
there's a new feature that you would find useful. For general questions about
using elm-3d-camera, try:
You can also find me on Twitter (@ianemackenzie),
where I occasionally post elm-geometry-related stuff like demos or new
releases. Have fun, and don't be afraid to ask for help!