canvas_performance.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - performance</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/Stats.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, renderer;
  22. var sphere, plane;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  29. camera.position.set( 0, 1000, 1000 );
  30. scene = new THREE.Scene();
  31. // Grid
  32. var geometry = new THREE.Geometry();
  33. geometry.vertices.push( new THREE.Vector3( - 500, 0, 0 ) );
  34. geometry.vertices.push( new THREE.Vector3( 500, 0, 0 ) );
  35. var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.5 } );
  36. for ( var i = 0; i <= 10; i ++ ) {
  37. var line = new THREE.Line( geometry, material );
  38. line.position.z = ( i * 100 ) - 500;
  39. scene.add( line );
  40. var line = new THREE.Line( geometry, material );
  41. line.position.x = ( i * 100 ) - 500;
  42. line.rotation.y = 90 * Math.PI / 180;
  43. scene.add( line );
  44. }
  45. // Spheres
  46. geometry = new THREE.SphereGeometry( 100, 26, 18 );
  47. material = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, overdraw: true } );
  48. for ( var i = 0; i < 20; i ++ ) {
  49. sphere = new THREE.Mesh( geometry, material );
  50. sphere.position.x = ( i % 5 ) * 200 - 400;
  51. sphere.position.z = Math.floor( i / 5 ) * 200 - 400;
  52. scene.add( sphere );
  53. }
  54. // Lights
  55. var ambientLight = new THREE.AmbientLight( Math.random() * 0x202020 );
  56. scene.add( ambientLight );
  57. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  58. directionalLight.position.x = 0;
  59. directionalLight.position.y = 1;
  60. directionalLight.position.z = 0;
  61. scene.add( directionalLight );
  62. var pointLight = new THREE.PointLight( 0xff0000, 1, 500 );
  63. scene.add( pointLight );
  64. renderer = new THREE.CanvasRenderer();
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. container.appendChild( renderer.domElement );
  67. stats = new Stats();
  68. stats.domElement.style.position = 'absolute';
  69. stats.domElement.style.top = '0px';
  70. container.appendChild(stats.domElement);
  71. //
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. //
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. render();
  83. stats.update();
  84. }
  85. function render() {
  86. camera.lookAt( scene.position );
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>