FirstPersonControls.js 5.9 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
	this.movementSpeed = 1.0;
	this.lookSpeed = 0.005;

	this.lookVertical = true;
	this.autoForward = false;
19
	// this.invertVertical = false;
C
Chris Killpack 已提交
20 21 22 23 24 25

	this.activeLook = true;

	this.heightSpeed = false;
	this.heightCoef = 1.0;
	this.heightMin = 0.0;
26
	this.heightMax = 1.0;
C
Chris Killpack 已提交
27 28 29

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

	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;

50 51
	this.viewHalfX = 0;
	this.viewHalfY = 0;
52

53
	if ( this.domElement !== document ) {
54

55
		this.domElement.setAttribute( 'tabindex', -1 );
56

57
	}
C
Chris Killpack 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	//

	this.handleResize = function () {

		if ( this.domElement === document ) {

			this.viewHalfX = window.innerWidth / 2;
			this.viewHalfY = window.innerHeight / 2;

		} else {

			this.viewHalfX = this.domElement.offsetWidth / 2;
			this.viewHalfY = this.domElement.offsetHeight / 2;

		}

	};

C
Chris Killpack 已提交
77 78
	this.onMouseDown = function ( event ) {

79
		if ( this.domElement !== document ) {
80

81
			this.domElement.focus();
82

83
		}
84

C
Chris Killpack 已提交
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		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 ) {

125
		if ( this.domElement === document ) {
126

127 128
			this.mouseX = event.pageX - this.viewHalfX;
			this.mouseY = event.pageY - this.viewHalfY;
129

130
		} else {
131

132 133
			this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
			this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
134

135
		}
C
Chris Killpack 已提交
136 137 138 139 140

	};

	this.onKeyDown = function ( event ) {

141
		//event.preventDefault();
142 143

		switch ( event.keyCode ) {
C
Chris Killpack 已提交
144 145

			case 38: /*up*/
146
			case 87: /*W*/ this.moveForward = true; break;
C
Chris Killpack 已提交
147 148

			case 37: /*left*/
149
			case 65: /*A*/ this.moveLeft = true; break;
C
Chris Killpack 已提交
150 151

			case 40: /*down*/
152
			case 83: /*S*/ this.moveBackward = true; break;
C
Chris Killpack 已提交
153 154

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

157 158
			case 82: /*R*/ this.moveUp = true; break;
			case 70: /*F*/ this.moveDown = true; break;
159

160
			case 81: /*Q*/ this.freeze = !this.freeze; break;
C
Chris Killpack 已提交
161 162 163 164 165 166 167 168 169 170

		}

	};

	this.onKeyUp = function ( event ) {

		switch( event.keyCode ) {

			case 38: /*up*/
171
			case 87: /*W*/ this.moveForward = false; break;
C
Chris Killpack 已提交
172 173

			case 37: /*left*/
174
			case 65: /*A*/ this.moveLeft = false; break;
C
Chris Killpack 已提交
175 176

			case 40: /*down*/
177
			case 83: /*S*/ this.moveBackward = false; break;
C
Chris Killpack 已提交
178 179

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

182 183
			case 82: /*R*/ this.moveUp = false; break;
			case 70: /*F*/ this.moveDown = false; break;
184

C
Chris Killpack 已提交
185 186 187 188
		}

	};

189
	this.update = function( delta ) {
190

Z
zz85 已提交
191
		if ( this.freeze ) {
192

Z
zz85 已提交
193
			return;
194

195
		}
C
Chris Killpack 已提交
196

197
		if ( this.heightSpeed ) {
C
Chris Killpack 已提交
198

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

202
			this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
C
Chris Killpack 已提交
203

204
		} else {
C
Chris Killpack 已提交
205

206
			this.autoSpeedFactor = 0.0;
C
Chris Killpack 已提交
207

208
		}
C
Chris Killpack 已提交
209

210
		var actualMoveSpeed = delta * this.movementSpeed;
211

212 213
		if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
		if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
C
Chris Killpack 已提交
214

215 216
		if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
		if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
217

218 219
		if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
		if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
C
Chris Killpack 已提交
220

221
		var actualLookSpeed = delta * this.lookSpeed;
C
Chris Killpack 已提交
222

223
		if ( !this.activeLook ) {
C
Chris Killpack 已提交
224

225
			actualLookSpeed = 0;
C
Chris Killpack 已提交
226 227 228

		}

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

		this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
241
		this.phi = THREE.Math.degToRad( 90 - this.lat );
242

243
		this.theta = THREE.Math.degToRad( this.lon );
C
Chris Killpack 已提交
244 245 246

		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

	};


	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 );
268 269 270
	
	window.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
	window.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
C
Chris Killpack 已提交
271 272 273 274 275 276 277 278 279 280 281

	function bind( scope, fn ) {

		return function () {

			fn.apply( scope, arguments );

		};

	};

282 283
	this.handleResize();

C
Chris Killpack 已提交
284
};