canvas_camera_orthographic.html 4.1 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, 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/custom/ThreeCanvas.js"></script>
  18. <script src="../build/custom/ThreeExtras.js"></script>
  19. <script src="js/RequestAnimationFrame.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script>
  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.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 2000, 1000 );
  37. camera.position.x = 200;
  38. camera.position.y = 100;
  39. camera.position.z = 200;
  40. scene = new THREE.Scene();
  41. // Grid
  42. var geometry = new THREE.Geometry();
  43. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  44. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  45. for ( var i = 0; i <= 20; i ++ ) {
  46. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  47. line.position.z = ( i * 50 ) - 500;
  48. scene.add( line );
  49. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
  50. line.position.x = ( i * 50 ) - 500;
  51. line.rotation.y = 90 * Math.PI / 180;
  52. scene.add( line );
  53. }
  54. // Cubes
  55. var geometry = new THREE.CubeGeometry( 50, 50, 50 );
  56. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  57. for ( var i = 0; i < 100; i ++ ) {
  58. var cube = new THREE.Mesh( geometry, material );
  59. cube.overdraw = true;
  60. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  61. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  62. cube.position.y = ( cube.scale.y * 50 ) / 2;
  63. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  64. scene.add(cube);
  65. }
  66. // Lights
  67. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  68. scene.add( ambientLight );
  69. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  70. directionalLight.position.x = Math.random() - 0.5;
  71. directionalLight.position.y = Math.random() - 0.5;
  72. directionalLight.position.z = Math.random() - 0.5;
  73. directionalLight.position.normalize();
  74. scene.add( directionalLight );
  75. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  76. directionalLight.position.x = Math.random() - 0.5;
  77. directionalLight.position.y = Math.random() - 0.5;
  78. directionalLight.position.z = Math.random() - 0.5;
  79. directionalLight.position.normalize();
  80. scene.add( directionalLight );
  81. renderer = new THREE.CanvasRenderer();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. container.appendChild( renderer.domElement );
  84. stats = new Stats();
  85. stats.domElement.style.position = 'absolute';
  86. stats.domElement.style.top = '0px';
  87. container.appendChild( stats.domElement );
  88. }
  89. //
  90. function animate() {
  91. requestAnimationFrame( animate );
  92. render();
  93. stats.update();
  94. }
  95. function render() {
  96. var timer = new Date().getTime() * 0.0001;
  97. camera.position.x = Math.cos( timer ) * 200;
  98. camera.position.z = Math.sin( timer ) * 200;
  99. camera.lookAt( scene.origin );
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>