webgl_camera.html 6.3 KB

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