FirstPersonControls.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import {
  2. MathUtils,
  3. Spherical,
  4. Vector3
  5. } from '../../../build/three.module.js';
  6. const _lookDirection = new Vector3();
  7. const _spherical = new Spherical();
  8. const _target = new Vector3();
  9. class FirstPersonControls {
  10. constructor( object, domElement ) {
  11. if ( domElement === undefined ) {
  12. console.warn( 'THREE.FirstPersonControls: The second parameter "domElement" is now mandatory.' );
  13. domElement = document;
  14. }
  15. this.object = object;
  16. this.domElement = domElement;
  17. // API
  18. this.enabled = true;
  19. this.movementSpeed = 1.0;
  20. this.lookSpeed = 0.005;
  21. this.lookVertical = true;
  22. this.autoForward = false;
  23. this.activeLook = true;
  24. this.heightSpeed = false;
  25. this.heightCoef = 1.0;
  26. this.heightMin = 0.0;
  27. this.heightMax = 1.0;
  28. this.constrainVertical = false;
  29. this.verticalMin = 0;
  30. this.verticalMax = Math.PI;
  31. this.mouseDragOn = false;
  32. // internals
  33. this.autoSpeedFactor = 0.0;
  34. this.mouseX = 0;
  35. this.mouseY = 0;
  36. this.moveForward = false;
  37. this.moveBackward = false;
  38. this.moveLeft = false;
  39. this.moveRight = false;
  40. this.viewHalfX = 0;
  41. this.viewHalfY = 0;
  42. // private variables
  43. let lat = 0;
  44. let lon = 0;
  45. //
  46. this.handleResize = function () {
  47. if ( this.domElement === document ) {
  48. this.viewHalfX = window.innerWidth / 2;
  49. this.viewHalfY = window.innerHeight / 2;
  50. } else {
  51. this.viewHalfX = this.domElement.offsetWidth / 2;
  52. this.viewHalfY = this.domElement.offsetHeight / 2;
  53. }
  54. };
  55. this.onMouseDown = function ( event ) {
  56. if ( this.domElement !== document ) {
  57. this.domElement.focus();
  58. }
  59. event.preventDefault();
  60. if ( this.activeLook ) {
  61. switch ( event.button ) {
  62. case 0: this.moveForward = true; break;
  63. case 2: this.moveBackward = true; break;
  64. }
  65. }
  66. this.mouseDragOn = true;
  67. };
  68. this.onMouseUp = function ( event ) {
  69. event.preventDefault();
  70. if ( this.activeLook ) {
  71. switch ( event.button ) {
  72. case 0: this.moveForward = false; break;
  73. case 2: this.moveBackward = false; break;
  74. }
  75. }
  76. this.mouseDragOn = false;
  77. };
  78. this.onMouseMove = function ( event ) {
  79. if ( this.domElement === document ) {
  80. this.mouseX = event.pageX - this.viewHalfX;
  81. this.mouseY = event.pageY - this.viewHalfY;
  82. } else {
  83. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  84. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  85. }
  86. };
  87. this.onKeyDown = function ( event ) {
  88. //event.preventDefault();
  89. switch ( event.code ) {
  90. case 'ArrowUp':
  91. case 'KeyW': this.moveForward = true; break;
  92. case 'ArrowLeft':
  93. case 'KeyA': this.moveLeft = true; break;
  94. case 'ArrowDown':
  95. case 'KeyS': this.moveBackward = true; break;
  96. case 'ArrowRight':
  97. case 'KeyD': this.moveRight = true; break;
  98. case 'KeyR': this.moveUp = true; break;
  99. case 'KeyF': this.moveDown = true; break;
  100. }
  101. };
  102. this.onKeyUp = function ( event ) {
  103. switch ( event.code ) {
  104. case 'ArrowUp':
  105. case 'KeyW': this.moveForward = false; break;
  106. case 'ArrowLeft':
  107. case 'KeyA': this.moveLeft = false; break;
  108. case 'ArrowDown':
  109. case 'KeyS': this.moveBackward = false; break;
  110. case 'ArrowRight':
  111. case 'KeyD': this.moveRight = false; break;
  112. case 'KeyR': this.moveUp = false; break;
  113. case 'KeyF': this.moveDown = false; break;
  114. }
  115. };
  116. this.lookAt = function ( x, y, z ) {
  117. if ( x.isVector3 ) {
  118. _target.copy( x );
  119. } else {
  120. _target.set( x, y, z );
  121. }
  122. this.object.lookAt( _target );
  123. setOrientation( this );
  124. return this;
  125. };
  126. this.update = function () {
  127. const targetPosition = new Vector3();
  128. return function update( delta ) {
  129. if ( this.enabled === false ) return;
  130. if ( this.heightSpeed ) {
  131. const y = MathUtils.clamp( this.object.position.y, this.heightMin, this.heightMax );
  132. const heightDelta = y - this.heightMin;
  133. this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  134. } else {
  135. this.autoSpeedFactor = 0.0;
  136. }
  137. const actualMoveSpeed = delta * this.movementSpeed;
  138. if ( this.moveForward || ( this.autoForward && ! this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
  139. if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
  140. if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
  141. if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
  142. if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
  143. if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
  144. let actualLookSpeed = delta * this.lookSpeed;
  145. if ( ! this.activeLook ) {
  146. actualLookSpeed = 0;
  147. }
  148. let verticalLookRatio = 1;
  149. if ( this.constrainVertical ) {
  150. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  151. }
  152. lon -= this.mouseX * actualLookSpeed;
  153. if ( this.lookVertical ) lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
  154. lat = Math.max( - 85, Math.min( 85, lat ) );
  155. let phi = MathUtils.degToRad( 90 - lat );
  156. const theta = MathUtils.degToRad( lon );
  157. if ( this.constrainVertical ) {
  158. phi = MathUtils.mapLinear( phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  159. }
  160. const position = this.object.position;
  161. targetPosition.setFromSphericalCoords( 1, phi, theta ).add( position );
  162. this.object.lookAt( targetPosition );
  163. };
  164. }();
  165. this.dispose = function () {
  166. this.domElement.removeEventListener( 'contextmenu', contextmenu );
  167. this.domElement.removeEventListener( 'mousedown', _onMouseDown );
  168. this.domElement.removeEventListener( 'mousemove', _onMouseMove );
  169. this.domElement.removeEventListener( 'mouseup', _onMouseUp );
  170. window.removeEventListener( 'keydown', _onKeyDown );
  171. window.removeEventListener( 'keyup', _onKeyUp );
  172. };
  173. const _onMouseMove = this.onMouseMove.bind( this );
  174. const _onMouseDown = this.onMouseDown.bind( this );
  175. const _onMouseUp = this.onMouseUp.bind( this );
  176. const _onKeyDown = this.onKeyDown.bind( this );
  177. const _onKeyUp = this.onKeyUp.bind( this );
  178. this.domElement.addEventListener( 'contextmenu', contextmenu );
  179. this.domElement.addEventListener( 'mousemove', _onMouseMove );
  180. this.domElement.addEventListener( 'mousedown', _onMouseDown );
  181. this.domElement.addEventListener( 'mouseup', _onMouseUp );
  182. window.addEventListener( 'keydown', _onKeyDown );
  183. window.addEventListener( 'keyup', _onKeyUp );
  184. function setOrientation( controls ) {
  185. const quaternion = controls.object.quaternion;
  186. _lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  187. _spherical.setFromVector3( _lookDirection );
  188. lat = 90 - MathUtils.radToDeg( _spherical.phi );
  189. lon = MathUtils.radToDeg( _spherical.theta );
  190. }
  191. this.handleResize();
  192. setOrientation( this );
  193. }
  194. }
  195. function contextmenu( event ) {
  196. event.preventDefault();
  197. }
  198. export { FirstPersonControls };