canvas_camera_orthographic2.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/RequestAnimationFrame.js"></script>
  24. <script src="js/Stats.js"></script>
  25. <div style="position: absolute; top: 10px; width: 100%; text-align: center; ">
  26. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - Combo Camera<br>
  27. View: <a href="#" onclick="setOrthographic();return false;"> Orthographic</a> |
  28. <a href="#" onclick="setPerspective();return false;">Perspective</a><br>
  29. Lens: <a href="#" onclick="setLens(12);return false;">12mm</a> |
  30. <a href="#" onclick="setLens(16);return false;">16mm</a> |
  31. <a href="#" onclick="setLens(24);return false;">24mm</a> |
  32. <a href="#" onclick="setLens(35);return false;">35mm</a> |
  33. <a href="#" onclick="setLens(50);return false;">50mm</a> |
  34. <a href="#" onclick="setLens(60);return false;">60mm</a> |
  35. <a href="#" onclick="setLens(85);return false;">85mm</a> |
  36. <a href="#" onclick="setLens(105);return false;">105mm</a><br>
  37. Fov: <a href="#" onclick="setFov(30);return false;">30°</a> |
  38. <a href="#" onclick="setFov(50);return false;">50°</a> |
  39. <a href="#" onclick="setFov(70);return false;">70°</a> |
  40. <a href="#" onclick="setFov(100);return false;">100°</a><br>
  41. Zoom: <a href="#" onclick="camera.setZoom(0.5);return false;">0.5x</a> |
  42. <a href="#" onclick="camera.setZoom(1);return false;">1x</a> |
  43. <a href="#" onclick="camera.setZoom(2);return false;">2x</a> |
  44. <br/>
  45. Views: <a href="#" onclick="camera.toTopView();return false;">Top view</a> |
  46. <a href="#" onclick="camera.toBottomView();return false;">Bottom view</a> |
  47. <a href="#" onclick="camera.toLeftView();return false;">Left view</a> |
  48. <a href="#" onclick="camera.toRightView();return false;">Right view</a> |
  49. <a href="#" onclick="camera.toFrontView();return false;">Front view</a> |
  50. <a href="#" onclick="camera.toBackView();return false;">Back view</a> |
  51. <a href="#" onclick="camera.rotationAutoUpdate = true;return false;">Look at Scene</a>
  52. <br/>
  53. <div id="fov"></div>
  54. </div>
  55. <script>
  56. var container, stats;
  57. var camera, scene, renderer;
  58. init();
  59. animate();
  60. function setFov(fov) {
  61. camera.setFov(fov);
  62. document.getElementById('fov').innerHTML = 'FOV '+ fov.toFixed(2) +'&deg;' ;
  63. }
  64. function setLens(lens) {
  65. // try adding a tween effect while changing focal length, and it'd be even cooler!
  66. var fov = camera.setLens(lens);
  67. document.getElementById('fov').innerHTML = 'Converted ' + lens + 'mm lens to FOV '+ fov.toFixed(2) +'&deg;' ;
  68. }
  69. function setOrthographic() {
  70. camera.toOrthographic();
  71. document.getElementById('fov').innerHTML = 'Orthographic mode' ;
  72. }
  73. function setPerspective() {
  74. camera.toPerspective();
  75. document.getElementById('fov').innerHTML = 'Perspective mode' ;
  76. }
  77. function init() {
  78. container = document.createElement( 'div' );
  79. document.body.appendChild( container );
  80. camera = new THREE.CombinedCamera( window.innerWidth /2, window.innerHeight/2, 70, 1, 1000, -1000, 1000, 1000 );
  81. camera.position.x = 200;
  82. camera.position.y = 100;
  83. camera.position.z = 200;
  84. scene = new THREE.Scene();
  85. // Grid
  86. var geometry = new THREE.Geometry();
  87. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  88. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 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.overdraw = true;
  104. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  105. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  106. cube.position.y = ( cube.scale.y * 50 ) / 2;
  107. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  108. scene.add(cube);
  109. }
  110. // Lights
  111. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  112. scene.add( ambientLight );
  113. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  114. directionalLight.position.x = Math.random() - 0.5;
  115. directionalLight.position.y = Math.random() - 0.5;
  116. directionalLight.position.z = Math.random() - 0.5;
  117. directionalLight.position.normalize();
  118. scene.add( directionalLight );
  119. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  120. directionalLight.position.x = Math.random() - 0.5;
  121. directionalLight.position.y = Math.random() - 0.5;
  122. directionalLight.position.z = Math.random() - 0.5;
  123. directionalLight.position.normalize();
  124. scene.add( directionalLight );
  125. renderer = new THREE.CanvasRenderer();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. container.appendChild( renderer.domElement );
  128. stats = new Stats();
  129. stats.domElement.style.position = 'absolute';
  130. stats.domElement.style.top = '0px';
  131. container.appendChild( stats.domElement );
  132. }
  133. //
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. render();
  137. stats.update();
  138. }
  139. function render() {
  140. var timer = new Date().getTime() * 0.0001;
  141. camera.position.x = Math.cos( timer ) * 200;
  142. camera.position.z = Math.sin( timer ) * 200;
  143. camera.lookAt( scene.position );
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>