Math Module
Constants, safe math helpers, remapping, easing, waves, and scalar/vector utility functions.
Turn the guide into code
Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.
Open PlaygroundConstants, safe math helpers, remapping, easing, waves, and scalar/vector utility functions.
Import
import Math
This module has no module imports.
Usage Examples
Stabilize Lighting and Direction Math
Use Math when a value can become degenerate at runtime: zero-length normals, tiny denominators, or dot products that need a clean lighting range.
import Math
float3 normal = Math::safe_normalize(input.normal);
float3 lightDir = Math::safe_normalize(resources.lightPosition - input.worldPos);
float ndotl = Math::saturate_dot(normal, lightDir);
float distanceToLight = length(resources.lightPosition - input.worldPos);
float attenuation = Math::safe_rcp(distanceToLight * distanceToLight);
output.color = float4(resources.lightColor * ndotl * attenuation, 1.0);
Remap Gameplay or Material Values
Use the remap helpers when host values arrive in engine units but the shader wants normalized ranges, artist-facing ramps, or clamped fade factors.
import Math
float height01 = Math::inverse_lerp_clamped(resources.waterLevel, resources.snowLine, input.worldPos.y);
float foam = Math::remap_clamped(resources.waveEnergy, 0.2, 0.8, 0.0, 1.0);
float windPhase = Math::triangle_wave(resources.time * resources.windSpeed);
float3 low = float3(0.05, 0.18, 0.1);
float3 high = float3(0.8, 0.85, 0.9);
float3 terrainColor = lerp(low, high, height01) + foam * float3(0.12);
output.color = float4(terrainColor * (0.85 + windPhase * 0.15), 1.0);
Constants
const float PI = 3.14159265358979323846const float TAU = 6.28318530717958647692const float E = 2.71828182845904523536const float PHI = 1.61803398874989484820const float INV_PI = 0.31830988618379067154const float INV_TAU = 0.15915494309189533577const float EPSILON = 1e-6const float SQRT_2 = 1.41421356237309504880const float INV_SQRT_2 = 0.70710678118654752440Structs
No structs found.
Enums
No enums found.
Functions
inverse_lerp
Returns the interpolation factor for value in the range [a, b].
inverse_lerp :: (float a, float b, float value) -> float
inverse_lerp
Returns the component-wise interpolation factor for value in [a, b].
inverse_lerp :: (float2 a, float2 b, float2 value) -> float2
inverse_lerp
Returns the component-wise interpolation factor for value in [a, b].
inverse_lerp :: (float3 a, float3 b, float3 value) -> float3
inverse_lerp
Returns the component-wise interpolation factor for value in [a, b].
inverse_lerp :: (float4 a, float4 b, float4 value) -> float4
inverse_lerp_clamped
Returns saturate(inverse_lerp(a, b, value)).
inverse_lerp_clamped :: (float a, float b, float value) -> float
remap
Remaps a scalar from one range to another.
remap :: (float value, float in_min, float in_max, float out_min, float out_max) -> float
remap
Remaps a vector from one range to another component-wise.
remap :: (float2 value, float2 in_min, float2 in_max, float2 out_min, float2 out_max) -> float2
remap
Remaps a vector from one range to another component-wise.
remap :: (float3 value, float3 in_min, float3 in_max, float3 out_min, float3 out_max) -> float3
remap
Remaps a vector from one range to another component-wise.
remap :: (float4 value, float4 in_min, float4 in_max, float4 out_min, float4 out_max) -> float4
remap_clamped
Remaps a scalar from one range to another, clamping the input range factor to [0, 1].
remap_clamped :: (float value, float in_min, float in_max, float out_min, float out_max) -> float
safe_rcp
Returns 1 / x, or 0 when x is too close to zero.
safe_rcp :: (float x) -> float
safe_divide
Returns a / b, or 0 when b is too close to zero.
safe_divide :: (float a, float b) -> float
safe_divide
Returns a / b, or fallback when b is too close to zero.
safe_divide :: (float a, float b, float fallback) -> float
safe_divide
Returns component-wise a / b, using zero for near-zero denominators.
safe_divide :: (float2 a, float2 b) -> float2
safe_divide
Returns component-wise a / b, using zero for near-zero denominators.
safe_divide :: (float3 a, float3 b) -> float3
safe_divide
Returns component-wise a / b, using zero for near-zero denominators.
safe_divide :: (float4 a, float4 b) -> float4
safe_normalize
Normalizes a float2, returning +X when length is too small.
safe_normalize :: (float2 v) -> float2
safe_normalize_with_fallback
Normalizes a float2, returning fallback when length is too small.
safe_normalize_with_fallback :: (float2 v, float2 fallback) -> float2
safe_normalize
Normalizes a float3, returning +Y when length is too small.
safe_normalize :: (float3 v) -> float3
safe_normalize_with_fallback
Normalizes a float3, returning fallback when length is too small.
safe_normalize_with_fallback :: (float3 v, float3 fallback) -> float3
safe_normalize
Normalizes a float4, returning +W when length is too small.
safe_normalize :: (float4 v) -> float4
safe_normalize_with_fallback
Normalizes a float4, returning fallback when length is too small.
safe_normalize_with_fallback :: (float4 v, float4 fallback) -> float4
length_squared
Returns dot(v, v) without taking a square root.
length_squared :: (float2 v) -> float
length_squared
Returns dot(v, v) without taking a square root.
length_squared :: (float3 v) -> float
length_squared
Returns dot(v, v) without taking a square root.
length_squared :: (float4 v) -> float
distance_squared
Returns squared distance between two points without taking a square root.
distance_squared :: (float2 a, float2 b) -> float
distance_squared
Returns squared distance between two points without taking a square root.
distance_squared :: (float3 a, float3 b) -> float
distance_squared
Returns squared distance between two points without taking a square root.
distance_squared :: (float4 a, float4 b) -> float
saturate_dot
Returns max(dot(a, b), 0), common in lighting equations.
saturate_dot :: (float3 a, float3 b) -> float
clamped_dot
Returns clamp(dot(a, b), -1, 1), useful before acos.
clamped_dot :: (float3 a, float3 b) -> float
angle_between
Reference: https://en.wikipedia.org/wiki/Dot_product
angle_between :: (float3 a, float3 b) -> float
project_onto
Reference: https://en.wikipedia.org/wiki/Vector_projection
project_onto :: (float3 v, float3 onto) -> float3
reject_from
Reference: https://en.wikipedia.org/wiki/Vector_projection
reject_from :: (float3 v, float3 onto) -> float3
orthonormal_tangent
Reference: https://en.wikipedia.org/wiki/Orthonormal_basis
orthonormal_tangent :: (float3 normal) -> float3
face_same_hemisphere
Returns a unit vector facing the same hemisphere as reference.
face_same_hemisphere :: (float3 v, float3 reference) -> float3
square
Returns x squared.
square :: (float x) -> float
square
Returns x squared component-wise.
square :: (float2 x) -> float2
square
Returns x squared component-wise.
square :: (float3 x) -> float3
square
Returns x squared component-wise.
square :: (float4 x) -> float4
cube
Returns x cubed.
cube :: (float x) -> float
cube
Returns x cubed component-wise.
cube :: (float2 x) -> float2
cube
Returns x cubed component-wise.
cube :: (float3 x) -> float3
cube
Returns x cubed component-wise.
cube :: (float4 x) -> float4
pow4
Returns x^4.
pow4 :: (float x) -> float
pow5
Reference: https://en.wikipedia.org/wiki/Schlick%27s_approximation
pow5 :: (float x) -> float
pow5
Returns x^5 component-wise.
pow5 :: (float3 x) -> float3
rotate_2d
Reference: https://en.wikipedia.org/wiki/Rotation_matrix
rotate_2d :: (float2 p, float angle) -> float2
rotate_axis_angle
Reference: https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
rotate_axis_angle :: (float3 v, float3 axis, float angle) -> float3
linear_step
Returns a clamped linear step from edge0 to edge1.
linear_step :: (float edge0, float edge1, float x) -> float
linear_step
Returns a clamped linear step from edge0 to edge1 component-wise.
linear_step :: (float2 edge0, float2 edge1, float2 x) -> float2
linear_step
Returns a clamped linear step from edge0 to edge1 component-wise.
linear_step :: (float3 edge0, float3 edge1, float3 x) -> float3
smootherstep
Reference: https://en.wikipedia.org/wiki/Smoothstep
smootherstep :: (float edge0, float edge1, float x) -> float
smootherstep
Reference: https://en.wikipedia.org/wiki/Smoothstep
smootherstep :: (float2 edge0, float2 edge1, float2 x) -> float2
smootherstep
Reference: https://en.wikipedia.org/wiki/Smoothstep
smootherstep :: (float3 edge0, float3 edge1, float3 x) -> float3
ease_in_out_quad
Reference: https://easings.net/#easeInOutQuad
ease_in_out_quad :: (float t) -> float
ease_in_out_cubic
Reference: https://easings.net/#easeInOutCubic
ease_in_out_cubic :: (float t) -> float
ease_in_out_quart
Reference: https://easings.net/#easeInOutQuart
ease_in_out_quart :: (float t) -> float
ease_out_elastic
Reference: https://easings.net/#easeOutElastic
ease_out_elastic :: (float t) -> float
ease_out_bounce
Reference: https://easings.net/#easeOutBounce
ease_out_bounce :: (float t) -> float
triangle_wave
Reference: https://en.wikipedia.org/wiki/Triangle_wave
triangle_wave :: (float x) -> float
sawtooth_wave
Reference: https://en.wikipedia.org/wiki/Sawtooth_wave
sawtooth_wave :: (float x) -> float
sin01
Sine wave remapped to [0, 1].
sin01 :: (float x) -> float
cos01
Cosine wave remapped to [0, 1].
cos01 :: (float x) -> float
fast_sin
Cheap sine approximation for visual effects. Best for inputs in radians.
fast_sin :: (float x) -> float
Source
Generated from compiler-reference/modules/math.bwsl.