| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263 |
- import { clamp } from './MathUtils.js';
- import { Quaternion } from './Quaternion.js';
- /**
- * Class representing a 3D vector. A 3D vector is an ordered triplet of numbers
- * (labeled x, y and z), which can be used to represent a number of things, such as:
- *
- * - A point in 3D space.
- * - A direction and length in 3D space. In three.js the length will
- * always be the Euclidean distance(straight-line distance) from `(0, 0, 0)` to `(x, y, z)`
- * and the direction is also measured from `(0, 0, 0)` towards `(x, y, z)`.
- * - Any arbitrary ordered triplet of numbers.
- *
- * There are other things a 3D vector can be used to represent, such as
- * momentum vectors and so on, however these are the most
- * common uses in three.js.
- *
- * Iterating through a vector instance will yield its components `(x, y, z)` in
- * the corresponding order.
- * ```js
- * const a = new THREE.Vector3( 0, 1, 0 );
- *
- * //no arguments; will be initialised to (0, 0, 0)
- * const b = new THREE.Vector3( );
- *
- * const d = a.distanceTo( b );
- * ```
- */
- class Vector3 {
- static {
- /**
- * This flag can be used for type testing.
- *
- * @type {boolean}
- * @readonly
- * @default true
- */
- Vector3.prototype.isVector3 = true;
- }
- /**
- * Constructs a new 3D vector.
- *
- * @param {number} [x=0] - The x value of this vector.
- * @param {number} [y=0] - The y value of this vector.
- * @param {number} [z=0] - The z value of this vector.
- */
- constructor( x = 0, y = 0, z = 0 ) {
- /**
- * The x value of this vector.
- *
- * @type {number}
- */
- this.x = x;
- /**
- * The y value of this vector.
- *
- * @type {number}
- */
- this.y = y;
- /**
- * The z value of this vector.
- *
- * @type {number}
- */
- this.z = z;
- }
- /**
- * Sets the vector components.
- *
- * @param {number} x - The value of the x component.
- * @param {number} y - The value of the y component.
- * @param {number} z - The value of the z component.
- * @return {Vector3} A reference to this vector.
- */
- set( x, y, z ) {
- if ( z === undefined ) z = this.z; // sprite.scale.set(x,y)
- this.x = x;
- this.y = y;
- this.z = z;
- return this;
- }
- /**
- * Sets the vector components to the same value.
- *
- * @param {number} scalar - The value to set for all vector components.
- * @return {Vector3} A reference to this vector.
- */
- setScalar( scalar ) {
- this.x = scalar;
- this.y = scalar;
- this.z = scalar;
- return this;
- }
- /**
- * Sets the vector's x component to the given value.
- *
- * @param {number} x - The value to set.
- * @return {Vector3} A reference to this vector.
- */
- setX( x ) {
- this.x = x;
- return this;
- }
- /**
- * Sets the vector's y component to the given value.
- *
- * @param {number} y - The value to set.
- * @return {Vector3} A reference to this vector.
- */
- setY( y ) {
- this.y = y;
- return this;
- }
- /**
- * Sets the vector's z component to the given value.
- *
- * @param {number} z - The value to set.
- * @return {Vector3} A reference to this vector.
- */
- setZ( z ) {
- this.z = z;
- return this;
- }
- /**
- * Allows to set a vector component with an index.
- *
- * @param {number} index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.
- * @param {number} value - The value to set.
- * @return {Vector3} A reference to this vector.
- */
- setComponent( index, value ) {
- switch ( index ) {
- case 0: this.x = value; break;
- case 1: this.y = value; break;
- case 2: this.z = value; break;
- default: throw new Error( 'index is out of range: ' + index );
- }
- return this;
- }
- /**
- * Returns the value of the vector component which matches the given index.
- *
- * @param {number} index - The component index. `0` equals to x, `1` equals to y, `2` equals to z.
- * @return {number} A vector component value.
- */
- getComponent( index ) {
- switch ( index ) {
- case 0: return this.x;
- case 1: return this.y;
- case 2: return this.z;
- default: throw new Error( 'index is out of range: ' + index );
- }
- }
- /**
- * Returns a new vector with copied values from this instance.
- *
- * @return {Vector3} A clone of this instance.
- */
- clone() {
- return new this.constructor( this.x, this.y, this.z );
- }
- /**
- * Copies the values of the given vector to this instance.
- *
- * @param {Vector3} v - The vector to copy.
- * @return {Vector3} A reference to this vector.
- */
- copy( v ) {
- this.x = v.x;
- this.y = v.y;
- this.z = v.z;
- return this;
- }
- /**
- * Adds the given vector to this instance.
- *
- * @param {Vector3} v - The vector to add.
- * @return {Vector3} A reference to this vector.
- */
- add( v ) {
- this.x += v.x;
- this.y += v.y;
- this.z += v.z;
- return this;
- }
- /**
- * Adds the given scalar value to all components of this instance.
- *
- * @param {number} s - The scalar to add.
- * @return {Vector3} A reference to this vector.
- */
- addScalar( s ) {
- this.x += s;
- this.y += s;
- this.z += s;
- return this;
- }
- /**
- * Adds the given vectors and stores the result in this instance.
- *
- * @param {Vector3} a - The first vector.
- * @param {Vector3} b - The second vector.
- * @return {Vector3} A reference to this vector.
- */
- addVectors( a, b ) {
- this.x = a.x + b.x;
- this.y = a.y + b.y;
- this.z = a.z + b.z;
- return this;
- }
- /**
- * Adds the given vector scaled by the given factor to this instance.
- *
- * @param {Vector3|Vector4} v - The vector.
- * @param {number} s - The factor that scales `v`.
- * @return {Vector3} A reference to this vector.
- */
- addScaledVector( v, s ) {
- this.x += v.x * s;
- this.y += v.y * s;
- this.z += v.z * s;
- return this;
- }
- /**
- * Subtracts the given vector from this instance.
- *
- * @param {Vector3} v - The vector to subtract.
- * @return {Vector3} A reference to this vector.
- */
- sub( v ) {
- this.x -= v.x;
- this.y -= v.y;
- this.z -= v.z;
- return this;
- }
- /**
- * Subtracts the given scalar value from all components of this instance.
- *
- * @param {number} s - The scalar to subtract.
- * @return {Vector3} A reference to this vector.
- */
- subScalar( s ) {
- this.x -= s;
- this.y -= s;
- this.z -= s;
- return this;
- }
- /**
- * Subtracts the given vectors and stores the result in this instance.
- *
- * @param {Vector3} a - The first vector.
- * @param {Vector3} b - The second vector.
- * @return {Vector3} A reference to this vector.
- */
- subVectors( a, b ) {
- this.x = a.x - b.x;
- this.y = a.y - b.y;
- this.z = a.z - b.z;
- return this;
- }
- /**
- * Multiplies the given vector with this instance.
- *
- * @param {Vector3} v - The vector to multiply.
- * @return {Vector3} A reference to this vector.
- */
- multiply( v ) {
- this.x *= v.x;
- this.y *= v.y;
- this.z *= v.z;
- return this;
- }
- /**
- * Multiplies the given scalar value with all components of this instance.
- *
- * @param {number} scalar - The scalar to multiply.
- * @return {Vector3} A reference to this vector.
- */
- multiplyScalar( scalar ) {
- this.x *= scalar;
- this.y *= scalar;
- this.z *= scalar;
- return this;
- }
- /**
- * Multiplies the given vectors and stores the result in this instance.
- *
- * @param {Vector3} a - The first vector.
- * @param {Vector3} b - The second vector.
- * @return {Vector3} A reference to this vector.
- */
- multiplyVectors( a, b ) {
- this.x = a.x * b.x;
- this.y = a.y * b.y;
- this.z = a.z * b.z;
- return this;
- }
- /**
- * Applies the given Euler rotation to this vector.
- *
- * @param {Euler} euler - The Euler angles.
- * @return {Vector3} A reference to this vector.
- */
- applyEuler( euler ) {
- return this.applyQuaternion( _quaternion.setFromEuler( euler ) );
- }
- /**
- * Applies a rotation specified by an axis and an angle to this vector.
- *
- * @param {Vector3} axis - A normalized vector representing the rotation axis.
- * @param {number} angle - The angle in radians.
- * @return {Vector3} A reference to this vector.
- */
- applyAxisAngle( axis, angle ) {
- return this.applyQuaternion( _quaternion.setFromAxisAngle( axis, angle ) );
- }
- /**
- * Multiplies this vector with the given 3x3 matrix.
- *
- * @param {Matrix3} m - The 3x3 matrix.
- * @return {Vector3} A reference to this vector.
- */
- applyMatrix3( m ) {
- const x = this.x, y = this.y, z = this.z;
- const e = m.elements;
- this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
- this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
- this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
- return this;
- }
- /**
- * Multiplies this vector by the given normal matrix and normalizes
- * the result.
- *
- * @param {Matrix3} m - The normal matrix.
- * @return {Vector3} A reference to this vector.
- */
- applyNormalMatrix( m ) {
- return this.applyMatrix3( m ).normalize();
- }
- /**
- * Multiplies this vector (with an implicit 1 in the 4th dimension) by m, and
- * divides by perspective.
- *
- * @param {Matrix4} m - The matrix to apply.
- * @return {Vector3} A reference to this vector.
- */
- applyMatrix4( m ) {
- const x = this.x, y = this.y, z = this.z;
- const e = m.elements;
- const w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
- this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
- this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
- this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;
- return this;
- }
- /**
- * Applies the given Quaternion to this vector.
- *
- * @param {Quaternion} q - The Quaternion.
- * @return {Vector3} A reference to this vector.
- */
- applyQuaternion( q ) {
- // quaternion q is assumed to have unit length
- const vx = this.x, vy = this.y, vz = this.z;
- const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
- // t = 2 * cross( q.xyz, v );
- const tx = 2 * ( qy * vz - qz * vy );
- const ty = 2 * ( qz * vx - qx * vz );
- const tz = 2 * ( qx * vy - qy * vx );
- // v + q.w * t + cross( q.xyz, t );
- this.x = vx + qw * tx + qy * tz - qz * ty;
- this.y = vy + qw * ty + qz * tx - qx * tz;
- this.z = vz + qw * tz + qx * ty - qy * tx;
- return this;
- }
- /**
- * Projects this vector from world space into the camera's normalized
- * device coordinate (NDC) space.
- *
- * @param {Camera} camera - The camera.
- * @return {Vector3} A reference to this vector.
- */
- project( camera ) {
- return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
- }
- /**
- * Unprojects this vector from the camera's normalized device coordinate (NDC)
- * space into world space.
- *
- * @param {Camera} camera - The camera.
- * @return {Vector3} A reference to this vector.
- */
- unproject( camera ) {
- return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld );
- }
- /**
- * Transforms the direction of this vector by a matrix (the upper left 3 x 3
- * subset of the given 4x4 matrix and then normalizes the result.
- *
- * @param {Matrix4} m - The matrix.
- * @return {Vector3} A reference to this vector.
- */
- transformDirection( m ) {
- // input: THREE.Matrix4 affine matrix
- // vector interpreted as a direction
- const x = this.x, y = this.y, z = this.z;
- const e = m.elements;
- this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
- this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
- this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
- return this.normalize();
- }
- /**
- * Divides this instance by the given vector.
- *
- * @param {Vector3} v - The vector to divide.
- * @return {Vector3} A reference to this vector.
- */
- divide( v ) {
- this.x /= v.x;
- this.y /= v.y;
- this.z /= v.z;
- return this;
- }
- /**
- * Divides this vector by the given scalar.
- *
- * @param {number} scalar - The scalar to divide.
- * @return {Vector3} A reference to this vector.
- */
- divideScalar( scalar ) {
- return this.multiplyScalar( 1 / scalar );
- }
- /**
- * If this vector's x, y or z value is greater than the given vector's x, y or z
- * value, replace that value with the corresponding min value.
- *
- * @param {Vector3} v - The vector.
- * @return {Vector3} A reference to this vector.
- */
- min( v ) {
- this.x = Math.min( this.x, v.x );
- this.y = Math.min( this.y, v.y );
- this.z = Math.min( this.z, v.z );
- return this;
- }
- /**
- * If this vector's x, y or z value is less than the given vector's x, y or z
- * value, replace that value with the corresponding max value.
- *
- * @param {Vector3} v - The vector.
- * @return {Vector3} A reference to this vector.
- */
- max( v ) {
- this.x = Math.max( this.x, v.x );
- this.y = Math.max( this.y, v.y );
- this.z = Math.max( this.z, v.z );
- return this;
- }
- /**
- * If this vector's x, y or z value is greater than the max vector's x, y or z
- * value, it is replaced by the corresponding value.
- * If this vector's x, y or z value is less than the min vector's x, y or z value,
- * it is replaced by the corresponding value.
- *
- * @param {Vector3} min - The minimum x, y and z values.
- * @param {Vector3} max - The maximum x, y and z values in the desired range.
- * @return {Vector3} A reference to this vector.
- */
- clamp( min, max ) {
- // assumes min < max, componentwise
- this.x = clamp( this.x, min.x, max.x );
- this.y = clamp( this.y, min.y, max.y );
- this.z = clamp( this.z, min.z, max.z );
- return this;
- }
- /**
- * If this vector's x, y or z values are greater than the max value, they are
- * replaced by the max value.
- * If this vector's x, y or z values are less than the min value, they are
- * replaced by the min value.
- *
- * @param {number} minVal - The minimum value the components will be clamped to.
- * @param {number} maxVal - The maximum value the components will be clamped to.
- * @return {Vector3} A reference to this vector.
- */
- clampScalar( minVal, maxVal ) {
- this.x = clamp( this.x, minVal, maxVal );
- this.y = clamp( this.y, minVal, maxVal );
- this.z = clamp( this.z, minVal, maxVal );
- return this;
- }
- /**
- * If this vector's length is greater than the max value, it is replaced by
- * the max value.
- * If this vector's length is less than the min value, it is replaced by the
- * min value.
- *
- * @param {number} min - The minimum value the vector length will be clamped to.
- * @param {number} max - The maximum value the vector length will be clamped to.
- * @return {Vector3} A reference to this vector.
- */
- clampLength( min, max ) {
- const length = this.length();
- return this.divideScalar( length || 1 ).multiplyScalar( clamp( length, min, max ) );
- }
- /**
- * The components of this vector are rounded down to the nearest integer value.
- *
- * @return {Vector3} A reference to this vector.
- */
- floor() {
- this.x = Math.floor( this.x );
- this.y = Math.floor( this.y );
- this.z = Math.floor( this.z );
- return this;
- }
- /**
- * The components of this vector are rounded up to the nearest integer value.
- *
- * @return {Vector3} A reference to this vector.
- */
- ceil() {
- this.x = Math.ceil( this.x );
- this.y = Math.ceil( this.y );
- this.z = Math.ceil( this.z );
- return this;
- }
- /**
- * The components of this vector are rounded to the nearest integer value
- *
- * @return {Vector3} A reference to this vector.
- */
- round() {
- this.x = Math.round( this.x );
- this.y = Math.round( this.y );
- this.z = Math.round( this.z );
- return this;
- }
- /**
- * The components of this vector are rounded towards zero (up if negative,
- * down if positive) to an integer value.
- *
- * @return {Vector3} A reference to this vector.
- */
- roundToZero() {
- this.x = Math.trunc( this.x );
- this.y = Math.trunc( this.y );
- this.z = Math.trunc( this.z );
- return this;
- }
- /**
- * Inverts this vector - i.e. sets x = -x, y = -y and z = -z.
- *
- * @return {Vector3} A reference to this vector.
- */
- negate() {
- this.x = - this.x;
- this.y = - this.y;
- this.z = - this.z;
- return this;
- }
- /**
- * Calculates the dot product of the given vector with this instance.
- *
- * @param {Vector3} v - The vector to compute the dot product with.
- * @return {number} The result of the dot product.
- */
- dot( v ) {
- return this.x * v.x + this.y * v.y + this.z * v.z;
- }
- /**
- * Computes the square of the Euclidean length (straight-line length) from
- * (0, 0, 0) to (x, y, z). If you are comparing the lengths of vectors, you should
- * compare the length squared instead as it is slightly more efficient to calculate.
- *
- * @return {number} The square length of this vector.
- */
- lengthSq() {
- return this.x * this.x + this.y * this.y + this.z * this.z;
- }
- /**
- * Computes the Euclidean length (straight-line length) from (0, 0, 0) to (x, y, z).
- *
- * @return {number} The length of this vector.
- */
- length() {
- return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
- }
- /**
- * Computes the Manhattan length of this vector.
- *
- * @return {number} The length of this vector.
- */
- manhattanLength() {
- return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
- }
- /**
- * Converts this vector to a unit vector - that is, sets it equal to a vector
- * with the same direction as this one, but with a vector length of `1`.
- *
- * @return {Vector3} A reference to this vector.
- */
- normalize() {
- return this.divideScalar( this.length() || 1 );
- }
- /**
- * Sets this vector to a vector with the same direction as this one, but
- * with the specified length.
- *
- * @param {number} length - The new length of this vector.
- * @return {Vector3} A reference to this vector.
- */
- setLength( length ) {
- return this.normalize().multiplyScalar( length );
- }
- /**
- * Linearly interpolates between the given vector and this instance, where
- * alpha is the percent distance along the line - alpha = 0 will be this
- * vector, and alpha = 1 will be the given one.
- *
- * @param {Vector3} v - The vector to interpolate towards.
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
- * @return {Vector3} A reference to this vector.
- */
- lerp( v, alpha ) {
- this.x += ( v.x - this.x ) * alpha;
- this.y += ( v.y - this.y ) * alpha;
- this.z += ( v.z - this.z ) * alpha;
- return this;
- }
- /**
- * Linearly interpolates between the given vectors, where alpha is the percent
- * distance along the line - alpha = 0 will be first vector, and alpha = 1 will
- * be the second one. The result is stored in this instance.
- *
- * @param {Vector3} v1 - The first vector.
- * @param {Vector3} v2 - The second vector.
- * @param {number} alpha - The interpolation factor, typically in the closed interval `[0, 1]`.
- * @return {Vector3} A reference to this vector.
- */
- lerpVectors( v1, v2, alpha ) {
- this.x = v1.x + ( v2.x - v1.x ) * alpha;
- this.y = v1.y + ( v2.y - v1.y ) * alpha;
- this.z = v1.z + ( v2.z - v1.z ) * alpha;
- return this;
- }
- /**
- * Calculates the cross product of the given vector with this instance.
- *
- * @param {Vector3} v - The vector to compute the cross product with.
- * @return {Vector3} The result of the cross product.
- */
- cross( v ) {
- return this.crossVectors( this, v );
- }
- /**
- * Calculates the cross product of the given vectors and stores the result
- * in this instance.
- *
- * @param {Vector3} a - The first vector.
- * @param {Vector3} b - The second vector.
- * @return {Vector3} A reference to this vector.
- */
- crossVectors( a, b ) {
- const ax = a.x, ay = a.y, az = a.z;
- const bx = b.x, by = b.y, bz = b.z;
- this.x = ay * bz - az * by;
- this.y = az * bx - ax * bz;
- this.z = ax * by - ay * bx;
- return this;
- }
- /**
- * Projects this vector onto the given one.
- *
- * @param {Vector3} v - The vector to project to.
- * @return {Vector3} A reference to this vector.
- */
- projectOnVector( v ) {
- const denominator = v.lengthSq();
- if ( denominator === 0 ) return this.set( 0, 0, 0 );
- const scalar = v.dot( this ) / denominator;
- return this.copy( v ).multiplyScalar( scalar );
- }
- /**
- * Projects this vector onto a plane by subtracting this
- * vector projected onto the plane's normal from this vector.
- *
- * @param {Vector3} planeNormal - The plane normal.
- * @return {Vector3} A reference to this vector.
- */
- projectOnPlane( planeNormal ) {
- _vector.copy( this ).projectOnVector( planeNormal );
- return this.sub( _vector );
- }
- /**
- * Reflects this vector off a plane orthogonal to the given normal vector.
- *
- * @param {Vector3} normal - The (normalized) normal vector.
- * @return {Vector3} A reference to this vector.
- */
- reflect( normal ) {
- return this.sub( _vector.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
- }
- /**
- * Returns the angle between the given vector and this instance in radians.
- *
- * @param {Vector3} v - The vector to compute the angle with.
- * @return {number} The angle in radians.
- */
- angleTo( v ) {
- const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
- if ( denominator === 0 ) return Math.PI / 2;
- const theta = this.dot( v ) / denominator;
- // clamp, to handle numerical problems
- return Math.acos( clamp( theta, - 1, 1 ) );
- }
- /**
- * Computes the distance from the given vector to this instance.
- *
- * @param {Vector3} v - The vector to compute the distance to.
- * @return {number} The distance.
- */
- distanceTo( v ) {
- return Math.sqrt( this.distanceToSquared( v ) );
- }
- /**
- * Computes the squared distance from the given vector to this instance.
- * If you are just comparing the distance with another distance, you should compare
- * the distance squared instead as it is slightly more efficient to calculate.
- *
- * @param {Vector3} v - The vector to compute the squared distance to.
- * @return {number} The squared distance.
- */
- distanceToSquared( v ) {
- const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
- return dx * dx + dy * dy + dz * dz;
- }
- /**
- * Computes the Manhattan distance from the given vector to this instance.
- *
- * @param {Vector3} v - The vector to compute the Manhattan distance to.
- * @return {number} The Manhattan distance.
- */
- manhattanDistanceTo( v ) {
- return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );
- }
- /**
- * Sets the vector components from the given spherical coordinates.
- *
- * @param {Spherical} s - The spherical coordinates.
- * @return {Vector3} A reference to this vector.
- */
- setFromSpherical( s ) {
- return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
- }
- /**
- * Sets the vector components from the given spherical coordinates.
- *
- * @param {number} radius - The radius.
- * @param {number} phi - The phi angle in radians.
- * @param {number} theta - The theta angle in radians.
- * @return {Vector3} A reference to this vector.
- */
- setFromSphericalCoords( radius, phi, theta ) {
- const sinPhiRadius = Math.sin( phi ) * radius;
- this.x = sinPhiRadius * Math.sin( theta );
- this.y = Math.cos( phi ) * radius;
- this.z = sinPhiRadius * Math.cos( theta );
- return this;
- }
- /**
- * Sets the vector components from the given cylindrical coordinates.
- *
- * @param {Cylindrical} c - The cylindrical coordinates.
- * @return {Vector3} A reference to this vector.
- */
- setFromCylindrical( c ) {
- return this.setFromCylindricalCoords( c.radius, c.theta, c.y );
- }
- /**
- * Sets the vector components from the given cylindrical coordinates.
- *
- * @param {number} radius - The radius.
- * @param {number} theta - The theta angle in radians.
- * @param {number} y - The y value.
- * @return {Vector3} A reference to this vector.
- */
- setFromCylindricalCoords( radius, theta, y ) {
- this.x = radius * Math.sin( theta );
- this.y = y;
- this.z = radius * Math.cos( theta );
- return this;
- }
- /**
- * Sets the vector components to the position elements of the
- * given transformation matrix.
- *
- * @param {Matrix4} m - The 4x4 matrix.
- * @return {Vector3} A reference to this vector.
- */
- setFromMatrixPosition( m ) {
- const e = m.elements;
- this.x = e[ 12 ];
- this.y = e[ 13 ];
- this.z = e[ 14 ];
- return this;
- }
- /**
- * Sets the vector components to the scale elements of the
- * given transformation matrix.
- *
- * @param {Matrix4} m - The 4x4 matrix.
- * @return {Vector3} A reference to this vector.
- */
- setFromMatrixScale( m ) {
- const sx = this.setFromMatrixColumn( m, 0 ).length();
- const sy = this.setFromMatrixColumn( m, 1 ).length();
- const sz = this.setFromMatrixColumn( m, 2 ).length();
- this.x = sx;
- this.y = sy;
- this.z = sz;
- return this;
- }
- /**
- * Sets the vector components from the specified matrix column.
- *
- * @param {Matrix4} m - The 4x4 matrix.
- * @param {number} index - The column index.
- * @return {Vector3} A reference to this vector.
- */
- setFromMatrixColumn( m, index ) {
- return this.fromArray( m.elements, index * 4 );
- }
- /**
- * Sets the vector components from the specified matrix column.
- *
- * @param {Matrix3} m - The 3x3 matrix.
- * @param {number} index - The column index.
- * @return {Vector3} A reference to this vector.
- */
- setFromMatrix3Column( m, index ) {
- return this.fromArray( m.elements, index * 3 );
- }
- /**
- * Sets the vector components from the given Euler angles.
- *
- * @param {Euler} e - The Euler angles to set.
- * @return {Vector3} A reference to this vector.
- */
- setFromEuler( e ) {
- this.x = e._x;
- this.y = e._y;
- this.z = e._z;
- return this;
- }
- /**
- * Sets the vector components from the RGB components of the
- * given color.
- *
- * @param {Color} c - The color to set.
- * @return {Vector3} A reference to this vector.
- */
- setFromColor( c ) {
- this.x = c.r;
- this.y = c.g;
- this.z = c.b;
- return this;
- }
- /**
- * Returns `true` if this vector is equal with the given one.
- *
- * @param {Vector3} v - The vector to test for equality.
- * @return {boolean} Whether this vector is equal with the given one.
- */
- equals( v ) {
- return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
- }
- /**
- * Sets this vector's x value to be `array[ offset ]`, y value to be `array[ offset + 1 ]`
- * and z value to be `array[ offset + 2 ]`.
- *
- * @param {Array<number>} array - An array holding the vector component values.
- * @param {number} [offset=0] - The offset into the array.
- * @return {Vector3} A reference to this vector.
- */
- fromArray( array, offset = 0 ) {
- this.x = array[ offset ];
- this.y = array[ offset + 1 ];
- this.z = array[ offset + 2 ];
- return this;
- }
- /**
- * Writes the components of this vector to the given array. If no array is provided,
- * the method returns a new instance.
- *
- * @param {Array<number>} [array=[]] - The target array holding the vector components.
- * @param {number} [offset=0] - Index of the first element in the array.
- * @return {Array<number>} The vector components.
- */
- toArray( array = [], offset = 0 ) {
- array[ offset ] = this.x;
- array[ offset + 1 ] = this.y;
- array[ offset + 2 ] = this.z;
- return array;
- }
- /**
- * Sets the components of this vector from the given buffer attribute.
- *
- * @param {BufferAttribute} attribute - The buffer attribute holding vector data.
- * @param {number} index - The index into the attribute.
- * @return {Vector3} A reference to this vector.
- */
- fromBufferAttribute( attribute, index ) {
- this.x = attribute.getX( index );
- this.y = attribute.getY( index );
- this.z = attribute.getZ( index );
- return this;
- }
- /**
- * Sets each component of this vector to a pseudo-random value between `0` and
- * `1`, excluding `1`.
- *
- * @return {Vector3} A reference to this vector.
- */
- random() {
- this.x = Math.random();
- this.y = Math.random();
- this.z = Math.random();
- return this;
- }
- /**
- * Sets this vector to a uniformly random point on a unit sphere.
- *
- * @return {Vector3} A reference to this vector.
- */
- randomDirection() {
- // https://mathworld.wolfram.com/SpherePointPicking.html
- const theta = Math.random() * Math.PI * 2;
- const u = Math.random() * 2 - 1;
- const c = Math.sqrt( 1 - u * u );
- this.x = c * Math.cos( theta );
- this.y = u;
- this.z = c * Math.sin( theta );
- return this;
- }
- *[ Symbol.iterator ]() {
- yield this.x;
- yield this.y;
- yield this.z;
- }
- }
- const _vector = /*@__PURE__*/ new Vector3();
- const _quaternion = /*@__PURE__*/ new Quaternion();
- export { Vector3 };
|