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 - Result
[1,1,1,1]
Example 2vec4(1., 2., 3., 4.)
1234 - Result
[1,2,3,4]
Example 3vec4(0.)
0000 - 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.
Related functions
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): vec4A 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.
VecFor4type VecFor4<T> = VecElemOf<T, vec4u, vec4i, vec4f64, vec4b, vec4>
Parameters
Return value
VecFor4<T>vec4
Examples
path/first-color/first-color.shade.tspath/uv-gradient/uv-gradient.shade.tspath/moving-wave/moving-wave.shade.tspath/sdf-shapes/sdf-shapes.shade.tspath/tile-pattern/tile-pattern.shade.tspath/fractal/fractal.shade.tspath/palette/palette.shade.tspath/bouncing-balls/bouncing-balls.shade.tspath/ray-tracer/ray-tracer.shade.tsgallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.tsgallery/domain-warp-twin/domain-warp-twin.shade.tsgallery/hillshade-twin/hillshade-twin.shade.tsgallery/julia-twin/julia-twin.shade.tsgallery/kaleidoscope-twin/kaleidoscope-twin.shade.tsgallery/mandelbrot-twin/mandelbrot-twin.shade.tsgallery/ocean-twin/ocean-twin.shade.tsgallery/path-tracer/path-tracer.shade.tsgallery/plasma-twin/plasma-twin.shade.tsgallery/rt-renderer-class/rt-renderer-class.shade.tsgallery/starfield-twin/starfield-twin.shade.tsgallery/tunnel-twin/tunnel-twin.shade.tsgallery/voronoi-twin/voronoi-twin.shade.tsgallery/fp64-burning-ship-twin/fp64-burning-ship-twin.shade.tsgallery/fp64-cancellation-twin/fp64-cancellation-twin.shade.tsgallery/fp64-checker-plane-twin/fp64-checker-plane-twin.shade.tsgallery/fp64-clock-twin/fp64-clock-twin.shade.tsgallery/fp64-deep-zoom-twin/fp64-deep-zoom-twin.shade.tsgallery/fp64-julia-twin/fp64-julia-twin.shade.tsgallery/fp64-lane-stripes/fp64-lane-stripes.shade.tsgallery/fp64-loran-twin/fp64-loran-twin.shade.tsgallery/fp64-mandelbrot-de-twin/fp64-mandelbrot-de-twin.shade.tsgallery/fp64-newton-twin/fp64-newton-twin.shade.tsgallery/fp64-rtc-twin/fp64-rtc-twin.shade.tsgallery/fp64-sine-sweep-twin/fp64-sine-sweep-twin.shade.tsgallery/array-literal-ramp/array-literal-ramp.shade.tsgallery/array-methods/array-methods.shade.tsgallery/bare-position/bare-position.shade.tsgallery/bit-bump/bit-bump.shade.tsgallery/bitfield-bands/bitfield-bands.shade.tsgallery/block-scope/block-scope.shade.tsgallery/bool-select/bool-select.shade.tsgallery/class-builder/class-builder.shade.tsgallery/class-parts/class-parts.shade.tsgallery/class-syntax/class-syntax.shade.tsgallery/clip-planes/clip-planes.shade.tsgallery/closures/closures.shade.tsgallery/convert-grid/convert-grid.shade.tsgallery/cube-array-gather/cube-array-gather.shade.tsgallery/cube-env/cube-env.shade.tsgallery/cutout/cutout.shade.tsgallery/default-args/default-args.shade.tsgallery/feedback-trail/feedback-trail.shade.tsgallery/generic-class/generic-class.shade.tsgallery/generic-helpers/generic-helpers.shade.tsgallery/gradient-twin/gradient-twin.shade.tsgallery/hello/hello.shade.tsgallery/hello-uniform/hello-uniform.shade.tsgallery/hello-uniform-struct/hello-uniform-struct.shade.tsgallery/hello-vsin/hello-vsin.shade.tsgallery/hello-vsout/hello-vsout.shade.tsgallery/higher-order/higher-order.shade.tsgallery/id-pick/id-pick.shade.tsgallery/imported-noise/imported-noise.shade.tsgallery/inferred-returns/inferred-returns.shade.tsgallery/integer-math/integer-math.shade.tsgallery/kernel-image-invert/kernel-image-invert.shade.tsgallery/loop-struct-array/loop-struct-array.shade.tsgallery/loops-over-data/loops-over-data.shade.tsgallery/mixin-surface/mixin-surface.shade.tsgallery/module-const/module-const.shade.tsgallery/msaa-resolve/msaa-resolve.shade.tsgallery/normal-matrix/normal-matrix.shade.tsgallery/null-values/null-values.shade.tsgallery/optional-fields/optional-fields.shade.tsgallery/optional-values/optional-values.shade.tsgallery/orbit-inout/orbit-inout.shade.tsgallery/packed-bytes/packed-bytes.shade.tsgallery/packing-bitcast/packing-bitcast.shade.tsgallery/palette-const/palette-const.shade.tsgallery/pick-composite/pick-composite.shade.tsgallery/private-state/private-state.shade.tsgallery/ray-class/ray-class.shade.tsgallery/reference-parameters/reference-parameters.shade.tsgallery/rng-method/rng-method.shade.tsgallery/sample-branch/sample-branch.shade.tsgallery/separable-blur/separable-blur.shade.tsgallery/shadow-compare/shadow-compare.shade.tsgallery/shape-inheritance/shape-inheritance.shade.tsgallery/storage-texture/storage-texture.shade.tsgallery/textured-quad/textured-quad.shade.tsgallery/tuple-and-brand/tuple-and-brand.shade.tsgallery/twin-structs/twin-structs.shade.tsgallery/uniform-array/uniform-array.shade.ts