Skip to content

Commit 2451173

Browse files
authored
Merge pull request #15650 from Mugen87/dev26
FirstPersonControls: Added .lookAt()
2 parents ee696d1 + 6c337ef commit 2451173

File tree

4 files changed

+69
-32
lines changed

4 files changed

+69
-32
lines changed

examples/js/controls/FirstPersonControls.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ THREE.FirstPersonControls = function ( object, domElement ) {
3434
this.mouseX = 0;
3535
this.mouseY = 0;
3636

37-
this.lat = 0;
38-
this.lon = 0;
39-
4037
this.moveForward = false;
4138
this.moveBackward = false;
4239
this.moveLeft = false;
@@ -47,6 +44,17 @@ THREE.FirstPersonControls = function ( object, domElement ) {
4744
this.viewHalfX = 0;
4845
this.viewHalfY = 0;
4946

47+
// private variables
48+
49+
var lat = 0;
50+
var lon = 0;
51+
52+
var lookDirection = new THREE.Vector3();
53+
var spherical = new THREE.Spherical();
54+
var target = new THREE.Vector3();
55+
56+
//
57+
5058
if ( this.domElement !== document ) {
5159

5260
this.domElement.setAttribute( 'tabindex', - 1 );
@@ -181,6 +189,26 @@ THREE.FirstPersonControls = function ( object, domElement ) {
181189

182190
};
183191

192+
this.lookAt = function ( x, y, z ) {
193+
194+
if ( x.isVector3 ) {
195+
196+
target.copy( x );
197+
198+
} else {
199+
200+
target.set( x, y, z );
201+
202+
}
203+
204+
this.object.lookAt( target );
205+
206+
setOrientation( this );
207+
208+
return this;
209+
210+
};
211+
184212
this.update = function () {
185213

186214
var targetPosition = new THREE.Vector3();
@@ -229,13 +257,13 @@ THREE.FirstPersonControls = function ( object, domElement ) {
229257

230258
}
231259

232-
this.lon -= this.mouseX * actualLookSpeed;
233-
if ( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
260+
lon -= this.mouseX * actualLookSpeed;
261+
if ( this.lookVertical ) lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
234262

235-
this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
263+
lat = Math.max( - 85, Math.min( 85, lat ) );
236264

237-
var phi = THREE.Math.degToRad( 90 - this.lat );
238-
var theta = THREE.Math.degToRad( this.lon );
265+
var phi = THREE.Math.degToRad( 90 - lat );
266+
var theta = THREE.Math.degToRad( lon );
239267

240268
if ( this.constrainVertical ) {
241269

@@ -295,6 +323,20 @@ THREE.FirstPersonControls = function ( object, domElement ) {
295323

296324
}
297325

326+
function setOrientation( controls ) {
327+
328+
var quaternion = controls.object.quaternion;
329+
330+
lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
331+
spherical.setFromVector3( lookDirection );
332+
333+
lat = 90 - THREE.Math.radToDeg( spherical.phi );
334+
lon = THREE.Math.radToDeg( spherical.theta );
335+
336+
}
337+
298338
this.handleResize();
299339

340+
setOrientation( this );
341+
300342
};

examples/webgl_nearestneighbour.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
controls.movementSpeed = 100;
9191
controls.lookSpeed = 0.1;
9292

93+
controls.lookAt( 500, 500, 500 );
94+
95+
9396
// add a skybox background
9497
var cubeTextureLoader = new THREE.CubeTextureLoader();
9598

examples/webgl_shadowmap.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,25 @@
8585
container = document.createElement( 'div' );
8686
document.body.appendChild( container );
8787

88-
// SCENE CAMERA
88+
// CAMERA
8989

9090
camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
9191
camera.position.set( 700, 50, 1900 );
9292

93+
// SCENE
94+
95+
scene = new THREE.Scene();
96+
scene.background = new THREE.Color( 0x59472b );
97+
scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
98+
9399
controls = new THREE.FirstPersonControls( camera );
94100

95101
controls.lookSpeed = 0.0125;
96102
controls.movementSpeed = 500;
97103
controls.noFly = false;
98104
controls.lookVertical = true;
99-
controls.constrainVertical = true;
100-
controls.verticalMin = 1.5;
101-
controls.verticalMax = 2.0;
102105

103-
controls.lon = 250;
104-
controls.lat = 30;
105-
106-
// SCENE
107-
108-
scene = new THREE.Scene();
109-
scene.background = new THREE.Color( 0x59472b );
110-
scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
106+
controls.lookAt( scene.position );
111107

112108
// LIGHTS
113109

examples/webgl_shadowmap_performance.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,25 @@
8181
container = document.createElement( 'div' );
8282
document.body.appendChild( container );
8383

84-
// SCENE CAMERA
84+
// CAMERA
8585

8686
camera = new THREE.PerspectiveCamera( 23, SCREEN_WIDTH / SCREEN_HEIGHT, NEAR, FAR );
8787
camera.position.set( 700, 50, 1900 );
8888

89+
// SCENE
90+
91+
scene = new THREE.Scene();
92+
scene.background = new THREE.Color( 0x59472b );
93+
scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
94+
8995
controls = new THREE.FirstPersonControls( camera );
9096

9197
controls.lookSpeed = 0.0125;
9298
controls.movementSpeed = 500;
9399
controls.noFly = false;
94100
controls.lookVertical = true;
95-
controls.constrainVertical = true;
96-
controls.verticalMin = 1.5;
97-
controls.verticalMax = 2.0;
98101

99-
controls.lon = 250;
100-
controls.lat = 30;
101-
102-
// SCENE
103-
104-
scene = new THREE.Scene();
105-
scene.background = new THREE.Color( 0x59472b );
106-
scene.fog = new THREE.Fog( 0x59472b, 1000, FAR );
102+
controls.lookAt( scene.position );
107103

108104
// LIGHTS
109105

0 commit comments

Comments
 (0)