Module: Mittsu::RBSLLoader

Defined in:
lib/mittsu/renderers/shaders/rbsl_loader.rb

Constant Summary collapse

UNIFORM_RGX =
/uniform\s+(\S+)\s+(\w+)(|\s+\=\s+(.+));/
COLOR_RGX =
/color\(([^\)]*)\)/

Class Method Summary collapse

Class Method Details

.extract_array_contents(str) ⇒ Object



22
23
24
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 22

def extract_array_contents(str)
  /\[([^\]]+)\]/.match(str)[1]
end

.load_shader(shader, chunks) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 7

def load_shader(shader, chunks)
  shader.lines.flat_map(&:chomp).map{ |line|
    if line =~ /(\s*)#include\s+(\w+)/
      indentation = $1
      chunk_name = $2.to_sym

      chunks[chunk_name].lines.map(&:chomp).map{ |l|
        "#{indentation}#{l}"
      }
    else
      line
    end
  }.join("\n") + "\n"
end

.load_uniforms(uniforms, uniforms_lib) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 139

def load_uniforms(uniforms, uniforms_lib)
  uniform_strings = nil;
  in_uniform = false

  uniforms.lines.map(&:strip).each_with_object({}) { |line, hash|
    if in_uniform
      uniform_strings << line
      if line.end_with?(';')
        in_uniform = false
        name, value = parse_uniform(uniform_strings.join(' '))
        hash[name] = value
      end
    elsif line =~ /#include\s+(\w+)/
      uniforms_lib[$1.to_sym].map { |(k, v)| hash[k] = v.clone }
    elsif line.start_with?('uniform')
      if line.end_with?(';')
        name, value = parse_uniform(line)
        hash[name] = value
      else
        in_uniform = true
        uniform_strings = [line]
      end
    end
  }
end

.parse_color(str) ⇒ Object



50
51
52
53
54
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 50

def parse_color(str)
  str =~ COLOR_RGX
  values = $1.split(',').map(&:strip)
  parse_single_color(values)
end

.parse_color_array(str) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 56

def parse_color_array(str)
  str = extract_array_contents(str)
  str.scan(COLOR_RGX).map{ |m|
    values = m.first.split(',').map(&:strip)
    parse_single_color(values)
  }
end

.parse_float(str) ⇒ Object



37
38
39
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 37

def parse_float(str)
  str.to_f
end

.parse_float_array(str) ⇒ Object



69
70
71
72
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 69

def parse_float_array(str)
  str = extract_array_contents(str)
  str.split(',').map(&:strip).map(&:to_f)
end

.parse_int(str) ⇒ Object



26
27
28
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 26

def parse_int(str)
  str.to_i
end

.parse_int_array(str) ⇒ Object



64
65
66
67
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 64

def parse_int_array(str)
  str = extract_array_contents(str)
  str.split(',').map(&:strip).map(&:to_i)
end

.parse_single_color(values) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 41

def parse_single_color(values)
  if values.length == 1
    values = values.map(&:to_i)
  else
    values = values.map(&:to_f)
  end
  Color.new(*values)
end

.parse_texture(_str) ⇒ Object



117
118
119
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 117

def parse_texture(_str)
  nil
end

.parse_texture_array(_str) ⇒ Object



121
122
123
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 121

def parse_texture_array(_str)
  []
end

.parse_uniform(uniform) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mittsu/renderers/shaders/rbsl_loader.rb', line 125

def parse_uniform(uniform)
  uniform =~ UNIFORM_RGX
  type_str = $1
  type = type_str.to_sym
  is_array = type_str.end_with?('[]')
  name = $2
  value_str = $4
  value = is_array ? [] : nil
  if value_str && !value_str.empty?
    value = send("parse_#{type.to_s.gsub(/\[\]/, '_array')}".to_s, value_str)
  end
  [name, Uniform.new(type, value)]
end