Class: Engine::Components::OrthographicCamera

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

Instance Attribute Summary collapse

Attributes inherited from Engine::Component

#game_object

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, #start, #ui_renderer?

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Attribute Details

#farObject (readonly)

Returns the value of attribute far.



6
7
8
# File 'lib/engine/components/orthographic_camera.rb', line 6

def far
  @far
end

#nearObject (readonly)

Returns the value of attribute near.



6
7
8
# File 'lib/engine/components/orthographic_camera.rb', line 6

def near
  @near
end

Instance Method Details

#awakeObject



10
11
12
13
# File 'lib/engine/components/orthographic_camera.rb', line 10

def awake
  @near ||= 0
  Engine::Camera.instance = self
end

#destroyObject



15
16
17
# File 'lib/engine/components/orthographic_camera.rb', line 15

def destroy
  Engine::Camera.instance = nil if Engine::Camera.instance == self
end

#inverse_vp_matrixObject



38
39
40
# File 'lib/engine/components/orthographic_camera.rb', line 38

def inverse_vp_matrix
  @inverse_vp_matrix ||= matrix.inverse
end

#matrixObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/engine/components/orthographic_camera.rb', line 19

def matrix
  @matrix ||=
    begin
      right = game_object.right
      up = game_object.up
      forward = game_object.forward
      world_pos = game_object.world_pos

      view_matrix = Matrix[
        [right[0], right[1], right[2], -right.dot(world_pos)],
        [up[0], up[1], up[2], -up.dot(world_pos)],
        [forward[0], forward[1], forward[2], -forward.dot(world_pos)],
        [0, 0, 0, 1]
      ]

      (projection * view_matrix).transpose
    end
end

#positionObject



42
43
44
# File 'lib/engine/components/orthographic_camera.rb', line 42

def position
  game_object.world_pos
end

#projectionObject



46
47
48
49
50
# File 'lib/engine/components/orthographic_camera.rb', line 46

def projection
  half_w = @width / 2.0
  half_h = @height / 2.0
  ortho(-half_w, half_w, -half_h, half_h, @near, @far)
end

#update(delta_time) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/engine/components/orthographic_camera.rb', line 52

def update(delta_time)
  if game_object.world_transform_version != @cached_transform_version
    @matrix = nil
    @inverse_vp_matrix = nil
  end
  @cached_transform_version = game_object.world_transform_version
end