webgl_instancing.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing</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. </head>
  9. <body>
  10. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import Stats from './jsm/libs/stats.module.js';
  13. var camera, scene, renderer, stats;
  14. var mouseX = 0, mouseY = 0;
  15. var windowHalfX = window.innerWidth / 2;
  16. var windowHalfY = window.innerHeight / 2;
  17. init();
  18. animate();
  19. function init() {
  20. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  21. camera.position.z = 25;
  22. scene = new THREE.Scene();
  23. scene.background = new THREE.Color( 0xffffff );
  24. var geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
  25. var material = new THREE.MeshNormalMaterial();
  26. var count = 10000;
  27. var mesh = new THREE.InstancedMesh( geometry, material, count );
  28. var dummy = new THREE.Object3D();
  29. for ( var i = 0; i < count; i ++ ) {
  30. dummy.position.set(
  31. Math.random() * 20 - 10,
  32. Math.random() * 20 - 10,
  33. Math.random() * 20 - 10
  34. );
  35. dummy.rotation.set(
  36. Math.random() * Math.PI,
  37. Math.random() * Math.PI,
  38. Math.random() * Math.PI
  39. );
  40. dummy.updateMatrix();
  41. mesh.setMatrixAt( i, dummy.matrix );
  42. }
  43. scene.add( mesh );
  44. //
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. document.body.appendChild( renderer.domElement );
  49. //
  50. stats = new Stats();
  51. document.body.appendChild( stats.dom );
  52. //
  53. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  54. //
  55. window.addEventListener( 'resize', onWindowResize, false );
  56. }
  57. function onWindowResize() {
  58. windowHalfX = window.innerWidth / 2;
  59. windowHalfY = window.innerHeight / 2;
  60. camera.aspect = window.innerWidth / window.innerHeight;
  61. camera.updateProjectionMatrix();
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. }
  64. function onDocumentMouseMove( event ) {
  65. mouseX = ( event.clientX - windowHalfX ) / 10;
  66. mouseY = ( event.clientY - windowHalfY ) / 10;
  67. }
  68. //
  69. function animate() {
  70. requestAnimationFrame( animate );
  71. render();
  72. stats.update();
  73. }
  74. function render() {
  75. var time = Date.now() * 0.001;
  76. var rx = Math.sin( time * 0.7 ) * 0.5,
  77. ry = Math.sin( time * 0.3 ) * 0.5,
  78. rz = Math.sin( time * 0.2 ) * 0.5;
  79. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  80. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  81. camera.lookAt( scene.position );
  82. var mesh = scene.children[ 0 ];
  83. mesh.rotation.x = rx;
  84. mesh.rotation.y = ry;
  85. mesh.rotation.z = rz;
  86. renderer.render( scene, camera );
  87. }
  88. </script>
  89. </body>
  90. </html>