PointerLockControls.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.PointerLockControls = function ( camera ) {
  5. var scope = this;
  6. camera.rotation.set( 0, 0, 0 );
  7. var pitchObject = new THREE.Object3D();
  8. pitchObject.add( camera );
  9. var yawObject = new THREE.Object3D();
  10. yawObject.position.y = 10;
  11. yawObject.add( pitchObject );
  12. var PI_2 = Math.PI / 2;
  13. var onMouseMove = function ( event ) {
  14. if ( scope.enabled === false ) return;
  15. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  16. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  17. yawObject.rotation.y -= movementX * 0.002;
  18. pitchObject.rotation.x -= movementY * 0.002;
  19. pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, pitchObject.rotation.x ) );
  20. };
  21. this.dispose = function() {
  22. document.removeEventListener( 'mousemove', onMouseMove, false );
  23. };
  24. document.addEventListener( 'mousemove', onMouseMove, false );
  25. this.enabled = false;
  26. this.getObject = function () {
  27. return yawObject;
  28. };
  29. this.getDirection = function() {
  30. // assumes the camera itself is not rotated
  31. var direction = new THREE.Vector3( 0, 0, - 1 );
  32. var rotation = new THREE.Euler( 0, 0, 0, "YXZ" );
  33. return function( v ) {
  34. rotation.set( pitchObject.rotation.x, yawObject.rotation.y, 0 );
  35. v.copy( direction ).applyEuler( rotation );
  36. return v;
  37. };
  38. }();
  39. };