vec4
On this page

vec4

Function

vec4 groups 4 components into a vector. A single scalar broadcasts to every component.

Compare examples

Compare a broadcast with distinct components and a zero value. Each component retains its position.

Example 1vec4(1.)
1111
Components in this example
Result
[1,1,1,1]
Example 2vec4(1., 2., 3., 4.)
1234
Components in this example
Result
[1,2,3,4]
Example 3vec4(0.)
0000
Components in this example
Result
[0,0,0,0]

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use vectors to represent coordinates, directions, colours, or several independent values of the same type.

Inputs and limits

A constructor does not normalize a direction. Integer, boolean, and floating-point vectors have different component types; this compiler emulates f64 vectors in software.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Builds a vec4 from four scalars, a vec3 and a scalar, a vec2 and two scalars, or broadcasts a single scalar to all four components.

Syntax

declare function vec4<T = f32>(): VecFor4<T>
declare function vec4<T = f32>(x: number, y: number, z: number, w: number): VecFor4<T>
declare function vec4<T = f32>(v: vec3, w: number): VecFor4<T>
declare function vec4<T = f32>(v: vec2, z: number, w: number): VecFor4<T>
declare function vec4<T = f32>(x: number, v: vec2, w: number): VecFor4<T>
declare function vec4<T = f32>(x: number, y: number, v: vec2): VecFor4<T>
declare function vec4<T = f32>(x: number, v: vec3): VecFor4<T>
declare function vec4<T = f32>(a: vec2, b: vec2): VecFor4<T>
declare function vec4<T = f32>(scalar: number): VecFor4<T>
declare function vec4<T = f32>(v: vec4): VecFor4<T>
declare function vec4<T>(x: bool, y: bool, z: bool, w: bool): VecFor4<T>
declare function vec4<T>(scalar: bool): VecFor4<T>
declare function vec4<T = f32>(v: vec4i): VecFor4<T>
declare function vec4<T = f32>(v: vec4u): VecFor4<T>
declare function vec4<T = f32>(v: vec4b): VecFor4<T>
declare function vec4(v: vec4f64): vec4

A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

type f32 = number & { readonly [f32Tag]?: true }

A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

VecFor4
type VecFor4<T> = VecElemOf<T, vec4u, vec4i, vec4f64, vec4b, vec4>

Parameters

Return value

VecFor4<T>vec4

Examples

See also

Edit this page Report a problem