canvas_camera_orthographic.html 4.0 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/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. scene.add( camera );
  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, overdraw: true } );
  57. for ( var i = 0; i < 100; i ++ ) {
  58. var cube = new THREE.Mesh( geometry, material );
  59. cube.scale.y = Math.floor( Math.random() * 2 + 1 );
  60. cube.position.x = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  61. cube.position.y = ( cube.scale.y * 50 ) / 2;
  62. cube.position.z = Math.floor( ( Math.random() * 1000 - 500 ) / 50 ) * 50 + 25;
  63. scene.add(cube);
  64. }
  65. // Lights
  66. var ambientLight = new THREE.AmbientLight( Math.random() * 0x10 );
  67. scene.add( ambientLight );
  68. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  69. directionalLight.position.x = Math.random() - 0.5;
  70. directionalLight.position.y = Math.random() - 0.5;
  71. directionalLight.position.z = Math.random() - 0.5;
  72. directionalLight.position.normalize();
  73. scene.add( directionalLight );
  74. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  75. directionalLight.position.x = Math.random() - 0.5;
  76. directionalLight.position.y = Math.random() - 0.5;
  77. directionalLight.position.z = Math.random() - 0.5;
  78. directionalLight.position.normalize();
  79. scene.add( directionalLight );
  80. renderer = new THREE.CanvasRenderer();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. stats = new Stats();
  84. stats.domElement.style.position = 'absolute';
  85. stats.domElement.style.top = '0px';
  86. container.appendChild( stats.domElement );
  87. }
  88. //
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. render();
  92. stats.update();
  93. }
  94. function render() {
  95. var timer = new Date().getTime() * 0.0001;
  96. camera.position.x = Math.cos( timer ) * 200;
  97. camera.position.z = Math.sin( timer ) * 200;
  98. camera.lookAt( scene.position );
  99. renderer.render( scene, camera );
  100. }
  101. </script>
  102. </body>
  103. </html>