FlyControls.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import {
  2. Controls,
  3. Quaternion,
  4. Vector3
  5. } from 'three';
  6. const _changeEvent = { type: 'change' };
  7. const _EPS = 0.000001;
  8. const _tmpQuaternion = new Quaternion();
  9. class FlyControls extends Controls {
  10. constructor( object, domElement = null ) {
  11. super( object, domElement );
  12. this.movementSpeed = 1.0;
  13. this.rollSpeed = 0.005;
  14. this.dragToLook = false;
  15. this.autoForward = false;
  16. // internals
  17. this._moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
  18. this._moveVector = new Vector3( 0, 0, 0 );
  19. this._rotationVector = new Vector3( 0, 0, 0 );
  20. this._lastQuaternion = new Quaternion();
  21. this._lastPosition = new Vector3();
  22. this._status = 0;
  23. // event listeners
  24. this._onKeyDown = onKeyDown.bind( this );
  25. this._onKeyUp = onKeyUp.bind( this );
  26. this._onPointerMove = onPointerMove.bind( this );
  27. this._onPointerDown = onPointerDown.bind( this );
  28. this._onPointerUp = onPointerUp.bind( this );
  29. this._onPointerCancel = onPointerCancel.bind( this );
  30. this._onContextMenu = onContextMenu.bind( this );
  31. //
  32. if ( domElement !== null ) {
  33. this.connect();
  34. }
  35. }
  36. connect() {
  37. window.addEventListener( 'keydown', this._onKeyDown );
  38. window.addEventListener( 'keyup', this._onKeyUp );
  39. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  40. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  41. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  42. this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
  43. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  44. }
  45. disconnect() {
  46. window.removeEventListener( 'keydown', this._onKeyDown );
  47. window.removeEventListener( 'keyup', this._onKeyUp );
  48. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  49. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  50. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  51. this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
  52. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  53. }
  54. dispose() {
  55. this.disconnect();
  56. }
  57. update( delta ) {
  58. if ( this.enabled === false ) return;
  59. const object = this.object;
  60. const moveMult = delta * this.movementSpeed;
  61. const rotMult = delta * this.rollSpeed;
  62. object.translateX( this._moveVector.x * moveMult );
  63. object.translateY( this._moveVector.y * moveMult );
  64. object.translateZ( this._moveVector.z * moveMult );
  65. _tmpQuaternion.set( this._rotationVector.x * rotMult, this._rotationVector.y * rotMult, this._rotationVector.z * rotMult, 1 ).normalize();
  66. object.quaternion.multiply( _tmpQuaternion );
  67. if (
  68. this._lastPosition.distanceToSquared( object.position ) > _EPS ||
  69. 8 * ( 1 - this._lastQuaternion.dot( object.quaternion ) ) > _EPS
  70. ) {
  71. this.dispatchEvent( _changeEvent );
  72. this._lastQuaternion.copy( object.quaternion );
  73. this._lastPosition.copy( object.position );
  74. }
  75. }
  76. // private
  77. _updateMovementVector() {
  78. const forward = ( this._moveState.forward || ( this.autoForward && ! this._moveState.back ) ) ? 1 : 0;
  79. this._moveVector.x = ( - this._moveState.left + this._moveState.right );
  80. this._moveVector.y = ( - this._moveState.down + this._moveState.up );
  81. this._moveVector.z = ( - forward + this._moveState.back );
  82. //console.log( 'move:', [ this._moveVector.x, this._moveVector.y, this._moveVector.z ] );
  83. }
  84. _updateRotationVector() {
  85. this._rotationVector.x = ( - this._moveState.pitchDown + this._moveState.pitchUp );
  86. this._rotationVector.y = ( - this._moveState.yawRight + this._moveState.yawLeft );
  87. this._rotationVector.z = ( - this._moveState.rollRight + this._moveState.rollLeft );
  88. //console.log( 'rotate:', [ this._rotationVector.x, this._rotationVector.y, this._rotationVector.z ] );
  89. }
  90. _getContainerDimensions() {
  91. if ( this.domElement != document ) {
  92. return {
  93. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  94. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  95. };
  96. } else {
  97. return {
  98. size: [ window.innerWidth, window.innerHeight ],
  99. offset: [ 0, 0 ]
  100. };
  101. }
  102. }
  103. }
  104. function onKeyDown( event ) {
  105. if ( event.altKey || this.enabled === false ) {
  106. return;
  107. }
  108. switch ( event.code ) {
  109. case 'ShiftLeft':
  110. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  111. case 'KeyW': this._moveState.forward = 1; break;
  112. case 'KeyS': this._moveState.back = 1; break;
  113. case 'KeyA': this._moveState.left = 1; break;
  114. case 'KeyD': this._moveState.right = 1; break;
  115. case 'KeyR': this._moveState.up = 1; break;
  116. case 'KeyF': this._moveState.down = 1; break;
  117. case 'ArrowUp': this._moveState.pitchUp = 1; break;
  118. case 'ArrowDown': this._moveState.pitchDown = 1; break;
  119. case 'ArrowLeft': this._moveState.yawLeft = 1; break;
  120. case 'ArrowRight': this._moveState.yawRight = 1; break;
  121. case 'KeyQ': this._moveState.rollLeft = 1; break;
  122. case 'KeyE': this._moveState.rollRight = 1; break;
  123. }
  124. this._updateMovementVector();
  125. this._updateRotationVector();
  126. }
  127. function onKeyUp( event ) {
  128. if ( this.enabled === false ) return;
  129. switch ( event.code ) {
  130. case 'ShiftLeft':
  131. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  132. case 'KeyW': this._moveState.forward = 0; break;
  133. case 'KeyS': this._moveState.back = 0; break;
  134. case 'KeyA': this._moveState.left = 0; break;
  135. case 'KeyD': this._moveState.right = 0; break;
  136. case 'KeyR': this._moveState.up = 0; break;
  137. case 'KeyF': this._moveState.down = 0; break;
  138. case 'ArrowUp': this._moveState.pitchUp = 0; break;
  139. case 'ArrowDown': this._moveState.pitchDown = 0; break;
  140. case 'ArrowLeft': this._moveState.yawLeft = 0; break;
  141. case 'ArrowRight': this._moveState.yawRight = 0; break;
  142. case 'KeyQ': this._moveState.rollLeft = 0; break;
  143. case 'KeyE': this._moveState.rollRight = 0; break;
  144. }
  145. this._updateMovementVector();
  146. this._updateRotationVector();
  147. }
  148. function onPointerDown( event ) {
  149. if ( this.enabled === false ) return;
  150. if ( this.dragToLook ) {
  151. this._status ++;
  152. } else {
  153. switch ( event.button ) {
  154. case 0: this._moveState.forward = 1; break;
  155. case 2: this._moveState.back = 1; break;
  156. }
  157. this._updateMovementVector();
  158. }
  159. }
  160. function onPointerMove( event ) {
  161. if ( this.enabled === false ) return;
  162. if ( ! this.dragToLook || this._status > 0 ) {
  163. const container = this._getContainerDimensions();
  164. const halfWidth = container.size[ 0 ] / 2;
  165. const halfHeight = container.size[ 1 ] / 2;
  166. this._moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  167. this._moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  168. this._updateRotationVector();
  169. }
  170. }
  171. function onPointerUp( event ) {
  172. if ( this.enabled === false ) return;
  173. if ( this.dragToLook ) {
  174. this._status --;
  175. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  176. } else {
  177. switch ( event.button ) {
  178. case 0: this._moveState.forward = 0; break;
  179. case 2: this._moveState.back = 0; break;
  180. }
  181. this._updateMovementVector();
  182. }
  183. this._updateRotationVector();
  184. }
  185. function onPointerCancel() {
  186. if ( this.enabled === false ) return;
  187. if ( this.dragToLook ) {
  188. this._status = 0;
  189. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  190. } else {
  191. this._moveState.forward = 0;
  192. this._moveState.back = 0;
  193. this._updateMovementVector();
  194. }
  195. this._updateRotationVector();
  196. }
  197. function onContextMenu( event ) {
  198. if ( this.enabled === false ) return;
  199. event.preventDefault();
  200. }
  201. export { FlyControls };