webgl_performance_static.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. var container, stats;
  19. var camera, scene, renderer;
  20. var mouseX = 0, mouseY = 0;
  21. var windowHalfX = window.innerWidth / 2;
  22. var windowHalfY = window.innerHeight / 2;
  23. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  24. init();
  25. animate();
  26. function init() {
  27. 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. var material = new THREE.MeshNormalMaterial();
  35. var loader = new THREE.BufferGeometryLoader();
  36. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  37. geometry.computeVertexNormals();
  38. for ( var i = 0; i < 7700; i ++ ) {
  39. var 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. //renderer.sortObjects = false;
  55. container.appendChild( renderer.domElement );
  56. stats = new Stats();
  57. container.appendChild( stats.dom );
  58. //
  59. window.addEventListener( 'resize', onWindowResize, false );
  60. }
  61. function onWindowResize() {
  62. windowHalfX = window.innerWidth / 2;
  63. windowHalfY = window.innerHeight / 2;
  64. camera.aspect = window.innerWidth / window.innerHeight;
  65. camera.updateProjectionMatrix();
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. }
  68. function onDocumentMouseMove( event ) {
  69. mouseX = ( event.clientX - windowHalfX ) * 10;
  70. mouseY = ( event.clientY - windowHalfY ) * 10;
  71. }
  72. //
  73. function animate() {
  74. requestAnimationFrame( animate );
  75. render();
  76. stats.update();
  77. }
  78. function render() {
  79. camera.position.x += ( mouseX - camera.position.x ) * .05;
  80. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  81. camera.lookAt( scene.position );
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>