canvas_performance.html 3.1 KB

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