Class: Engine::Components::PointLight

Inherits:
Engine::Component show all
Includes:
MatrixHelpers
Defined in:
lib/engine/components/point_light.rb

Constant Summary collapse

NR_SHADOW_CASTING_POINT_LIGHTS =
4
CUBE_DIRECTIONS =

Cubemap face directions: X, -X, Y, -Y, +Z, -Z

[
  { dir: Vector[1, 0, 0],  up: Vector[0, -1, 0] },   # +X
  { dir: Vector[-1, 0, 0], up: Vector[0, -1, 0] },   # -X
  { dir: Vector[0, 1, 0],  up: Vector[0, 0, 1] },    # +Y
  { dir: Vector[0, -1, 0], up: Vector[0, 0, -1] },   # -Y
  { dir: Vector[0, 0, 1],  up: Vector[0, -1, 0] },   # +Z
  { dir: Vector[0, 0, -1], up: Vector[0, -1, 0] }    # -Z
].freeze

Instance Attribute Summary collapse

Attributes inherited from Engine::Component

#game_object

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MatrixHelpers

#look_at, #ortho, #perspective

Methods inherited from Engine::Component

#_erase!, #destroy, #destroyed?, destroyed_components, erase_destroyed_components, method_added, #renderer?, #set_game_object, #ui_renderer?

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Attribute Details

#cast_shadowsObject

Returns the value of attribute cast_shadows.



21
22
23
# File 'lib/engine/components/point_light.rb', line 21

def cast_shadows
  @cast_shadows
end

#colourObject

Returns the value of attribute colour.



21
22
23
# File 'lib/engine/components/point_light.rb', line 21

def colour
  @colour
end

#rangeObject

Returns the value of attribute range.



21
22
23
# File 'lib/engine/components/point_light.rb', line 21

def range
  @range
end

#shadow_layer_indexObject

Returns the value of attribute shadow_layer_index.



21
22
23
# File 'lib/engine/components/point_light.rb', line 21

def shadow_layer_index
  @shadow_layer_index
end

Class Method Details

.point_lightsObject



59
60
61
# File 'lib/engine/components/point_light.rb', line 59

def self.point_lights
  @point_lights ||= []
end

.shadow_casting_lightsObject



63
64
65
# File 'lib/engine/components/point_light.rb', line 63

def self.shadow_casting_lights
  point_lights.select(&:cast_shadows).take(NR_SHADOW_CASTING_POINT_LIGHTS)
end

Instance Method Details

#awakeObject



23
24
25
26
27
28
29
# File 'lib/engine/components/point_light.rb', line 23

def awake
  @range ||= 300
  @colour ||= [1.0, 1.0, 1.0]
  @cast_shadows = false if @cast_shadows.nil?
  @shadow_layer_index = nil
  @cached_light_space_matrices = nil
end

#destroy!Object



35
36
37
# File 'lib/engine/components/point_light.rb', line 35

def destroy!
  PointLight.point_lights.delete(self)
end

#light_space_matricesObject



55
56
57
# File 'lib/engine/components/point_light.rb', line 55

def light_space_matrices
  @cached_light_space_matrices ||= compute_light_space_matrices
end

#positionObject



43
44
45
# File 'lib/engine/components/point_light.rb', line 43

def position
  game_object.local_to_world_coordinate(Vector[0, 0, 0])
end

#shadow_farObject



51
52
53
# File 'lib/engine/components/point_light.rb', line 51

def shadow_far
  @range * 2.0
end

#shadow_nearObject



47
48
49
# File 'lib/engine/components/point_light.rb', line 47

def shadow_near
  @range * 0.01
end

#startObject



31
32
33
# File 'lib/engine/components/point_light.rb', line 31

def start
  PointLight.point_lights << self
end

#update(delta_time) ⇒ Object



39
40
41
# File 'lib/engine/components/point_light.rb', line 39

def update(delta_time)
  @cached_light_space_matrices = nil if @cast_shadows
end