Skip to Content
defold-typescript libraries
Dragoshadefold-light-and-shadows

Dragosha/defold-light-and-shadows/light_and_shadows.light

The light sources manager. It keeps the list of point light sources and the camera position, and each frame writes the sorted sources and the camera values into light_and_shadows.constants. light_setting.script calls set_color and update, and a scene controller calls set_position as its camera moves. Assigning a new value to a field goes through a cast, since a namespace import is read-only.

  • GitHub: Dragosha/defold-light-and-shadows — pinned to v0.9.1
    1. Pick a release from Dragosha/defold-light-and-shadows releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as light from "light_and_shadows.light"

Functions

set_color(fog_color?: Vector4, fog?: Vector4, ambient?: Vector4, clear_color?: Vector4)

Sets the base light uniform constants in light_and_shadows.constants. An omitted value leaves its constant as it is. It also posts clear_color to @render: with clear_color, or fog_color when that is omitted.

Parameters

  • fog_color?: Vector4 — The fog color in RGB; w is the fog density.
  • fog?: Vector4 — The minimum and maximum Z in camera view space where the fog begins and ends.
  • ambient?: Vector4 — The ambient color.
  • clear_color?: Vector4 — The color that clears the default render target, which is the background color.

set_position(cam_rotation: Quaternion, cam_look_at_position: Vector3, cam_position: Vector3, cam_z?: number)

Sets the camera rotation, the point the camera looks at, and the camera position.

Parameters

  • cam_rotation: Quaternion — The camera rotation.
  • cam_look_at_position: Vector3 — The point the camera looks at.
  • cam_position: Vector3 — The camera world position.
  • cam_z?: number — The camera Z coordinate, the distance from the focus point, for depth of field. Omitted, cam_z keeps its value.

reset(self: object)

Clears the light sources sent to the shader and restores the module's default ambient, fog color and fog constants.

Parameters

  • self: object — Unused.

update(self: object, dt: number)

Prepares the constants for the render script: updates each source's position and spotlight direction from its game object, sorts the sources by distance from the point the camera looks at into light_and_shadows.constants.lights, attenuates their power, and copies the camera values. Call it from update.

Parameters

  • self: object — Unused.
  • dt: number — Unused.

Variables

list: LuaTable<Hash, LightSource>

Every point light source, keyed by its game object id. update reads it every frame.

cam_look_at: Vector3

The point the camera looks at, set by set_position. Light sources are sorted by their distance from it.

cam_position: Vector3

The camera world position, set by set_position.

cam_view: Quaternion

The camera rotation, set by set_position. A source with rotate set turns to it.

top_v: Vector3

The up vector, rotated by a game object's world rotation to give a spotlight's direction.

cam_z: number

The camera Z coordinate, the distance from the focus point that depth of field needs. Set it here or with set_position's cam_z.

attenuation_coefficient: number

Divides a source's squared distance from the camera focus point before it scales the source's power.

Types

LightSource

LightSource.value: Vector4

The color in x, y, z. w is the power update attenuates by distance; bulb.script sets it to 1 / power.

LightSource.current_value?: Vector4

The color sent to the shader, its w attenuated by distance. update creates it from nothing when it is absent.

LightSource.position: Vector4

The world position. w carries the spotlight smoothness, from 0 (soft) to 1 (hard).

LightSource.direction: Vector4

The spotlight direction. w is the cone angle in radians; 0 or less turns the spotlight off.

LightSource.num: number

A positive number keeps the source out of the distance sort and always visible; the default is -1.

LightSource.static?: boolean

When true, update never reads the game object's world position again.

LightSource.static_spot?: boolean

When true, update never reads the game object's rotation for the spotlight direction again.

LightSource.rotate?: boolean

When true, update rotates the game object to the camera rotation, as a billboard.

LightSource.y_offset?: number

Added to the world Y position each frame.

LightSource.id?: Hash

The game object id, as bulb.script stores it.

LightSource.timer?: number

A timer bulb.script stores; update does not read it.

On this page