webgl_performance.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. let stats;
  30. let camera, scene, renderer;
  31. let objects;
  32. let mouseX = 0, mouseY = 0;
  33. let windowHalfX = window.innerWidth / 2;
  34. let windowHalfY = window.innerHeight / 2;
  35. document.addEventListener( 'mousemove', onDocumentMouseMove );
  36. init();
  37. animate();
  38. function init() {
  39. const container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  42. camera.position.z = 3200;
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0xffffff );
  45. objects = [];
  46. const material = new THREE.MeshNormalMaterial();
  47. const loader = new THREE.BufferGeometryLoader();
  48. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  49. geometry.computeVertexNormals();
  50. for ( let i = 0; i < 5000; i ++ ) {
  51. const mesh = new THREE.Mesh( geometry, material );
  52. mesh.position.x = Math.random() * 8000 - 4000;
  53. mesh.position.y = Math.random() * 8000 - 4000;
  54. mesh.position.z = Math.random() * 8000 - 4000;
  55. mesh.rotation.x = Math.random() * 2 * Math.PI;
  56. mesh.rotation.y = Math.random() * 2 * Math.PI;
  57. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50 + 100;
  58. objects.push( mesh );
  59. scene.add( mesh );
  60. }
  61. } );
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. stats = new Stats();
  67. container.appendChild( stats.dom );
  68. //
  69. window.addEventListener( 'resize', onWindowResize );
  70. }
  71. function onWindowResize() {
  72. windowHalfX = window.innerWidth / 2;
  73. windowHalfY = window.innerHeight / 2;
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function onDocumentMouseMove( event ) {
  79. mouseX = ( event.clientX - windowHalfX ) * 10;
  80. mouseY = ( event.clientY - windowHalfY ) * 10;
  81. }
  82. //
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. render();
  86. stats.update();
  87. }
  88. function render() {
  89. camera.position.x += ( mouseX - camera.position.x ) * .05;
  90. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  91. camera.lookAt( scene.position );
  92. for ( let i = 0, il = objects.length; i < il; i ++ ) {
  93. objects[ i ].rotation.x += 0.01;
  94. objects[ i ].rotation.y += 0.02;
  95. }
  96. renderer.render( scene, camera );
  97. }
  98. </script>
  99. </body>
  100. </html>