123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author *kile / http://kile.stravaganza.org/
- * @author philogb / http://blog.thejit.org/
- * @author mikael emtinger / http://gomo.se/
- * @author egraether / http://egraether.com/
- * @author WestLangley / http://github.com/WestLangley
- */
- THREE.Vector3 = function ( x, y, z ) {
- this.x = x || 0;
- this.y = y || 0;
- this.z = z || 0;
- };
- THREE.extend( THREE.Vector3.prototype, {
- set: function ( x, y, z ) {
- this.x = x;
- this.y = y;
- this.z = z;
- return this;
- },
- setX: function ( x ) {
- this.x = x;
- return this;
- },
- setY: function ( y ) {
- this.y = y;
- return this;
- },
- setZ: function ( z ) {
- this.z = z;
- return this;
- },
- setComponent: function ( 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 );
- }
- },
- getComponent: function ( 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 );
- }
- },
- copy: function ( v ) {
- this.x = v.x;
- this.y = v.y;
- this.z = v.z;
- return this;
- },
- add: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'DEPRECATED: Vector3\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
- return this.addVectors( v, w );
- }
- this.x += v.x;
- this.y += v.y;
- this.z += v.z;
- return this;
- },
- addScalar: function ( s ) {
- this.x += s;
- this.y += s;
- this.z += s;
- return this;
- },
- addVectors: function ( a, b ) {
- this.x = a.x + b.x;
- this.y = a.y + b.y;
- this.z = a.z + b.z;
- return this;
- },
- sub: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'DEPRECATED: Vector3\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
- return this.subVectors( v, w );
- }
- this.x -= v.x;
- this.y -= v.y;
- this.z -= v.z;
- return this;
- },
- subVectors: function ( a, b ) {
- this.x = a.x - b.x;
- this.y = a.y - b.y;
- this.z = a.z - b.z;
- return this;
- },
- multiply: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'DEPRECATED: Vector3\'s .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
- return this.multiplyVectors( v, w );
- }
- this.x *= v.x;
- this.y *= v.y;
- this.z *= v.z;
- return this;
- },
- multiplyScalar: function ( s ) {
- this.x *= s;
- this.y *= s;
- this.z *= s;
- return this;
- },
- multiplyVectors: function ( a, b ) {
- this.x = a.x * b.x;
- this.y = a.y * b.y;
- this.z = a.z * b.z;
- return this;
- },
- applyMatrix3: function ( m ) {
- var x = this.x;
- var y = this.y;
- var z = this.z;
- var 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;
- },
- applyMatrix4: function ( m ) {
- // input: THREE.Matrix4 affine matrix
- var x = this.x, y = this.y, z = this.z;
- var e = m.elements;
- this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
- this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
- this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
- return this;
- },
- applyProjection: function ( m ) {
- // input: THREE.Matrix4 projection matrix
- var x = this.x, y = this.y, z = this.z;
- var e = m.elements;
- var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] ); // perspective divide
- this.x = ( e[0] * x + e[4] * y + e[8] * z + e[12] ) * d;
- this.y = ( e[1] * x + e[5] * y + e[9] * z + e[13] ) * d;
- this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d;
- return this;
- },
- applyQuaternion: function ( q ) {
- var x = this.x;
- var y = this.y;
- var z = this.z;
- var qx = q.x;
- var qy = q.y;
- var qz = q.z;
- var qw = q.w;
- // calculate quat * vector
- var ix = qw * x + qy * z - qz * y;
- var iy = qw * y + qz * x - qx * z;
- var iz = qw * z + qx * y - qy * x;
- var iw = -qx * x - qy * y - qz * z;
- // calculate result * inverse quat
- this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
- this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
- this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
- return this;
- },
- applyEuler: function () {
- var q1 = new THREE.Quaternion();
- return function ( v, eulerOrder ) {
- var quaternion = q1.setFromEuler( v, eulerOrder );
- this.applyQuaternion( quaternion );
- return this;
- };
- }(),
- applyAxisAngle: function () {
- var q1 = new THREE.Quaternion();
- return function ( axis, angle ) {
- var quaternion = q1.setFromAxisAngle( axis, angle );
- this.applyQuaternion( quaternion );
- return this;
- };
- }(),
- transformDirection: function ( m ) {
- // input: THREE.Matrix4 affine matrix
- // vector interpreted as a direction
- var x = this.x, y = this.y, z = this.z;
- var 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;
- this.normalize();
- return this;
- },
- divide: function ( v ) {
- this.x /= v.x;
- this.y /= v.y;
- this.z /= v.z;
- return this;
- },
- divideScalar: function ( s ) {
- if ( s !== 0 ) {
- this.x /= s;
- this.y /= s;
- this.z /= s;
- } else {
- this.x = 0;
- this.y = 0;
- this.z = 0;
- }
- return this;
- },
- min: function ( v ) {
- if ( this.x > v.x ) {
- this.x = v.x;
- }
- if ( this.y > v.y ) {
- this.y = v.y;
- }
- if ( this.z > v.z ) {
- this.z = v.z;
- }
- return this;
- },
- max: function ( v ) {
- if ( this.x < v.x ) {
- this.x = v.x;
- }
- if ( this.y < v.y ) {
- this.y = v.y;
- }
- if ( this.z < v.z ) {
- this.z = v.z;
- }
- return this;
- },
- clamp: function ( min, max ) {
- // This function assumes min < max, if this assumption isn't true it will not operate correctly
- if ( this.x < min.x ) {
- this.x = min.x;
- } else if ( this.x > max.x ) {
- this.x = max.x;
- }
- if ( this.y < min.y ) {
- this.y = min.y;
- } else if ( this.y > max.y ) {
- this.y = max.y;
- }
- if ( this.z < min.z ) {
- this.z = min.z;
- } else if ( this.z > max.z ) {
- this.z = max.z;
- }
- return this;
- },
- negate: function () {
- return this.multiplyScalar( - 1 );
- },
- dot: function ( v ) {
- return this.x * v.x + this.y * v.y + this.z * v.z;
- },
- lengthSq: function () {
- return this.x * this.x + this.y * this.y + this.z * this.z;
- },
- length: function () {
- return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
- },
- lengthManhattan: function () {
- return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
- },
- normalize: function () {
- return this.divideScalar( this.length() );
- },
- setLength: function ( l ) {
- var oldLength = this.length();
- if ( oldLength !== 0 && l !== oldLength ) {
- this.multiplyScalar( l / oldLength );
- }
- return this;
- },
- lerp: function ( 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;
- },
- cross: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'DEPRECATED: Vector3\'s .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
- return this.crossVectors( v, w );
- }
- var x = this.x, y = this.y, z = this.z;
- this.x = y * v.z - z * v.y;
- this.y = z * v.x - x * v.z;
- this.z = x * v.y - y * v.x;
- return this;
- },
- crossVectors: function ( a, b ) {
- this.x = a.y * b.z - a.z * b.y;
- this.y = a.z * b.x - a.x * b.z;
- this.z = a.x * b.y - a.y * b.x;
- return this;
- },
- projectOnVector: function () {
- var v1 = new THREE.Vector3();
- return function( vector ) {
- v1.copy( vector ).normalize();
- var d = this.dot( v1 );
- return this.copy( v1 ).multiplyScalar( d );
- };
- }(),
- projectOnPlane: function () {
- var v1 = new THREE.Vector3();
- return function( planeNormal ) {
- v1.copy( this ).projectOnVector( planeNormal );
- return this.sub( v1 );
- }
- }(),
- reflect: function () {
- var v1 = new THREE.Vector3();
- return function ( vector ) {
- v1.copy( this ).projectOnVector( vector ).multiplyScalar( 2 );
- return this.subVectors( v1, this );
- }
- }(),
- angleTo: function ( v ) {
- var theta = this.dot( v ) / ( this.length() * v.length() );
- // clamp, to handle numerical problems
- return Math.acos( THREE.Math.clamp ( theta, -1, 1 ) );
- },
- distanceTo: function ( v ) {
- return Math.sqrt( this.distanceToSquared( v ) );
- },
- distanceToSquared: function ( v ) {
- var dx = this.x - v.x;
- var dy = this.y - v.y;
- var dz = this.z - v.z;
- return dx * dx + dy * dy + dz * dz;
- },
- getPositionFromMatrix: function ( m ) {
- this.x = m.elements[12];
- this.y = m.elements[13];
- this.z = m.elements[14];
- return this;
- },
- setEulerFromRotationMatrix: function ( m, order ) {
- // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
- // clamp, to handle numerical problems
- function clamp( x ) {
- return Math.min( Math.max( x, -1 ), 1 );
- }
- var te = m.elements;
- var m11 = te[0], m12 = te[4], m13 = te[8];
- var m21 = te[1], m22 = te[5], m23 = te[9];
- var m31 = te[2], m32 = te[6], m33 = te[10];
- if ( order === undefined || order === 'XYZ' ) {
- this.y = Math.asin( clamp( m13 ) );
- if ( Math.abs( m13 ) < 0.99999 ) {
- this.x = Math.atan2( - m23, m33 );
- this.z = Math.atan2( - m12, m11 );
- } else {
- this.x = Math.atan2( m32, m22 );
- this.z = 0;
- }
- } else if ( order === 'YXZ' ) {
- this.x = Math.asin( - clamp( m23 ) );
- if ( Math.abs( m23 ) < 0.99999 ) {
- this.y = Math.atan2( m13, m33 );
- this.z = Math.atan2( m21, m22 );
- } else {
- this.y = Math.atan2( - m31, m11 );
- this.z = 0;
- }
- } else if ( order === 'ZXY' ) {
- this.x = Math.asin( clamp( m32 ) );
- if ( Math.abs( m32 ) < 0.99999 ) {
- this.y = Math.atan2( - m31, m33 );
- this.z = Math.atan2( - m12, m22 );
- } else {
- this.y = 0;
- this.z = Math.atan2( m21, m11 );
- }
- } else if ( order === 'ZYX' ) {
- this.y = Math.asin( - clamp( m31 ) );
- if ( Math.abs( m31 ) < 0.99999 ) {
- this.x = Math.atan2( m32, m33 );
- this.z = Math.atan2( m21, m11 );
- } else {
- this.x = 0;
- this.z = Math.atan2( - m12, m22 );
- }
- } else if ( order === 'YZX' ) {
- this.z = Math.asin( clamp( m21 ) );
- if ( Math.abs( m21 ) < 0.99999 ) {
- this.x = Math.atan2( - m23, m22 );
- this.y = Math.atan2( - m31, m11 );
- } else {
- this.x = 0;
- this.y = Math.atan2( m13, m33 );
- }
- } else if ( order === 'XZY' ) {
- this.z = Math.asin( - clamp( m12 ) );
- if ( Math.abs( m12 ) < 0.99999 ) {
- this.x = Math.atan2( m32, m22 );
- this.y = Math.atan2( m13, m11 );
- } else {
- this.x = Math.atan2( - m23, m33 );
- this.y = 0;
- }
- }
- return this;
- },
- setEulerFromQuaternion: function ( q, order ) {
- // q is assumed to be normalized
- // clamp, to handle numerical problems
- function clamp( x ) {
- return Math.min( Math.max( x, -1 ), 1 );
- }
- // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
- var sqx = q.x * q.x;
- var sqy = q.y * q.y;
- var sqz = q.z * q.z;
- var sqw = q.w * q.w;
- if ( order === undefined || order === 'XYZ' ) {
- this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
- this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
- this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
- } else if ( order === 'YXZ' ) {
- this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
- this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
- this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
- } else if ( order === 'ZXY' ) {
- this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
- this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
- this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
- } else if ( order === 'ZYX' ) {
- this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
- this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
- this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
- } else if ( order === 'YZX' ) {
- this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
- this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
- this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
- } else if ( order === 'XZY' ) {
- this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
- this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
- this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
- }
- return this;
- },
- getScaleFromMatrix: function ( m ) {
- var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
- var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
- var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
- this.x = sx;
- this.y = sy;
- this.z = sz;
- return this;
- },
- equals: function ( v ) {
- return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
- },
- toArray: function () {
- return [ this.x, this.y, this.z ];
-
- },
- clone: function () {
- return new THREE.Vector3( this.x, this.y, this.z );
- }
- } );
|