webgl_performance.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. var container, stats;
  19. var camera, scene, renderer;
  20. var objects;
  21. var mouseX = 0, mouseY = 0;
  22. var windowHalfX = window.innerWidth / 2;
  23. var windowHalfY = window.innerHeight / 2;
  24. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  31. camera.position.z = 3200;
  32. scene = new THREE.Scene();
  33. scene.background = new THREE.Color( 0xffffff );
  34. objects = [];
  35. var material = new THREE.MeshNormalMaterial();
  36. var loader = new THREE.BufferGeometryLoader();
  37. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  38. geometry.computeVertexNormals();
  39. for ( var i = 0; i < 5000; i ++ ) {
  40. var mesh = new THREE.Mesh( geometry, material );
  41. mesh.position.x = Math.random() * 8000 - 4000;
  42. mesh.position.y = Math.random() * 8000 - 4000;
  43. mesh.position.z = Math.random() * 8000 - 4000;
  44. mesh.rotation.x = Math.random() * 2 * Math.PI;
  45. mesh.rotation.y = Math.random() * 2 * Math.PI;
  46. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50 + 100;
  47. objects.push( mesh );
  48. scene.add( mesh );
  49. }
  50. } );
  51. renderer = new THREE.WebGLRenderer();
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. container.appendChild( renderer.domElement );
  55. stats = new Stats();
  56. container.appendChild( stats.dom );
  57. //
  58. window.addEventListener( 'resize', onWindowResize, false );
  59. }
  60. function onWindowResize() {
  61. windowHalfX = window.innerWidth / 2;
  62. windowHalfY = window.innerHeight / 2;
  63. camera.aspect = window.innerWidth / window.innerHeight;
  64. camera.updateProjectionMatrix();
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. }
  67. function onDocumentMouseMove( event ) {
  68. mouseX = ( event.clientX - windowHalfX ) * 10;
  69. mouseY = ( event.clientY - windowHalfY ) * 10;
  70. }
  71. //
  72. function animate() {
  73. requestAnimationFrame( animate );
  74. render();
  75. stats.update();
  76. }
  77. function render() {
  78. camera.position.x += ( mouseX - camera.position.x ) * .05;
  79. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  80. camera.lookAt( scene.position );
  81. for ( var i = 0, il = objects.length; i < il; i ++ ) {
  82. objects[ i ].rotation.x += 0.01;
  83. objects[ i ].rotation.y += 0.02;
  84. }
  85. renderer.render( scene, camera );
  86. }
  87. </script>
  88. </body>
  89. </html>