vec3
ClassA three dimensional vector.
- Available on all platforms
A three dimensional vector. Vectors can only store finite numbers in the range Number.MIN_VALUE to Number.MAX_VALUE.
Index
Properties
Methods
- add
- addInPlace
- angleTo
- back
- clampLength
- clampLengthInPlace
- clone
- copyFrom
- cross
- crossInPlace
- distance
- distanceSquared
- div
- divInPlace
- dot
- down
- equal
- fill
- forward
- left
- lerp
- lerpInPlace
- max
- min
- moveTowards
- moveTowardsInPlace
- mult
- multInPlace
- normalize
- normalizeInPlace
- one
- orthonormalize
- project
- projectInPlace
- projectOnPlane
- projectOnPlaneInPlace
- randomUnitVector
- reflect
- reflectInPlace
- right
- rotateTowards
- rotateTowardsInPlace
- scale
- scaleInPlace
- setRandomUnitVector
- setRGB
- setXYZ
- slerp
- slerpInPlace
- sub
- subInPlace
- toString
- uniformScale
- uniformScaleInPlace
- up
- zero
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
| Parameter | Type |
|---|---|
x | number |
y | number |
z | number |
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
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
addInPlace()
addInPlace(
vec):void
Adds vec to this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
void
angleTo()
angleTo(
vec):number
Returns the angle between the vector and vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
back()
staticback():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
| Parameter | Type |
|---|---|
length | number |
Returns
vec3
clampLengthInPlace()
clampLengthInPlace(
length):void
Clamps this vector's length to length in place.
Parameters
| Parameter | Type |
|---|---|
length | number |
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
Returns
void
cross()
cross(
vec):vec3
Returns the cross product of the vector and vec
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
crossInPlace()
crossInPlace(
vec):void
Sets this vector to the cross product of this vector and vec in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
void
distance()
distance(
vec):number
Returns the distance between the vector and the vector vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
distanceSquared()
distanceSquared(
vec):number
Like distance(), but returns the squared distance between vectors.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
div()
div(
vec):vec3
Returns the division of the vector by the vector vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
divInPlace()
divInPlace(
vec):void
Divides this vector by vec component-wise in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
void
dot()
dot(
vec):number
Returns the dot product of the vector and vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
number
down()
staticdown():vec3
Returns the vector (0, -1, 0).
Returns
vec3
equal()
equal(
vec):boolean
Returns whether this is equal to vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
boolean
fill()
fill(
scalar):void
Sets the x, y, and z components of this vector to scalar.
Parameters
| Parameter | Type |
|---|---|
scalar | number |
Returns
void
forward()
staticforward():vec3
Returns the vector (0, 0, 1).
Returns
vec3
left()
staticleft():vec3
Returns the vector (-1, 0, 0).
Returns
vec3
lerp()
staticlerp(from,to,t):vec3
Linearly interpolates between the two vectors from and to by the factor t.
Parameters
| Parameter | Type |
|---|---|
from | vec3 |
to | vec3 |
t | number |
Returns
vec3
lerpInPlace()
lerpInPlace(
target,t):void
Linearly interpolates this vector towards target by factor t in place.
Parameters
| Parameter | Type |
|---|---|
target | vec3 |
t | number |
Returns
void
max()
staticmax(a,b):vec3
Returns a new vector containing the largest value of each component in the two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec3 |
b | vec3 |
Returns
vec3
min()
staticmin(a,b):vec3
Returns a new vector containing the smallest value of each component in the two vectors.
Parameters
| Parameter | Type |
|---|---|
a | vec3 |
b | vec3 |
Returns
vec3
moveTowards()
moveTowards(
target,step):vec3
Returns a copy of the vector moved towards the point point by the amount magnitude.
Parameters
| Parameter | Type |
|---|---|
target | vec3 |
step | number |
Returns
vec3
moveTowardsInPlace()
moveTowardsInPlace(
point,magnitude):void
Moves this vector towards point by magnitude in place.
Parameters
| Parameter | Type |
|---|---|
point | vec3 |
magnitude | number |
Returns
void
mult()
mult(
vec):vec3
Returns the component-wise multiplication product of the vector and vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
multInPlace()
multInPlace(
vec):void
Multiplies this vector by vec component-wise in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
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()
staticone():vec3
Returns the vector (1, 1, 1).
Returns
vec3
orthonormalize()
staticorthonormalize(a,b):void
Makes the vectors vecA and vecB normalized and orthogonal to each other.
Parameters
| Parameter | Type |
|---|---|
a | vec3 |
b | vec3 |
Returns
void
project()
project(
onto):vec3
Returns a copy of the vector projected onto the vector onto.
Parameters
| Parameter | Type |
|---|---|
onto | vec3 |
Returns
vec3
projectInPlace()
projectInPlace(
onto):void
Projects this vector onto the vector onto in place.
Parameters
| Parameter | Type |
|---|---|
onto | vec3 |
Returns
void
projectOnPlane()
projectOnPlane(
planeNormal):vec3
Projects the vector onto the plane defined by planeNormal.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
vec3
projectOnPlaneInPlace()
projectOnPlaneInPlace(
planeNormal):void
Projects this vector onto the plane defined by planeNormal in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
void
randomUnitVector()
staticrandomUnitVector():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
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
vec3
reflectInPlace()
reflectInPlace(
planeNormal):void
Reflects this vector across the plane defined by planeNormal in place.
Parameters
| Parameter | Type |
|---|---|
planeNormal | vec3 |
Returns
void
right()
staticright():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
stepexceeds the angle between vectors, it stops at thetargetdirection. - If
stepis negative, this rotates the source vector away fromtarget. It stops when the direction is precisely opposite totarget. - 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
| Parameter | Type |
|---|---|
target | vec3 |
step | number |
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
stepexceeds the angle between vectors, it stops at thetargetdirection. - If
stepis negative, this rotates this vector away fromtarget. It stops when the direction is precisely opposite totarget. - 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
| Parameter | Type |
|---|---|
target | vec3 |
step | number |
Returns
void
scale()
scale(
vec):vec3
Returns the component-wise multiplication product of the vector and vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
scaleInPlace()
scaleInPlace(
vec):void
Multiplies this vector by vec component-wise in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
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
| Parameter | Type |
|---|---|
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
| Parameter | Type |
|---|---|
x? | number |
y? | number |
z? | number |
Returns
void
slerp()
staticslerp(from,to,t):vec3
Spherically interpolates between the two vectors from and to by the factor t.
Parameters
| Parameter | Type |
|---|---|
from | vec3 |
to | vec3 |
t | number |
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
| Parameter | Type |
|---|---|
target | vec3 |
t | number |
Returns
void
sub()
sub(
vec):vec3
Returns the vector minus vec.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
Returns
vec3
subInPlace()
subInPlace(
vec):void
Subtracts vec from this vector in place.
Parameters
| Parameter | Type |
|---|---|
vec | vec3 |
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
| Parameter | Type |
|---|---|
scale | number |
Returns
vec3
uniformScaleInPlace()
uniformScaleInPlace(
scale):void
Multiplies the components of this vector by scale in place.
Parameters
| Parameter | Type |
|---|---|
scale | number |
Returns
void
up()
staticup():vec3
Returns the vector (0, 1, 0).
Returns
vec3
zero()
staticzero():vec3
Returns the vector (0, 0, 0).
Returns
vec3