FirstPersonControls.js 6.9 KB

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