Class: Ashton::Lighting::Manager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ashton/lighting/manager.rb

Overview

Based on Catalin Zima’s shader based dynamic shadows system. www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ashton/lighting/manager.rb', line 18

def initialize(options = {})
  options = {
      width: $window.width,
      height: $window.height,
      camera_x: 0,
      camera_y: 0,
      z: 0,
  }.merge! options

  @camera_x, @camera_y = options[:camera_x], options[:camera_y]
  @z = options[:z]

  @lights = Set.new
  @shadows = Ashton::Texture.new options[:width], options[:height]
end

Instance Attribute Details

#camera_xObject

Returns the value of attribute camera_x.



10
11
12
# File 'lib/ashton/lighting/manager.rb', line 10

def camera_x
  @camera_x
end

#camera_yObject

Returns the value of attribute camera_y.



10
11
12
# File 'lib/ashton/lighting/manager.rb', line 10

def camera_y
  @camera_y
end

#zObject

Returns the value of attribute z.



10
11
12
# File 'lib/ashton/lighting/manager.rb', line 10

def z
  @z
end

Instance Method Details

#add(light) ⇒ Ashton::LightSource Also known as: <<

Parameters:

  • light (Ashton::LightSource)

Returns:

  • (Ashton::LightSource)

Raises:

  • (TypeError)


36
37
38
39
40
41
# File 'lib/ashton/lighting/manager.rb', line 36

def add(light)
  raise TypeError unless light.is_a? LightSource

  @lights << light
  light
end

#create_light(*args) ⇒ Ashton::LightSource

Returns:

  • (Ashton::LightSource)

See Also:

  • Ashton::LightSource#new


52
53
54
# File 'lib/ashton/lighting/manager.rb', line 52

def create_light(*args)
  add LightSource.new(*args)
end

#draw(options = {}) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ashton/lighting/manager.rb', line 56

def draw(options = {})
  options = {
      mode: :multiply,
  }.merge! options

  @shadows.draw @camera_x, @camera_y, @z, options
end

#each(&block) ⇒ Object



12
# File 'lib/ashton/lighting/manager.rb', line 12

def each(&block); @lights.each(&block) end

#empty?Boolean

Returns:

  • (Boolean)


14
# File 'lib/ashton/lighting/manager.rb', line 14

def empty?; @lights.empty? end

#heightObject



16
# File 'lib/ashton/lighting/manager.rb', line 16

def height; @shadows.height end

#remove(light) ⇒ Object



44
45
46
47
# File 'lib/ashton/lighting/manager.rb', line 44

def remove(light)
  @lights -= [light]
  light
end

#sizeObject



13
# File 'lib/ashton/lighting/manager.rb', line 13

def size; @lights.size end

#update_shadow_casters(&block) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ashton/lighting/manager.rb', line 64

def update_shadow_casters(&block)
  raise ArgumentError, "Requires block" unless block_given?

  unless empty?
    # TODO: Need to only render to lights that are on-screen.
    @lights.each do |light|
      light.send :render_shadow_casters, &block
    end

    # Use each shader on every light, to save setting and un-setting shaders (a bit faster, depending on number of light sources).
    LightSource.distort_shader.enable do
      @lights.each {|light| light.send :distort }
    end

    LightSource.draw_shadows_shader.enable do
      @lights.each {|light| light.send :draw_shadows }
    end

    LightSource.blur_shader.enable do
      @lights.each {|light| light.send :blur }
    end
  end

  @shadows.render do |buffer|
    buffer.clear
    $window.translate(-@camera_x, -@camera_y) do
      @lights.each {|light| light.draw } unless empty?
    end
  end

  nil
end

#widthObject



15
# File 'lib/ashton/lighting/manager.rb', line 15

def width; @shadows.width end