Module: RLSL::CodeGenerator::MathPrelude

Defined in:
lib/rlsl/code_generator/math_prelude.rb

Class Method Summary collapse

Class Method Details

.codeObject



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rlsl/code_generator/math_prelude.rb', line 8

def code
  "    static inline vec2 vec2_new(float x, float y) { return (vec2){x, y}; }\n    static inline vec3 vec3_new(float x, float y, float z) { return (vec3){x, y, z}; }\n    static inline vec4 vec4_new(float x, float y, float z, float w) { return (vec4){x, y, z, w}; }\n\n    static inline vec2 vec2_add(vec2 a, vec2 b) { return (vec2){a.x + b.x, a.y + b.y}; }\n    static inline vec3 vec3_add(vec3 a, vec3 b) { return (vec3){a.x + b.x, a.y + b.y, a.z + b.z}; }\n\n    static inline vec2 vec2_sub(vec2 a, vec2 b) { return (vec2){a.x - b.x, a.y - b.y}; }\n    static inline vec3 vec3_sub(vec3 a, vec3 b) { return (vec3){a.x - b.x, a.y - b.y, a.z - b.z}; }\n\n    static inline vec2 vec2_mul(vec2 a, float s) { return (vec2){a.x * s, a.y * s}; }\n    static inline vec3 vec3_mul(vec3 a, float s) { return (vec3){a.x * s, a.y * s, a.z * s}; }\n\n    static inline vec2 vec2_div(vec2 a, float s) { return (vec2){a.x / s, a.y / s}; }\n    static inline vec3 vec3_div(vec3 a, float s) { return (vec3){a.x / s, a.y / s, a.z / s}; }\n\n    static inline float vec2_dot(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; }\n    static inline float vec3_dot(vec3 a, vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }\n\n    static inline float vec2_length(vec2 v) { return sqrtf(v.x * v.x + v.y * v.y); }\n    static inline float vec3_length(vec3 v) { return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); }\n\n    static inline vec2 vec2_normalize(vec2 v) { float l = vec2_length(v); return l > 0 ? vec2_div(v, l) : v; }\n    static inline vec3 vec3_normalize(vec3 v) { float l = vec3_length(v); return l > 0 ? vec3_div(v, l) : v; }\n\n    static inline float fract(float x) { return x - floorf(x); }\n    static inline float mix_f(float a, float b, float t) { return a + (b - a) * t; }\n    static inline vec3 mix_v3(vec3 a, vec3 b, float t) {\n      return (vec3){a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t};\n    }\n\n    static inline float clamp_f(float x, float lo, float hi) {\n      return x < lo ? lo : (x > hi ? hi : x);\n    }\n\n    static inline float smoothstep(float edge0, float edge1, float x) {\n      float t = clamp_f((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);\n      return t * t * (3.0f - 2.0f * t);\n    }\n\n    static inline float hash21(vec2 p) {\n      float dot = p.x * 12.9898f + p.y * 78.233f;\n      return fract(sinf(dot) * 43758.5453f);\n    }\n\n    static inline vec2 hash22(vec2 p) {\n      float n = sinf(vec2_dot(p, vec2_new(127.1f, 311.7f)));\n      return vec2_new(fract(n * 43758.5453f), fract(n * 12345.6789f));\n    }\n\n    static inline vec3 reflect(vec3 I, vec3 N) {\n      float d = vec3_dot(N, I);\n      return vec3_new(I.x - 2.0f * d * N.x, I.y - 2.0f * d * N.y, I.z - 2.0f * d * N.z);\n    }\n\n    static inline vec3 refract(vec3 I, vec3 N, float eta) {\n      float d = vec3_dot(N, I);\n      float k = 1.0f - eta * eta * (1.0f - d * d);\n      if (k < 0.0f) {\n        return vec3_new(0.0f, 0.0f, 0.0f);\n      }\n\n      float s = eta * d + sqrtf(k);\n      return vec3_new(eta * I.x - s * N.x, eta * I.y - s * N.y, eta * I.z - s * N.z);\n    }\n  C\nend\n"