canvas_camera_orthographic.html 4.3 KB

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