CombinedCamera.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @author zz85 / http://twitter.com/blurspline / http://www.lab4games.net/zz85/blog
  3. *
  4. * A general perpose camera, for setting FOV, Lens Focal Length,
  5. * and switching between perspective and orthographic views easily.
  6. * Use this only if you do not wish to manage
  7. * both a Orthographic and Perspective Camera
  8. *
  9. */
  10. THREE.CombinedCamera = function ( width, height, fov, near, far, orthoNear, orthoFar ) {
  11. THREE.Camera.call( this );
  12. this.fov = fov;
  13. this.left = -width / 2;
  14. this.right = width / 2
  15. this.top = height / 2;
  16. this.bottom = -height / 2;
  17. // We could also handle the projectionMatrix internally, but just wanted to test nested camera objects
  18. this.cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, orthoNear, orthoFar );
  19. this.cameraP = new THREE.PerspectiveCamera( fov, width / height, near, far );
  20. this.zoom = 1;
  21. this.toPerspective();
  22. var aspect = width / height;
  23. };
  24. THREE.CombinedCamera.prototype = Object.create( THREE.Camera.prototype );
  25. THREE.CombinedCamera.prototype.constructor = THREE.CombinedCamera;
  26. THREE.CombinedCamera.prototype.toPerspective = function () {
  27. // Switches to the Perspective Camera
  28. this.near = this.cameraP.near;
  29. this.far = this.cameraP.far;
  30. this.cameraP.fov = this.fov / this.zoom ;
  31. this.cameraP.updateProjectionMatrix();
  32. this.projectionMatrix = this.cameraP.projectionMatrix;
  33. this.inPerspectiveMode = true;
  34. this.inOrthographicMode = false;
  35. };
  36. THREE.CombinedCamera.prototype.toOrthographic = function () {
  37. // Switches to the Orthographic camera estimating viewport from Perspective
  38. var fov = this.fov;
  39. var aspect = this.cameraP.aspect;
  40. var near = this.cameraP.near;
  41. var far = this.cameraP.far;
  42. // The size that we set is the mid plane of the viewing frustum
  43. var hyperfocus = ( near + far ) / 2;
  44. var halfHeight = Math.tan( fov * Math.PI / 180 / 2 ) * hyperfocus;
  45. var planeHeight = 2 * halfHeight;
  46. var planeWidth = planeHeight * aspect;
  47. var halfWidth = planeWidth / 2;
  48. halfHeight /= this.zoom;
  49. halfWidth /= this.zoom;
  50. this.cameraO.left = -halfWidth;
  51. this.cameraO.right = halfWidth;
  52. this.cameraO.top = halfHeight;
  53. this.cameraO.bottom = -halfHeight;
  54. // this.cameraO.left = -farHalfWidth;
  55. // this.cameraO.right = farHalfWidth;
  56. // this.cameraO.top = farHalfHeight;
  57. // this.cameraO.bottom = -farHalfHeight;
  58. // this.cameraO.left = this.left / this.zoom;
  59. // this.cameraO.right = this.right / this.zoom;
  60. // this.cameraO.top = this.top / this.zoom;
  61. // this.cameraO.bottom = this.bottom / this.zoom;
  62. this.cameraO.updateProjectionMatrix();
  63. this.near = this.cameraO.near;
  64. this.far = this.cameraO.far;
  65. this.projectionMatrix = this.cameraO.projectionMatrix;
  66. this.inPerspectiveMode = false;
  67. this.inOrthographicMode = true;
  68. };
  69. THREE.CombinedCamera.prototype.setSize = function( width, height ) {
  70. this.cameraP.aspect = width / height;
  71. this.left = -width / 2;
  72. this.right = width / 2
  73. this.top = height / 2;
  74. this.bottom = -height / 2;
  75. };
  76. THREE.CombinedCamera.prototype.setFov = function( fov ) {
  77. this.fov = fov;
  78. if ( this.inPerspectiveMode ) {
  79. this.toPerspective();
  80. } else {
  81. this.toOrthographic();
  82. }
  83. };
  84. // For mantaining similar API with PerspectiveCamera
  85. THREE.CombinedCamera.prototype.updateProjectionMatrix = function() {
  86. if ( this.inPerspectiveMode ) {
  87. this.toPerspective();
  88. } else {
  89. this.toPerspective();
  90. this.toOrthographic();
  91. }
  92. };
  93. /*
  94. * Uses Focal Length (in mm) to estimate and set FOV
  95. * 35mm (fullframe) camera is used if frame size is not specified;
  96. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  97. */
  98. THREE.CombinedCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  99. if ( frameHeight === undefined ) frameHeight = 24;
  100. var fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) );
  101. this.setFov( fov );
  102. return fov;
  103. };
  104. THREE.CombinedCamera.prototype.setZoom = function( zoom ) {
  105. this.zoom = zoom;
  106. if ( this.inPerspectiveMode ) {
  107. this.toPerspective();
  108. } else {
  109. this.toOrthographic();
  110. }
  111. };
  112. THREE.CombinedCamera.prototype.toFrontView = function() {
  113. this.rotation.x = 0;
  114. this.rotation.y = 0;
  115. this.rotation.z = 0;
  116. // should we be modifing the matrix instead?
  117. this.rotationAutoUpdate = false;
  118. };
  119. THREE.CombinedCamera.prototype.toBackView = function() {
  120. this.rotation.x = 0;
  121. this.rotation.y = Math.PI;
  122. this.rotation.z = 0;
  123. this.rotationAutoUpdate = false;
  124. };
  125. THREE.CombinedCamera.prototype.toLeftView = function() {
  126. this.rotation.x = 0;
  127. this.rotation.y = - Math.PI / 2;
  128. this.rotation.z = 0;
  129. this.rotationAutoUpdate = false;
  130. };
  131. THREE.CombinedCamera.prototype.toRightView = function() {
  132. this.rotation.x = 0;
  133. this.rotation.y = Math.PI / 2;
  134. this.rotation.z = 0;
  135. this.rotationAutoUpdate = false;
  136. };
  137. THREE.CombinedCamera.prototype.toTopView = function() {
  138. this.rotation.x = - Math.PI / 2;
  139. this.rotation.y = 0;
  140. this.rotation.z = 0;
  141. this.rotationAutoUpdate = false;
  142. };
  143. THREE.CombinedCamera.prototype.toBottomView = function() {
  144. this.rotation.x = Math.PI / 2;
  145. this.rotation.y = 0;
  146. this.rotation.z = 0;
  147. this.rotationAutoUpdate = false;
  148. };