Class: Ashton::Lighting::LightSource
- Inherits:
-
Object
- Object
- Ashton::Lighting::LightSource
- Includes:
- Mixins::VersionChecking
- Defined in:
- lib/ashton/lighting/light_source.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/
Class Attribute Summary collapse
-
.blur_shader ⇒ Object
Returns the value of attribute blur_shader.
-
.distort_shader ⇒ Object
Returns the value of attribute distort_shader.
-
.draw_shadows_shader ⇒ Object
Returns the value of attribute draw_shadows_shader.
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#radius ⇒ Object
readonly
Returns the value of attribute radius.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #draw(options = {}) ⇒ Object
-
#draw_debug ⇒ Object
Draw some quadrant lines (for debugging).
- #height ⇒ Object
-
#initialize(x, y, z, radius, options = {}) ⇒ LightSource
constructor
A new instance of LightSource.
-
#render_shadows(&block) ⇒ Object
Only need to render shadows again if anything has actually changed!.
- #width ⇒ Object
Methods included from Mixins::VersionChecking
#check_opengl_extension, #check_opengl_version
Constructor Details
#initialize(x, y, z, radius, options = {}) ⇒ LightSource
Returns a new instance of LightSource.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ashton/lighting/light_source.rb', line 20 def initialize(x, y, z, radius, = {}) #check_opengl_extension PIXEL_BUFFER_EXTENSION @x, @y, @z, @radius = x, y, z, radius.to_i @color = [:color] || Gosu::Color::WHITE @shadow_casters = Ashton::Texture.new width, height @shadow_map = Ashton::Texture.new 2, height @shadows = Ashton::Texture.new width, height @blurred = Ashton::Texture.new width, height load_shaders end |
Class Attribute Details
.blur_shader ⇒ Object
Returns the value of attribute blur_shader.
11 12 13 |
# File 'lib/ashton/lighting/light_source.rb', line 11 def blur_shader @blur_shader end |
.distort_shader ⇒ Object
Returns the value of attribute distort_shader.
11 12 13 |
# File 'lib/ashton/lighting/light_source.rb', line 11 def distort_shader @distort_shader end |
.draw_shadows_shader ⇒ Object
Returns the value of attribute draw_shadows_shader.
11 12 13 |
# File 'lib/ashton/lighting/light_source.rb', line 11 def draw_shadows_shader @draw_shadows_shader end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
15 16 17 |
# File 'lib/ashton/lighting/light_source.rb', line 15 def color @color end |
#radius ⇒ Object (readonly)
Returns the value of attribute radius.
14 15 16 |
# File 'lib/ashton/lighting/light_source.rb', line 14 def radius @radius end |
#x ⇒ Object
Returns the value of attribute x.
15 16 17 |
# File 'lib/ashton/lighting/light_source.rb', line 15 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
15 16 17 |
# File 'lib/ashton/lighting/light_source.rb', line 15 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
15 16 17 |
# File 'lib/ashton/lighting/light_source.rb', line 15 def z @z end |
Instance Method Details
#draw(options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ashton/lighting/light_source.rb', line 96 def draw( = {}) = { mode: :add, color: @color, }.merge! @blurred.draw @x - @radius, @y - @radius, @z, @shadow_casters.draw @x - @radius, @y - @radius, @z, nil end |
#draw_debug ⇒ Object
Draw some quadrant lines (for debugging).
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ashton/lighting/light_source.rb', line 109 def draw_debug color = @color.dup color.alpha = 75 $window.translate(-@radius, -@radius) do $window.draw_line x, y, color, x + width, y, color, z $window.draw_line x + width, y, color, x + width, y + height, color, z $window.draw_line x + width, y + height, color, x, y + height, color, z $window.draw_line x, y + height, color, x, y, color, z $window.draw_line x, y, color, x + width, y + height, color, z $window.draw_line x, y + height, color, x + width, y, color, z end end |
#height ⇒ Object
18 |
# File 'lib/ashton/lighting/light_source.rb', line 18 def height; @radius * 2 end |
#render_shadows(&block) ⇒ Object
Only need to render shadows again if anything has actually changed!
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ashton/lighting/light_source.rb', line 36 def render_shadows(&block) raise "block required" unless block_given? render_shadow_casters(&block) # Distort the shadow casters and reduce into a a 2-pixel wide shadow-map of the blockages. LightSource.distort_shader.enable { distort } # Render the shadows themselves, before blurring. LightLightSource.draw_shadows_shader.enable { draw_shadows } # Finally blur it up and apply the radial lighting. LightSource.blur_shader.enable { blur } nil end |
#width ⇒ Object
17 |
# File 'lib/ashton/lighting/light_source.rb', line 17 def width; @radius * 2 end |