webgl_camera.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. a {
  10. color: #08f;
  11. }
  12. b {
  13. color: lightgreen;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - cameras<br/>
  19. <b>O</b> orthographic <b>P</b> perspective
  20. </div>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import Stats from './jsm/libs/stats.module.js';
  24. var SCREEN_WIDTH = window.innerWidth;
  25. var SCREEN_HEIGHT = window.innerHeight;
  26. var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  27. var container, stats;
  28. var camera, scene, renderer, mesh;
  29. var cameraRig, activeCamera, activeHelper;
  30. var cameraPerspective, cameraOrtho;
  31. var cameraPerspectiveHelper, cameraOrthoHelper;
  32. var frustumSize = 600;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. scene = new THREE.Scene();
  39. //
  40. camera = new THREE.PerspectiveCamera( 50, 0.5 * aspect, 1, 10000 );
  41. camera.position.z = 2500;
  42. cameraPerspective = new THREE.PerspectiveCamera( 50, 0.5 * aspect, 150, 1000 );
  43. cameraPerspectiveHelper = new THREE.CameraHelper( cameraPerspective );
  44. scene.add( cameraPerspectiveHelper );
  45. //
  46. cameraOrtho = new THREE.OrthographicCamera( 0.5 * frustumSize * aspect / - 2, 0.5 * frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 150, 1000 );
  47. cameraOrthoHelper = new THREE.CameraHelper( cameraOrtho );
  48. scene.add( cameraOrthoHelper );
  49. //
  50. activeCamera = cameraPerspective;
  51. activeHelper = cameraPerspectiveHelper;
  52. // counteract different front orientation of cameras vs rig
  53. cameraOrtho.rotation.y = Math.PI;
  54. cameraPerspective.rotation.y = Math.PI;
  55. cameraRig = new THREE.Group();
  56. cameraRig.add( cameraPerspective );
  57. cameraRig.add( cameraOrtho );
  58. scene.add( cameraRig );
  59. //
  60. mesh = new THREE.Mesh(
  61. new THREE.SphereBufferGeometry( 100, 16, 8 ),
  62. new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } )
  63. );
  64. scene.add( mesh );
  65. var mesh2 = new THREE.Mesh(
  66. new THREE.SphereBufferGeometry( 50, 16, 8 ),
  67. new THREE.MeshBasicMaterial( { color: 0x00ff00, wireframe: true } )
  68. );
  69. mesh2.position.y = 150;
  70. mesh.add( mesh2 );
  71. var mesh3 = new THREE.Mesh(
  72. new THREE.SphereBufferGeometry( 5, 16, 8 ),
  73. new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } )
  74. );
  75. mesh3.position.z = 150;
  76. cameraRig.add( mesh3 );
  77. //
  78. var geometry = new THREE.BufferGeometry();
  79. var vertices = [];
  80. for ( var i = 0; i < 10000; i ++ ) {
  81. vertices.push( THREE.MathUtils.randFloatSpread( 2000 ) ); // x
  82. vertices.push( THREE.MathUtils.randFloatSpread( 2000 ) ); // y
  83. vertices.push( THREE.MathUtils.randFloatSpread( 2000 ) ); // z
  84. }
  85. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  86. var particles = new THREE.Points( geometry, new THREE.PointsMaterial( { color: 0x888888 } ) );
  87. scene.add( particles );
  88. //
  89. renderer = new THREE.WebGLRenderer( { antialias: true } );
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  92. container.appendChild( renderer.domElement );
  93. renderer.autoClear = false;
  94. //
  95. stats = new Stats();
  96. container.appendChild( stats.dom );
  97. //
  98. window.addEventListener( 'resize', onWindowResize, false );
  99. document.addEventListener( 'keydown', onKeyDown, false );
  100. }
  101. //
  102. function onKeyDown( event ) {
  103. switch ( event.keyCode ) {
  104. case 79: /*O*/
  105. activeCamera = cameraOrtho;
  106. activeHelper = cameraOrthoHelper;
  107. break;
  108. case 80: /*P*/
  109. activeCamera = cameraPerspective;
  110. activeHelper = cameraPerspectiveHelper;
  111. break;
  112. }
  113. }
  114. //
  115. function onWindowResize() {
  116. SCREEN_WIDTH = window.innerWidth;
  117. SCREEN_HEIGHT = window.innerHeight;
  118. aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  119. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  120. camera.aspect = 0.5 * aspect;
  121. camera.updateProjectionMatrix();
  122. cameraPerspective.aspect = 0.5 * aspect;
  123. cameraPerspective.updateProjectionMatrix();
  124. cameraOrtho.left = - 0.5 * frustumSize * aspect / 2;
  125. cameraOrtho.right = 0.5 * frustumSize * aspect / 2;
  126. cameraOrtho.top = frustumSize / 2;
  127. cameraOrtho.bottom = - frustumSize / 2;
  128. cameraOrtho.updateProjectionMatrix();
  129. }
  130. //
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. render();
  134. stats.update();
  135. }
  136. function render() {
  137. var r = Date.now() * 0.0005;
  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.visible = true;
  149. cameraOrthoHelper.visible = false;
  150. } else {
  151. cameraOrtho.far = mesh.position.length();
  152. cameraOrtho.updateProjectionMatrix();
  153. cameraOrthoHelper.update();
  154. cameraOrthoHelper.visible = true;
  155. cameraPerspectiveHelper.visible = false;
  156. }
  157. cameraRig.lookAt( mesh.position );
  158. renderer.clear();
  159. activeHelper.visible = false;
  160. renderer.setViewport( 0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT );
  161. renderer.render( scene, activeCamera );
  162. activeHelper.visible = true;
  163. renderer.setViewport( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT );
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>