FirstPersonControls.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. /**
  12. * This class is an alternative implementation of {@link FlyControls}.
  13. *
  14. * @augments Controls
  15. * @three_import import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  16. */
  17. class FirstPersonControls extends Controls {
  18. /**
  19. * Constructs a new controls instance.
  20. *
  21. * @param {Object3D} object - The object that is managed by the controls.
  22. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  23. */
  24. constructor( object, domElement = null ) {
  25. super( object, domElement );
  26. /**
  27. * The movement speed.
  28. *
  29. * @type {number}
  30. * @default 1
  31. */
  32. this.movementSpeed = 1.0;
  33. /**
  34. * The look around speed.
  35. *
  36. * @type {number}
  37. * @default 0.005
  38. */
  39. this.lookSpeed = 0.005;
  40. /**
  41. * Whether it's possible to vertically look around or not.
  42. *
  43. * @type {boolean}
  44. * @default true
  45. */
  46. this.lookVertical = true;
  47. /**
  48. * Whether the camera is automatically moved forward or not.
  49. *
  50. * @type {boolean}
  51. * @default false
  52. */
  53. this.autoForward = false;
  54. /**
  55. * Whether it's possible to look around or not.
  56. *
  57. * @type {boolean}
  58. * @default true
  59. */
  60. this.activeLook = true;
  61. /**
  62. * Whether or not the camera's height influences the forward movement speed.
  63. * Use the properties `heightCoef`, `heightMin` and `heightMax` for configuration.
  64. *
  65. * @type {boolean}
  66. * @default false
  67. */
  68. this.heightSpeed = false;
  69. /**
  70. * Determines how much faster the camera moves when it's y-component is near `heightMax`.
  71. *
  72. * @type {number}
  73. * @default 1
  74. */
  75. this.heightCoef = 1.0;
  76. /**
  77. * Lower camera height limit used for movement speed adjustment.
  78. *
  79. * @type {number}
  80. * @default 0
  81. */
  82. this.heightMin = 0.0;
  83. /**
  84. * Upper camera height limit used for movement speed adjustment.
  85. *
  86. * @type {number}
  87. * @default 1
  88. */
  89. this.heightMax = 1.0;
  90. /**
  91. * Whether or not looking around is vertically constrained by `verticalMin` and `verticalMax`.
  92. *
  93. * @type {boolean}
  94. * @default false
  95. */
  96. this.constrainVertical = false;
  97. /**
  98. * How far you can vertically look around, lower limit. Range is `0` to `Math.PI` in radians.
  99. *
  100. * @type {number}
  101. * @default 0
  102. */
  103. this.verticalMin = 0;
  104. /**
  105. * How far you can vertically look around, upper limit. Range is `0` to `Math.PI` in radians.
  106. *
  107. * @type {number}
  108. * @default 0
  109. */
  110. this.verticalMax = Math.PI;
  111. /**
  112. * Whether the mouse is pressed down or not.
  113. *
  114. * @type {boolean}
  115. * @readonly
  116. * @default false
  117. */
  118. this.mouseDragOn = false;
  119. // internals
  120. this._autoSpeedFactor = 0.0;
  121. this._pointerX = 0;
  122. this._pointerY = 0;
  123. this._moveForward = false;
  124. this._moveBackward = false;
  125. this._moveLeft = false;
  126. this._moveRight = false;
  127. this._viewHalfX = 0;
  128. this._viewHalfY = 0;
  129. this._lat = 0;
  130. this._lon = 0;
  131. // event listeners
  132. this._onPointerMove = onPointerMove.bind( this );
  133. this._onPointerDown = onPointerDown.bind( this );
  134. this._onPointerUp = onPointerUp.bind( this );
  135. this._onContextMenu = onContextMenu.bind( this );
  136. this._onKeyDown = onKeyDown.bind( this );
  137. this._onKeyUp = onKeyUp.bind( this );
  138. //
  139. if ( domElement !== null ) {
  140. this.connect( domElement );
  141. this.handleResize();
  142. }
  143. this._setOrientation();
  144. }
  145. connect( element ) {
  146. super.connect( element );
  147. window.addEventListener( 'keydown', this._onKeyDown );
  148. window.addEventListener( 'keyup', this._onKeyUp );
  149. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  150. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  151. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  152. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  153. }
  154. disconnect() {
  155. window.removeEventListener( 'keydown', this._onKeyDown );
  156. window.removeEventListener( 'keyup', this._onKeyUp );
  157. this.domElement.removeEventListener( 'pointerdown', this._onPointerMove );
  158. this.domElement.removeEventListener( 'pointermove', this._onPointerDown );
  159. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  160. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  161. }
  162. dispose() {
  163. this.disconnect();
  164. }
  165. /**
  166. * Must be called if the application window is resized.
  167. */
  168. handleResize() {
  169. if ( this.domElement === document ) {
  170. this._viewHalfX = window.innerWidth / 2;
  171. this._viewHalfY = window.innerHeight / 2;
  172. } else {
  173. this._viewHalfX = this.domElement.offsetWidth / 2;
  174. this._viewHalfY = this.domElement.offsetHeight / 2;
  175. }
  176. }
  177. /**
  178. * Rotates the camera towards the defined target position.
  179. *
  180. * @param {number|Vector3} x - The x coordinate of the target position or alternatively a vector representing the target position.
  181. * @param {number} y - The y coordinate of the target position.
  182. * @param {number} z - The z coordinate of the target position.
  183. * @return {FirstPersonControls} A reference to this controls.
  184. */
  185. lookAt( x, y, z ) {
  186. if ( x.isVector3 ) {
  187. _target.copy( x );
  188. } else {
  189. _target.set( x, y, z );
  190. }
  191. this.object.lookAt( _target );
  192. this._setOrientation();
  193. return this;
  194. }
  195. update( delta ) {
  196. if ( this.enabled === false ) return;
  197. if ( this.heightSpeed ) {
  198. const y = MathUtils.clamp( this.object.position.y, this.heightMin, this.heightMax );
  199. const heightDelta = y - this.heightMin;
  200. this._autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  201. } else {
  202. this._autoSpeedFactor = 0.0;
  203. }
  204. const actualMoveSpeed = delta * this.movementSpeed;
  205. if ( this._moveForward || ( this.autoForward && ! this._moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this._autoSpeedFactor ) );
  206. if ( this._moveBackward ) this.object.translateZ( actualMoveSpeed );
  207. if ( this._moveLeft ) this.object.translateX( - actualMoveSpeed );
  208. if ( this._moveRight ) this.object.translateX( actualMoveSpeed );
  209. if ( this._moveUp ) this.object.translateY( actualMoveSpeed );
  210. if ( this._moveDown ) this.object.translateY( - actualMoveSpeed );
  211. let actualLookSpeed = delta * this.lookSpeed;
  212. if ( ! this.activeLook ) {
  213. actualLookSpeed = 0;
  214. }
  215. let verticalLookRatio = 1;
  216. if ( this.constrainVertical ) {
  217. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  218. }
  219. this._lon -= this._pointerX * actualLookSpeed;
  220. if ( this.lookVertical ) this._lat -= this._pointerY * actualLookSpeed * verticalLookRatio;
  221. this._lat = Math.max( - 85, Math.min( 85, this._lat ) );
  222. let phi = MathUtils.degToRad( 90 - this._lat );
  223. const theta = MathUtils.degToRad( this._lon );
  224. if ( this.constrainVertical ) {
  225. phi = MathUtils.mapLinear( phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  226. }
  227. const position = this.object.position;
  228. _targetPosition.setFromSphericalCoords( 1, phi, theta ).add( position );
  229. this.object.lookAt( _targetPosition );
  230. }
  231. _setOrientation() {
  232. const quaternion = this.object.quaternion;
  233. _lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  234. _spherical.setFromVector3( _lookDirection );
  235. this._lat = 90 - MathUtils.radToDeg( _spherical.phi );
  236. this._lon = MathUtils.radToDeg( _spherical.theta );
  237. }
  238. }
  239. function onPointerDown( event ) {
  240. if ( this.domElement !== document ) {
  241. this.domElement.focus();
  242. }
  243. if ( this.activeLook ) {
  244. switch ( event.button ) {
  245. case 0: this._moveForward = true; break;
  246. case 2: this._moveBackward = true; break;
  247. }
  248. }
  249. this.mouseDragOn = true;
  250. }
  251. function onPointerUp( event ) {
  252. if ( this.activeLook ) {
  253. switch ( event.button ) {
  254. case 0: this._moveForward = false; break;
  255. case 2: this._moveBackward = false; break;
  256. }
  257. }
  258. this.mouseDragOn = false;
  259. }
  260. function onPointerMove( event ) {
  261. if ( this.domElement === document ) {
  262. this._pointerX = event.pageX - this._viewHalfX;
  263. this._pointerY = event.pageY - this._viewHalfY;
  264. } else {
  265. this._pointerX = event.pageX - this.domElement.offsetLeft - this._viewHalfX;
  266. this._pointerY = event.pageY - this.domElement.offsetTop - this._viewHalfY;
  267. }
  268. }
  269. function onKeyDown( event ) {
  270. switch ( event.code ) {
  271. case 'ArrowUp':
  272. case 'KeyW': this._moveForward = true; break;
  273. case 'ArrowLeft':
  274. case 'KeyA': this._moveLeft = true; break;
  275. case 'ArrowDown':
  276. case 'KeyS': this._moveBackward = true; break;
  277. case 'ArrowRight':
  278. case 'KeyD': this._moveRight = true; break;
  279. case 'KeyR': this._moveUp = true; break;
  280. case 'KeyF': this._moveDown = true; break;
  281. }
  282. }
  283. function onKeyUp( event ) {
  284. switch ( event.code ) {
  285. case 'ArrowUp':
  286. case 'KeyW': this._moveForward = false; break;
  287. case 'ArrowLeft':
  288. case 'KeyA': this._moveLeft = false; break;
  289. case 'ArrowDown':
  290. case 'KeyS': this._moveBackward = false; break;
  291. case 'ArrowRight':
  292. case 'KeyD': this._moveRight = false; break;
  293. case 'KeyR': this._moveUp = false; break;
  294. case 'KeyF': this._moveDown = false; break;
  295. }
  296. }
  297. function onContextMenu( event ) {
  298. if ( this.enabled === false ) return;
  299. event.preventDefault();
  300. }
  301. export { FirstPersonControls };