FreeCamera.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.FreeCamera = function ( x, y, z ) {
  5. THREE.Camera.call( this, x, y, z );
  6. this.rotation = new THREE.Vector3( 0, 0, 0 );
  7. this.moveZ = function ( amount ) {
  8. direction.set( this.matrix.n11, this.target.position, this.position );
  9. depth = direction.length();
  10. direction.normalize();
  11. vector.copy( direction );
  12. vector.multiplyScalar( amount );
  13. direction.multiplyScalar( depth );
  14. this.position.addSelf( vector );
  15. this.target.position.add( this.position, direction );
  16. };
  17. this.updateMatrix = function () {
  18. this.matrix.identity();
  19. this.matrix.multiplySelf( THREE.Matrix4.translationMatrix( this.position.x, this.position.y, this.position.z ) );
  20. this.matrix.multiplySelf( THREE.Matrix4.rotationXMatrix( this.rotation.x ) );
  21. this.matrix.multiplySelf( THREE.Matrix4.rotationYMatrix( this.rotation.y ) );
  22. this.matrix.multiplySelf( THREE.Matrix4.rotationZMatrix( this.rotation.z ) );
  23. };
  24. /*
  25. var depth = 0;
  26. var vector = new THREE.Vector3();
  27. var direction = new THREE.Vector3();
  28. this.moveX = function ( amount ) {
  29. direction.sub( this.target.position, this.position );
  30. depth = direction.length();
  31. direction.normalize();
  32. vector.copy( this.up );
  33. vector.crossSelf( direction );
  34. vector.multiplyScalar( amount );
  35. direction.multiplyScalar( depth );
  36. this.position.subSelf( vector );
  37. this.target.position.add( this.position, direction );
  38. };
  39. this.moveZ = function ( amount ) {
  40. direction.sub( this.target.position, this.position );
  41. depth = direction.length();
  42. direction.normalize();
  43. vector.copy( direction );
  44. vector.multiplyScalar( amount );
  45. direction.multiplyScalar( depth );
  46. this.position.addSelf( vector );
  47. this.target.position.add( this.position, direction );
  48. };
  49. this.rotateX = function( amount ) { // pitch
  50. amount *= Math.PI / 180;
  51. vector.x = direction.x;
  52. vector.y = ( direction.y * Math.cos( amount ) ) - ( direction.z * Math.sin( amount ) );
  53. vector.z = ( direction.y * Math.sin( amount ) ) + ( direction.z * Math.cos( amount ) );
  54. direction.copy( vector );
  55. vector.set( direction.x, direction.y, direction.z );
  56. vector.multiplyScalar( 400 );
  57. this.target.position.copy( this.position );
  58. this.target.position.addSelf( vector );
  59. };
  60. this.rotateY = function( amount ) { // yaw
  61. amount *= Math.PI / 180;
  62. direction.sub( this.position, this.target.position );
  63. depth = direction.length();
  64. direction.normalize();
  65. vector.x = ( direction.z * Math.sin( amount ) ) + ( direction.x * Math.cos( amount ) );
  66. vector.y = direction.y;
  67. vector.z = ( direction.z * Math.cos( amount ) ) - ( direction.x * Math.sin( amount ) );
  68. direction.copy( vector );
  69. direction.multiplyScalar( depth );
  70. this.target.position.sub( this.position, direction );
  71. };
  72. this.rotateZ = function( amount ) { // roll
  73. amount *= Math.PI / 180;
  74. vector.x = ( this.up.x * Math.cos( amount ) ) - ( this.up.y * Math.sin( amount ) );
  75. vector.y = ( this.up.x * Math.sin( amount ) ) + ( this.up.y * Math.cos( amount ) );
  76. vector.z = this.up.z;
  77. this.up.copy( vector );
  78. };
  79. */
  80. };
  81. THREE.FreeCamera.prototype = new THREE.Camera();
  82. THREE.FreeCamera.prototype.constructor = THREE.FreeCamera;