Module: RBGL::Engine::ShaderBuiltins

Included in:
BaseShader
Defined in:
lib/rbgl/engine/shader/builtins.rb

Instance Method Summary collapse

Instance Method Details

#abs(x) ⇒ Object



103
104
105
# File 'lib/rbgl/engine/shader/builtins.rb', line 103

def abs(x)
  component_map(x, &:abs)
end

#acos(x) ⇒ Object



143
144
145
# File 'lib/rbgl/engine/shader/builtins.rb', line 143

def acos(x)
  component_map(x) { |value| Math.acos(value) }
end

#asin(x) ⇒ Object



139
140
141
# File 'lib/rbgl/engine/shader/builtins.rb', line 139

def asin(x)
  component_map(x) { |value| Math.asin(value) }
end

#atan(y, x = nil) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/rbgl/engine/shader/builtins.rb', line 147

def atan(y, x = nil)
  if x
    component_binary_map(y, x) { |left, right| Math.atan2(left, right) }
  else
    component_map(y) { |value| Math.atan(value) }
  end
end

#ceil(x) ⇒ Object



115
116
117
# File 'lib/rbgl/engine/shader/builtins.rb', line 115

def ceil(x)
  component_map(x, &:ceil)
end

#clamp(v, min_val, max_val) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/rbgl/engine/shader/builtins.rb', line 71

def clamp(v, min_val, max_val)
  case v
  when Numeric then v.clamp(min_val, max_val)
  when Larb::Color then v.clamp
  else
    component_map(v) { |component| component.clamp(min_val, max_val) }
  end
end

#color_from_vec3(v) ⇒ Object



185
186
187
# File 'lib/rbgl/engine/shader/builtins.rb', line 185

def color_from_vec3(v)
  Larb::Color.from_vec3(v)
end

#color_from_vec4(v) ⇒ Object



189
190
191
# File 'lib/rbgl/engine/shader/builtins.rb', line 189

def color_from_vec4(v)
  Larb::Color.from_vec4(v)
end

#cos(x) ⇒ Object



131
132
133
# File 'lib/rbgl/engine/shader/builtins.rb', line 131

def cos(x)
  component_map(x) { |value| Math.cos(value) }
end

#cross(a, b) ⇒ Object



38
39
40
# File 'lib/rbgl/engine/shader/builtins.rb', line 38

def cross(a, b)
  a.cross(b)
end

#dot(a, b) ⇒ Object



34
35
36
# File 'lib/rbgl/engine/shader/builtins.rb', line 34

def dot(a, b)
  a.dot(b)
end

#floor(x) ⇒ Object



111
112
113
# File 'lib/rbgl/engine/shader/builtins.rb', line 111

def floor(x)
  component_map(x, &:floor)
end

#fract(x) ⇒ Object



95
96
97
# File 'lib/rbgl/engine/shader/builtins.rb', line 95

def fract(x)
  component_map(x) { |value| value - value.floor }
end

#length(v) ⇒ Object



46
47
48
# File 'lib/rbgl/engine/shader/builtins.rb', line 46

def length(v)
  v.length
end

#max(*args) ⇒ Object



162
163
164
165
166
167
# File 'lib/rbgl/engine/shader/builtins.rb', line 162

def max(*args)
  values = args.flatten
  return values.max unless values.any? { |value| vector_value?(value) }

  values.reduce { |current, value| component_binary_map(current, value) { |left, right| [left, right].max } }
end

#min(*args) ⇒ Object



155
156
157
158
159
160
# File 'lib/rbgl/engine/shader/builtins.rb', line 155

def min(*args)
  values = args.flatten
  return values.min unless values.any? { |value| vector_value?(value) }

  values.reduce { |current, value| component_binary_map(current, value) { |left, right| [left, right].min } }
end

#mix(a, b, t) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rbgl/engine/shader/builtins.rb', line 63

def mix(a, b, t)
  case a
  when Numeric then a + (b - a) * t
  when Larb::Vec2, Larb::Vec3, Larb::Vec4 then a.lerp(b, t)
  when Larb::Color then a.lerp(b, t)
  end
end

#mod(x, y) ⇒ Object



99
100
101
# File 'lib/rbgl/engine/shader/builtins.rb', line 99

def mod(x, y)
  component_binary_map(x, y) { |left, right| left - right * (left / right).floor }
end

#normalize(v) ⇒ Object



42
43
44
# File 'lib/rbgl/engine/shader/builtins.rb', line 42

def normalize(v)
  v.normalize
end

#pow(x, y) ⇒ Object



119
120
121
# File 'lib/rbgl/engine/shader/builtins.rb', line 119

