webgl_performance_static.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. let stats;
  19. let camera, scene, renderer;
  20. let mouseX = 0, mouseY = 0;
  21. let windowHalfX = window.innerWidth / 2;
  22. let windowHalfY = window.innerHeight / 2;
  23. document.addEventListener( 'mousemove', onDocumentMouseMove );
  24. init();
  25. animate();
  26. function init() {
  27. const container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  30. camera.position.z = 3200;
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0xffffff );
  33. scene.matrixAutoUpdate = false;
  34. const material = new THREE.MeshNormalMaterial();
  35. const loader = new THREE.BufferGeometryLoader();
  36. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  37. geometry.computeVertexNormals();
  38. for ( let i = 0; i < 7700; i ++ ) {
  39. const mesh = new THREE.Mesh( geometry, material );
  40. mesh.position.x = Math.random() * 10000 - 5000;
  41. mesh.position.y = Math.random() * 10000 - 5000;
  42. mesh.position.z = Math.random() * 10000 - 5000;
  43. mesh.rotation.x = Math.random() * 2 * Math.PI;
  44. mesh.rotation.y = Math.random() * 2 * Math.PI;
  45. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50 + 100;
  46. mesh.matrixAutoUpdate = false;
  47. mesh.updateMatrix();
  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 );
  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. renderer.render( scene, camera );
  82. }
  83. </script>
  84. </body>
  85. </html>