webgl_performance_static.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - performance [static]</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 mouseX = 0, mouseY = 0;
  31. let windowHalfX = window.innerWidth / 2;
  32. let windowHalfY = window.innerHeight / 2;
  33. document.addEventListener( 'mousemove', onDocumentMouseMove );
  34. init();
  35. animate();
  36. function init() {
  37. const container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  40. camera.position.z = 3200;
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xffffff );
  43. scene.matrixAutoUpdate = false;
  44. const material = new THREE.MeshNormalMaterial();
  45. const loader = new THREE.BufferGeometryLoader();
  46. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  47. geometry.computeVertexNormals();
  48. for ( let i = 0; i < 7700; i ++ ) {
  49. const mesh = new THREE.Mesh( geometry, material );
  50. mesh.position.x = Math.random() * 10000 - 5000;
  51. mesh.position.y = Math.random() * 10000 - 5000;
  52. mesh.position.z = Math.random() * 10000 - 5000;
  53. mesh.rotation.x = Math.random() * 2 * Math.PI;
  54. mesh.rotation.y = Math.random() * 2 * Math.PI;
  55. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50 + 100;
  56. mesh.matrixAutoUpdate = false;
  57. mesh.updateMatrix();
  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. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>