webgl_camera.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - cameras</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #808080;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. z-index: 100;
  22. }
  23. a {
  24. color: #0080ff;
  25. }
  26. b { color: lightgreen }
  27. #stats { position: absolute; top:0; left: 0 }
  28. #stats #fps { background: transparent !important }
  29. #stats #fps #fpsText { color: #777 !important }
  30. #stats #fps #fpsGraph { display: none }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="container"></div>
  35. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - cameras<br/>
  36. <b>O</b> orthographic <b>P</b> perspective
  37. </div>
  38. <script src="../build/three.min.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script>
  41. var SCREEN_WIDTH = window.innerWidth;
  42. var SCREEN_HEIGHT = window.innerHeight;
  43. var container, stats;
  44. var camera, scene, renderer, mesh;
  45. var cameraRig, activeCamera, activeHelper;
  46. var cameraPerspective, cameraOrtho;
  47. var cameraPerspectiveHelper, cameraOrthoHelper;
  48. init();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. scene = new THREE.Scene();
  54. //
  55. camera = new THREE.PerspectiveCamera( 50, 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  56. camera.position.z = 2500;
  57. cameraPerspective = new THREE.PerspectiveCamera( 50, 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT, 150, 1000 );
  58. cameraPerspectiveHelper = new THREE.CameraHelper( cameraPerspective );
  59. scene.add( cameraPerspectiveHelper );
  60. //
  61. cameraOrtho = new THREE.OrthographicCamera( 0.5 * SCREEN_WIDTH / - 2, 0.5 * SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, 150, 1000 );
  62. cameraOrthoHelper = new THREE.CameraHelper( cameraOrtho );
  63. scene.add( cameraOrthoHelper );
  64. //
  65. activeCamera = cameraPerspective;
  66. activeHelper = cameraPerspectiveHelper;
  67. // counteract different front orientation of cameras vs rig
  68. cameraOrtho.rotation.y = Math.PI;
  69. cameraPerspective.rotation.y = Math.PI;
  70. cameraRig = new THREE.Object3D();
  71. cameraRig.add( cameraPerspective );
  72. cameraRig.add( cameraOrtho );
  73. scene.add( cameraRig );
  74. //
  75. mesh = new THREE.Mesh( new THREE.SphereGeometry( 100, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } ) );
  76. scene.add( mesh );
  77. var mesh2 = new THREE.Mesh( new THREE.SphereGeometry( 50, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0x00ff00, wireframe: true } ) );
  78. mesh2.position.y = 150;
  79. mesh.add( mesh2 );
  80. var mesh3 = new THREE.Mesh( new THREE.SphereGeometry( 5, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } ) );
  81. mesh3.position.z = 150;
  82. cameraRig.add( mesh3 );
  83. //
  84. var geometry = new THREE.Geometry();
  85. for ( var i = 0; i < 10000; i ++ ) {
  86. var vertex = new THREE.Vector3();
  87. vertex.x = THREE.Math.randFloatSpread( 2000 );
  88. vertex.y = THREE.Math.randFloatSpread( 2000 );
  89. vertex.z = THREE.Math.randFloatSpread( 2000 );
  90. geometry.vertices.push( vertex );
  91. }
  92. var particles = new THREE.ParticleSystem( geometry, new THREE.ParticleSystemMaterial( { color: 0x888888 } ) );
  93. scene.add( particles );
  94. //
  95. renderer = new THREE.WebGLRenderer( { antialias: true } );
  96. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  97. renderer.domElement.style.position = "relative";
  98. container.appendChild( renderer.domElement );
  99. renderer.autoClear = false;
  100. //
  101. stats = new Stats();
  102. container.appendChild( stats.domElement );
  103. //
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. document.addEventListener( 'keydown', onKeyDown, false );
  106. }
  107. //
  108. function onKeyDown ( event ) {
  109. switch( event.keyCode ) {
  110. case 79: /*O*/
  111. activeCamera = cameraOrtho;
  112. activeHelper = cameraOrthoHelper;
  113. break;
  114. case 80: /*P*/
  115. activeCamera = cameraPerspective;
  116. activeHelper = cameraPerspectiveHelper;
  117. break;
  118. }
  119. };
  120. //
  121. function onWindowResize( event ) {
  122. SCREEN_WIDTH = window.innerWidth;
  123. SCREEN_HEIGHT = window.innerHeight;
  124. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  125. camera.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
  126. camera.updateProjectionMatrix();
  127. cameraPerspective.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
  128. cameraPerspective.updateProjectionMatrix();
  129. cameraOrtho.left = - 0.5 * SCREEN_WIDTH / 2;
  130. cameraOrtho.right = 0.5 * SCREEN_WIDTH / 2;
  131. cameraOrtho.top = SCREEN_HEIGHT / 2;
  132. cameraOrtho.bottom = - SCREEN_HEIGHT / 2;
  133. cameraOrtho.updateProjectionMatrix();
  134. }
  135. //
  136. function animate() {
  137. requestAnimationFrame( animate );
  138. render();
  139. stats.update();
  140. }
  141. function render() {
  142. var r = Date.now() * 0.0005;
  143. mesh.position.x = 700 * Math.cos( r );
  144. mesh.position.z = 700 * Math.sin( r );
  145. mesh.position.y = 700 * Math.sin( r );
  146. mesh.children[ 0 ].position.x = 70 * Math.cos( 2 * r );
  147. mesh.children[ 0 ].position.z = 70 * Math.sin( r );
  148. if ( activeCamera === cameraPerspective ) {
  149. cameraPerspective.fov = 35 + 30 * Math.sin( 0.5 * r );
  150. cameraPerspective.far = mesh.position.length();
  151. cameraPerspective.updateProjectionMatrix();
  152. cameraPerspectiveHelper.update();
  153. cameraPerspectiveHelper.visible = true;
  154. cameraOrthoHelper.visible = false;
  155. } else {
  156. cameraOrtho.far = mesh.position.length();
  157. cameraOrtho.updateProjectionMatrix();
  158. cameraOrthoHelper.update();
  159. cameraOrthoHelper.visible = true;
  160. cameraPerspectiveHelper.visible = false;
  161. }
  162. cameraRig.lookAt( mesh.position );
  163. renderer.clear();
  164. activeHelper.visible = false;
  165. renderer.setViewport( 0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
  166. renderer.render( scene, activeCamera );
  167. activeHelper.visible = true;
  168. renderer.setViewport( SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
  169. renderer.render( scene, camera );
  170. }
  171. </script>
  172. </body>
  173. </html>