Skip to main content

vec3

Class

A three 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 three dimensional vector. Vectors can only store finite numbers in the range Number.MIN_VALUE to Number.MAX_VALUE.

Index

Example

// Check the distance between two objects
//@input SceneObject objectA
//@input SceneObject objectB
//@input float distCheck

var pointA = script.objectA.getTransform().getWorldPosition();
var pointB = script.objectB.getTransform().getWorldPosition();
var distance = pointA.distance(pointB);
if(distance > script.distCheck)
{
print("Too Far.");
}

Constructors

Constructor

new vec3(x, y, z): vec3

Parameters

ParameterType
xnumber
ynumber
znumber

Returns

vec3

Properties

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.


x

x: number

x component of the vec3.


y

y: number

y component of the vec3.


z

z: number

z component of the vec3.

Methods

add()

add(vec): vec3

Returns the vector plus vec.

Parameters

ParameterType
vecvec3

Returns

vec3


addInPlace()

addInPlace(vec): void

Adds vec to this vector in place.

Parameters

ParameterType
vecvec3

Returns

void


angleTo()

angleTo(vec): number

Returns the angle between the vector and vec.

Parameters

ParameterType
vecvec3

Returns

number


back()

static back(): vec3

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

Returns

vec3


clampLength()

clampLength(length): vec3

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

Parameters

ParameterType
lengthnumber

Returns

vec3


clampLengthInPlace()

clampLengthInPlace(length): void

Clamps this vector's length to length in place.

Parameters

ParameterType
lengthnumber

Returns

void


clone()

clone(): vec3

Returns a copy of this vector.

Returns

vec3


copyFrom()

copyFrom(source): void

Copies matching components from the source vector, leaving other destination components untouched and ignoring extra source components. For example, copying a vec2 leaves z unchanged, while copying a vec4 ignores w.

Parameters

ParameterType
sourcevec2 | vec3 | vec4

Returns

void


cross()

cross(vec): vec3

Returns the cross product of the vector and vec

Parameters

ParameterType
vecvec3

Returns

vec3


crossInPlace()

crossInPlace(vec): void

Sets this vector to the cross product of this vector and vec in place.

Parameters

ParameterType
vecvec3

Returns

void


distance()

distance(vec): number

Returns the distance between the vector and the vector vec.

Parameters

ParameterType
vecvec3

Returns

number


distanceSquared()

distanceSquared(vec): number

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

Parameters

ParameterType
vecvec3

Returns

number


div()

div(vec): vec3

Returns the division of the vector by the vector vec.

Parameters

ParameterType
vecvec3

Returns

vec3


divInPlace()

divInPlace(vec): void

Divides this vector by vec component-wise in place.

Parameters

ParameterType
vecvec3

Returns

void


dot()

dot(vec): number

Returns the dot product of the vector and vec.

Parameters

ParameterType
vecvec3

Returns

number


down()

static down(): vec3

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

Returns

vec3


equal()

equal(vec): boolean

Returns whether this is equal to vec.

Parameters

ParameterType
vecvec3

Returns

boolean


fill()

fill(scalar): void

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

Parameters

ParameterType
scalarnumber

Returns

void


forward()

static forward(): vec3

Returns the vector (0, 0, 1).

Returns

vec3


left()

static left(): vec3

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

Returns

vec3


lerp()

static lerp(from, to, t): vec3

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

Parameters

ParameterType
fromvec3
tovec3
tnumber

Returns

vec3


lerpInPlace()

lerpInPlace(target, t): void

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

Parameters

ParameterType
targetvec3
tnumber

Returns

void


max()

static max(a, b): vec3

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

Parameters

ParameterType
avec3
bvec3

Returns

vec3


min()

static min(a, b): vec3

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

Parameters

ParameterType
avec3
bvec3

Returns

vec3


moveTowards()

moveTowards(target, step): vec3

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

Parameters

ParameterType
targetvec3
stepnumber

Returns

vec3


moveTowardsInPlace()

moveTowardsInPlace(point, magnitude): void

Moves this vector towards point by magnitude in place.

Parameters

ParameterType
pointvec3
magnitudenumber

Returns

void


mult()

mult(vec): vec3

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

Parameters

ParameterType
vecvec3

Returns

vec3


multInPlace()

multInPlace(vec): void

Multiplies this vector by vec component-wise in place.

Parameters

ParameterType
vecvec3

Returns

void


normalize()

normalize(): vec3

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

Returns

vec3


normalizeInPlace()

normalizeInPlace(): void

Scales this vector's length to 1 in place.

Returns

