Skip to content

Commit

Permalink
FirstPersonControls: Added .lookAt()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Jan 28, 2019
1 parent 77adaa3 commit 6c337ef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
58 changes: 50 additions & 8 deletions examples/js/controls/FirstPersonControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ THREE.FirstPersonControls = function ( object, domElement ) {
this.mouseX = 0;
this.mouseY = 0;

this.lat = 0;
this.lon = 0;

this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
Expand All @@ -47,6 +44,17 @@ THREE.FirstPersonControls = function ( object, domElement ) {
this.viewHalfX = 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 ) {

this.domElement.setAttribute( 'tabindex', - 1 );
Expand Down Expand Up @@ -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 () {

var targetPosition = new THREE.Vector3();
Expand Down Expand Up @@ -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 ) {

Expand Down Expand Up @@ -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();

setOrientation( this );

};
3 changes: 3 additions & 0 deletions examples/webgl_nearestneighbour.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
controls.movementSpeed = 100;
controls.lookSpeed = 0.1;

controls.lookAt( 500, 500, 500 );


// add a skybox background
var cubeTextureLoader = new THREE.CubeTextureLoader();

Expand Down
20 changes: 8 additions & 12 deletions examples/webgl_shadowmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,25 @@
container = document.createElement( 'div' );
document.body.appendChild( container );

// SCENE CAMERA
// CAMERA

camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
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.lookSpeed = 0.0125;
controls.movementSpeed = 500;
controls.noFly = false;
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

Expand Down
20 changes: 8 additions & 12 deletions examples/webgl_shadowmap_performance.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,25 @@
container = document.createElement( 'div' );
document.body.appendChild( container );

// SCENE CAMERA
// CAMERA

camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
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.lookSpeed = 0.0125;
controls.movementSpeed = 500;
controls.noFly = false;
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

Expand Down

0 comments on commit 6c337ef

Please sign in to comment.