1
0

FlyControls.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import {
  2. Controls,
  3. Quaternion,
  4. Vector3
  5. } from 'three';
  6. /**
  7. * Fires when the camera has been transformed by the controls.
  8. *
  9. * @event FlyControls#change
  10. * @type {Object}
  11. */
  12. const _changeEvent = { type: 'change' };
  13. const _EPS = 0.000001;
  14. const _tmpQuaternion = new Quaternion();
  15. /**
  16. * This class enables a navigation similar to fly modes in DCC tools like Blender.
  17. * You can arbitrarily transform the camera in 3D space without any limitations
  18. * (e.g. focus on a specific target).
  19. *
  20. * @augments Controls
  21. */
  22. class FlyControls extends Controls {
  23. /**
  24. * Constructs a new controls instance.
  25. *
  26. * @param {Object3D} object - The object that is managed by the controls.
  27. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  28. */
  29. constructor( object, domElement = null ) {
  30. super( object, domElement );
  31. /**
  32. * The movement speed.
  33. *
  34. * @type {number}
  35. * @default 1
  36. */
  37. this.movementSpeed = 1.0;
  38. /**
  39. * The rotation speed.
  40. *
  41. * @type {number}
  42. * @default 0.005
  43. */
  44. this.rollSpeed = 0.005;
  45. /**
  46. * If set to `true`, you can only look around by performing a drag interaction.
  47. *
  48. * @type {boolean}
  49. * @default false
  50. */
  51. this.dragToLook = false;
  52. /**
  53. * If set to `true`, the camera automatically moves forward (and does not stop) when initially translated.
  54. *
  55. * @type {boolean}
  56. * @default false
  57. */
  58. this.autoForward = false;
  59. // internals
  60. 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 };
  61. this._moveVector = new Vector3( 0, 0, 0 );
  62. this._rotationVector = new Vector3( 0, 0, 0 );
  63. this._lastQuaternion = new Quaternion();
  64. this._lastPosition = new Vector3();
  65. this._status = 0;
  66. // event listeners
  67. this._onKeyDown = onKeyDown.bind( this );
  68. this._onKeyUp = onKeyUp.bind( this );
  69. this._onPointerMove = onPointerMove.bind( this );
  70. this._onPointerDown = onPointerDown.bind( this );
  71. this._onPointerUp = onPointerUp.bind( this );
  72. this._onPointerCancel = onPointerCancel.bind( this );
  73. this._onContextMenu = onContextMenu.bind( this );
  74. //
  75. if ( domElement !== null ) {
  76. this.connect( domElement );
  77. }
  78. }
  79. connect( element ) {
  80. super.connect( element );
  81. window.addEventListener( 'keydown', this._onKeyDown );
  82. window.addEventListener( 'keyup', this._onKeyUp );
  83. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  84. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  85. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  86. this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
  87. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  88. }
  89. disconnect() {
  90. window.removeEventListener( 'keydown', this._onKeyDown );
  91. window.removeEventListener( 'keyup', this._onKeyUp );
  92. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  93. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  94. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  95. this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
  96. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  97. }
  98. dispose() {
  99. this.disconnect();
  100. }
  101. update( delta ) {
  102. if ( this.enabled === false ) return;
  103. const object = this.object;
  104. const moveMult = delta * this.movementSpeed;
  105. const rotMult = delta * this.rollSpeed;
  106. object.translateX( this._moveVector.x * moveMult );
  107. object.translateY( this._moveVector.y * moveMult );
  108. object.translateZ( this._moveVector.z * moveMult );
  109. _tmpQuaternion.set( this._rotationVector.x * rotMult, this._rotationVector.y * rotMult, this._rotationVector.z * rotMult, 1 ).normalize();
  110. object.quaternion.multiply( _tmpQuaternion );
  111. if (
  112. this._lastPosition.distanceToSquared( object.position ) > _EPS ||
  113. 8 * ( 1 - this._lastQuaternion.dot( object.quaternion ) ) > _EPS
  114. ) {
  115. this.dispatchEvent( _changeEvent );
  116. this._lastQuaternion.copy( object.quaternion );
  117. this._lastPosition.copy( object.position );
  118. }
  119. }
  120. // private
  121. _updateMovementVector() {
  122. const forward = ( this._moveState.forward || ( this.autoForward && ! this._moveState.back ) ) ? 1 : 0;
  123. this._moveVector.x = ( - this._moveState.left + this._moveState.right );
  124. this._moveVector.y = ( - this._moveState.down + this._moveState.up );
  125. this._moveVector.z = ( - forward + this._moveState.back );
  126. //console.log( 'move:', [ this._moveVector.x, this._moveVector.y, this._moveVector.z ] );
  127. }
  128. _updateRotationVector() {
  129. this._rotationVector.x = ( - this._moveState.pitchDown + this._moveState.pitchUp );
  130. this._rotationVector.y = ( - this._moveState.yawRight + this._moveState.yawLeft );
  131. this._rotationVector.z = ( - this._moveState.rollRight + this._moveState.rollLeft );
  132. //console.log( 'rotate:', [ this._rotationVector.x, this._rotationVector.y, this._rotationVector.z ] );
  133. }
  134. _getContainerDimensions() {
  135. if ( this.domElement != document ) {
  136. return {
  137. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  138. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  139. };
  140. } else {
  141. return {
  142. size: [ window.innerWidth, window.innerHeight ],
  143. offset: [ 0, 0 ]
  144. };
  145. }
  146. }
  147. }
  148. function onKeyDown( event ) {
  149. if ( event.altKey || this.enabled === false ) {
  150. return;
  151. }
  152. switch ( event.code ) {
  153. case 'ShiftLeft':
  154. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  155. case 'KeyW': this._moveState.forward = 1; break;
  156. case 'KeyS': this._moveState.back = 1; break;
  157. case 'KeyA': this._moveState.left = 1; break;
  158. case 'KeyD': this._moveState.right = 1; break;
  159. case 'KeyR': this._moveState.up = 1; break;
  160. case 'KeyF': this._moveState.down = 1; break;
  161. case 'ArrowUp': this._moveState.pitchUp = 1; break;
  162. case 'ArrowDown': this._moveState.pitchDown = 1; break;
  163. case 'ArrowLeft': this._moveState.yawLeft = 1; break;
  164. case 'ArrowRight': this._moveState.yawRight = 1; break;
  165. case 'KeyQ': this._moveState.rollLeft = 1; break;
  166. case 'KeyE': this._moveState.rollRight = 1; break;
  167. }
  168. this._updateMovementVector();
  169. this._updateRotationVector();
  170. }
  171. function onKeyUp( event ) {
  172. if ( this.enabled === false ) return;
  173. switch ( event.code ) {
  174. case 'ShiftLeft':
  175. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  176. case 'KeyW': this._moveState.forward = 0; break;
  177. case 'KeyS': this._moveState.back = 0; break;
  178. case 'KeyA': this._moveState.left = 0; break;
  179. case 'KeyD': this._moveState.right = 0; break;
  180. case 'KeyR': this._moveState.up = 0; break;
  181. case 'KeyF': this._moveState.down = 0; break;
  182. case 'ArrowUp': this._moveState.pitchUp = 0; break;
  183. case 'ArrowDown': this._moveState.pitchDown = 0; break;
  184. case 'ArrowLeft': this._moveState.yawLeft = 0; break;
  185. case 'ArrowRight': this._moveState.yawRight = 0; break;
  186. case 'KeyQ': this._moveState.rollLeft = 0; break;
  187. case 'KeyE': this._moveState.rollRight = 0; break;
  188. }
  189. this._updateMovementVector();
  190. this._updateRotationVector();
  191. }
  192. function onPointerDown( event ) {
  193. if ( this.enabled === false ) return;
  194. if ( this.dragToLook ) {
  195. this._status ++;
  196. } else {
  197. switch ( event.button ) {
  198. case 0: this._moveState.forward = 1; break;
  199. case 2: this._moveState.back = 1; break;
  200. }
  201. this._updateMovementVector();
  202. }
  203. }
  204. function onPointerMove( event ) {
  205. if ( this.enabled === false ) return;
  206. if ( ! this.dragToLook || this._status > 0 ) {
  207. const container = this._getContainerDimensions();
  208. const halfWidth = container.size[ 0 ] / 2;
  209. const halfHeight = container.size[ 1 ] / 2;
  210. this._moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  211. this._moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  212. this._updateRotationVector();
  213. }
  214. }
  215. function onPointerUp( event ) {
  216. if ( this.enabled === false ) return;
  217. if ( this.dragToLook ) {
  218. this._status --;
  219. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  220. } else {
  221. switch ( event.button ) {
  222. case 0: this._moveState.forward = 0; break;
  223. case 2: this._moveState.back = 0; break;
  224. }
  225. this._updateMovementVector();
  226. }
  227. this._updateRotationVector();
  228. }
  229. function onPointerCancel() {
  230. if ( this.enabled === false ) return;
  231. if ( this.dragToLook ) {
  232. this._status = 0;
  233. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  234. } else {
  235. this._moveState.forward = 0;
  236. this._moveState.back = 0;
  237. this._updateMovementVector();
  238. }
  239. this._updateRotationVector();
  240. }
  241. function onContextMenu( event ) {
  242. if ( this.enabled === false ) return;
  243. event.preventDefault();
  244. }
  245. export { FlyControls };
粤ICP备19079148号