Class: Shader

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/models/shader.rb,
mrb_doc/models/shader.rb

Overview

The Shader class is for doing operations on the GPU

Defined Under Namespace

Modules: Uniform

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Shader

Creates a new instance of Shader

Parameters:

  • id (Integer)


5
6
7
8
9
# File 'mrb_doc/models/shader.rb', line 5

def initialize(id)
  # mrb_Shader_initialize
  # src/mruby_integration/models/shader.cpp
  Shader.new
end

Instance Attribute Details

#idInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/shader.rb', line 4

def id
  @id
end

Class Method Details

.load(vertex_shader_path: nil, fragment_shader_path: nil, vertex_shader_code: nil, fragment_shader_code: nil) ⇒ Shader

Loads a Shader from either paths or code, but not both at once.

Parameters:

  • vertex_shader_path (String) (defaults to: nil)
  • fragment_shader_path (String) (defaults to: nil)
  • vertex_shader_code (String) (defaults to: nil)
  • fragment_shader_code (String) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'src/ruby/models/shader.rb', line 13

def self.load(vertex_shader_path: nil, fragment_shader_path: nil, vertex_shader_code: nil, fragment_shader_code: nil)
  unless vertex_shader_path || fragment_shader_path || vertex_shader_code || fragment_shader_code
    raise ArgumentError, "You can only specify paths or code, not both"
  end

  if (vertex_shader_path || fragment_shader_path) && (vertex_shader_code || fragment_shader_code)
    raise ArgumentError, "You can only specify paths or code, not both"
  end

  if vertex_shader_path || fragment_shader_path
    load_shader(vertex_shader_path, fragment_shader_path)
  elsif vertex_shader_code || fragment_shader_code
    load_shader_from_string(vertex_shader_code, fragment_shader_code)
  end
end

Instance Method Details

#draw { ... } ⇒ nil

Instead of rendering straight to the screen, render through this Shader first

Yields:

  • The block that calls your rendering logic

Returns:

  • (nil)


38
39
40
41
42
# File 'src/ruby/models/shader.rb', line 38

def draw(&block)
  begin_shader_mode(self)
  block.call
  end_shader_mode
end

#get_uniform_location(variable) ⇒ Integer

Returns the location of uniform variable, will return nil if not found.

Parameters:

  • variable (String)

Returns:

  • (Integer)


53
54
55
56
# File 'src/ruby/models/shader.rb', line 53

def get_uniform_location(variable)
  value = get_shader_location(self, variable)
  value == -1 ? nil : value
end

#ready?Boolean

Is the Shader ready to be used?

Returns:

  • (Boolean)


46
47
48
# File 'src/ruby/models/shader.rb', line 46

def ready?
  shader_ready?(self)
end

#set_value(value:, variable: nil, variable_location: nil, variable_type: nil) ⇒ nil

Sets the variable to the specified value, will auto detect the variable_type if no variable_type is passed in. You must pass in either variable or variable_location.

Parameters:

Returns:

  • (nil)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'src/ruby/models/shader.rb', line 70

def set_value(value:, variable: nil, variable_location: nil, variable_type: nil)
  if variable.nil? && variable_location.nil?
    raise ArgumentError, "You must specify at least one of variable or variable_location"
  end

  if variable_location && variable_type
    return set_values(values: [value], variable_location: variable_location, variable_type: variable_type)
  end

  if variable && variable_location.nil?
    variable_location = get_uniform_location(variable)
  end

  if variable_type.nil?
    variable_type = detect_uniform_type(values: [value])
  end

  set_values(
    values: [value],
    variable_location: variable_location,
    variable_type: variable_type,
  )
end

#set_values(values:, variable: nil, variable_location: nil, variable_type: nil) ⇒ Nil

Sets the variables to the specified values, will auto detect the variable_type if no variable_type is passed in. You must pass in either variable or variable_location.

Parameters:

Returns:

  • (Nil)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'src/ruby/models/shader.rb', line 106

def set_values(values:, variable: nil, variable_location: nil, variable_type: nil)
  unless values.is_a?(Array)
    raise ArgumentError, "You must pass an array of values"
  end
  if variable.nil? && variable_location.nil?
    raise ArgumentError, "You must specify at least one of variable or variable_location"
  end

  if variable && variable_location.nil?
    variable_location = get_uniform_location(variable)
  end

  if variable_type.nil?
    variable_type = detect_uniform_type(values: values)
  end

  set_shader_values(
    self,
    variable_location,
    values,
    variable_type,
  )
end

#unloadnil

Unloads the Shader from memory

Returns:

  • (nil)


31
32
33
# File 'src/ruby/models/shader.rb', line 31

def unload
  unload_shader(self)
end