Class: Ray::Effect
- Inherits:
-
Object
- Object
- Ray::Effect
- Defined in:
- lib/ray/effect.rb,
lib/ray/effect/generator.rb,
lib/ray/effect/grayscale.rb,
lib/ray/effect/black_and_white.rb,
lib/ray/effect/color_inversion.rb
Overview
Effects are used to generate fragment shaders automatically. They represent one of the transformations applied to the color.
Effects must generate a structure to store arguments passed to the effect (structure generation being done through a DSL), and a function to apply the effect (a call to the #code method returns )
Direct Known Subclasses
Defined Under Namespace
Classes: BlackAndWhite, ColorInversion, Generator, Grayscale
Class Method Summary collapse
-
.attribute(name, type) ⇒ Object
Adds an attribute to the shader.
-
.attributes ⇒ Hash
All the attributes of the struct, matched with their type.
- .effect_name(value = nil) ⇒ Object
Instance Method Summary collapse
-
#apply_defaults(shader) ⇒ Object
Apply the defaults to a shader.
-
#code ⇒ String
abstract
Code of the function.
-
#defaults ⇒ Hash
abstract
Default value for each attribute.
-
#header ⇒ String
abstract
Code that must be written before the definiton of the structure.
-
#name ⇒ String
Name of the effect.
-
#struct ⇒ String
GLSL code of the struct.
Class Method Details
.attribute(name, type) ⇒ Object
Adds an attribute to the shader. It is an element of the struct
33 34 35 |
# File 'lib/ray/effect.rb', line 33 def self.attribute(name, type) attributes[name] = type end |
.attributes ⇒ Hash
Returns All the attributes of the struct, matched with their type.
38 39 40 |
# File 'lib/ray/effect.rb', line 38 def self.attributes @attributes ||= {} end |
Instance Method Details
#apply_defaults(shader) ⇒ Object
Apply the defaults to a shader.
100 101 102 103 104 105 106 107 108 |
# File 'lib/ray/effect.rb', line 100 def apply_defaults(shader) effect_name = self.class.effect_name shader["#{effect_name}.enabled"] = true defaults.each do |name, value| shader["#{effect_name}.#{name}"] = value end end |
#code ⇒ String
Returns Code of the function. This must contain the header of the function. The name of the function is the name of the effect prepended by “do_”. It is passed a ray_#effect_name structure and the color, and is expected to return the changed color.
87 88 89 |
# File 'lib/ray/effect.rb', line 87 def code raise NotImplementedError end |
#defaults ⇒ Hash
Returns Default value for each attribute.
94 95 96 |
# File 'lib/ray/effect.rb', line 94 def defaults {} end |
#header ⇒ String
Returns Code that must be written before the definiton of the structure. Just returns an empty string by default.
67 68 69 |
# File 'lib/ray/effect.rb', line 67 def header "" end |
#name ⇒ String
Returns Name of the effect.
43 44 45 |
# File 'lib/ray/effect.rb', line 43 def name self.class.effect_name end |
#struct ⇒ String
Returns GLSL code of the struct.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ray/effect.rb', line 48 def struct str = "struct ray_#{self.class.effect_name} {\n" str << " bool enabled;\n" self.class.attributes.each do |name, type| if type =~ /\A(\w+)\[(\d+)\]\z/ str << " #$1 #{name}[#$2];\n" else str << " #{type} #{name};\n" end end str << "};\n" end |