Camera

Namespace: camera Language: Lua Type: Defold Lua File: camera_ddf.proto Source: engine/gamesys/proto/gamesys/camera_ddf.proto

Messages to control camera components and camera focus.

API

aspect_ratio

Type: PROPERTY The ratio between the frustum width and height. Used when calculating the projection of a perspective camera. The type of the property is number.

Examples

function init(self)
  local aspect_ratio = go.get("#camera", "aspect_ratio")
  go.set("#camera", "aspect_ratio", 1.2)
end

camera.get_aspect_ratio

Type: FUNCTION Gets the effective aspect ratio of the camera. If auto aspect ratio is enabled, returns the aspect ratio calculated from the current render target dimensions. Otherwise returns the manually set aspect ratio.

Parameters

Returns

camera.get_auto_aspect_ratio

Type: FUNCTION Returns whether auto aspect ratio is enabled. When enabled, the camera automatically calculates aspect ratio from render target dimensions. When disabled, uses the manually set aspect ratio value.

Parameters

Returns

camera.get_cameras

Type: FUNCTION This function returns a table with all the camera URLs that have been registered in the render context.

Returns

Examples

for k,v in pairs(camera.get_cameras()) do
    render.set_camera(v)
    render.draw(...)
    render.set_camera()
end

camera.get_enabled

Type: FUNCTION get enabled

Parameters

Returns

camera.get_far_z

Type: FUNCTION get far z

Parameters

Returns

camera.get_fov

Type: FUNCTION get field of view

Parameters

Returns

camera.get_near_z

Type: FUNCTION get near z

Parameters

Returns

camera.get_orthographic_mode

Type: FUNCTION get orthographic zoom mode

Parameters

Returns

camera.get_orthographic_zoom

Type: FUNCTION get orthographic zoom

Parameters

Returns

camera.get_projection

Type: FUNCTION get projection matrix

Parameters

Returns

camera.get_view

Type: FUNCTION get view matrix

Parameters

Returns

camera.ORTHO_MODE_AUTO_COVER

Type: CONSTANT Computes zoom so the original display area covers the entire window while preserving aspect ratio. Equivalent to using max(window_width/width, window_height/height).

camera.ORTHO_MODE_AUTO_FIT

Type: CONSTANT Computes zoom so the original display area (game.project width/height) fits inside the window while preserving aspect ratio. Equivalent to using min(window_width/width, window_height/height).

camera.ORTHO_MODE_FIXED

Type: CONSTANT Uses the manually set orthographic zoom value (camera.set_orthographic_zoom).

camera.screen_to_world

Type: FUNCTION Converts a screen-space 2D point with view depth to a 3D world point. z is the view depth in world units measured from the camera plane along the camera forward axis. If a camera isn’t specified, the last enabled camera is used.

Parameters

Returns

Examples

Place objects at the touch point with a random Z position, keeping them within the visible view zone.

 function on_input(self, action_id, action)
     if action_id == hash("touch") then
         if action.pressed then
             local percpective_camera = msg.url("#perspective_camera")
             local random_z = math.random(camera.get_near_z(percpective_camera) + 0.01, camera.get_far_z(percpective_camera) - 0.01)
             local world_position = camera.screen_to_world(vmath.vector3(action.screen_x, action.screen_y, random_z), percpective_camera)
             go.set_position(world_position, "/go1")
         end
     end
 end

camera.screen_xy_to_world

Type: FUNCTION Converts 2D screen coordinates (x,y) to the 3D world-space point on the camera’s near plane for that pixel. If a camera isn’t specified, the last enabled camera is used.

Parameters

Returns

Examples

Place objects at the touch point.

 function on_input(self, action_id, action)
     if action_id == hash("touch") then
         if action.pressed then
             local world_position = camera.screen_xy_to_world(action.screen_x, action.screen_y)
             go.set_position(world_position, "/go1")
         end
     end
 end

camera.set_aspect_ratio

Type: FUNCTION Sets the manual aspect ratio for the camera. This value is only used when auto aspect ratio is disabled. To disable auto aspect ratio and use this manual value, call camera.set_auto_aspect_ratio(camera, false).

Parameters

camera.set_auto_aspect_ratio

Type: FUNCTION Enables or disables automatic aspect ratio calculation. When enabled (true), the camera automatically calculates aspect ratio from render target dimensions. When disabled (false), uses the manually set aspect ratio value.

Parameters

camera.set_far_z

Type: FUNCTION set far z

Parameters

camera.set_fov

Type: FUNCTION set field of view

Parameters

camera.set_near_z

Type: FUNCTION set near z

Parameters

camera.set_orthographic_mode

Type: FUNCTION set orthographic zoom mode

Parameters

camera.set_orthographic_zoom

Type: FUNCTION set orthographic zoom

Parameters

camera.world_to_screen

Type: FUNCTION Converts a 3D world position to screen-space coordinates with view depth. Returns a vector3 where x and y are in screen pixels and z is the view depth in world units measured from the camera plane along the camera forward axis. The returned z can be used with camera.screen_to_world to reconstruct the world position on the same pixel ray. If a camera isn’t specified, the last enabled camera is used.

Parameters

Returns

Examples

Convert go position into screen pisition

 go.update_world_transform("/go1")
 local world_pos = go.get_world_position("/go1")
 local screen_pos = camera.world_to_screen(world_pos)

far_z

Type: PROPERTY Camera frustum far plane. The type of the property is float.

Examples

function init(self)
  local far_z = go.get("#camera", "far_z")
  go.set("#camera", "far_z", 10)
end

fov

Type: PROPERTY Vertical field of view of the camera. The type of the property is float.

Examples

function init(self)
  local fov = go.get("#camera", "fov")
  go.set("#camera", "fov", fov + 0.1)
  go.animate("#camera", "fov", go.PLAYBACK_ONCE_PINGPONG, 1.2, go.EASING_LINEAR, 1)
end

near_z

Type: PROPERTY Camera frustum near plane. The type of the property is float.

Examples

function init(self)
  local near_z = go.get("#camera", "near_z")
  go.set("#camera", "near_z", 10)
end

orthographic_zoom

Type: PROPERTY Zoom level when using an orthographic projection. The type of the property is float.

Examples

function init(self)
  local orthographic_zoom = go.get("#camera", "orthographic_zoom")
  go.set("#camera", "orthographic_zoom", 2.0)
  go.animate("#camera", "orthographic_zoom", go.PLAYBACK_ONCE_PINGPONG, 0.5, go.EASING_INOUTQUAD, 2)
end

projection

Type: PROPERTY READ ONLY The calculated projection matrix of the camera. The type of the property is matrix4.

Examples

function init(self)
  local projection = go.get("#camera", "projection")
end

set_camera

Type: MESSAGE Post this message to a camera-component to set its properties at run-time.

Parameters

Examples

In the examples, it is assumed that the instance of the script has a camera-component with id “camera”.

msg.post("#camera", "set_camera", {aspect_ratio = 16/9, fov = math.pi * 0.5, near_z = 0.1, far_z = 500})

view

Type: PROPERTY READ ONLY The calculated view matrix of the camera. The type of the property is matrix4.

Examples

function init(self)
  local view = go.get("#camera", "view")
end