webgl_camera.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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://github.com/mrdoob/three.js" target="_blank">three.js</a> - cameras<br/>
  32. <b>O</b> orthographic <b>P</b> perspective
  33. </div>
  34. <script src="../build/Three.js"></script>
  35. <script src="js/Stats.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;
  42. var cameraPerspective, cameraOrtho;
  43. var cameraPerspectiveHelper, cameraOrthoHelper;
  44. var r = 0;
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. scene = new THREE.Scene();
  51. //
  52. camera = new THREE.PerspectiveCamera( 50, 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  53. camera.position.z = 2500;
  54. scene.add( camera );
  55. cameraPerspective = new THREE.PerspectiveCamera( 50, 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT, 150, 1000 );
  56. cameraPerspectiveHelper = new THREE.CameraHelper( cameraPerspective );
  57. cameraPerspective.add( cameraPerspectiveHelper );
  58. //
  59. cameraOrtho = new THREE.OrthographicCamera( 0.5 * SCREEN_WIDTH / - 2, 0.5 * SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, 150, 1000 );
  60. cameraOrthoHelper = new THREE.CameraHelper( cameraOrtho );
  61. cameraOrtho.add( cameraOrthoHelper );
  62. //
  63. activeCamera = cameraPerspective;
  64. // counteract different front orientation of cameras vs rig
  65. cameraOrtho.rotation.y = Math.PI;
  66. cameraPerspective.rotation.y = Math.PI;
  67. cameraRig = new THREE.Object3D();
  68. cameraRig.add( cameraPerspective );
  69. cameraRig.add( cameraOrtho );
  70. scene.add( cameraRig );
  71. //
  72. mesh = new THREE.Mesh( new THREE.SphereGeometry( 100, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } ) );
  73. scene.add( mesh );
  74. var mesh2 = new THREE.Mesh( new THREE.SphereGeometry( 50, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0x00ff00, wireframe: true } ) );
  75. mesh2.position.y = 150;
  76. mesh.add( mesh2 );
  77. var mesh3 = new THREE.Mesh( new THREE.SphereGeometry( 5, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } ) );
  78. mesh3.position.z = 150;
  79. cameraRig.add( mesh3 );
  80. //
  81. var x, y, z, geo = new THREE.Geometry();
  82. for ( var i = 0; i < 10000; i ++ ) {
  83. x = THREE.Math.randFloatSpread( 2000 );
  84. y = THREE.Math.randFloatSpread( 2000 );
  85. z = THREE.Math.randFloatSpread( 2000 );
  86. geo.vertices.push( new THREE.Vertex( x, y, z ) );
  87. }
  88. var particles = new THREE.ParticleSystem( geo, 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*/ activeCamera = cameraOrtho; break;
  113. case 80: /*P*/ activeCamera = cameraPerspective; break;
  114. }
  115. };
  116. //
  117. function onWindowResize( event ) {
  118. SCREEN_WIDTH = window.innerWidth;
  119. SCREEN_HEIGHT = window.innerHeight;
  120. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  121. camera.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
  122. camera.updateProjectionMatrix();
  123. cameraPerspective.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
  124. cameraPerspective.updateProjectionMatrix();
  125. cameraOrtho.left = - 0.5 * SCREEN_WIDTH / 2;
  126. cameraOrtho.right = 0.5 * SCREEN_WIDTH / 2;
  127. cameraOrtho.top = SCREEN_HEIGHT / 2;
  128. cameraOrtho.bottom = - SCREEN_HEIGHT / 2;
  129. cameraOrtho.updateProjectionMatrix();
  130. }
  131. //
  132. function animate() {
  133. requestAnimationFrame( animate );
  134. render();
  135. stats.update();
  136. }
  137. function render() {
  138. mesh.position.x = 700 * Math.cos( r );
  139. mesh.position.z = 700 * Math.sin( r );
  140. mesh.position.y = 700 * Math.sin( r );
  141. mesh.children[ 0 ].position.x = 70 * Math.cos( 2 * r );
  142. mesh.children[ 0 ].position.z = 70 * Math.sin( r );
  143. if ( activeCamera === cameraPerspective ) {
  144. cameraPerspective.fov = 35 + 30 * Math.sin( 0.5 * r );
  145. cameraPerspective.far = mesh.position.length();
  146. cameraPerspective.updateProjectionMatrix();
  147. cameraPerspectiveHelper.update();
  148. cameraPerspectiveHelper.lines.visible = true;
  149. cameraOrthoHelper.lines.visible = false;
  150. } else {
  151. cameraOrtho.far = mesh.position.length();
  152. cameraOrtho.updateProjectionMatrix();
  153. cameraOrthoHelper.update();
  154. cameraOrthoHelper.lines.visible = true;
  155. cameraPerspectiveHelper.lines.visible = false;
  156. }
  157. cameraRig.lookAt( mesh.position );
  158. renderer.clear();
  159. renderer.setViewport( 0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
  160. renderer.render( scene, activeCamera );
  161. renderer.setViewport( SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
  162. renderer.render( scene, camera );
  163. r += 0.01;
  164. }
  165. </script>
  166. </body>
  167. </html>