FirstPersonControls.js 7.2 KB

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