2
0

canvas_camera_orthographic.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - camera - orthographic</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script type="text/javascript" src="../build/custom/ThreeCanvas.js"></script>
  18. <script type="text/javascript" src="../build/custom/ThreeExtras.js"></script>
  19. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  20. <script type="text/javascript" src="js/Stats.js"></script>
  21. <script type="text/javascript">
  22. var container, stats;
  23. var camera, scene, renderer;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. var info = document.createElement( 'div' );
  30. info.style.position = 'absolute';
  31. info.style.top = '10px';
  32. info.style.width = '100%';
  33. info.style.textAlign = 'center';
  34. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - orthographic view';
  35. container.appendChild( info );
  36. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, - 2000, 1000 );
  37. camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 2000, 1000 );
  38. camera.position.x = 200;
  39. camera.position.y = 100;
  40. camera.position.z = 200;
  41. scene = new THREE.Scene();
  42. // Grid
  43. var geometry = new THREE.Geometry();
  44. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  45. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  46. for ( var i = 0; i <= 20; i ++ ) {
  47. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  48. line.position.z = ( i * 50 ) - 500;
  49. scene.addObject( line );
  50. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  51. line.position.x = ( i * 50 ) - 500;
  52. line.rotation.y = 90 * Math.PI / 180;
  53. scene.addObject( line );
  54. }
  55. // Cubes
  56. var geometry = new THREE.Cube( 50, 50, 50 );
  57. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  58. for ( var i = 0; i < 100; i ++ ) {
  59. var cube = new THREE.Mesh( geometry, material );
  60. cube.overdraw = true;
  61. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  62. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  63. cube.position.y = ( cube.scale.y * 50 ) / 2;
  64. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  65. scene.addObject(cube);
  66. }
  67. // Lights
  68. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  69. scene.addLight( ambientLight );
  70. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  71. directionalLight.position.x = Math.random() - 0.5;
  72. directionalLight.position.y = Math.random() - 0.5;
  73. directionalLight.position.z = Math.random() - 0.5;
  74. directionalLight.position.normalize();
  75. scene.addLight( directionalLight );
  76. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  77. directionalLight.position.x = Math.random() - 0.5;
  78. directionalLight.position.y = Math.random() - 0.5;
  79. directionalLight.position.z = Math.random() - 0.5;
  80. directionalLight.position.normalize();
  81. scene.addLight( directionalLight );
  82. renderer = new THREE.CanvasRenderer();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild( renderer.domElement );
  85. stats = new Stats();
  86. stats.domElement.style.position = 'absolute';
  87. stats.domElement.style.top = '0px';
  88. container.appendChild( stats.domElement );
  89. }
  90. //
  91. function animate() {
  92. requestAnimationFrame( animate );
  93. render();
  94. stats.update();
  95. }
  96. function render() {
  97. var timer = new Date().getTime() * 0.0001;
  98. camera.position.x = Math.cos( timer ) * 200;
  99. camera.position.z = Math.sin( timer ) * 200;
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>