Skip to main content

vec4

Class

A four dimensional vector.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

A four dimensional vector. Vectors can only store finite numbers in the range Number.MIN_VALUE to Number.MAX_VALUE.

Index

Example

// Lightens and darkens a color with vec4 methods
//@input Asset.Material material
//@input vec4 myColor = {1,0,0,1} {"widget":"color"}
//@input float value

script.material.mainColor = script.myColor.uniformScale(script.value);

Constructors

Constructor

new vec4(x, y, z, w): vec4

Parameters

ParameterType
xnumber
ynumber
znumber
wnumber

Returns

vec4

Properties

a

a: number

Alternate name for the w component.


b

b: number

Alternate name for the z component.


g

g: number

Alternate name for the y component.


length

length: number

Returns the length of the vector.


lengthSquared

lengthSquared: number

Returns the squared length of the vector.


r

r: number

Alternate name for the x component.


w

w: number

w component of the vec4.


x

x: number

x component of the vec4.


y

y: number

y component of the vec4.


z

z: number

z component of the vec4.

Methods

add()

add(vec): vec4

Returns the vector plus vec.

Parameters

ParameterType
vecvec4

Returns

vec4


addInPlace()

addInPlace(vec): void

Adds vec to this vector in place.

Parameters

ParameterType
vecvec4

Returns

void


angleTo()

angleTo(vec): number

Returns the angle between the vector and vec.

Parameters

ParameterType
vecvec4

Returns

number


clampLength()

clampLength(length): vec4

Returns a copy of the vector with its length clamped to length.

Parameters

ParameterType
lengthnumber

Returns

vec4


clampLengthInPlace()

clampLengthInPlace(length): void

Clamps this vector's length to length in place.

Parameters

ParameterType
lengthnumber

Returns

void


clone()

clone(): vec4

Returns a copy of this vector.

Returns

vec4


copyFrom()

copyFrom(source): void

Copies matching components from the source vector, leaving other destination components untouched. For example, copying a vec3 into a vec4 will update x, y, and z but leave w unchanged.

Parameters

ParameterType
sourcevec2 | vec3 | vec4

Returns

void


distance()

distance(vec): number

Returns the distance between the vector and the vector vec.

Parameters

ParameterType
vecvec4

Returns

number


distanceSquared()

distanceSquared(vec): number

Like distance(), but returns the squared distance between vectors.

Parameters

ParameterType
vecvec4

Returns

number


div()

div(vec): vec4

Returns the division of the vector by the vector vec.

Parameters

ParameterType
vecvec4

Returns

vec4


divInPlace()

divInPlace(vec): void

Divides this vector by vec component-wise in place.

Parameters

ParameterType
vecvec4

Returns

void


dot()

dot(vec): number

Returns the dot product of the vector and vec.

Parameters

ParameterType
vecvec4

Returns

number


equal()

equal(vec): boolean

Returns whether this is equal to vec.

Parameters

ParameterType
vecvec4

Returns

boolean


fill()

fill(scalar): void

Sets the x, y, z, and w components of this vector to scalar.

Parameters

ParameterType
scalarnumber

Returns

void


lerp()

static lerp(from, to, t): vec4

Linearly interpolates between the two vectors from and to by the factor t.

Parameters

ParameterType
fromvec4
tovec4
tnumber

Returns

vec4


lerpInPlace()

lerpInPlace(target, t): void

Linearly interpolates this vector towards target by factor t in place.

Parameters

ParameterType
targetvec4
tnumber

Returns

void


max()

static max(a, b): vec4

Returns a new vector containing the largest value of each component in the two vectors.

Parameters

ParameterType
avec4
bvec4

Returns

vec4


min()

static min(a, b): vec4

Returns a new vector containing the smallest value of each component in the two vectors.

Parameters

ParameterType
avec4
bvec4

Returns

vec4


moveTowards()

moveTowards(target, step): vec4

Returns a copy of the vector moved towards the point point by the amount magnitude.

Parameters

ParameterType
targetvec4
stepnumber

Returns

vec4


moveTowardsInPlace()

moveTowardsInPlace(point, magnitude): void

Moves this vector towards point by magnitude in place.

Parameters

ParameterType
pointvec4
magnitudenumber

Returns

void


mult()

mult(vec): vec4

Returns the component-wise multiplication product of the vector and vec.

Parameters

ParameterType
vecvec4

Returns

vec4


multInPlace()

multInPlace(vec): void

Multiplies this vector by vec component-wise in place.

Parameters

ParameterType
vecvec4

Returns

void


normalize()

normalize(): vec4

Returns a copy of the vector with its length scaled to 1.

Returns

vec4


normalizeInPlace()

normalizeInPlace(): void

Scales this vector's length to 1 in place.

Returns

void


one()

static one(): vec4

Returns the vector (1, 1, 1, 1).

Returns

vec4


project()

project(onto): vec4

Returns a copy of the vector projected onto the vector onto.

Parameters

ParameterType
ontovec4

Returns

vec4


projectInPlace()

projectInPlace(onto): void

Projects this vector onto the vector onto in place.

Parameters

ParameterType
ontovec4

Returns

void


projectOnPlane()

projectOnPlane(planeNormal): vec4

Projects the vector onto the plane defined by planeNormal.

Parameters

ParameterType
planeNormalvec4

Returns

vec4


projectOnPlaneInPlace()

projectOnPlaneInPlace(planeNormal): void

Projects this vector onto the plane defined by planeNormal in place.

Parameters

ParameterType
planeNormalvec4

Returns

void


reflect()

reflect(planeNormal): vec4

Returns a copy of the vector reflected across the plane defined by planeNormal.

Parameters

ParameterType
planeNormalvec4

Returns

vec4


reflectInPlace()

reflectInPlace(planeNormal): void

Reflects this vector across the plane defined by planeNormal in place.

Parameters

ParameterType
planeNormalvec4

Returns

void


scale()

scale(vec): vec4

Returns the component-wise multiplication product of the vector and vec.

Parameters

ParameterType
vecvec4

Returns

vec4


scaleInPlace()

scaleInPlace(vec): void

Multiplies this vector by vec component-wise in place.

Parameters

ParameterType
vecvec4

Returns

void


setRGBA()

setRGBA(r?, g?, b?, a?): void

Alternative to setXYZW. Sets the x, y, z and w components of this vector. Omit parameters or pass null or undefined to leave a component unchanged.

Parameters

ParameterType
r?number
g?number
b?number
a?number

Returns

void


setXYZW()

setXYZW(x?, y?, z?, w?): void

Sets the x, y, z, and w components of this vector. Omit parameters or pass null or undefined to leave a component unchanged.

Parameters

ParameterType
x?number
y?number
z?number
w?number

Returns

void


sub()

sub(vec): vec4

Returns the vector minus vec.

Parameters

ParameterType
vecvec4

Returns

vec4


subInPlace()

subInPlace(vec): void

Subtracts vec from this vector in place.

Parameters

ParameterType
vecvec4

Returns

void


toString()

toString(): string

Returns a string representation of the vector.

Returns

string


uniformScale()

uniformScale(scale): vec4

Multiplies the components of this vector by scale.

Parameters

ParameterType
scalenumber

Returns

vec4


uniformScaleInPlace()

uniformScaleInPlace(scale): void

Multiplies the components of this vector by scale in place.

Parameters

ParameterType
scalenumber

Returns

void


zero()

static zero(): vec4

Returns the vector (0, 0, 0, 0).

Returns

vec4