FirstPersonControls.js 6.5 KB

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