提交 dbedded4 编写于 作者: R richtr

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

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