FirstPersonControls.js 9.0 KB

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