Skip to content

vec4

A four-dimensional vector containing x, y, z, and w floating-point values.

Initialize a 4D vector with specific values or by cloning an existing one.

-- create a vector at 0,0,0,0
local v1 = vec4()
-- create a vector with specific components
local v2 = vec4(1.0, 0.5, 0.0, 1.0)
-- create a vector from another vec4 (cloning)
local v3 = vec4(v2)

The vec4 primitive is often used as a container for data that needs to be processed by the GPU.

When working with functions such as ui.renderShader or render.mesh, vec4 is commonly used to pass custom parameters, such as clipping planes or grouped constants, into shader constant buffers. The type supports standard mathematical operators (+, -, *, /) and equality checks, with the # operator returning the vector’s 4D magnitude.

vec4 is an FFI-backed structure, making property access inexpensive. However, creating large numbers of new instances per frame should be avoided in performance-critical code.

PropertyTypeDescription
xnumberThe first component.
ynumberThe second component.
znumberThe third component.
wnumberThe fourth component.
MethodDescription
:set(x, y, z, w)Updates all four components in place.
:normalize(out)Scales the vector to a 4D length of 1.
:lerp(other, mix, out)Linearly interpolates between two 4D vectors.
:dot(other)Calculates the 4D dot product.
:unpack()Returns x, y, z, and w as separate numbers.
:table()Returns the values as a standard Lua array.

Preparing a vec4 to pass custom data (like position and a radius) into a shader.

local shaderParams = vec4(10.5, 0.0, 50.2, 5.0) -- x,y,z position + radius in w
ui.renderShader({
values = {
gTargetData = shaderParams
},
shader = [[
float4 main(PS_IN pin) {
float3 pos = gTargetData.xyz;
float radius = gTargetData.w;
// ... shader logic using the 4D container
}
]]
})