2
0

canvas_camera_orthographic2.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - combo camera - orthographic + perspective</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. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. color: purple;
  14. }
  15. a {
  16. color: red;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../build/Three.js"></script>
  22. <script src="../src/extras/cameras/CombinedCamera.js"></script>
  23. <script src="js/Stats.js"></script>
  24. <div style="position: absolute; top: 10px; width: 100%; text-align: center; ">
  25. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - Combo Camera<br>
  26. View: <a href="#" onclick="setOrthographic();return false;"> Orthographic</a> |
  27. <a href="#" onclick="setPerspective();return false;">Perspective</a><br>
  28. Lens: <a href="#" onclick="setLens(12);return false;">12mm</a> |
  29. <a href="#" onclick="setLens(16);return false;">16mm</a> |
  30. <a href="#" onclick="setLens(24);return false;">24mm</a> |
  31. <a href="#" onclick="setLens(35);return false;">35mm</a> |
  32. <a href="#" onclick="setLens(50);return false;">50mm</a> |
  33. <a href="#" onclick="setLens(60);return false;">60mm</a> |
  34. <a href="#" onclick="setLens(85);return false;">85mm</a> |
  35. <a href="#" onclick="setLens(105);return false;">105mm</a><br>
  36. Fov: <a href="#" onclick="setFov(30);return false;">30°</a> |
  37. <a href="#" onclick="setFov(50);return false;">50°</a> |
  38. <a href="#" onclick="setFov(70);return false;">70°</a> |
  39. <a href="#" onclick="setFov(100);return false;">100°</a><br>
  40. Zoom: <a href="#" onclick="camera.setZoom(0.5);return false;">0.5x</a> |
  41. <a href="#" onclick="camera.setZoom(1);return false;">1x</a> |
  42. <a href="#" onclick="camera.setZoom(2);return false;">2x</a> |
  43. <br/>
  44. Views: <a href="#" onclick="camera.toTopView();return false;">Top view</a> |
  45. <a href="#" onclick="camera.toBottomView();return false;">Bottom view</a> |
  46. <a href="#" onclick="camera.toLeftView();return false;">Left view</a> |
  47. <a href="#" onclick="camera.toRightView();return false;">Right view</a> |
  48. <a href="#" onclick="camera.toFrontView();return false;">Front view</a> |
  49. <a href="#" onclick="camera.toBackView();return false;">Back view</a> |
  50. <a href="#" onclick="camera.rotationAutoUpdate = true;return false;">Look at Scene</a>
  51. <br/>
  52. <div id="fov"></div>
  53. </div>
  54. <script>
  55. var container, stats;
  56. var camera, scene, renderer;
  57. init();
  58. animate();
  59. function setFov( fov ) {
  60. camera.setFov( fov );
  61. document.getElementById('fov').innerHTML = 'FOV '+ fov.toFixed(2) +'&deg;' ;
  62. }
  63. function setLens( lens ) {
  64. // try adding a tween effect while changing focal length, and it'd be even cooler!
  65. var fov = camera.setLens( lens );
  66. document.getElementById('fov').innerHTML = 'Converted ' + lens + 'mm lens to FOV '+ fov.toFixed(2) +'&deg;' ;
  67. }
  68. function setOrthographic() {
  69. camera.toOrthographic();
  70. document.getElementById('fov').innerHTML = 'Orthographic mode' ;
  71. }
  72. function setPerspective() {
  73. camera.toPerspective();
  74. document.getElementById('fov').innerHTML = 'Perspective mode' ;
  75. }
  76. function init() {
  77. container = document.createElement( 'div' );
  78. document.body.appendChild( container );
  79. camera = new THREE.CombinedCamera( window.innerWidth /2, window.innerHeight/2, 70, 1, 1000, -1000, 1000, 1000 );
  80. camera.position.x = 200;
  81. camera.position.y = 100;
  82. camera.position.z = 200;
  83. scene = new THREE.Scene();
  84. scene.add( camera );
  85. // Grid
  86. var geometry = new THREE.Geometry();
  87. geometry.vertices.push( new THREE.Vertex( - 500, 0, 0 ) );
  88. geometry.vertices.push( new THREE.Vertex( 500, 0, 0 ) );
  89. for ( var i = 0; i <= 20; i ++ ) {
  90. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  91. line.position.z = ( i * 50 ) - 500;
  92. scene.add( line );
  93. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  94. line.position.x = ( i * 50 ) - 500;
  95. line.rotation.y = 90 * Math.PI / 180;
  96. scene.add( line );
  97. }
  98. // Cubes
  99. var geometry = new THREE.CubeGeometry( 50, 50, 50 );
  100. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, overdraw: true } );
  101. for ( var i = 0; i < 100; i ++ ) {
  102. var cube = new THREE.Mesh( geometry, material );
  103. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  104. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  105. cube.position.y = ( cube.scale.y * 50 ) / 2;
  106. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  107. scene.add(cube);
  108. }
  109. // Lights
  110. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  111. scene.add( ambientLight );
  112. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  113. directionalLight.position.x = Math.random() - 0.5;
  114. directionalLight.position.y = Math.random() - 0.5;
  115. directionalLight.position.z = Math.random() - 0.5;
  116. directionalLight.position.normalize();
  117. scene.add( directionalLight );
  118. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  119. directionalLight.position.x = Math.random() - 0.5;
  120. directionalLight.position.y = Math.random() - 0.5;
  121. directionalLight.position.z = Math.random() - 0.5;
  122. directionalLight.position.normalize();
  123. scene.add( directionalLight );
  124. renderer = new THREE.CanvasRenderer();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. container.appendChild( renderer.domElement );
  127. stats = new Stats();
  128. stats.domElement.style.position = 'absolute';
  129. stats.domElement.style.top = '0px';
  130. container.appendChild( stats.domElement );
  131. window.addEventListener( 'resize', onWindowResize, false );
  132. function onWindowResize(){
  133. camera.setSize( window.innerWidth, window.innerHeight );
  134. camera.updateProjectionMatrix();
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. }
  137. }
  138. //
  139. function animate() {
  140. requestAnimationFrame( animate );
  141. render();
  142. stats.update();
  143. }
  144. function render() {
  145. var timer = Date.now() * 0.0001;
  146. camera.position.x = Math.cos( timer ) * 200;
  147. camera.position.z = Math.sin( timer ) * 200;
  148. camera.lookAt( scene.position );
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>