Class: Ray::Effect::Generator
- Inherits:
-
Object
- Object
- Ray::Effect::Generator
- Includes:
- Enumerable
- Defined in:
- lib/ray/effect/generator.rb
Instance Attribute Summary collapse
-
#color ⇒ String
Name of the variable containing the color once the default code is run.
-
#default ⇒ String
Code to ste the default color.
-
#effects ⇒ Array<Ray::Effect>
(also: #to_a)
readonly
All of the effects used by the generator.
-
#input ⇒ String
Code defining GLSL input (with varying or in, depending on the GLSL version).
-
#uniforms ⇒ String
Code defining uniforms.
-
#version ⇒ Integer
readonly
GLSL version number.
Instance Method Summary collapse
-
#apply_defaults(shader) ⇒ Object
Apply generator defaults to a shader.
-
#build(shader = Ray::Shader.new) ⇒ Ray::Shader
Generates a shader.
-
#code ⇒ String
Code of the pixel shader.
- #each(&block) ⇒ Object
-
#initialize(version = 110) { ... } ⇒ Generator
constructor
A new instance of Generator.
- #push(*effects) ⇒ Object (also: #<<)
Constructor Details
#initialize(version = 110) { ... } ⇒ Generator
Returns a new instance of Generator.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ray/effect/generator.rb', line 8 def initialize(version = 110) @effects = [] @version = version if version >= 130 @input = <<-input in vec4 var_Color; in vec2 var_TexCoord; input else @input = <<-input varying vec4 var_Color; varying vec2 var_TexCoord; input end @uniforms = <<-uniforms uniform sampler2D in_Texture; uniform bool in_TextureEnabled; uniforms @color = "color" @default = <<-default /* Apply default value */ vec4 color; if (in_TextureEnabled) color = texture#{"2D" if version<130}(in_Texture, var_TexCoord) * var_Color; else color = var_Color; default yield self if block_given? end |
Instance Attribute Details
#color ⇒ String
Returns Name of the variable containing the color once the default code is run. “color” by default.
71 72 73 |
# File 'lib/ray/effect/generator.rb', line 71 def color @color end |
#default ⇒ String
Returns Code to ste the default color. It should apply texturing, for example.
75 76 77 |
# File 'lib/ray/effect/generator.rb', line 75 def default @default end |
#effects ⇒ Array<Ray::Effect> (readonly) Also known as: to_a
Returns All of the effects used by the generator.
56 57 58 |
# File 'lib/ray/effect/generator.rb', line 56 def effects @effects end |
#input ⇒ String
Returns Code defining GLSL input (with varying or in, depending on the GLSL version).
64 65 66 |
# File 'lib/ray/effect/generator.rb', line 64 def input @input end |
#uniforms ⇒ String
Returns Code defining uniforms.
67 68 69 |
# File 'lib/ray/effect/generator.rb', line 67 def uniforms @uniforms end |
#version ⇒ Integer (readonly)
Returns GLSL version number.
60 61 62 |
# File 'lib/ray/effect/generator.rb', line 60 def version @version end |
Instance Method Details
#apply_defaults(shader) ⇒ Object
Apply generator defaults to a shader
140 141 142 |
# File 'lib/ray/effect/generator.rb', line 140 def apply_defaults(shader) each { |effect| effect.apply_defaults(shader) } end |
#build(shader = Ray::Shader.new) ⇒ Ray::Shader
Generates a shader
132 133 134 135 136 |
# File 'lib/ray/effect/generator.rb', line 132 def build(shader = Ray::Shader.new) shader.compile :frag => StringIO.new(code) apply_defaults shader shader end |
#code ⇒ String
Returns code of the pixel shader.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ray/effect/generator.rb', line 78 def code str = "#version #@version\n" str << "\n" str << input << "\n" str << uniforms << "\n" str << "out vec4 out_FragColor;\n" if version >= 130 str << "\n" str << "/* Headers */\n" each do |effect| str << effect.header << "\n" end str << "\n" str << "/* Structs */\n\n" each do |effect| str << effect.struct << "\n" end str << "/* Effects parameters */\n" each do |effect| str << "uniform ray_#{effect.name} #{effect.name};\n" end str << "\n" str << "/* Functions */\n" each do |effect| str << effect.code << "\n" end str << "void main() {\n" str << default << "\n" each do |effect| str << " if (#{effect.name}.enabled)\n" str << " #@color = do_#{effect.name}(#{effect.name}, #@color);\n" str << "\n" end if version >= 130 str << " out_FragColor = #@color;\n" else str << " gl_FragColor = #@color;\n" end str << "}\n" end |
#each(&block) ⇒ Object
51 52 53 |
# File 'lib/ray/effect/generator.rb', line 51 def each(&block) @effects.each(&block) end |
#push(*effects) ⇒ Object Also known as: <<
44 45 46 47 |
# File 'lib/ray/effect/generator.rb', line 44 def push(*effects) @effects.concat effects self end |