Constructors
In WGSL, matrices can be constructed or initialized using three primary constructor styles: zero-value initialization, column-vector composition, or element-by-element scalar initialization in column-major order.
| Style | Syntax Pattern | Description |
|---|---|---|
| Zero-Value | matCxRf()matCxR<T>() |
Initializes all elements of the matrix to their default zero value (0.0). |
| Column-Wise | matCxR<T>(c0, c1, ...) |
Constructs the matrix by supplying exactly C column vectors. |
| Scalar-Wise | matCxR<T>(s0, s1, ...) |
Constructs the matrix from a flat sequence of scalars in column-major order. |
Zero-Value Constructors
Constructs a matrix with all elements initialized to 0.0. You can use either the generic syntax or the convenient type aliases (like mat3x4f).
let m_zero = mat3x4f(); // Equivalent to mat3x4<f32>() with all 0.0s
let m_f32 = mat2x2<f32>(); // 2x2 matrix filled with 0.0
Column-Wise Constructors
Constructs a matrix by supplying exactly C column vectors, where each column vector has exactly R components. This is the most common way to compose matrices.
Because WGSL is column-major, the first vector argument becomes the first (leftmost) column of the matrix, the second argument becomes the second column, and so on.
let col_0 = vec2f(1.0, 2.0);
let col_1 = vec2f(3.0, 4.0);
let col_2 = vec2f(5.0, 6.0);
// Composes a 3x2 matrix from three column vectors
let m_cols = mat3x2f(col_0, col_1, col_2);
Scalar-Wise Constructors
Constructs a matrix from a flat list of individual scalar values. To specify the scalar values, they must be supplied in column-major order: the elements of the first column are specified first (top-to-bottom), followed by the elements of the second column, and so forth.
For a matrix with C columns and R rows, you must supply exactly \(C \times R\) scalar arguments.
For example, mat2x3f(a, b, c, d, e, f) constructs a 2-column, 3-row matrix of f32s, where the first column is populated by (a, b, c) and the second column is populated by (d, e, f):
// Constructing a 2x3 matrix (2 columns, 3 rows) using 6 scalars
let m_scalars = mat2x3f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
The resulting matrix layout is:
/* * Copyright ©2026 Michael R. Bernstein. All new modifications licensed under Apache 2.0. * Upstream lineage ©2023 governed by original BSD 3-Clause. See README.md. */ const zero_init = mat3x4f(); const column_wise = mat3x2f(vec2f(1, 2), vec2f(3, 4), vec2f(5, 6)); const scalar_wise = mat2x3f(1, 2, 3, 4, 5, 6);