FirstPersonControls.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author paulirish / http://paulirish.com/
  5. */
  6. THREE.FirstPersonControls = function ( object, domElement ) {
  7. this.object = object;
  8. this.target = new THREE.Vector3( 0, 0, 0 );
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. this.movementSpeed = 1.0;
  11. this.lookSpeed = 0.005;
  12. this.noFly = false;
  13. this.lookVertical = true;
  14. this.autoForward = false;
  15. this.activeLook = true;
  16. this.heightSpeed = false;
  17. this.heightCoef = 1.0;
  18. this.heightMin = 0.0;
  19. this.constrainVertical = false;
  20. this.verticalMin = 0;
  21. this.verticalMax = Math.PI;
  22. this.lastUpdate = new Date().getTime();
  23. this.tdiff = 0;
  24. this.autoSpeedFactor = 0.0;
  25. this.mouseX = 0;
  26. this.mouseY = 0;
  27. this.lat = 0;
  28. this.lon = 0;
  29. this.phi = 0;
  30. this.theta = 0;
  31. this.moveForward = false;
  32. this.moveBackward = false;
  33. this.moveLeft = false;
  34. this.moveRight = false;
  35. this.freeze = false;
  36. this.mouseDragOn = false;
  37. if ( this.domElement === document ) {
  38. this.viewHalfX = window.innerWidth / 2;
  39. this.viewHalfY = window.innerHeight / 2;
  40. } else {
  41. this.viewHalfX = this.domElement.offsetWidth / 2;
  42. this.viewHalfY = this.domElement.offsetHeight / 2;
  43. this.domElement.setAttribute( 'tabindex', -1 );
  44. }
  45. this.onMouseDown = function ( event ) {
  46. if ( this.domElement !== document ) {
  47. this.domElement.focus();
  48. }
  49. event.preventDefault();
  50. event.stopPropagation();
  51. if ( this.activeLook ) {
  52. switch ( event.button ) {
  53. case 0: this.moveForward = true; break;
  54. case 2: this.moveBackward = true; break;
  55. }
  56. }
  57. this.mouseDragOn = true;
  58. };
  59. this.onMouseUp = function ( event ) {
  60. event.preventDefault();
  61. event.stopPropagation();
  62. if ( this.activeLook ) {
  63. switch ( event.button ) {
  64. case 0: this.moveForward = false; break;
  65. case 2: this.moveBackward = false; break;
  66. }
  67. }
  68. this.mouseDragOn = false;
  69. };
  70. this.onMouseMove = function ( event ) {
  71. if ( this.domElement === document ) {
  72. this.mouseX = event.pageX - this.viewHalfX;
  73. this.mouseY = event.pageY - this.viewHalfY;
  74. } else {
  75. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  76. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  77. }
  78. };
  79. this.onKeyDown = function ( event ) {
  80. switch( event.keyCode ) {
  81. case 38: /*up*/
  82. case 87: /*W*/ this.moveForward = true; break;
  83. case 37: /*left*/
  84. case 65: /*A*/ this.moveLeft = true; break;
  85. case 40: /*down*/
  86. case 83: /*S*/ this.moveBackward = true; break;
  87. case 39: /*right*/
  88. case 68: /*D*/ this.moveRight = true; break;
  89. case 82: /*R*/ this.moveUp = true; break;
  90. case 70: /*F*/ this.moveDown = true; break;
  91. case 81: this.freeze = !this.freeze; break;
  92. }
  93. };
  94. this.onKeyUp = function ( event ) {
  95. switch( event.keyCode ) {
  96. case 38: /*up*/
  97. case 87: /*W*/ this.moveForward = false; break;
  98. case 37: /*left*/
  99. case 65: /*A*/ this.moveLeft = false; break;
  100. case 40: /*down*/
  101. case 83: /*S*/ this.moveBackward = false; break;
  102. case 39: /*right*/
  103. case 68: /*D*/ this.moveRight = false; break;
  104. case 82: /*R*/ this.moveUp = false; break;
  105. case 70: /*F*/ this.moveDown = false; break;
  106. }
  107. };
  108. this.update = function() {
  109. var now = new Date().getTime();
  110. this.tdiff = ( now - this.lastUpdate ) / 1000;
  111. this.lastUpdate = now;
  112. if ( !this.freeze ) {
  113. if ( this.heightSpeed ) {
  114. var y = clamp( this.object.position.y, this.heightMin, this.heightMax );
  115. var delta = y - this.heightMin;
  116. this.autoSpeedFactor = this.tdiff * ( delta * this.heightCoef );
  117. } else {
  118. this.autoSpeedFactor = 0.0;
  119. }
  120. var actualMoveSpeed = this.tdiff * this.movementSpeed;
  121. if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
  122. if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
  123. if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
  124. if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
  125. if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
  126. if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
  127. var actualLookSpeed = this.tdiff * this.lookSpeed;
  128. if ( !this.activeLook ) {
  129. actualLookSpeed = 0;
  130. }
  131. this.lon += this.mouseX * actualLookSpeed;
  132. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed;
  133. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  134. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  135. this.theta = this.lon * Math.PI / 180;
  136. var targetPosition = this.target,
  137. position = this.object.position;
  138. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  139. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  140. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  141. }
  142. var verticalLookRatio = 1;
  143. if ( this.constrainVertical ) {
  144. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  145. }
  146. this.lon += this.mouseX * actualLookSpeed;
  147. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
  148. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  149. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  150. this.theta = this.lon * Math.PI / 180;
  151. if ( this.constrainVertical ) {
  152. this.phi = map_linear( this.phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  153. }
  154. var targetPosition = this.target,
  155. position = this.object.position;
  156. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  157. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  158. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  159. this.object.lookAt( targetPosition );
  160. };
  161. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  162. this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
  163. this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
  164. this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
  165. this.domElement.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
  166. this.domElement.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
  167. function bind( scope, fn ) {
  168. return function () {
  169. fn.apply( scope, arguments );
  170. };
  171. };
  172. function map_linear( x, sa, sb, ea, eb ) {
  173. return ( x - sa ) * ( eb - ea ) / ( sb - sa ) + ea;
  174. };
  175. function clamp_bottom( x, a ) {
  176. return x < a ? a : x;
  177. };
  178. function clamp( x, a, b ) {
  179. return x < a ? a : ( x > b ? b : x );
  180. };
  181. };