Euler.js 5.2 KB
Newer Older
B
Ben Houston 已提交
1
/**
2 3
 * @author mrdoob / http://mrdoob.com/
 * @author WestLangley / http://github.com/WestLangley
B
Ben Houston 已提交
4 5 6
 * @author bhouston / http://exocortex.com
 */

7
THREE.Euler = function ( x, y, z, order ) {
B
Ben Houston 已提交
8

9 10 11 12
	this._x = x || 0;
	this._y = y || 0;
	this._z = z || 0;
	this._order = order || THREE.Euler.DefaultOrder;
B
Ben Houston 已提交
13 14 15

};

16
THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
B
Ben Houston 已提交
17

18
THREE.Euler.DefaultOrder = 'XYZ';
B
Ben Houston 已提交
19

20 21 22
THREE.Euler.prototype = {

	constructor: THREE.Euler,
B
Ben Houston 已提交
23

24 25 26 27 28 29 30 31 32 33 34
	_x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,

	get x () {

		return this._x;

	},

	set x ( value ) {

		this._x = value;
35
		this.onChangeCallback();
36 37 38 39 40 41 42 43 44 45 46 47

	},

	get y () {

		return this._y;

	},

	set y ( value ) {

		this._y = value;
48
		this.onChangeCallback();
49 50 51 52 53 54 55 56 57 58 59 60

	},

	get z () {

		return this._z;

	},

	set z ( value ) {

		this._z = value;
61
		this.onChangeCallback();
62 63 64 65 66 67 68 69 70 71 72 73

	},

	get order () {

		return this._order;

	},

	set order ( value ) {

		this._order = value;
74
		this.onChangeCallback();
75 76 77

	},

B
Ben Houston 已提交
78 79
	set: function ( x, y, z, order ) {

80 81 82 83 84
		this._x = x;
		this._y = y;
		this._z = z;
		this._order = order || this._order;

85
		this.onChangeCallback();
B
Ben Houston 已提交
86 87 88 89 90

		return this;

	},

91 92 93 94 95 96
	copy: function ( euler ) {

		this._x = euler._x;
		this._y = euler._y;
		this._z = euler._z;
		this._order = euler._order;
B
Ben Houston 已提交
97

98
		this.onChangeCallback();
B
Ben Houston 已提交
99 100 101 102 103

		return this;

	},

104
	setFromRotationMatrix: function ( m, order, update ) {
B
Ben Houston 已提交
105

106
		var clamp = THREE.Math.clamp;
B
Ben Houston 已提交
107

108
		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
B
Ben Houston 已提交
109 110

		var te = m.elements;
111 112 113
		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 ];
B
Ben Houston 已提交
114

115
		order = order || this._order;
116 117

		if ( order === 'XYZ' ) {
B
Ben Houston 已提交
118

119
			this._y = Math.asin( clamp( m13, - 1, 1 ) );
B
Ben Houston 已提交
120 121 122

			if ( Math.abs( m13 ) < 0.99999 ) {

123 124
				this._x = Math.atan2( - m23, m33 );
				this._z = Math.atan2( - m12, m11 );
B
Ben Houston 已提交
125 126 127

			} else {

128 129
				this._x = Math.atan2( m32, m22 );
				this._z = 0;
B
Ben Houston 已提交
130 131 132 133 134

			}

		} else if ( order === 'YXZ' ) {

135
			this._x = Math.asin( - clamp( m23, - 1, 1 ) );
B
Ben Houston 已提交
136 137 138

			if ( Math.abs( m23 ) < 0.99999 ) {

139 140
				this._y = Math.atan2( m13, m33 );
				this._z = Math.atan2( m21, m22 );
B
Ben Houston 已提交
141 142 143

			} else {

144 145
				this._y = Math.atan2( - m31, m11 );
				this._z = 0;
B
Ben Houston 已提交
146 147 148 149 150

			}

		} else if ( order === 'ZXY' ) {

151
			this._x = Math.asin( clamp( m32, - 1, 1 ) );
B
Ben Houston 已提交
152 153 154

			if ( Math.abs( m32 ) < 0.99999 ) {

155 156
				this._y = Math.atan2( - m31, m33 );
				this._z = Math.atan2( - m12, m22 );
B
Ben Houston 已提交
157 158 159

			} else {

160 161
				this._y = 0;
				this._z = Math.atan2( m21, m11 );
B
Ben Houston 已提交
162 163 164 165 166

			}

		} else if ( order === 'ZYX' ) {

167
			this._y = Math.asin( - clamp( m31, - 1, 1 ) );
B
Ben Houston 已提交
168 169 170

			if ( Math.abs( m31 ) < 0.99999 ) {

171 172
				this._x = Math.atan2( m32, m33 );
				this._z = Math.atan2( m21, m11 );
B
Ben Houston 已提交
173 174 175

			} else {

176 177
				this._x = 0;
				this._z = Math.atan2( - m12, m22 );
B
Ben Houston 已提交
178 179 180 181 182

			}

		} else if ( order === 'YZX' ) {

183
			this._z = Math.asin( clamp( m21, - 1, 1 ) );
B
Ben Houston 已提交
184 185 186

			if ( Math.abs( m21 ) < 0.99999 ) {

187 188
				this._x = Math.atan2( - m23, m22 );
				this._y = Math.atan2( - m31, m11 );
B
Ben Houston 已提交
189 190 191

			} else {

192 193
				this._x = 0;
				this._y = Math.atan2( m13, m33 );
B
Ben Houston 已提交
194 195 196 197 198

			}

		} else if ( order === 'XZY' ) {

199
			this._z = Math.asin( - clamp( m12, - 1, 1 ) );
B
Ben Houston 已提交
200 201 202

			if ( Math.abs( m12 ) < 0.99999 ) {

203 204
				this._x = Math.atan2( m32, m22 );
				this._y = Math.atan2( m13, m11 );
B
Ben Houston 已提交
205 206 207

			} else {

208 209
				this._x = Math.atan2( - m23, m33 );
				this._y = 0;
B
Ben Houston 已提交
210 211 212

			}

M
Mr.doob 已提交
213
		} else {
214

215
			console.warn( 'THREE.Euler: .setFromRotationMatrix() given unsupported order: ' + order )
M
Mr.doob 已提交
216

B
Ben Houston 已提交
217 218
		}

219 220
		this._order = order;

221
		if ( update !== false ) this.onChangeCallback();
B
Ben Houston 已提交
222 223 224 225 226

		return this;

	},

