2
0

canvas_camera_orthographic2.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. <script>
  26. var container, stats;
  27. var camera, scene, renderer;
  28. init();
  29. animate();
  30. function setFov(fov) {
  31. camera.setFov(fov);
  32. document.getElementById('fov').innerHTML = 'FOV '+ fov.toFixed(2) +'&deg;' ;
  33. }
  34. function setLens(lens) {
  35. // try adding a tween effect while changing focal length, and it'd be even cooler!
  36. var fov = camera.setLens(lens);
  37. document.getElementById('fov').innerHTML = 'Converted ' + lens + 'mm lens to FOV '+ fov.toFixed(2) +'&deg;' ;
  38. }
  39. function setOrthographic() {
  40. camera.toOrthographic();
  41. document.getElementById('fov').innerHTML = 'Orthographic mode' ;
  42. }
  43. function setPerspective() {
  44. camera.toPerspective();
  45. document.getElementById('fov').innerHTML = 'Perspective mode' ;
  46. }
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. var info = document.createElement( 'div' );
  51. info.style.position = 'absolute';
  52. info.style.top = '10px';
  53. info.style.width = '100%';
  54. info.style.textAlign = 'center';
  55. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - Combo Camera' +
  56. '<br/>View: <a href="#" onclick="setOrthographic();return false;"> Orthographic</a> | <a href="#" onclick="setPerspective();return false;">Perspective</a>' +
  57. '<br/>Lens: <a href="#" onclick="setLens(12);return false;">12mm</a> | <a href="#" onclick="setLens(16);return false;">16mm</a> | <a href="#" onclick="setLens(24);return false;">24mm</a> | ' +
  58. '<a href="#" onclick="setLens(35);return false;">35mm</a> | <a href="#" onclick="setLens(50);return false;">50mm</a> | <a href="#" onclick="setLens(60);return false;">60mm</a> |' +
  59. ' <a href="#" onclick="setLens(85);return false;">85mm</a> | <a href="#" onclick="setLens(105);return false;">105mm</a>' +
  60. '<br/>Fov: <a href="#" onclick="setFov(30);return false;">30&deg;</a> | <a href="#" onclick="setFov(50);return false;">50&deg;</a> | <a href="#" onclick="setFov(70);return false;">70&deg;</a> | <a href="#" onclick="setFov(100);return false;">100&deg;</a> <br/><div id="fov"/>';
  61. container.appendChild( info );
  62. camera = new THREE.CombinedCamera( window.innerWidth, window.innerHeight, 70, 1, 1000, -1000, 1000, 1000 );
  63. camera.position.x = 200;
  64. camera.position.y = 100;
  65. camera.position.z = 200;
  66. scene = new THREE.Scene();
  67. // Grid
  68. var geometry = new THREE.Geometry();
  69. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  70. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  71. for ( var i = 0; i <= 20; i ++ ) {
  72. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  73. line.position.z = ( i * 50 ) - 500;
  74. scene.add( line );
  75. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  76. line.position.x = ( i * 50 ) - 500;
  77. line.rotation.y = 90 * Math.PI / 180;
  78. scene.add( line );
  79. }
  80. // Cubes
  81. var geometry = new THREE.CubeGeometry( 50, 50, 50 );
  82. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  83. for ( var i = 0; i < 100; i ++ ) {
  84. var cube = new THREE.Mesh( geometry, material );
  85. cube.overdraw = true;
  86. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  87. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  88. cube.position.y = ( cube.scale.y * 50 ) / 2;
  89. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  90. scene.add(cube);
  91. }
  92. // Lights
  93. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  94. scene.add( ambientLight );
  95. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  96. directionalLight.position.x = Math.random() - 0.5;
  97. directionalLight.position.y = Math.random() - 0.5;
  98. directionalLight.position.z = Math.random() - 0.5;
  99. directionalLight.position.normalize();
  100. scene.add( directionalLight );
  101. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  102. directionalLight.position.x = Math.random() - 0.5;
  103. directionalLight.position.y = Math.random() - 0.5;
  104. directionalLight.position.z = Math.random() - 0.5;
  105. directionalLight.position.normalize();
  106. scene.add( directionalLight );
  107. renderer = new THREE.CanvasRenderer();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. container.appendChild( renderer.domElement );
  110. stats = new Stats();
  111. stats.domElement.style.position = 'absolute';
  112. stats.domElement.style.top = '0px';
  113. container.appendChild( stats.domElement );
  114. }
  115. //
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. render();
  119. stats.update();
  120. }
  121. function render() {
  122. var timer = new Date().getTime() * 0.0001;
  123. camera.position.x = Math.cos( timer ) * 200;
  124. camera.position.z = Math.sin( timer ) * 200;
  125. camera.lookAt( scene.position );
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>