// BuildLang Graphics Standard Library
// Industry-standard shader math for game engines and rendering pipelines
// === Color Space Conversions ===
fn linear_to_srgb(c: f64) -> f64 {
if c <= 0.0031308 {
c * 12.92
} else {
1.055 * pow(c, 1.0 / 2.4) - 0.055
}
}
fn srgb_to_linear(c: f64) -> f64 {
if c <= 0.04045 {
c / 12.92
} else {
pow((c + 0.055) / 1.055, 2.4)
}
}
fn rgb_to_luminance(r: f64, g: f64, b: f64) -> f64 {
r * 0.2126 + g * 0.7152 + b * 0.0722
}
// === Tone Mapping Operators ===
fn reinhard(x: f64) -> f64 {
x / (1.0 + x)
}
fn reinhard_extended(x: f64, max_white: f64) -> f64 {
let numerator = x * (1.0 + x / (max_white * max_white));
numerator / (1.0 + x)
}
fn aces_channel(x: f64) -> f64 {
let a = 2.51;
let b = 0.03;
let c = 2.43;
let d = 0.59;
let e = 0.14;
let result = (x * (a * x + b)) / (x * (c * x + d) + e);
if result > 1.0 { 1.0 } else { if result < 0.0 { 0.0 } else { result } }
}
// Uncharted 2 tone mapping (used in many AAA games)
fn uncharted2_partial(x: f64) -> f64 {
let a = 0.15;
let b = 0.50;
let c = 0.10;
let d = 0.20;
let e = 0.02;
let f = 0.30;
((x * (a * x + c * b) + d * e) / (x * (a * x + b) + d * f)) - e / f
}
fn uncharted2(x: f64) -> f64 {
let exposure_bias = 2.0;
let curr = uncharted2_partial(x * exposure_bias);
let white_scale = 1.0 / uncharted2_partial(11.2);
curr * white_scale
}
// === Noise Functions ===
fn hash(x: f64) -> f64 {
let s = sin(x * 127.1) * 43758.5453123;
s - floor(s)
}
fn noise_1d(x: f64) -> f64 {
let i = floor(x);
let f = x - i;
let u = f * f * (3.0 - 2.0 * f);
mix(hash(i), hash(i + 1.0), u)
}
// === Easing Functions ===
fn ease_in_quad(t: f64) -> f64 { t * t }
fn ease_out_quad(t: f64) -> f64 { t * (2.0 - t) }
fn ease_in_out_quad(t: f64) -> f64 {
if t < 0.5 { 2.0 * t * t } else { -1.0 + (4.0 - 2.0 * t) * t }
}
fn ease_in_cubic(t: f64) -> f64 { t * t * t }
fn ease_out_cubic(t: f64) -> f64 {
let t1 = t - 1.0;
t1 * t1 * t1 + 1.0
}
// === Geometric Functions ===
fn degrees_to_radians(deg: f64) -> f64 { deg * 3.14159265358979323846 / 180.0 }
fn radians_to_degrees(rad: f64) -> f64 { rad * 180.0 / 3.14159265358979323846 }
fn distance_2d(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
let dx = x2 - x1;
let dy = y2 - y1;
sqrt(dx * dx + dy * dy)
}
fn distance_3d(x1: f64, y1: f64, z1: f64, x2: f64, y2: f64, z2: f64) -> f64 {
let dx = x2 - x1;
let dy = y2 - y1;
let dz = z2 - z1;
sqrt(dx * dx + dy * dy + dz * dz)
}
// === Interpolation ===
fn lerp_f64(a: f64, b: f64, t: f64) -> f64 { a + (b - a) * t }
fn inverse_lerp(a: f64, b: f64, value: f64) -> f64 {
(value - a) / (b - a)
}
fn remap(value: f64, from_min: f64, from_max: f64, to_min: f64, to_max: f64) -> f64 {
let t = inverse_lerp(from_min, from_max, value);
lerp_f64(to_min, to_max, t)
}
fn smoothstep_f64(edge0: f64, edge1: f64, x: f64) -> f64 {
let t = (x - edge0) / (edge1 - edge0);
let tc = if t < 0.0 { 0.0 } else { if t > 1.0 { 1.0 } else { t } };
tc * tc * (3.0 - 2.0 * tc)
}
// === PBR Helpers ===
fn fresnel_schlick(cos_theta: f64, f0: f64) -> f64 {
let one_minus = 1.0 - cos_theta;
let om2 = one_minus * one_minus;
let om5 = om2 * om2 * one_minus;
f0 + (1.0 - f0) * om5
}
fn ggx_distribution(n_dot_h: f64, roughness: f64) -> f64 {
let a = roughness * roughness;
let a2 = a * a;
let denom = n_dot_h * n_dot_h * (a2 - 1.0) + 1.0;
a2 / (3.14159265358979323846 * denom * denom)
}