M
Mr.doob 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
	setFromQuaternion: function () {

		var matrix;

		return function ( q, order, update ) {

			if ( matrix === undefined ) matrix = new THREE.Matrix4();
			matrix.makeRotationFromQuaternion( q );
			this.setFromRotationMatrix( matrix, order, update );

			return this;

		};
B
Ben Houston 已提交
240

241
	}(),
B
Ben Houston 已提交
242

243
	setFromVector3: function ( v, order ) {
B
Ben Houston 已提交
244 245 246 247 248

		return this.set( v.x, v.y, v.z, order || this._order );

	},

249
	reorder: function () {
B
Ben Houston 已提交
250

251
		// WARNING: this discards revolution information -bhouston
B
Ben Houston 已提交
252

253
		var q = new THREE.Quaternion();
B
Ben Houston 已提交
254

255
		return function ( newOrder ) {
B
Ben Houston 已提交
256

257 258
			q.setFromEuler( this );
			this.setFromQuaternion( q, newOrder );
259

260 261 262
		};

	}(),
263

264 265 266 267 268 269
	equals: function ( euler ) {

		return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );

	},

270 271
	fromArray: function ( array ) {

272 273 274
		this._x = array[ 0 ];
		this._y = array[ 1 ];
		this._z = array[ 2 ];
M
Mr.doob 已提交
275
		if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
276

277
		this.onChangeCallback();
278 279 280 281 282

		return this;

	},

283
	toArray: function( array, offset, withoutOrder ) {
284

285 286
		if ( array === undefined ) array = [];
		if ( offset === undefined ) offset = 0;
B
Ben Houston 已提交
287

288 289 290 291 292 293 294 295 296
		array[ offset + 0 ] = this._x;
		array[ offset + 1 ] = this._y;
		array[ offset + 2 ] = this._z;

		if( !withoutOrder ) {
			array[ offset + 3 ] = this._order;
		}

		return array;
B
Ben Houston 已提交
297
	},
298

B
Ben Houston 已提交
299 300
	toVector3: function ( optionalResult ) {

M
Mr.doob 已提交
301
		if ( optionalResult ) {
302

B
Ben Houston 已提交
303
			return optionalResult.set( this._x, this._y, this._z );
304

M
Mr.doob 已提交
305
		} else {
306

B
Ben Houston 已提交
307
			return new THREE.Vector3( this._x, this._y, this._z );
308

B
Ben Houston 已提交
309 310 311
		}

	},
B
Ben Houston 已提交
312

313
	onChange: function ( callback ) {
B
Ben Houston 已提交
314

315 316 317
		this.onChangeCallback = callback;

		return this;
B
Ben Houston 已提交
318 319 320

	},

321 322
	onChangeCallback: function () {},

B
Ben Houston 已提交
323 324
	clone: function () {

325
		return new THREE.Euler( this._x, this._y, this._z, this._order );
B
Ben Houston 已提交
326 327 328

	}

M
Mr.doob 已提交
329
};