Class: Carbuncle::Shader
- Defined in:
- gems/carbuncle-graphics/mrblib/shader.rb
Constant Summary collapse
- VALID_UNIFORM_CLASSES =
[ Integer, Float, Carbuncle::Point, Carbuncle::Vector3, Carbuncle::Vector4, Carbuncle::Rect, Carbuncle::Color, Carbuncle::Matrix, Carbuncle::Texture ].freeze
Instance Method Summary collapse
- #add_uniform(name, uniform_class, value = nil) ⇒ Object
- #assign_uniform(name, value) ⇒ Object
- #default_value_for(uniform_class) ⇒ Object
- #uniforms ⇒ Object
- #uniforms=(values) ⇒ Object
- #update(dt = 0) ⇒ Object
- #update_uniforms ⇒ Object
Instance Method Details
#add_uniform(name, uniform_class, value = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 10 def add_uniform(name, uniform_class, value = nil) symbol = name.to_sym variable = name.to_s.underscore @uniform_types[symbol] = uniform_class @uniform_values[symbol] = value.presence || default_value_for(uniform_class) @uniform_locations[symbol] = find_uniform_location(name) if @uniform_locations[symbol] < 0 raise ArgumentError, "Couldn't find location for uniform '#{name}'" end unless VALID_UNIFORM_CLASSES.include?(uniform_class) raise ArgumentError, "#{uniform_class.name} is not a valid uniform type." end define_singleton_method(variable) { @uniform_values[symbol] } define_singleton_method(:"#{variable}=") { |new_value| assign_uniform(symbol, new_value) } end |
#assign_uniform(name, value) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 31 def assign_uniform(name, value) symbol = name.to_sym klass = @uniform_types[symbol] unless @uniform_types.key?(symbol) raise ArgumentError, "Uniform '#{name}' does not exists on shader." end value = value.to_f if klass == Float value = value.to_i if klass == Integer unless value.is_a?(klass) raise ArgumentError, "Uniform '#{name}' should be of type #{klass.name}" end @uniform_values[symbol] = value end |
#default_value_for(uniform_class) ⇒ Object
63 64 65 66 67 68 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 63 def default_value_for(uniform_class) return 0 if uniform_class == Integer return 0.0 if uniform_class == Float uniform_class.new end |
#uniforms ⇒ Object
53 54 55 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 53 def uniforms @uniform_values.dup end |
#uniforms=(values) ⇒ Object
57 58 59 60 61 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 57 def uniforms=(values) values.each do |name, value| assign_uniform(name, value) end end |
#update(dt = 0) ⇒ Object
27 28 29 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 27 def update(dt = 0) update_uniforms end |
#update_uniforms ⇒ Object
47 48 49 50 51 |
# File 'gems/carbuncle-graphics/mrblib/shader.rb', line 47 def update_uniforms @uniform_values.each do |symbol, value| send_uniform_value(@uniform_locations[symbol], value) end end |