webgl_performance_static.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "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 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. scene.matrixAutoUpdate = false;
  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 < 7700; i ++ ) {
  50. const mesh = new THREE.Mesh( geometry, material );
  51. mesh.position.x = Math.random() * 10000 - 5000;
  52. mesh.position.y = Math.random() * 10000 - 5000;
  53. mesh.position.z = Math.random() * 10000 - 5000;
  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. mesh.matrixAutoUpdate = false;
  58. mesh.updateMatrix();
  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. renderer.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>