FirstPersonControls.js 6.3 KB
Newer Older
C
Chris Killpack 已提交
1 2 3 4 5 6
/**
 * @author mrdoob / http://mrdoob.com/
 * @author alteredq / http://alteredqualia.com/
 * @author paulirish / http://paulirish.com/
 */

7
THREE.FirstPersonControls = function ( object, domElement ) {
C
Chris Killpack 已提交
8

9
	this.object = object;
10
	this.target = new THREE.Vector3( 0, 0, 0 );
M
Mr.doob 已提交
11

12 13
	this.domElement = ( domElement !== undefined ) ? domElement : document;

C
Chris Killpack 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	this.movementSpeed = 1.0;
	this.lookSpeed = 0.005;

	this.noFly = false;
	this.lookVertical = true;
	this.autoForward = false;

	this.activeLook = true;

	this.heightSpeed = false;
	this.heightCoef = 1.0;
	this.heightMin = 0.0;

	this.constrainVertical = false;
	this.verticalMin = 0;
B
Bc. Jan Kaláb 已提交
29
	this.verticalMax = Math.PI;
C
Chris Killpack 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

	this.autoSpeedFactor = 0.0;

	this.mouseX = 0;
	this.mouseY = 0;

	this.lat = 0;
	this.lon = 0;
	this.phi = 0;
	this.theta = 0;

	this.moveForward = false;
	this.moveBackward = false;
	this.moveLeft = false;
	this.moveRight = false;
	this.freeze = false;

	this.mouseDragOn = false;

49
	if ( this.domElement === document ) {
50

51 52
		this.viewHalfX = window.innerWidth / 2;
		this.viewHalfY = window.innerHeight / 2;
53

54
	} else {
55

56 57 58
		this.viewHalfX = this.domElement.offsetWidth / 2;
		this.viewHalfY = this.domElement.offsetHeight / 2;
		this.domElement.setAttribute( 'tabindex', -1 );
59

60
	}
C
Chris Killpack 已提交
61 62 63

	this.onMouseDown = function ( event ) {

64
		if ( this.domElement !== document ) {
65

66
			this.domElement.focus();
67

68
		}
69

C
Chris Killpack 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		event.preventDefault();
		event.stopPropagation();

		if ( this.activeLook ) {

			switch ( event.button ) {

				case 0: this.moveForward = true; break;
				case 2: this.moveBackward = true; break;

			}

		}

		this.mouseDragOn = true;

	};

	this.onMouseUp = function ( event ) {

		event.preventDefault();
		event.stopPropagation();

		if ( this.activeLook ) {

			switch ( event.button ) {

				case 0: this.moveForward = false; break;
				case 2: this.moveBackward = false; break;

			}

		}

		this.mouseDragOn = false;

	};

	this.onMouseMove = function ( event ) {

110
		if ( this.domElement === document ) {
111

112 113
			this.mouseX = event.pageX - this.viewHalfX;
			this.mouseY = event.pageY - this.viewHalfY;
114

115
		} else {
116

117 118
			this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
			this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
119

120
		}
C
Chris Killpack 已提交
121 122 123 124 125 126 127 128

	};

	this.onKeyDown = function ( event ) {

		switch( event.keyCode ) {

			case 38: /*up*/
129
			case 87: /*W*/ this.moveForward = true; break;
C
Chris Killpack 已提交
130 131

			case 37: /*left*/
132
			case 65: /*A*/ this.moveLeft = true; break;
C
Chris Killpack 已提交
133 134

			case 40: /*down*/
135
			case 83: /*S*/ this.moveBackward = true; break;
C
Chris Killpack 已提交
136 137

			case 39: /*right*/
138
			case 68: /*D*/ this.moveRight = true; break;
C
Chris Killpack 已提交
139

140 141
			case 82: /*R*/ this.moveUp = true; break;
			case 70: /*F*/ this.moveDown = true; break;
142

143
			case 81: /*Q*/ this.freeze = !this.freeze; break;
C
Chris Killpack 已提交
144 145 146 147 148 149 150 151 152 153

		}

	};

	this.onKeyUp = function ( event ) {

		switch( event.keyCode ) {

			case 38: /*up*/
154
			case 87: /*W*/ this.moveForward = false; break;
C
Chris Killpack 已提交
155 156

			case 37: /*left*/
157
			case 65: /*A*/ this.moveLeft = false; break;
C
Chris Killpack 已提交
158 159

			case 40: /*down*/
160
			case 83: /*S*/ this.moveBackward = false; break;
C
Chris Killpack 已提交
161 162

			case 39: /*right*/
163
			case 68: /*D*/ this.moveRight = false; break;
C
Chris Killpack 已提交
164

165 166
			case 82: /*R*/ this.moveUp = false; break;
			case 70: /*F*/ this.moveDown = false; break;
167

C
Chris Killpack 已提交
168 169 170 171
		}

	};

172
	this.update = function( delta ) {
173
		var actualMoveSpeed = 0;
Z
zz85 已提交
174 175 176 177 178 179
		
		if ( this.freeze ) {
			
			return;
			
		} else {
C
Chris Killpack 已提交
180 181 182

			if ( this.heightSpeed ) {

183 184
				var y = THREE.Math.clamp( this.object.position.y, this.heightMin, this.heightMax );
				var heightDelta = y - this.heightMin;
C
Chris Killpack 已提交
185

186
				this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
C
Chris Killpack 已提交
187 188 189 190 191 192 193

			} else {

				this.autoSpeedFactor = 0.0;

			}

194
			actualMoveSpeed = delta * this.movementSpeed;
C
Chris Killpack 已提交
195

196 197
			if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
			if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
198

199 200
			if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
			if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
C
Chris Killpack 已提交
201

202 203
			if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
			if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
204

205
			var actualLookSpeed = delta * this.lookSpeed;
C
Chris Killpack 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219

			if ( !this.activeLook ) {

				actualLookSpeed = 0;

			}

			this.lon += this.mouseX * actualLookSpeed;
			if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed;

			this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
			this.phi = ( 90 - this.lat ) * Math.PI / 180;
			this.theta = this.lon * Math.PI / 180;

220 221
			var targetPosition = this.target,
				position = this.object.position;
C
Chris Killpack 已提交
222 223 224 225 226 227 228

			targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
			targetPosition.y = position.y + 100 * Math.cos( this.phi );
			targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );

		}

229 230 231 232
		var verticalLookRatio = 1;

		if ( this.constrainVertical ) {

B
Bc. Jan Kaláb 已提交
233
			verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
234 235 236

		}

C
Chris Killpack 已提交
237
		this.lon += this.mouseX * actualLookSpeed;
238
		if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
C
Chris Killpack 已提交
239 240 241

		this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
		this.phi = ( 90 - this.lat ) * Math.PI / 180;
242

C
Chris Killpack 已提交
243 244 245 246
		this.theta = this.lon * Math.PI / 180;

		if ( this.constrainVertical ) {

247
			this.phi = THREE.Math.mapLinear( this.phi, 0, Math.PI, this.verticalMin, this.verticalMax );
C
Chris Killpack 已提交
248 249 250

		}

251 252
		var targetPosition = this.target,
			position = this.object.position;
C
Chris Killpack 已提交
253 254 255 256 257

		targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
		targetPosition.y = position.y + 100 * Math.cos( this.phi );
		targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );

258
		this.object.lookAt( targetPosition );
C
Chris Killpack 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

	};


	this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );

	this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
	this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
	this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
	this.domElement.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
	this.domElement.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );

	function bind( scope, fn ) {

		return function () {

			fn.apply( scope, arguments );

		};

	};

};