FirstPersonControls.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import {
  2. Controls,
  3. MathUtils,
  4. Spherical,
  5. Vector3
  6. } from 'three';
  7. const _lookDirection = new Vector3();
  8. const _spherical = new Spherical();
  9. const _target = new Vector3();
  10. const _targetPosition = new Vector3();
  11. class FirstPersonControls extends Controls {
  12. constructor( object, domElement = null ) {
  13. super( object, domElement );
  14. // API
  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._pointerX = 0;
  31. this._pointerY = 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. this._lat = 0;
  39. this._lon = 0;
  40. // event listeners
  41. this._onPointerMove = onPointerMove.bind( this );
  42. this._onPointerDown = onPointerDown.bind( this );
  43. this._onPointerUp = onPointerUp.bind( this );
  44. this._onContextMenu = onContextMenu.bind( this );
  45. this._onKeyDown = onKeyDown.bind( this );
  46. this._onKeyUp = onKeyUp.bind( this );
  47. //
  48. if ( domElement !== null ) {
  49. this.connect();
  50. this.handleResize();
  51. }
  52. this._setOrientation();
  53. }
  54. connect() {
  55. window.addEventListener( 'keydown', this._onKeyDown );
  56. window.addEventListener( 'keyup', this._onKeyUp );
  57. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  58. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  59. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  60. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  61. }
  62. disconnect() {
  63. window.removeEventListener( 'keydown', this._onKeyDown );
  64. window.removeEventListener( 'keyup', this._onKeyUp );
  65. this.domElement.removeEventListener( 'pointerdown', this._onPointerMove );
  66. this.domElement.removeEventListener( 'pointermove', this._onPointerDown );
  67. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  68. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  69. }
  70. dispose() {
  71. this.disconnect();
  72. }
  73. handleResize() {
  74. if ( this.domElement === document ) {
  75. this._viewHalfX = window.innerWidth / 2;
  76. this._viewHalfY = window.innerHeight / 2;
  77. } else {
  78. this._viewHalfX = this.domElement.offsetWidth / 2;
  79. this._viewHalfY = this.domElement.offsetHeight / 2;
  80. }
  81. }
  82. lookAt( x, y, z ) {
  83. if ( x.isVector3 ) {
  84. _target.copy( x );
  85. } else {
  86. _target.set( x, y, z );
  87. }
  88. this.object.lookAt( _target );
  89. this._setOrientation();
  90. return this;
  91. }
  92. update( delta ) {
  93. if ( this.enabled === false ) return;
  94. if ( this.heightSpeed ) {
  95. const y = MathUtils.clamp( this.object.position.y, this.heightMin, this.heightMax );
  96. const heightDelta = y - this.heightMin;
  97. this._autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  98. } else {
  99. this._autoSpeedFactor = 0.0;
  100. }
  101. const actualMoveSpeed = delta * this.movementSpeed;
  102. if ( this._moveForward || ( this.autoForward && ! this._moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this._autoSpeedFactor ) );
  103. if ( this._moveBackward ) this.object.translateZ( actualMoveSpeed );
  104. if ( this._moveLeft ) this.object.translateX( - actualMoveSpeed );
  105. if ( this._moveRight ) this.object.translateX( actualMoveSpeed );
  106. if ( this._moveUp ) this.object.translateY( actualMoveSpeed );
  107. if ( this._moveDown ) this.object.translateY( - actualMoveSpeed );
  108. let actualLookSpeed = delta * this.lookSpeed;
  109. if ( ! this.activeLook ) {
  110. actualLookSpeed = 0;
  111. }
  112. let verticalLookRatio = 1;
  113. if ( this.constrainVertical ) {
  114. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  115. }
  116. this._lon -= this._pointerX * actualLookSpeed;
  117. if ( this.lookVertical ) this._lat -= this._pointerY * actualLookSpeed * verticalLookRatio;
  118. this._lat = Math.max( - 85, Math.min( 85, this._lat ) );
  119. let phi = MathUtils.degToRad( 90 - this._lat );
  120. const theta = MathUtils.degToRad( this._lon );
  121. if ( this.constrainVertical ) {
  122. phi = MathUtils.mapLinear( phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  123. }
  124. const position = this.object.position;
  125. _targetPosition.setFromSphericalCoords( 1, phi, theta ).add( position );
  126. this.object.lookAt( _targetPosition );
  127. }
  128. _setOrientation() {
  129. const quaternion = this.object.quaternion;
  130. _lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  131. _spherical.setFromVector3( _lookDirection );
  132. this._lat = 90 - MathUtils.radToDeg( _spherical.phi );
  133. this._lon = MathUtils.radToDeg( _spherical.theta );
  134. }
  135. }
  136. function onPointerDown( event ) {
  137. if ( this.domElement !== document ) {
  138. this.domElement.focus();
  139. }
  140. if ( this.activeLook ) {
  141. switch ( event.button ) {
  142. case 0: this._moveForward = true; break;
  143. case 2: this._moveBackward = true; break;
  144. }
  145. }
  146. this.mouseDragOn = true;
  147. }
  148. function onPointerUp( event ) {
  149. if ( this.activeLook ) {
  150. switch ( event.button ) {
  151. case 0: this._moveForward = false; break;
  152. case 2: this._moveBackward = false; break;
  153. }
  154. }
  155. this.mouseDragOn = false;
  156. }
  157. function onPointerMove( event ) {
  158. if ( this.domElement === document ) {
  159. this._pointerX = event.pageX - this._viewHalfX;
  160. this._pointerY = event.pageY - this._viewHalfY;
  161. } else {
  162. this._pointerX = event.pageX - this.domElement.offsetLeft - this._viewHalfX;
  163. this._pointerY = event.pageY - this.domElement.offsetTop - this._viewHalfY;
  164. }
  165. }
  166. function onKeyDown( event ) {
  167. switch ( event.code ) {
  168. case 'ArrowUp':
  169. case 'KeyW': this._moveForward = true; break;
  170. case 'ArrowLeft':
  171. case 'KeyA': this._moveLeft = true; break;
  172. case 'ArrowDown':
  173. case 'KeyS': this._moveBackward = true; break;
  174. case 'ArrowRight':
  175. case 'KeyD': this._moveRight = true; break;
  176. case 'KeyR': this._moveUp = true; break;
  177. case 'KeyF': this._moveDown = true; break;
  178. }
  179. }
  180. function onKeyUp( event ) {
  181. switch ( event.code ) {
  182. case 'ArrowUp':
  183. case 'KeyW': this._moveForward = false; break;
  184. case 'ArrowLeft':
  185. case 'KeyA': this._moveLeft = false; break;
  186. case 'ArrowDown':
  187. case 'KeyS': this._moveBackward = false; break;
  188. case 'ArrowRight':
  189. case 'KeyD': this._moveRight = false; break;
  190. case 'KeyR': this._moveUp = false; break;
  191. case 'KeyF': this._moveDown = false; break;
  192. }
  193. }
  194. function onContextMenu( event ) {
  195. if ( this.enabled === false ) return;
  196. event.preventDefault();
  197. }
  198. export { FirstPersonControls };
粤ICP备19079148号