playcanvas
Version:
PlayCanvas WebGL game engine
558 lines (557 loc) • 18.5 kB
TypeScript
/**
* 3-dimensional vector.
*
* @category Math
*/
export class Vec3 {
/**
* A constant vector set to [0, 0, 0].
*
* @type {Vec3}
* @readonly
*/
static readonly ZERO: Vec3;
/**
* A constant vector set to [0.5, 0.5, 0.5].
*
* @type {Vec3}
* @readonly
*/
static readonly HALF: Vec3;
/**
* A constant vector set to [1, 1, 1].
*
* @type {Vec3}
* @readonly
*/
static readonly ONE: Vec3;
/**
* A constant vector set to [0, 1, 0].
*
* @type {Vec3}
* @readonly
*/
static readonly UP: Vec3;
/**
* A constant vector set to [0, -1, 0].
*
* @type {Vec3}
* @readonly
*/
static readonly DOWN: Vec3;
/**
* A constant vector set to [1, 0, 0].
*
* @type {Vec3}
* @readonly
*/
static readonly RIGHT: Vec3;
/**
* A constant vector set to [-1, 0, 0].
*
* @type {Vec3}
* @readonly
*/
static readonly LEFT: Vec3;
/**
* A constant vector set to [0, 0, -1].
*
* @type {Vec3}
* @readonly
*/
static readonly FORWARD: Vec3;
/**
* A constant vector set to [0, 0, 1].
*
* @type {Vec3}
* @readonly
*/
static readonly BACK: Vec3;
/**
* Creates a new Vec3 instance.
*
* @overload
* @param {number} [x] - The x value. Defaults to 0.
* @param {number} [y] - The y value. Defaults to 0.
* @param {number} [z] - The z value. Defaults to 0.
* @example
* const v1 = new pc.Vec3(); // defaults to 0, 0, 0
* const v2 = new pc.Vec3(1, 2, 3);
*/
constructor(x?: number, y?: number, z?: number);
/**
* Creates a new Vec3 instance.
*
* @overload
* @param {number[]} arr - The array to set the vector values from.
* @example
* const v = new pc.Vec3([1, 2, 3]);
*/
constructor(arr: number[]);
/**
* The first component of the vector.
*
* @type {number}
*/
x: number;
/**
* The second component of the vector.
*
* @type {number}
*/
y: number;
/**
* The third component of the vector.
*
* @type {number}
*/
z: number;
/**
* Adds a 3-dimensional vector to another in place.
*
* @param {Vec3} rhs - The vector to add to the specified vector.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(10, 10, 10);
* const b = new pc.Vec3(20, 20, 20);
*
* a.add(b);
*
* // Outputs [30, 30, 30]
* console.log("The result of the addition is: " + a.toString());
*/
add(rhs: Vec3): Vec3;
/**
* Adds two 3-dimensional vectors together and returns the result.
*
* @param {Vec3} lhs - The first vector operand for the addition.
* @param {Vec3} rhs - The second vector operand for the addition.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(10, 10, 10);
* const b = new pc.Vec3(20, 20, 20);
* const r = new pc.Vec3();
*
* r.add2(a, b);
* // Outputs [30, 30, 30]
*
* console.log("The result of the addition is: " + r.toString());
*/
add2(lhs: Vec3, rhs: Vec3): Vec3;
/**
* Adds a number to each element of a vector.
*
* @param {number} scalar - The number to add.
* @returns {Vec3} Self for chaining.
* @example
* const vec = new pc.Vec3(3, 4, 5);
*
* vec.addScalar(2);
*
* // Outputs [5, 6, 7]
* console.log("The result of the addition is: " + vec.toString());
*/
addScalar(scalar: number): Vec3;
/**
* Adds a 3-dimensional vector scaled by scalar value. Does not modify the vector being added.
*
* @param {Vec3} rhs - The vector to add to the specified vector.
* @param {number} scalar - The number to multiply the added vector with.
* @returns {Vec3} Self for chaining.
* @example
* const vec = new pc.Vec3(1, 2, 3);
*
* vec.addScaled(pc.Vec3.UP, 2);
*
* // Outputs [1, 4, 3]
* console.log("The result of the addition is: " + vec.toString());
*/
addScaled(rhs: Vec3, scalar: number): Vec3;
/**
* Returns an identical copy of the specified 3-dimensional vector.
*
* @returns {this} A 3-dimensional vector containing the result of the cloning.
* @example
* const v = new pc.Vec3(10, 20, 30);
* const vclone = v.clone();
* console.log("The result of the cloning is: " + vclone.toString());
*/
clone(): this;
/**
* Copies the contents of a source 3-dimensional vector to a destination 3-dimensional vector.
*
* @param {Vec3} rhs - A vector to copy to the specified vector.
* @returns {Vec3} Self for chaining.
* @example
* const src = new pc.Vec3(10, 20, 30);
* const dst = new pc.Vec3();
*
* dst.copy(src);
*
* console.log("The two vectors are " + (dst.equals(src) ? "equal" : "different"));
*/
copy(rhs: Vec3): Vec3;
/**
* Returns the result of a cross product operation performed on the two specified 3-dimensional
* vectors.
*
* @param {Vec3} lhs - The first 3-dimensional vector operand of the cross product.
* @param {Vec3} rhs - The second 3-dimensional vector operand of the cross product.
* @returns {Vec3} Self for chaining.
* @example
* const back = new pc.Vec3().cross(pc.Vec3.RIGHT, pc.Vec3.UP);
*
* // Prints the Z axis (i.e. [0, 0, 1])
* console.log("The result of the cross product is: " + back.toString());
*/
cross(lhs: Vec3, rhs: Vec3): Vec3;
/**
* Returns the distance between the two specified 3-dimensional vectors.
*
* @param {Vec3} rhs - The second 3-dimensional vector to test.
* @returns {number} The distance between the two vectors.
* @example
* const v1 = new pc.Vec3(5, 10, 20);
* const v2 = new pc.Vec3(10, 20, 40);
* const d = v1.distance(v2);
* console.log("The distance between v1 and v2 is: " + d);
*/
distance(rhs: Vec3): number;
/**
* Divides a 3-dimensional vector by another in place.
*
* @param {Vec3} rhs - The vector to divide the specified vector by.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(4, 9, 16);
* const b = new pc.Vec3(2, 3, 4);
*
* a.div(b);
*
* // Outputs [2, 3, 4]
* console.log("The result of the division is: " + a.toString());
*/
div(rhs: Vec3): Vec3;
/**
* Divides one 3-dimensional vector by another and writes the result to the specified vector.
*
* @param {Vec3} lhs - The dividend vector (the vector being divided).
* @param {Vec3} rhs - The divisor vector (the vector dividing the dividend).
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(4, 9, 16);
* const b = new pc.Vec3(2, 3, 4);
* const r = new pc.Vec3();
*
* r.div2(a, b);
*
* // Outputs [2, 3, 4]
* console.log("The result of the division is: " + r.toString());
*/
div2(lhs: Vec3, rhs: Vec3): Vec3;
/**
* Divides each element of a vector by a number.
*
* @param {number} scalar - The number to divide by.
* @returns {Vec3} Self for chaining.
* @example
* const vec = new pc.Vec3(3, 6, 9);
*
* vec.divScalar(3);
*
* // Outputs [1, 2, 3]
* console.log("The result of the division is: " + vec.toString());
*/
divScalar(scalar: number): Vec3;
/**
* Returns the result of a dot product operation performed on the two specified 3-dimensional
* vectors.
*
* @param {Vec3} rhs - The second 3-dimensional vector operand of the dot product.
* @returns {number} The result of the dot product operation.
* @example
* const v1 = new pc.Vec3(5, 10, 20);
* const v2 = new pc.Vec3(10, 20, 40);
* const v1dotv2 = v1.dot(v2);
* console.log("The result of the dot product is: " + v1dotv2);
*/
dot(rhs: Vec3): number;
/**
* Reports whether two vectors are equal.
*
* @param {Vec3} rhs - The vector to compare to the specified vector.
* @returns {boolean} True if the vectors are equal and false otherwise.
* @example
* const a = new pc.Vec3(1, 2, 3);
* const b = new pc.Vec3(4, 5, 6);
* console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));
*/
equals(rhs: Vec3): boolean;
/**
* Reports whether two vectors are equal using an absolute error tolerance.
*
* @param {Vec3} rhs - The vector to be compared against.
* @param {number} [epsilon] - The maximum difference between each component of the two
* vectors. Defaults to 1e-6.
* @returns {boolean} True if the vectors are equal and false otherwise.
* @example
* const a = new pc.Vec3();
* const b = new pc.Vec3();
* console.log("The two vectors are approximately " + (a.equalsApprox(b, 1e-9) ? "equal" : "different"));
*/
equalsApprox(rhs: Vec3, epsilon?: number): boolean;
/**
* Returns the magnitude of the specified 3-dimensional vector.
*
* @returns {number} The magnitude of the specified 3-dimensional vector.
* @example
* const vec = new pc.Vec3(3, 4, 0);
* const len = vec.length();
* // Outputs 5
* console.log("The length of the vector is: " + len);
*/
length(): number;
/**
* Returns the magnitude squared of the specified 3-dimensional vector.
*
* @returns {number} The magnitude of the specified 3-dimensional vector.
* @example
* const vec = new pc.Vec3(3, 4, 0);
* const len = vec.lengthSq();
* // Outputs 25
* console.log("The length squared of the vector is: " + len);
*/
lengthSq(): number;
/**
* Returns the result of a linear interpolation between two specified 3-dimensional vectors.
*
* @param {Vec3} lhs - The 3-dimensional to interpolate from.
* @param {Vec3} rhs - The 3-dimensional to interpolate to.
* @param {number} alpha - The value controlling the point of interpolation. Between 0 and 1,
* the linear interpolant will occur on a straight line between lhs and rhs. Outside of this
* range, the linear interpolant will occur on a ray extrapolated from this line.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(0, 0, 0);
* const b = new pc.Vec3(10, 10, 10);
* const r = new pc.Vec3();
*
* r.lerp(a, b, 0); // r is equal to a
* r.lerp(a, b, 0.5); // r is 5, 5, 5
* r.lerp(a, b, 1); // r is equal to b
*/
lerp(lhs: Vec3, rhs: Vec3, alpha: number): Vec3;
/**
* Multiplies a 3-dimensional vector to another in place.
*
* @param {Vec3} rhs - The 3-dimensional vector used as the second multiplicand of the operation.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(2, 3, 4);
* const b = new pc.Vec3(4, 5, 6);
*
* a.mul(b);
*
* // Outputs [8, 15, 24]
* console.log("The result of the multiplication is: " + a.toString());
*/
mul(rhs: Vec3): Vec3;
/**
* Returns the result of multiplying the specified 3-dimensional vectors together.
*
* @param {Vec3} lhs - The 3-dimensional vector used as the first multiplicand of the operation.
* @param {Vec3} rhs - The 3-dimensional vector used as the second multiplicand of the operation.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(2, 3, 4);
* const b = new pc.Vec3(4, 5, 6);
* const r = new pc.Vec3();
*
* r.mul2(a, b);
*
* // Outputs [8, 15, 24]
* console.log("The result of the multiplication is: " + r.toString());
*/
mul2(lhs: Vec3, rhs: Vec3): Vec3;
/**
* Multiplies each element of a vector by a number.
*
* @param {number} scalar - The number to multiply by.
* @returns {Vec3} Self for chaining.
* @example
* const vec = new pc.Vec3(3, 6, 9);
*
* vec.mulScalar(3);
*
* // Outputs [9, 18, 27]
* console.log("The result of the multiplication is: " + vec.toString());
*/
mulScalar(scalar: number): Vec3;
/**
* Returns this 3-dimensional vector converted to a unit vector in place. If the vector has a
* length of zero, the vector's elements will be set to zero.
*
* @param {Vec3} [src] - The vector to normalize. If not set, the operation is done in place.
* @returns {Vec3} Self for chaining.
* @example
* const v = new pc.Vec3(25, 0, 0);
*
* v.normalize();
*
* // Outputs [1, 0, 0]
* console.log("The result of the vector normalization is: " + v.toString());
*/
normalize(src?: Vec3): Vec3;
/**
* Each element is set to the largest integer less than or equal to its value.
*
* @param {Vec3} [src] - The vector to floor. If not set, the operation is done in place.
* @returns {Vec3} Self for chaining.
*/
floor(src?: Vec3): Vec3;
/**
* Each element is rounded up to the next largest integer.
*
* @param {Vec3} [src] - The vector to ceil. If not set, the operation is done in place.
* @returns {Vec3} Self for chaining.
*/
ceil(src?: Vec3): Vec3;
/**
* Each element is rounded up or down to the nearest integer.
*
* @param {Vec3} [src] - The vector to round. If not set, the operation is done in place.
* @returns {Vec3} Self for chaining.
*/
round(src?: Vec3): Vec3;
/**
* Each element is assigned a value from rhs parameter if it is smaller.
*
* @param {Vec3} rhs - The 3-dimensional vector used as the source of elements to compare to.
* @returns {Vec3} Self for chaining.
*/
min(rhs: Vec3): Vec3;
/**
* Each element is assigned a value from rhs parameter if it is larger.
*
* @param {Vec3} rhs - The 3-dimensional vector used as the source of elements to compare to.
* @returns {Vec3} Self for chaining.
*/
max(rhs: Vec3): Vec3;
/**
* Projects this 3-dimensional vector onto the specified vector.
*
* @param {Vec3} rhs - The vector onto which the original vector will be projected on.
* @returns {Vec3} Self for chaining.
* @example
* const v = new pc.Vec3(5, 5, 5);
* const normal = new pc.Vec3(1, 0, 0);
*
* v.project(normal);
*
* // Outputs [5, 0, 0]
* console.log("The result of the vector projection is: " + v.toString());
*/
project(rhs: Vec3): Vec3;
/**
* Sets the specified 3-dimensional vector to the supplied numerical values.
*
* @param {number} x - The value to set on the first component of the vector.
* @param {number} y - The value to set on the second component of the vector.
* @param {number} z - The value to set on the third component of the vector.
* @returns {Vec3} Self for chaining.
* @example
* const v = new pc.Vec3();
* v.set(5, 10, 20);
*
* // Outputs [5, 10, 20]
* console.log("The result of the vector set is: " + v.toString());
*/
set(x: number, y: number, z: number): Vec3;
/**
* Subtracts a 3-dimensional vector from another in place.
*
* @param {Vec3} rhs - The vector to subtract from the specified vector.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(10, 10, 10);
* const b = new pc.Vec3(20, 20, 20);
*
* a.sub(b);
*
* // Outputs [-10, -10, -10]
* console.log("The result of the subtraction is: " + a.toString());
*/
sub(rhs: Vec3): Vec3;
/**
* Subtracts two 3-dimensional vectors from one another and returns the result.
*
* @param {Vec3} lhs - The first vector operand for the subtraction.
* @param {Vec3} rhs - The second vector operand for the subtraction.
* @returns {Vec3} Self for chaining.
* @example
* const a = new pc.Vec3(10, 10, 10);
* const b = new pc.Vec3(20, 20, 20);
* const r = new pc.Vec3();
*
* r.sub2(a, b);
*
* // Outputs [-10, -10, -10]
* console.log("The result of the subtraction is: " + r.toString());
*/
sub2(lhs: Vec3, rhs: Vec3): Vec3;
/**
* Subtracts a number from each element of a vector.
*
* @param {number} scalar - The number to subtract.
* @returns {Vec3} Self for chaining.
* @example
* const vec = new pc.Vec3(3, 4, 5);
*
* vec.subScalar(2);
*
* // Outputs [1, 2, 3]
* console.log("The result of the subtraction is: " + vec.toString());
*/
subScalar(scalar: number): Vec3;
/**
* Set the values of the vector from an array.
*
* @param {number[]|ArrayBufferView} arr - The array to set the vector values from.
* @param {number} [offset] - The zero-based index at which to start copying elements from the
* array. Default is 0.
* @returns {Vec3} Self for chaining.
* @example
* const v = new pc.Vec3();
* v.fromArray([20, 10, 5]);
* // v is set to [20, 10, 5]
*/
fromArray(arr: number[] | ArrayBufferView, offset?: number): Vec3;
/**
* Converts the vector to string form.
*
* @returns {string} The vector in string form.
* @example
* const v = new pc.Vec3(20, 10, 5);
* // Outputs [20, 10, 5]
* console.log(v.toString());
*/
toString(): string;
/**
* @overload
* @param {number[]} [arr] - The array to populate with the vector's number
* components. If not specified, a new array is created.
* @param {number} [offset] - The zero-based index at which to start copying elements to the
* array. Default is 0.
* @returns {number[]} The vector as an array.
*/
toArray(arr?: number[], offset?: number): number[];
/**
* @overload
* @param {ArrayBufferView} arr - The array to populate with the vector's number
* components. If not specified, a new array is created.
* @param {number} [offset] - The zero-based index at which to start copying elements to the
* array. Default is 0.
* @returns {ArrayBufferView} The vector as an array.
*/
toArray(arr: ArrayBufferView, offset?: number): ArrayBufferView;
}