Class: Ray::Effect

Inherits:
Object
  • Object
show all
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

BlackAndWhite, ColorInversion, Grayscale

Defined Under Namespace

Classes: BlackAndWhite, ColorInversion, Generator, Grayscale

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, type) ⇒ Object

Adds an attribute to the shader. It is an element of the struct

Parameters:

  • name (String)

    Name of the attribute

  • type (String)

    Type of the attribute. Arrays are written using this notation: type.



33
34
35
# File 'lib/ray/effect.rb', line 33

def self.attribute(name, type)
  attributes[name] = type
end

.attributesHash

Returns All the attributes of the struct, matched with their type.

Returns:

  • (Hash)

    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

.effect_name(name) ⇒ Object .effect_nameString

Overloads:

  • .effect_name(name) ⇒ Object

    Sets the name of the effect

    Parameters:

    • value (String)
  • .effect_nameString

    Returns Name of the effect.

    Returns:

    • (String)

      Name of the effect



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ray/effect.rb', line 15

def self.effect_name(value = nil)
  if value
    @name = value

    klass = self
    Ray::Helper.send :define_method, @name do |*args|
      klass.new(*args)
    end
  else
    @name
  end
end

Instance Method Details

#apply_defaults(shader) ⇒ Object

Apply the defaults to a shader.

Parameters:



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

#codeString

This method is abstract.

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.

Examples:

def code
  return %{
    vec4 do_some_effect(ray_some_effect arg, vec4 color) {
      /* Do magic here. */
    }
  }
end

Returns:

  • (String)

    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.

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/ray/effect.rb', line 87

def code
  raise NotImplementedError
end

#defaultsHash

This method is abstract.

Returns Default value for each attribute.

Returns:

  • (Hash)

    Default value for each attribute.



94
95
96
# File 'lib/ray/effect.rb', line 94

def defaults
  {}
end

#headerString

This method is abstract.

Returns Code that must be written before the definiton of the structure. Just returns an empty string by default.

Returns:

  • (String)

    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

#nameString

Returns Name of the effect.

Returns:

  • (String)

    Name of the effect



43
44
45
# File 'lib/ray/effect.rb', line 43

def name
  self.class.effect_name
end

#structString

Returns GLSL code of the struct.

Returns:

  • (String)

    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