FirstPersonControls.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author paulirish / http://paulirish.com/
  5. */
  6. THREE.FirstPersonControls = function ( object, domElement ) {
  7. this.object = object;
  8. this.target = new THREE.Vector3( 0, 0, 0 );
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. this.movementSpeed = 1.0;
  11. this.lookSpeed = 0.005;
  12. this.lookVertical = true;
  13. this.autoForward = false;
  14. // this.invertVertical = false;
  15. this.activeLook = true;
  16. this.heightSpeed = false;
  17. this.heightCoef = 1.0;
  18. this.heightMin = 0.0;
  19. this.heightMax = 1.0;
  20. this.constrainVertical = false;
  21. this.verticalMin = 0;
  22. this.verticalMax = Math.PI;
  23. this.autoSpeedFactor = 0.0;
  24. this.mouseX = 0;
  25. this.mouseY = 0;
  26. this.lat = 0;
  27. this.lon = 0;
  28. this.phi = 0;
  29. this.theta = 0;
  30. this.moveForward = false;
  31. this.moveBackward = false;
  32. this.moveLeft = false;
  33. this.moveRight = false;
  34. this.freeze = false;
  35. this.mouseDragOn = false;
  36. this.viewHalfX = 0;
  37. this.viewHalfY = 0;
  38. if ( this.domElement !== document ) {
  39. this.domElement.setAttribute( 'tabindex', -1 );
  40. }
  41. //
  42. this.handleResize = function () {
  43. if ( this.domElement === document ) {
  44. this.viewHalfX = window.innerWidth / 2;
  45. this.viewHalfY = window.innerHeight / 2;
  46. } else {
  47. this.viewHalfX = this.domElement.offsetWidth / 2;
  48. this.viewHalfY = this.domElement.offsetHeight / 2;
  49. }
  50. };
  51. this.onMouseDown = function ( event ) {
  52. if ( this.domElement !== document ) {
  53. this.domElement.focus();
  54. }
  55. event.preventDefault();
  56. event.stopPropagation();
  57. if ( this.activeLook ) {
  58. switch ( event.button ) {
  59. case 0: this.moveForward = true; break;
  60. case 2: this.moveBackward = true; break;
  61. }
  62. }
  63. this.mouseDragOn = true;
  64. };
  65. this.onMouseUp = function ( event ) {
  66. event.preventDefault();
  67. event.stopPropagation();
  68. if ( this.activeLook ) {
  69. switch ( event.button ) {
  70. case 0: this.moveForward = false; break;
  71. case 2: this.moveBackward = false; break;
  72. }
  73. }
  74. this.mouseDragOn = false;
  75. };
  76. this.onMouseMove = function ( event ) {
  77. if ( this.domElement === document ) {
  78. this.mouseX = event.pageX - this.viewHalfX;
  79. this.mouseY = event.pageY - this.viewHalfY;
  80. } else {
  81. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  82. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  83. }
  84. };
  85. this.onKeyDown = function ( event ) {
  86. //event.preventDefault();
  87. switch ( event.keyCode ) {
  88. case 38: /*up*/
  89. case 87: /*W*/ this.moveForward = true; break;
  90. case 37: /*left*/
  91. case 65: /*A*/ this.moveLeft = true; break;
  92. case 40: /*down*/
  93. case 83: /*S*/ this.moveBackward = true; break;
  94. case 39: /*right*/
  95. case 68: /*D*/ this.moveRight = true; break;
  96. case 82: /*R*/ this.moveUp = true; break;
  97. case 70: /*F*/ this.moveDown = true; break;
  98. case 81: /*Q*/ this.freeze = !this.freeze; break;
  99. }
  100. };
  101. this.onKeyUp = function ( event ) {
  102. switch( event.keyCode ) {
  103. case 38: /*up*/
  104. case 87: /*W*/ this.moveForward = false; break;
  105. case 37: /*left*/
  106. case 65: /*A*/ this.moveLeft = false; break;
  107. case 40: /*down*/
  108. case 83: /*S*/ this.moveBackward = false; break;
  109. case 39: /*right*/
  110. case 68: /*D*/ this.moveRight = false; break;
  111. case 82: /*R*/ this.moveUp = false; break;
  112. case 70: /*F*/ this.moveDown = false; break;
  113. }
  114. };
  115. this.update = function( delta ) {
  116. var actualMoveSpeed = 0;
  117. if ( this.freeze ) {
  118. return;
  119. } else {
  120. if ( this.heightSpeed ) {
  121. var y = THREE.Math.clamp( this.object.position.y, this.heightMin, this.heightMax );
  122. var heightDelta = y - this.heightMin;
  123. this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  124. } else {
  125. this.autoSpeedFactor = 0.0;
  126. }
  127. actualMoveSpeed = delta * this.movementSpeed;
  128. if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
  129. if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
  130. if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
  131. if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
  132. if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
  133. if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
  134. var actualLookSpeed = delta * this.lookSpeed;
  135. if ( !this.activeLook ) {
  136. actualLookSpeed = 0;
  137. }
  138. }
  139. var verticalLookRatio = 1;
  140. if ( this.constrainVertical ) {
  141. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  142. }
  143. this.lon += this.mouseX * actualLookSpeed;
  144. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
  145. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  146. this.phi = THREE.Math.degToRad( 90 - this.lat );
  147. this.theta = THREE.Math.degToRad( this.lon );
  148. if ( this.constrainVertical ) {
  149. this.phi = THREE.Math.mapLinear( this.phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  150. }
  151. var targetPosition = this.target,
  152. position = this.object.position;
  153. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  154. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  155. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  156. this.object.lookAt( targetPosition );
  157. };
  158. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  159. this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
  160. this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
  161. this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
  162. this.domElement.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
  163. this.domElement.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
  164. function bind( scope, fn ) {
  165. return function () {
  166. fn.apply( scope, arguments );
  167. };
  168. };
  169. this.handleResize();
  170. };