提交 dbedded4 编写于 作者: R richtr

DeviceOrientationControls.js: Minor code performance optimizations and clean-up

上级 49fdb26d
...@@ -8,14 +8,15 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -8,14 +8,15 @@ THREE.DeviceOrientationControls = function ( object ) {
this.object = object; this.object = object;
this.degtorad = Math.PI / 180;
this.freeze = true; this.freeze = true;
this.deviceOrientation = {}; this.deviceOrientation = {};
this.screenOrientation = 0; this.screenOrientation = 0;
this._rotationVector = new THREE.Vector3(); var degtorad = Math.PI / 180;
this._rotationMatrix = new THREE.Matrix4();
var _objectRotationMatrix = new THREE.Matrix4();
var _tmpRotationMatrix = new THREE.Matrix4();
this.onDeviceOrientationChangeEvent = function( rawEvtData ) { this.onDeviceOrientationChangeEvent = function( rawEvtData ) {
this.deviceOrientation = rawEvtData; this.deviceOrientation = rawEvtData;
...@@ -25,20 +26,10 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -25,20 +26,10 @@ THREE.DeviceOrientationControls = function ( object ) {
this.screenOrientation = window.orientation || 0; this.screenOrientation = window.orientation || 0;
}; };
this.setRotationVectorFromDeviceOrientation = function( alpha, beta, gamma) { this.setObjectRotationMatrixFromDeviceOrientation = function() {
this._rotationVector.set( var x = this.deviceOrientation.beta ? this.deviceOrientation.beta * degtorad : 0; // beta
(beta || 0) * this.degtorad, // x var y = this.deviceOrientation.gamma ? this.deviceOrientation.gamma * degtorad : 0; // gamma
(gamma || 0) * this.degtorad, // y var z = this.deviceOrientation.alpha ? this.deviceOrientation.alpha * degtorad : 0; // alpha
(alpha || 0) * this.degtorad // z
);
return this._rotationVector;
};
this.setRotationMatrixFromRotationVector = function( rotationVector ) {
var x = rotationVector.x || 0; // beta
var y = rotationVector.y || 0; // gamma
var z = rotationVector.z || 0; // alpha
var cX = Math.cos(x); var cX = Math.cos(x);
var cY = Math.cos(y); var cY = Math.cos(y);
...@@ -65,21 +56,20 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -65,21 +56,20 @@ THREE.DeviceOrientationControls = function ( object ) {
var m32 = sX; var m32 = sX;
var m33 = cX * cY; var m33 = cX * cY;
this._rotationMatrix.set( _objectRotationMatrix.set(
m11, m12, m13, 0, m11, m12, m13, 0,
m21, m22, m23, 0, m21, m22, m23, 0,
m31, m32, m33, 0, m31, m32, m33, 0,
0, 0, 0, 1 0, 0, 0, 1
); );
return this._rotationMatrix; return _objectRotationMatrix;
}; };
this.remapRotationMatrixByScreenOrientation = function( screenOrientation, rotationMatrix ) { this.remapObjectRotationMatrixFromScreenOrientation = function() {
var transformedRotationMatrix = new THREE.Matrix4(); _tmpRotationMatrix.copy( _objectRotationMatrix );
transformedRotationMatrix.copy( rotationMatrix );
switch( screenOrientation ) { switch( this.screenOrientation ) {
case 90: case 90:
case -270: case -270:
// //
...@@ -93,17 +83,17 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -93,17 +83,17 @@ THREE.DeviceOrientationControls = function ( object ) {
// (see: http://bit.ly/1itCOq2) // (see: http://bit.ly/1itCOq2)
// //
transformedRotationMatrix.elements[0] = -rotationMatrix.elements[4]; _objectRotationMatrix.elements[0] = - _tmpRotationMatrix.elements[4];
transformedRotationMatrix.elements[4] = rotationMatrix.elements[0]; _objectRotationMatrix.elements[4] = _tmpRotationMatrix.elements[0];
transformedRotationMatrix.elements[8] = rotationMatrix.elements[8]; _objectRotationMatrix.elements[8] = _tmpRotationMatrix.elements[8];
transformedRotationMatrix.elements[1] = -rotationMatrix.elements[5]; _objectRotationMatrix.elements[1] = - _tmpRotationMatrix.elements[5];
transformedRotationMatrix.elements[5] = rotationMatrix.elements[1]; _objectRotationMatrix.elements[5] = _tmpRotationMatrix.elements[1];
transformedRotationMatrix.elements[9] = rotationMatrix.elements[9]; _objectRotationMatrix.elements[9] = _tmpRotationMatrix.elements[9];
transformedRotationMatrix.elements[2] = -rotationMatrix.elements[6]; _objectRotationMatrix.elements[2] = - _tmpRotationMatrix.elements[6];
transformedRotationMatrix.elements[6] = rotationMatrix.elements[2]; _objectRotationMatrix.elements[6] = _tmpRotationMatrix.elements[2];
transformedRotationMatrix.elements[10] = rotationMatrix.elements[10]; _objectRotationMatrix.elements[10] = _tmpRotationMatrix.elements[10];
break; break;
case 180: case 180:
...@@ -119,17 +109,17 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -119,17 +109,17 @@ THREE.DeviceOrientationControls = function ( object ) {
// (see: http://bit.ly/1dIrx0I) // (see: http://bit.ly/1dIrx0I)
// //
transformedRotationMatrix.elements[0] = -rotationMatrix.elements[0]; _objectRotationMatrix.elements[0] = - _tmpRotationMatrix.elements[0];
transformedRotationMatrix.elements[4] = -rotationMatrix.elements[4]; _objectRotationMatrix.elements[4] = - _tmpRotationMatrix.elements[4];
transformedRotationMatrix.elements[8] = rotationMatrix.elements[8]; _objectRotationMatrix.elements[8] = _tmpRotationMatrix.elements[8];
transformedRotationMatrix.elements[1] = -rotationMatrix.elements[1]; _objectRotationMatrix.elements[1] = - _tmpRotationMatrix.elements[1];
transformedRotationMatrix.elements[5] = -rotationMatrix.elements[5]; _objectRotationMatrix.elements[5] = - _tmpRotationMatrix.elements[5];
transformedRotationMatrix.elements[9] = rotationMatrix.elements[9]; _objectRotationMatrix.elements[9] = _tmpRotationMatrix.elements[9];
transformedRotationMatrix.elements[2] = -rotationMatrix.elements[2]; _objectRotationMatrix.elements[2] = - _tmpRotationMatrix.elements[2];
transformedRotationMatrix.elements[6] = -rotationMatrix.elements[6]; _objectRotationMatrix.elements[6] = - _tmpRotationMatrix.elements[6];
transformedRotationMatrix.elements[10] = rotationMatrix.elements[10]; _objectRotationMatrix.elements[10] = _tmpRotationMatrix.elements[10];
break; break;
case 270: case 270:
...@@ -145,17 +135,17 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -145,17 +135,17 @@ THREE.DeviceOrientationControls = function ( object ) {
// (see: http://bit.ly/1h73sQ0) // (see: http://bit.ly/1h73sQ0)
// //
transformedRotationMatrix.elements[0] = rotationMatrix.elements[4]; _objectRotationMatrix.elements[0] = _tmpRotationMatrix.elements[4];
transformedRotationMatrix.elements[4] = -rotationMatrix.elements[0]; _objectRotationMatrix.elements[4] = - _tmpRotationMatrix.elements[0];
transformedRotationMatrix.elements[8] = rotationMatrix.elements[8]; _objectRotationMatrix.elements[8] = _tmpRotationMatrix.elements[8];
transformedRotationMatrix.elements[1] = rotationMatrix.elements[5]; _objectRotationMatrix.elements[1] = _tmpRotationMatrix.elements[5];
transformedRotationMatrix.elements[5] = -rotationMatrix.elements[1]; _objectRotationMatrix.elements[5] = - _tmpRotationMatrix.elements[1];
transformedRotationMatrix.elements[9] = rotationMatrix.elements[9]; _objectRotationMatrix.elements[9] = _tmpRotationMatrix.elements[9];
transformedRotationMatrix.elements[2] = rotationMatrix.elements[6]; _objectRotationMatrix.elements[2] = _tmpRotationMatrix.elements[6];
transformedRotationMatrix.elements[6] = -rotationMatrix.elements[2]; _objectRotationMatrix.elements[6] = - _tmpRotationMatrix.elements[2];
transformedRotationMatrix.elements[10] = rotationMatrix.elements[10]; _objectRotationMatrix.elements[10] = _tmpRotationMatrix.elements[10];
break; break;
default: default:
...@@ -172,9 +162,7 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -172,9 +162,7 @@ THREE.DeviceOrientationControls = function ( object ) {
break; break;
} }
this._rotationMatrix.copy( transformedRotationMatrix ); return _objectRotationMatrix;
return this._rotationMatrix;
}; };
this.update = function( delta ) { this.update = function( delta ) {
...@@ -182,19 +170,11 @@ THREE.DeviceOrientationControls = function ( object ) { ...@@ -182,19 +170,11 @@ THREE.DeviceOrientationControls = function ( object ) {
return; return;
} }
this.setRotationVectorFromDeviceOrientation( this.setObjectRotationMatrixFromDeviceOrientation();
this.deviceOrientation.alpha,
this.deviceOrientation.beta,
this.deviceOrientation.gamma
);
this.setRotationMatrixFromRotationVector( this._rotationVector );
this.remapRotationMatrixByScreenOrientation( this.screenOrientation, this._rotationMatrix );
this.object.quaternion.setFromRotationMatrix( this._rotationMatrix ); this.remapObjectRotationMatrixFromScreenOrientation();
this.object.updateProjectionMatrix(); this.object.quaternion.setFromRotationMatrix( _objectRotationMatrix );
}; };
function bind( scope, fn ) { function bind( scope, fn ) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册