void


one()

static one(): vec3

Returns the vector (1, 1, 1).

Returns

vec3


orthonormalize()

static orthonormalize(a, b): void

Makes the vectors vecA and vecB normalized and orthogonal to each other.

Parameters

ParameterType
avec3
bvec3

Returns

void


project()

project(onto): vec3

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

Parameters

ParameterType
ontovec3

Returns

vec3


projectInPlace()

projectInPlace(onto): void

Projects this vector onto the vector onto in place.

Parameters

ParameterType
ontovec3

Returns

void


projectOnPlane()

projectOnPlane(planeNormal): vec3

Projects the vector onto the plane defined by planeNormal.

Parameters

ParameterType
planeNormalvec3

Returns

vec3


projectOnPlaneInPlace()

projectOnPlaneInPlace(planeNormal): void

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

Parameters

ParameterType
planeNormalvec3

Returns

void


randomUnitVector()

static randomUnitVector(): vec3

Generates a random 3D unit vector. Equivalent to a random point on a unit-radius sphere.

Returns

vec3


reflect()

reflect(planeNormal): vec3

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

Parameters

ParameterType
planeNormalvec3

Returns

vec3


reflectInPlace()

reflectInPlace(planeNormal): void

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

Parameters

ParameterType
planeNormalvec3

Returns

void


static right(): vec3

Returns the vector (1, 0, 0).

Returns

vec3


rotateTowards()

rotateTowards(target, step): vec3

Returns a copy of the vector rotated towards the target vector by step radians.

  • The vectors may be non-normalized. The function always returns a vector with the source vector's magnitude.
  • This prevents overshoot. If step exceeds the angle between vectors, it stops at the target direction.
  • If step is negative, this rotates the source vector away from target. It stops when the direction is precisely opposite to target.
  • If the vectors are in opposite directions, the result is rotated along an arbitrary (but consistent) axis.
  • If either vector is zero magnitude, it returns the source vector.

Parameters

ParameterType
targetvec3
stepnumber

Returns

vec3


rotateTowardsInPlace()

rotateTowardsInPlace(target, step): void

Rotates this vector towards the target vector by step radians in place.

  • The vectors may be non-normalized. The function preserves this vector's magnitude.
  • This prevents overshoot. If step exceeds the angle between vectors, it stops at the target direction.
  • If step is negative, this rotates this vector away from target. It stops when the direction is precisely opposite to target.
  • If the vectors are in opposite directions, this vector is rotated along an arbitrary (but consistent) axis.
  • If either vector is zero magnitude, this vector is not modified.

Parameters

ParameterType
targetvec3
stepnumber

Returns

void


scale()

scale(vec): vec3

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

Parameters

ParameterType
vecvec3

Returns

vec3


scaleInPlace()

scaleInPlace(vec): void

Multiplies this vector by vec component-wise in place.

Parameters

ParameterType
vecvec3

Returns

void


setRandomUnitVector()

setRandomUnitVector(): void

Sets this vector to a random unit vector in place. Equivalent to a random point on a unit-radius sphere.

Returns

void


setRGB()

setRGB(r?, g?, b?): void

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

Parameters

ParameterType
r?number
g?number
b?number

Returns

void


setXYZ()

setXYZ(x?, y?, z?): void

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

Parameters

ParameterType
x?number
y?number
z?number

Returns

void


slerp()

static slerp(from, to, t): vec3

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

Parameters

ParameterType
fromvec3
tovec3
tnumber

Returns

vec3


slerpInPlace()

slerpInPlace(target, t): void

Spherically interpolates this vector towards target by factor t in place. Preserves vector magnitude by interpolating both direction and length.

Parameters

ParameterType
targetvec3
tnumber

Returns

void


sub()

sub(vec): vec3

Returns the vector minus vec.

Parameters

ParameterType
vecvec3

Returns

vec3


subInPlace()

subInPlace(vec): void

Subtracts vec from this vector in place.

Parameters

ParameterType
vecvec3

Returns

void


toString()

toString(): string

Returns a string representation of the vector.

Returns

string


uniformScale()

uniformScale(scale): vec3

Multiplies the components of this vector by scale.

Parameters

ParameterType
scalenumber

Returns

vec3


uniformScaleInPlace()

uniformScaleInPlace(scale): void

Multiplies the components of this vector by scale in place.

Parameters

ParameterType
scalenumber

Returns

void


up()

static up(): vec3

Returns the vector (0, 1, 0).

Returns

vec3


zero()

static zero(): vec3

Returns the vector (0, 0, 0).

Returns

vec3