Skip to content

camera3d

stable

3D camera plugin providing a Camera3D class with position/target/FOV state, orbit, zoom and fly-style movement controls, and real column-major 4x4 view, perspective and orthographic matrix computation.

use plugin camera3d::{Camera3D.new, set_position, get_position, …}
22 functions Systems
/ filter jk navigate Esc clear
Functions (22)
  1. Camera3D.new Create a camera with sensible defaults
  2. set_position Set the camera eye position
  3. get_position Get the camera eye position
  4. set_target Set the point the camera looks at
  5. get_target Get the current look-at target
  6. set_fov Set the vertical field of view in degrees
  7. get_fov Get the vertical field of view in degrees
  8. set_aspect Set the viewport aspect ratio
  9. set_near_far Set the near and far clip planes
  10. set_up Set the camera up vector
  11. get_up Get the camera up vector
  12. get_direction Get the normalized forward direction
  13. orbit Rotate the camera around its target
  14. zoom Move the camera closer to or farther from the target
  15. move_forward Move camera and target along the view direction
  16. move_right Strafe camera and target left/right
  17. move_up Move camera and target along the up vector
  18. get_view_matrix Compute the look-at view matrix
  19. get_projection_matrix Compute the perspective projection matrix
  20. get_ortho_matrix Compute an orthographic projection matrix
  21. get_distance Get the distance from camera to target
  22. look_at Aim the camera at a point without moving it

Create a camera with sensible defaults

Creates a new 3D camera with default state: position (0, 1, -5), target (0, 0, 0), up (0, 1, 0), 60° FOV, 16:9 aspect ratio, and near/far planes of 0.1/1000.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
print("fov: {cam.get_fov()}")  // 60

Set the camera eye position

Sets the camera eye position in world space. The camera keeps looking at its current target.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_position(10, 5, -10)

Get the camera eye position

Returns the current eye position as a table with x, y, and z number fields.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
let pos = cam.get_position()
print("camera at ({pos["x"]}, {pos["y"]}, {pos["z"]})")

Set the point the camera looks at

Sets the world-space point the camera is looking at.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_target(0, 2, 0)

Get the current look-at target

Returns the current look-at target as a table with x, y, and z number fields.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
let t = cam.get_target()
print("looking at ({t["x"]}, {t["y"]}, {t["z"]})")

Set the vertical field of view in degrees

Sets the vertical field of view in degrees. The value is clamped to the range 1179.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_fov(90)
print("fov: {cam.get_fov()}")  // 90

Get the vertical field of view in degrees

Returns the current vertical field of view in degrees.

Set the viewport aspect ratio

Sets the viewport aspect ratio (width divided by height) used by the projection matrices. Update this whenever the window is resized.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_aspect(1920.0 / 1080.0)

Set the near and far clip planes

Sets the near and far clip plane distances used by the projection matrices.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_near_far(0.01, 500)

Set the camera up vector

Sets the camera up vector used for view matrix computation and strafing. Defaults to (0, 1, 0).

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_up(0, 0, 1)  // Z-up convention

Get the camera up vector

Returns the current up vector as a table with x, y, and z number fields.

Get the normalized forward direction

Returns the normalized forward direction (from position toward target) as a table with x, y, and z number fields. Useful for raycasting or spawning objects in front of the camera.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
let dir = cam.get_direction()
print("forward: ({dir["x"]}, {dir["y"]}, {dir["z"]})")

Rotate the camera around its target

Rotates the camera around its target by the given yaw and pitch deltas, in degrees, keeping the distance constant. Pitch is clamped to ±89° to avoid flipping over the poles. Ideal for mouse-drag orbit controls.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.orbit(45, 0)    // rotate 45 degrees around the target
cam.orbit(0, -15)   // tilt down 15 degrees

Move the camera closer to or farther from the target

Moves the camera along the line to its target, changing the orbit radius by delta world units. Positive values move away, negative values move closer. Distance is clamped to a minimum of 0.01.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.zoom(-2)  // move 2 units closer
print("distance: {cam.get_distance()}")

Move camera and target along the view direction

Translates both the camera position and its target along the view direction by amount units (fly-style movement). Negative values move backward.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.move_forward(1.5)   // W key
cam.move_forward(-1.5)  // S key

Strafe camera and target left/right

Strafes both the camera position and its target sideways, along the cross product of the view direction and the up vector. Negative values strafe left.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.move_right(2)   // D key
cam.move_right(-2)  // A key

Move camera and target along the up vector

Translates both the camera position and its target along the normalized up vector. Negative values move down.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.move_up(1)

Compute the look-at view matrix

Computes the look-at view matrix from the current position, target, and up vector. Returns a table of 16 numbers indexed 015 in column-major order, ready to upload to a GPU uniform.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
let view = cam.get_view_matrix()
print("m[0] = {view[0]}")

Compute the perspective projection matrix

Computes the perspective projection matrix from the current FOV, aspect ratio, and near/far planes. Returns a table of 16 numbers indexed 015 in column-major order.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_fov(75)
let proj = cam.get_projection_matrix()

Compute an orthographic projection matrix

Computes an orthographic projection matrix using the camera's aspect ratio and near/far planes, where size is the vertical extent of the view volume in world units (width is size * aspect). Returns a table of 16 numbers in column-major order. Useful for 2D overlays or isometric views.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
let ortho = cam.get_ortho_matrix(10)

Get the distance from camera to target

Returns the distance between the camera position and its target, i.e. the current orbit radius.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
print("distance: {cam.get_distance()}")

Aim the camera at a point without moving it

Aims the camera at the given world-space point without moving its position. Equivalent to set_target.

use plugin camera3d::{Camera3D}

let cam = Camera3D.new()
cam.set_position(5, 5, 5)
cam.look_at(0, 1, 0)
enespt-br