Constructors
In WGSL, vectors can be constructed or converted using several built-in constructor forms. There are three primary constructor styles, alongside type inference and element-wise conversion syntax.
| Style | Syntax Pattern | Description |
|---|---|---|
| Zero-Value | vecN<T>() / vecNf() |
Initializes all components to their default zero value. |
| Splat | vecN<T>(val) / vecNf(val) |
Replicates a single scalar value across all components. |
| Element-Wise | vecN<T>(x, y, ...) |
Initializes components from individual scalars or nested vectors. |
| Type-Inferring | vecN(x, y, ...) |
Infers the component type from the supplied arguments. |
| Type-Converting | vecN<T>(vector) |
Converts a vector of one element type to another element type. |
Zero-Value Constructors
Constructs a vector with each component initialized to its default/zero value (e.g., 0.0 for float, 0 for integer, or false for boolean).
let z_float = vec2f(); // Equivalent to vec2f(0.0, 0.0)
let z_bool = vec3<bool>(); // Equivalent to vec3<bool>(false, false, false)
Splat Constructors
Constructs a vector by replicating (or "splatting") a single scalar value across all of its components.
let s_int = vec4i(5); // Equivalent to vec4i(5, 5, 5, 5)
let s_bool = vec2<bool>(true); // Equivalent to vec2<bool>(true, true)
Element-Wise Constructors
Constructs a vector by specifying the exact value of each component. The arguments can be individual scalars, or a mix of scalars and smaller vectors.
If a constructor argument is a vector, WGSL automatically unpacks its components in order. This allows you to easily build larger vectors from smaller ones.
// Constructing from individual scalar values
let coord = vec3u(1, 2, 3);
// Constructing by mixing scalars and a vec2 (automatic unpacking)
let color = vec4f(1.0, vec2f(2.0, 3.0), 4.0); // Equivalent to vec4f(1.0, 2.0, 3.0, 4.0)
Type-Inferring Constructors
If you omit the element type suffix or generic parameter (using just the bare vec2, vec3, or vec4 form), the compiler automatically infers the element type from the supplied arguments.
let v_u32 = vec4(1u); // Infers vec4<u32> because '1u' is a u32
let v_aint = vec2(1, 2); // Infers vec2 of abstract-int (1, 2)
let v_afloat = vec2(1, 2.5); // Infers vec2 of abstract-float (1.0, 2.5)
Vector Type Conversions
You can convert a vector from one component type to another (e.g., from f32 to u32) by passing the original vector as the sole argument to a constructor with the target type. This performs an element-wise cast of each component.
let v_float = vec3f(5.0, 7.5, 10.0);
let v_uint = vec3u(v_float); // Truncates components: vec3u(5u, 7u, 10u)
/* * 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 = vec2f(); const splat = vec2u(9); const construct_with_scalars = vec3f(4, 3, 2); const construct_mix_of_scalar_and_vec = vec4i(10, vec2i(20, 30), 40); const convert_vec3f_to_vec3u = vec3u(vec3f(5, 7.5, 10)); // 'infer_type' is a vec3 of abstract-float. // The abstract-int arguments are implicitly converted to abstract-float. const infer_type = vec3(1, 2.5, 4); // Vectors of abstract-numerics can implictly convert to a concrete type. const implict_abstract_convert : vec3f = infer_type;