2
0
Эх сурвалжийг харах

FirstPersonControls: Added .lookAt()

Mugen87 6 жил өмнө
parent
commit
6c337ef42b

+ 50 - 8
examples/js/controls/FirstPersonControls.js

@@ -34,9 +34,6 @@ THREE.FirstPersonControls = function ( object, domElement ) {
 	this.mouseX = 0;
 	this.mouseX = 0;
 	this.mouseY = 0;
 	this.mouseY = 0;
 
 
-	this.lat = 0;
-	this.lon = 0;
-
 	this.moveForward = false;
 	this.moveForward = false;
 	this.moveBackward = false;
 	this.moveBackward = false;
 	this.moveLeft = false;
 	this.moveLeft = false;
@@ -47,6 +44,17 @@ THREE.FirstPersonControls = function ( object, domElement ) {
 	this.viewHalfX = 0;
 	this.viewHalfX = 0;
 	this.viewHalfY = 0;
 	this.viewHalfY = 0;
 
 
+	// private variables
+
+	var lat = 0;
+	var lon = 0;
+
+	var lookDirection = new THREE.Vector3();
+	var spherical = new THREE.Spherical();
+	var target = new THREE.Vector3();
+
+	//
+
 	if ( this.domElement !== document ) {
 	if ( this.domElement !== document ) {
 
 
 		this.domElement.setAttribute( 'tabindex', - 1 );
 		this.domElement.setAttribute( 'tabindex', - 1 );
@@ -181,6 +189,26 @@ THREE.FirstPersonControls = function ( object, domElement ) {
 
 
 	};
 	};
 
 
+	this.lookAt = function ( x, y, z ) {
+
+		if ( x.isVector3 ) {
+
+			target.copy( x );
+
+		} else {
+
+			target.set( x, y, z );
+
+		}
+
+		this.object.lookAt( target );
+
+		setOrientation( this );
+
+		return this;
+
+	};
+
 	this.update = function () {
 	this.update = function () {
 
 
 		var targetPosition = new THREE.Vector3();
 		var targetPosition = new THREE.Vector3();
@@ -229,13 +257,13 @@ THREE.FirstPersonControls = function ( object, domElement ) {
 
 
 			}
 			}
 
 
-			this.lon -= this.mouseX * actualLookSpeed;
-			if ( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
+			lon -= this.mouseX * actualLookSpeed;
+			if ( this.lookVertical ) lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
 
 
-			this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
+			lat = Math.max( - 85, Math.min( 85, lat ) );
 
 
-			var phi = THREE.Math.degToRad( 90 - this.lat );
-			var theta = THREE.Math.degToRad( this.lon );
+			var phi = THREE.Math.degToRad( 90 - lat );
+			var theta = THREE.Math.degToRad( lon );
 
 
 			if ( this.constrainVertical ) {
 			if ( this.constrainVertical ) {
 
 
@@ -295,6 +323,20 @@ THREE.FirstPersonControls = function ( object, domElement ) {
 
 
 	}
 	}
 
 
+	function setOrientation( controls ) {
+
+		var quaternion = controls.object.quaternion;
+
+		lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
+		spherical.setFromVector3( lookDirection );
+
+		lat = 90 - THREE.Math.radToDeg( spherical.phi );
+		lon = THREE.Math.radToDeg( spherical.theta );
+
+	}
+
 	this.handleResize();
 	this.handleResize();
 
 
+	setOrientation( this );
+
 };
 };

+ 3 - 0
examples/webgl_nearestneighbour.html

@@ -90,6 +90,9 @@
 				controls.movementSpeed = 100;
 				controls.movementSpeed = 100;
 				controls.lookSpeed = 0.1;
 				controls.lookSpeed = 0.1;
 
 
+				controls.lookAt( 500, 500, 500 );
+
+
 				// add a skybox background
 				// add a skybox background
 				var cubeTextureLoader = new THREE.CubeTextureLoader();
 				var cubeTextureLoader = new THREE.CubeTextureLoader();
 
 

+ 8 - 12
examples/webgl_shadowmap.html

@@ -85,29 +85,25 @@
 				container = document.createElement( 'div' );
 				container = document.createElement( 'div' );
 				document.body.appendChild( container );
 				document.body.appendChild( container );
 
 
-				// SCENE CAMERA
+				// CAMERA
 
 
 				camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
 				camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
 				camera.position.set( 700, 50, 1900 );
 				camera.position.set( 700, 50, 1900 );
 
 
+				// SCENE
+
+				scene = new THREE.Scene();
+				scene.background = new THREE.Color( 0x59472b );
+				scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
+
 				controls = new THREE.FirstPersonControls( camera );
 				controls = new THREE.FirstPersonControls( camera );
 
 
 				controls.lookSpeed = 0.0125;
 				controls.lookSpeed = 0.0125;
 				controls.movementSpeed = 500;
 				controls.movementSpeed = 500;
 				controls.noFly = false;
 				controls.noFly = false;
 				controls.lookVertical = true;
 				controls.lookVertical = true;
-				controls.constrainVertical = true;
-				controls.verticalMin = 1.5;
-				controls.verticalMax = 2.0;
 
 
-				controls.lon = 250;
-				controls.lat = 30;
-
-				// SCENE
-
-				scene = new THREE.Scene();
-				scene.background = new THREE.Color( 0x59472b );
-				scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
+				controls.lookAt( scene.position );
 
 
 				// LIGHTS
 				// LIGHTS
 
 

+ 8 - 12
examples/webgl_shadowmap_performance.html

@@ -81,29 +81,25 @@
 				container = document.createElement( 'div' );
 				container = document.createElement( 'div' );
 				document.body.appendChild( container );
 				document.body.appendChild( container );
 
 
-				// SCENE CAMERA
+				// CAMERA
 
 
 				camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
 				camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
 				camera.position.set( 700, 50, 1900 );
 				camera.position.set( 700, 50, 1900 );
 
 
+				// SCENE
+
+				scene = new THREE.Scene();
+				scene.background = new THREE.Color( 0x59472b );
+				scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
+
 				controls = new THREE.FirstPersonControls( camera );
 				controls = new THREE.FirstPersonControls( camera );
 
 
 				controls.lookSpeed = 0.0125;
 				controls.lookSpeed = 0.0125;
 				controls.movementSpeed = 500;
 				controls.movementSpeed = 500;
 				controls.noFly = false;
 				controls.noFly = false;
 				controls.lookVertical = true;
 				controls.lookVertical = true;
-				controls.constrainVertical = true;
-				controls.verticalMin = 1.5;
-				controls.verticalMax = 2.0;
 
 
-				controls.lon = 250;
-				controls.lat = 30;
-
-				// SCENE
-
-				scene = new THREE.Scene();
-				scene.background = new THREE.Color( 0x59472b );
-				scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
+				controls.lookAt( scene.position );
 
 
 				// LIGHTS
 				// LIGHTS