canvas_camera_orthographic.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.min.js"></script>
  18. <script src="js/Stats.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, renderer;
  22. init();
  23. animate();
  24. function init() {
  25. container = document.createElement( 'div' );
  26. document.body.appendChild( container );
  27. var info = document.createElement( 'div' );
  28. info.style.position = 'absolute';
  29. info.style.top = '10px';
  30. info.style.width = '100%';
  31. info.style.textAlign = 'center';
  32. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - orthographic view';
  33. container.appendChild( info );
  34. camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 2000, 1000 );
  35. camera.position.x = 200;
  36. camera.position.y = 100;
  37. camera.position.z = 200;
  38. scene = new THREE.Scene();
  39. // Grid
  40. var size = 500, step = 50;
  41. var geometry = new THREE.Geometry();
  42. for ( var i = - size; i <= size; i += step ) {
  43. geometry.vertices.push( new THREE.Vector3( - size, 0, i ) );
  44. geometry.vertices.push( new THREE.Vector3( size, 0, i ) );
  45. geometry.vertices.push( new THREE.Vector3( i, 0, - size ) );
  46. geometry.vertices.push( new THREE.Vector3( i, 0, size ) );
  47. }
  48. var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } );
  49. var line = new THREE.Line( geometry, material );
  50. line.type = THREE.LinePieces;
  51. scene.add( line );
  52. // Cubes
  53. var geometry = new THREE.CubeGeometry( 50, 50, 50 );
  54. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, overdraw: true } );
  55. for ( var i = 0; i < 100; i ++ ) {
  56. var cube = new THREE.Mesh( geometry, material );
  57. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  58. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  59. cube.position.y = ( cube.scale.y * 50 ) / 2;
  60. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  61. scene.add( cube );
  62. }
  63. // Lights
  64. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  65. scene.add( ambientLight );
  66. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  67. directionalLight.position.x = Math.random() - 0.5;
  68. directionalLight.position.y = Math.random() - 0.5;
  69. directionalLight.position.z = Math.random() - 0.5;
  70. directionalLight.position.normalize();
  71. scene.add( directionalLight );
  72. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  73. directionalLight.position.x = Math.random() - 0.5;
  74. directionalLight.position.y = Math.random() - 0.5;
  75. directionalLight.position.z = Math.random() - 0.5;
  76. directionalLight.position.normalize();
  77. scene.add( directionalLight );
  78. renderer = new THREE.CanvasRenderer();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. container.appendChild( renderer.domElement );
  81. stats = new Stats();
  82. stats.domElement.style.position = 'absolute';
  83. stats.domElement.style.top = '0px';
  84. container.appendChild( stats.domElement );
  85. //
  86. window.addEventListener( 'resize', onWindowResize, false );
  87. }
  88. function onWindowResize() {
  89. camera.left = window.innerWidth / - 2;
  90. camera.right = window.innerWidth / 2;
  91. camera.top = window.innerHeight / 2;
  92. camera.bottom = window.innerHeight / - 2;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. //
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. render();
  100. stats.update();
  101. }
  102. function render() {
  103. var timer = Date.now() * 0.0001;
  104. camera.position.x = Math.cos( timer ) * 200;
  105. camera.position.z = Math.sin( timer ) * 200;
  106. camera.lookAt( scene.position );
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>