CameraControl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author paulirish / http://paulirish.com/
  5. */
  6. function bind( scope, fn ) {
  7. return function () {
  8. fn.apply( scope, arguments );
  9. };
  10. }
  11. CameraControlWASD = function ( camera, movement_speed, look_speed, nofly, look_vertical ) {
  12. this.movement_speed = movement_speed !== undefined ? movement_speed : 1.0;
  13. this.look_speed = look_speed !== undefined ? look_speed : 0.005;
  14. this.nofly = nofly;
  15. this.look_vertical = look_vertical;
  16. this.camera = camera;
  17. this.mouseX = 0;
  18. this.mouseY = 0;
  19. this.lat = 0;
  20. this.lon = 0;
  21. this.phy = 0;
  22. this.theta = 0;
  23. this.moveForward = false;
  24. this.moveBackward = false;
  25. this.moveLeft = false;
  26. this.moveRight = false;
  27. this.windowHalfX = window.innerWidth / 2;
  28. this.windowHalfY = window.innerHeight / 2;
  29. this.onDocumentMouseDown = function ( event ) {
  30. event.preventDefault();
  31. event.stopPropagation();
  32. switch ( event.button ) {
  33. case 0: this.moveForward = true; break;
  34. case 2: this.moveBackward = true; break;
  35. }
  36. };
  37. this.onDocumentMouseUp = function ( event ) {
  38. event.preventDefault();
  39. event.stopPropagation();
  40. switch ( event.button ) {
  41. case 0: this.moveForward = false; break;
  42. case 2: this.moveBackward = false; break;
  43. }
  44. };
  45. this.onDocumentMouseMove = function (event) {
  46. this.mouseX = event.clientX - this.windowHalfX;
  47. this.mouseY = event.clientY - this.windowHalfY;
  48. };
  49. this.onDocumentKeyDown = function ( event ) {
  50. switch( event.keyCode ) {
  51. case 38: /*up*/
  52. case 87: /*W*/ this.moveForward = true; break;
  53. case 37: /*left*/
  54. case 65: /*A*/ this.moveLeft = true; break;
  55. case 40: /*down*/
  56. case 83: /*S*/ this.moveBackward = true; break;
  57. case 39: /*right*/
  58. case 68: /*D*/ this.moveRight = true; break;
  59. }
  60. };
  61. this.onDocumentKeyUp = function ( event ) {
  62. switch( event.keyCode ) {
  63. case 38: /*up*/
  64. case 87: /*W*/ this.moveForward = false; break;
  65. case 37: /*left*/
  66. case 65: /*A*/ this.moveLeft = false; break;
  67. case 40: /*down*/
  68. case 83: /*S*/ this.moveBackward = false; break;
  69. case 39: /*right*/
  70. case 68: /*D*/ this.moveRight = false; break;
  71. }
  72. };
  73. this.update = function() {
  74. if ( this.moveForward ) this.camera.translateZ( - this.movement_speed, this.nofly );
  75. if ( this.moveBackward ) this.camera.translateZ( this.movement_speed, this.nofly );
  76. if ( this.moveLeft ) this.camera.translateX( - this.movement_speed, this.nofly );
  77. if ( this.moveRight ) this.camera.translateX( this.movement_speed, this.nofly );
  78. this.lon += this.mouseX * this.look_speed;
  79. if( this.look_vertical ) this.lat -= this.mouseY * this.look_speed;
  80. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  81. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  82. this.theta = this.lon * Math.PI / 180;
  83. this.camera.target.position.x = 100 * Math.sin( this.phi ) * Math.cos( this.theta ) + this.camera.position.x;
  84. this.camera.target.position.y = 100 * Math.cos( this.phi ) + this.camera.position.y;
  85. this.camera.target.position.z = 100 * Math.sin( this.phi ) * Math.sin( this.theta ) + this.camera.position.z;
  86. };
  87. document.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  88. document.addEventListener( 'mousemove', bind( this, this.onDocumentMouseMove ), false );
  89. document.addEventListener( 'mousedown', bind( this, this.onDocumentMouseDown ), false );
  90. document.addEventListener( 'mouseup', bind( this, this.onDocumentMouseUp ), false );
  91. document.addEventListener( 'keydown', bind( this, this.onDocumentKeyDown ), false );
  92. document.addEventListener( 'keyup', bind( this, this.onDocumentKeyUp ), false );
  93. };