FirstPersonControls.js 6.3 KB

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