def pow(x, y)
  component_binary_map(x, y) { |left, right| left**right }
end

#reflect(v, n) ⇒ Object



50
51
52
# File 'lib/rbgl/engine/shader/builtins.rb', line 50

def reflect(v, n)
  v.reflect(n)
end

#refract(v, n, eta) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/rbgl/engine/shader/builtins.rb', line 54

def refract(v, n, eta)
  cos_i = -dot(n, v)
  sin_t2 = eta * eta * (1.0 - cos_i * cos_i)
  return vec3(0) if sin_t2 > 1.0

  cos_t = Math.sqrt(1.0 - sin_t2)
  v * eta + n * (eta * cos_i - cos_t)
end

#rgb(r, g, b) ⇒ Object



177
178
179
# File 'lib/rbgl/engine/shader/builtins.rb', line 177

def rgb(r, g, b)
  Larb::Color.rgb(r, g, b)
end

#rgba(r, g, b, a) ⇒ Object



181
182
183
# File 'lib/rbgl/engine/shader/builtins.rb', line 181

def rgba(r, g, b, a)
  Larb::Color.rgba(r, g, b, a)
end

#saturate(v) ⇒ Object



80
81
82
# File 'lib/rbgl/engine/shader/builtins.rb', line 80

def saturate(v)
  clamp(v, 0.0, 1.0)
end

#sign(x) ⇒ Object



107
108
109
# File 'lib/rbgl/engine/shader/builtins.rb', line 107

def sign(x)
  component_map(x) { |value| value <=> 0 }
end

#sin(x) ⇒ Object



127
128
129
# File 'lib/rbgl/engine/shader/builtins.rb', line 127

def sin(x)
  component_map(x) { |value| Math.sin(value) }
end

#smoothstep(edge0, edge1, x) ⇒ Object



84
85
86
87
88
89
# File 'lib/rbgl/engine/shader/builtins.rb', line 84

def smoothstep(edge0, edge1, x)
  component_ternary_map(edge0, edge1, x) do |e0, e1, value|
    t = ((value - e0) / (e1 - e0)).clamp(0.0, 1.0)
    t * t * (3.0 - 2.0 * t)
  end
end

#sqrt(x) ⇒ Object



123
124
125
# File 'lib/rbgl/engine/shader/builtins.rb', line 123

def sqrt(x)
  component_map(x) { |value| Math.sqrt(value) }
end

#step(edge, x) ⇒ Object



91
92
93
# File 'lib/rbgl/engine/shader/builtins.rb', line 91

def step(edge, x)
  component_binary_map(edge, x) { |limit, value| value < limit ? 0.0 : 1.0 }
end

#tan(x) ⇒ Object



135
136
137
# File 'lib/rbgl/engine/shader/builtins.rb', line 135

def tan(x)
  component_map(x) { |value| Math.tan(value) }
end

#texture(tex, uv) ⇒ Object



169
170
171
# File 'lib/rbgl/engine/shader/builtins.rb', line 169

def texture(tex, uv)
  tex.sample(uv.x, uv.y)
end

#texture_lod(tex, uv, lod) ⇒ Object



173
174
175
# File 'lib/rbgl/engine/shader/builtins.rb', line 173

def texture_lod(tex, uv, lod)
  tex.sample(uv.x, uv.y, lod: lod)
end

#vec2(x, y = nil) ⇒ Object



6
7
8
9
# File 'lib/rbgl/engine/shader/builtins.rb', line 6

def vec2(x, y = nil)
  y ||= x
  Larb::Vec2.new(x, y)
end

#vec3(x, y = nil, z = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rbgl/engine/shader/builtins.rb', line 11

def vec3(x, y = nil, z = nil)
  if y.nil? && z.nil?
    Larb::Vec3.new(x, x, x)
  elsif z.nil? && y.is_a?(Larb::Vec2)
    Larb::Vec3.new(x, y.x, y.y)
  else
    Larb::Vec3.new(x, y, z)
  end
end

#vec4(x, y = nil, z = nil, w = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbgl/engine/shader/builtins.rb', line 21

def vec4(x, y = nil, z = nil, w = nil)
  case
  when y.nil? && z.nil? && w.nil?
    Larb::Vec4.new(x, x, x, x)
  when x.is_a?(Larb::Vec3) && !y.nil?
    Larb::Vec4.new(x.x, x.y, x.z, y)
  when x.is_a?(Larb::Vec2) && y.is_a?(Larb::Vec2)
    Larb::Vec4.new(x.x, x.y, y.x, y.y)
  else
    Larb::Vec4.new(x, y, z, w)
  end
end