webgl_performance.html 3.4 KB

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