| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /**
- * @author mr.doob / 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/
- */
- THREE.Vector3 = function ( x, y, z ) {
- this.set(
- x || 0,
- y || 0,
- z || 0
- );
- };
- THREE.Vector3.prototype = {
- constructor: THREE.Vector3,
- set: function ( x, y, z ) {
- this.x = x;
- this.y = y;
- this.z = z;
- return this;
- },
- copy: function ( v ) {
- this.x = v.x;
- this.y = v.y;
- this.z = v.z;
- return this;
- },
- clone: function () {
- return new THREE.Vector3( this.x, this.y, this.z );
- },
- add: function ( v1, v2 ) {
- this.x = v1.x + v2.x;
- this.y = v1.y + v2.y;
- this.z = v1.z + v2.z;
- return this;
- },
- addSelf: function ( v ) {
- 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;
- },
- sub: function ( v1, v2 ) {
- this.x = v1.x - v2.x;
- this.y = v1.y - v2.y;
- this.z = v1.z - v2.z;
- return this;
- },
- subSelf: function ( v ) {
- this.x -= v.x;
- this.y -= v.y;
- this.z -= v.z;
- return this;
- },
- multiply: function ( a, b ) {
- this.x = a.x * b.x;
- this.y = a.y * b.y;
- this.z = a.z * b.z;
- return this;
- },
- multiplySelf: function ( v ) {
- 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;
- },
- divideSelf: function ( v ) {
- return this.divide( this, v );
- },
- divideScalar: function ( s ) {
- if ( s ) {
- this.x /= s;
- this.y /= s;
- this.z /= s;
- } else {
- this.set( 0, 0, 0 );
- }
- 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.lengthSq() );
- },
- lengthManhattan: function () {
- // correct version
- // return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
- return this.x + this.y + this.z;
- },
- normalize: function () {
- return this.divideScalar( this.length() );
- },
- setLength: function ( l ) {
- return this.normalize().multiplyScalar( l );
- },
- cross: 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;
- },
- crossSelf: function ( v ) {
- return this.set(
- this.y * v.z - this.z * v.y,
- this.z * v.x - this.x * v.z,
- this.x * v.y - this.y * v.x
- );
- },
- distanceTo: function ( v ) {
- return Math.sqrt( this.distanceToSquared( v ) );
- },
- distanceToSquared: function ( v ) {
- return new THREE.Vector3().sub( this, v ).lengthSq();
- },
- setPositionFromMatrix: function ( m ) {
- this.x = m.n14;
- this.y = m.n24;
- this.z = m.n34;
- },
- setRotationFromMatrix: function ( m ) {
- var cosY = Math.cos( this.y );
- this.y = Math.asin( m.n13 );
- if ( Math.abs( cosY ) > 0.00001 ) {
- this.x = Math.atan2( - m.n23 / cosY, m.n33 / cosY );
- this.z = Math.atan2( - m.n12 / cosY, m.n11 / cosY );
- } else {
- this.x = 0;
- this.z = Math.atan2( m.n21, m.n22 );
- }
- },
- isZero: function () {
- return ( this.lengthSq() < 0.0001 /* almostZero */ );
- }
- };
|