Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Matrix & Vector Functions

wgsl_rs::std provides vector and matrix types, constructors, and the WGSL builtins that operate on them.

Types

Vector and matrix types are aliases over the generic Vec and Mat structs:

TypeWGSLComponents
Vec2f, Vec3f, Vec4fvec2f, vec3f, vec4f2/3/4 × f32
Vec2i, Vec3i, Vec4ivec2i, vec3i, vec4i2/3/4 × i32
Vec2u, Vec3u, Vec4uvec2u, vec3u, vec4u2/3/4 × u32
Mat2x2f, Mat2x3f, Mat2x4fmat2x2f, ...2 columns
Mat3x2f, Mat3x3f, Mat3x4fmat3x2f, ...3 columns
Mat4x2f, Mat4x3f, Mat4x4fmat4x2f, ...4 columns
Mat4fmat4x4falias for Mat4x4f

See Scalars & Literals, Vectors & Swizzles, and Matrices for full coverage.

Constructors

Constructors are free functions named like the WGSL type:

#![allow(unused)]
fn main() {
let a = vec2f(1.0, 2.0);
let b = vec3f(1.0, 2.0, 3.0);
let c = vec4f(0.0);                 // splat
let m = mat4x4f(
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 0.0, 0.0, 1.0,
);
}

Constructors accept scalars, smaller vectors, and combinations thereof, just like WGSL.

Vector operations

FunctionWGSL EquivalentDescription
dot(a, b)dotDot product.
cross(a, b)cross3D cross product (Vec3f only).
length(v)lengthEuclidean length.
distance(a, b)distancelength(a - b).
normalize(v)normalizeUnit vector.
reflect(i, n)reflectReflect incident i about normal n.
refract(i, n, eta)refractRefraction per Snell's law.
face_forward(n, i, ng)faceForwardOrient n to face away from i.
step(edge, x)stepHeaviside-like step.
mix(a, b, t)mixLinear blend.
clamp(x, lo, hi)clampPer-component clamp.
min(a, b), max(a, b)min, maxPer-component min/max.
abs(v)absPer-component absolute value.
sign(v)signPer-component sign.
floor(v), ceil(v), round(v), trunc(v), fract(v)samePer-component rounding.
pow(v, e)powPer-component power.
exp(v), log(v), sqrt(v), inverse_sqrt(v)samePer-component.
sin(v), cos(v), tan(v), ...samePer-component trig.

Matrix builtins

FunctionWGSL EquivalentDescription
transpose(m)transposeMatrix transpose.
determinant(m)determinantDeterminant of a square matrix.
#![allow(unused)]
fn main() {
#[wgsl]
pub mod matrix_example {
    use wgsl_rs::std::*;

    pub fn normal_matrix(model: Mat4f) -> Mat3f {
        let upper = mat3x3f(
            model[0].xyz(),
            model[1].xyz(),
            model[2].xyz(),
        );
        let det = determinant(upper);
        if abs(det) < 1e-8 {
            return mat3x3f(1.0, 0.0, 0.0,
                           0.0, 1.0, 0.0,
                           0.0, 0.0, 1.0);
        }
        transpose(upper) * (1.0 / det)
    }
}
}

inverse is not a WGSL builtin. Compute it from the adjugate and determinant, or use transpose of the cofactor matrix for the common 3×3 normal-matrix case.

Component access

Vector components are accessed with .x(), .y(), .z(), .w() or via swizzle methods like .xyz(), .xy(), .xx(). See Vectors & Swizzles.

Matrix columns are indexed with m[i] (returns a vector) and individual entries with m[i][j], matching WGSL semantics.