webgl_camera.html 6.4 KB

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