Procházet zdrojové kódy

Simplified DeviceOrientationControls a bit more.

Mr.doob před 11 roky
rodič
revize
93602b0d1a
1 změnil soubory, kde provedl 37 přidání a 45 odebrání
  1. 37 45
      examples/js/controls/DeviceOrientationControls.js

+ 37 - 45
examples/js/controls/DeviceOrientationControls.js

@@ -7,6 +7,8 @@
 
 THREE.DeviceOrientationControls = function ( object ) {
 
+	var scope = this;
+
 	this.object = object;
 
 	this.object.rotation.reorder( "YXZ" );
@@ -17,85 +19,75 @@ THREE.DeviceOrientationControls = function ( object ) {
 
 	this.screenOrientation = 0;
 
-	this.onDeviceOrientationChangeEvent = function( rawEvtData ) {
+	var onDeviceOrientationChangeEvent = function ( event ) {
 
-		this.deviceOrientation = rawEvtData;
+		scope.deviceOrientation = event;
 
 	};
 
-	this.onScreenOrientationChangeEvent = function() {
+	var onScreenOrientationChangeEvent = function () {
 
-		this.screenOrientation = window.orientation || 0;
+		scope.screenOrientation = window.orientation || 0;
 
 	};
 
-	this.update = function () {
-
-		if ( this.freeze ) return;
+	// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
 
-		var alpha  = this.deviceOrientation.gamma ? THREE.Math.degToRad( this.deviceOrientation.alpha ) : 0; // Z
-		var beta   = this.deviceOrientation.beta  ? THREE.Math.degToRad( this.deviceOrientation.beta  ) : 0; // X'
-		var gamma  = this.deviceOrientation.gamma ? THREE.Math.degToRad( this.deviceOrientation.gamma ) : 0; // Y''
-		var orient = this.screenOrientation       ? THREE.Math.degToRad( this.screenOrientation       ) : 0; // O
+	var setObjectQuaternion = function () {
 
-		setObjectQuaternion( this.object.quaternion, alpha, beta, gamma, orient );
+		var zee = new THREE.Vector3( 0, 0, 1 );
 
-	};
+		var euler = new THREE.Euler();
 
-	function bind( scope, fn ) {
+		var q0 = new THREE.Quaternion();
 
-		return function () {
+		var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
 
-			fn.apply( scope, arguments );
+		return function ( quaternion, alpha, beta, gamma, orient ) {
 
-		};
+			euler.set( beta, alpha, - gamma, 'YXZ' );                       // 'ZXY' for the device, but 'YXZ' for us
 
-	};
+			quaternion.setFromEuler( euler );                               // orient the device
 
-	this.connect = function() {
+			quaternion.multiply( q1 );                                      // camera looks out the back of the device, not the top
 
-		this.onScreenOrientationChangeEvent(); // run once on load
+			quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) );    // adjust for screen orientation
 
-		window.addEventListener( 'orientationchange', bind( this, this.onScreenOrientationChangeEvent ), false );
-		window.addEventListener( 'deviceorientation', bind( this, this.onDeviceOrientationChangeEvent ), false );
+		}
 
-		this.freeze = false;
+	}();
 
-	};
+	this.connect = function() {
 
-	this.disconnect = function() {
+		onScreenOrientationChangeEvent(); // run once on load
 
-		this.freeze = true;
+		window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
+		window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
 
-		window.removeEventListener( 'orientationchange', bind( this, this.onScreenOrientationChangeEvent ), false );
-		window.removeEventListener( 'deviceorientation', bind( this, this.onDeviceOrientationChangeEvent ), false );
+		scope.freeze = false;
 
 	};
 
-	// The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''
-
-	setObjectQuaternion = function () {
-
-		var zee = new THREE.Vector3( 0, 0, 1 );
-
-		var euler = new THREE.Euler();
-
-		var q0 = new THREE.Quaternion();
+	this.disconnect = function() {
 
-		var q1 = new THREE.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
+		scope.freeze = true;
 
-		return function ( quaternion, alpha, beta, gamma, orient ) {
+		window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent, false );
+		window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent, false );
 
-			euler.set( beta, alpha, - gamma, 'YXZ' );                       // 'ZXY' for the device, but 'YXZ' for us
+	};
 
-			quaternion.setFromEuler( euler );                               // orient the device
+	this.update = function () {
 
-			quaternion.multiply( q1 );                                      // camera looks out the back of the device, not the top
+		if ( scope.freeze ) return;
 
-			quaternion.multiply( q0.setFromAxisAngle( zee, - orient ) );    // adjust for screen orientation
+		var alpha  = scope.deviceOrientation.gamma ? THREE.Math.degToRad( scope.deviceOrientation.alpha ) : 0; // Z
+		var beta   = scope.deviceOrientation.beta  ? THREE.Math.degToRad( scope.deviceOrientation.beta  ) : 0; // X'
+		var gamma  = scope.deviceOrientation.gamma ? THREE.Math.degToRad( scope.deviceOrientation.gamma ) : 0; // Y''
+		var orient = scope.screenOrientation       ? THREE.Math.degToRad( scope.screenOrientation       ) : 0; // O
 
-		}
+		setObjectQuaternion( scope.object.quaternion, alpha, beta, gamma, orient );
 
-	}();
+	};
 
 };