canvas_camera_orthographic.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, 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. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.js"></script>
  18. <script src="js/renderers/Projector.js"></script>
  19. <script src="js/renderers/CanvasRenderer.js"></script>
  20. <script src="js/libs/stats.min.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var frustumSize = 1000;
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. var info = document.createElement( 'div' );
  31. info.style.position = 'absolute';
  32. info.style.top = '10px';
  33. info.style.width = '100%';
  34. info.style.textAlign = 'center';
  35. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - orthographic view';
  36. container.appendChild( info );
  37. var aspect = window.innerWidth / window.innerHeight;
  38. camera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 2000 );
  39. camera.position.y = 400;
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0xf0f0f0 );
  42. // Grid
  43. var gridHelper = new THREE.GridHelper( 1000, 20 );
  44. scene.add( gridHelper );
  45. // Cubes
  46. var geometry = new THREE.BoxBufferGeometry( 50, 50, 50 );
  47. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, overdraw: 0.5 } );
  48. for ( var i = 0; i < 100; i ++ ) {
  49. var cube = new THREE.Mesh( geometry, material );
  50. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  51. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  52. cube.position.y = ( cube.scale.y * 50 ) / 2;
  53. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  54. scene.add( cube );
  55. }
  56. // Lights
  57. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  58. scene.add( ambientLight );
  59. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  60. directionalLight.position.x = Math.random() - 0.5;
  61. directionalLight.position.y = Math.random() - 0.5;
  62. directionalLight.position.z = Math.random() - 0.5;
  63. directionalLight.position.normalize();
  64. scene.add( directionalLight );
  65. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  66. directionalLight.position.x = Math.random() - 0.5;
  67. directionalLight.position.y = Math.random() - 0.5;
  68. directionalLight.position.z = Math.random() - 0.5;
  69. directionalLight.position.normalize();
  70. scene.add( directionalLight );
  71. renderer = new THREE.CanvasRenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. //
  78. window.addEventListener( 'resize', onWindowResize, false );
  79. }
  80. function onWindowResize() {
  81. var aspect = window.innerWidth / window.innerHeight;
  82. camera.left = - frustumSize * aspect / 2;
  83. camera.right = frustumSize * aspect / 2;
  84. camera.top = frustumSize / 2;
  85. camera.bottom = - frustumSize / 2;
  86. camera.updateProjectionMatrix();
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. }
  89. //
  90. function animate() {
  91. requestAnimationFrame( animate );
  92. stats.begin();
  93. render();
  94. stats.end();
  95. }
  96. function render() {
  97. var timer = Date.now() * 0.0001;
  98. camera.position.x = Math.cos( timer ) * 800;
  99. camera.position.z = Math.sin( timer ) * 800;
  100. camera.lookAt( scene.position );
  101. renderer.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>