FlyControls.js 7.8 KB

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