Class: Ashton::Lighting::Manager
- Inherits:
-
Object
- Object
- Ashton::Lighting::Manager
- 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
-
#camera_x ⇒ Object
Returns the value of attribute camera_x.
-
#camera_y ⇒ Object
Returns the value of attribute camera_y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #add(light) ⇒ Ashton::LightSource (also: #<<)
- #create_light(*args) ⇒ Ashton::LightSource
- #draw(options = {}) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #height ⇒ Object
-
#initialize(options = {}) ⇒ Manager
constructor
A new instance of Manager.
- #remove(light) ⇒ Object
- #size ⇒ Object
- #update_shadow_casters(&block) ⇒ Object
- #width ⇒ Object
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( = {}) = { width: $window.width, height: $window.height, camera_x: 0, camera_y: 0, z: 0, }.merge! @camera_x, @camera_y = [:camera_x], [:camera_y] @z = [:z] @lights = Set.new @shadows = Ashton::Texture.new [:width], [:height] end |
Instance Attribute Details
#camera_x ⇒ Object
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_y ⇒ Object
Returns the value of attribute camera_y.
10 11 12 |
# File 'lib/ashton/lighting/manager.rb', line 10 def camera_y @camera_y end |
#z ⇒ Object
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: <<
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
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( = {}) = { mode: :multiply, }.merge! @shadows.draw @camera_x, @camera_y, @z, end |
#each(&block) ⇒ Object
12 |
# File 'lib/ashton/lighting/manager.rb', line 12 def each(&block); @lights.each(&block) end |
#empty? ⇒ Boolean
14 |
# File 'lib/ashton/lighting/manager.rb', line 14 def empty?; @lights.empty? end |
#height ⇒ Object
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 |
#size ⇒ Object
13 |
# File 'lib/ashton/lighting/manager.rb', line 13 def size; @lights.size end |
#update_shadow_casters(&block) ⇒ Object
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 |
#width ⇒ Object
15 |
# File 'lib/ashton/lighting/manager.rb', line 15 def width; @shadows.width end |