webgl_instancing_dynamic.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - dynamic</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="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.module.js",
  14. "three/addons/": "./jsm/"
  15. }
  16. }
  17. </script>
  18. <script type="module">
  19. import * as THREE from 'three';
  20. import Stats from 'three/addons/libs/stats.module.js';
  21. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  22. let camera, scene, renderer, stats;
  23. let mesh;
  24. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  25. const count = Math.pow( amount, 3 );
  26. const dummy = new THREE.Object3D();
  27. init();
  28. function init() {
  29. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  30. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  31. camera.lookAt( 0, 0, 0 );
  32. scene = new THREE.Scene();
  33. const loader = new THREE.BufferGeometryLoader();
  34. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  35. geometry.computeVertexNormals();
  36. geometry.scale( 0.5, 0.5, 0.5 );
  37. const material = new THREE.MeshNormalMaterial();
  38. // check overdraw
  39. // let material = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.1, transparent: true } );
  40. mesh = new THREE.InstancedMesh( geometry, material, count );
  41. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  42. scene.add( mesh );
  43. //
  44. const gui = new GUI();
  45. gui.add( mesh, 'count', 0, count );
  46. } );
  47. //
  48. renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. renderer.setAnimationLoop( animate );
  52. document.body.appendChild( renderer.domElement );
  53. //
  54. stats = new Stats();
  55. document.body.appendChild( stats.dom );
  56. //
  57. window.addEventListener( 'resize', onWindowResize );
  58. }
  59. function onWindowResize() {
  60. camera.aspect = window.innerWidth / window.innerHeight;
  61. camera.updateProjectionMatrix();
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. }
  64. //
  65. function animate() {
  66. render();
  67. stats.update();
  68. }
  69. function render() {
  70. if ( mesh ) {
  71. const time = Date.now() * 0.001;
  72. mesh.rotation.x = Math.sin( time / 4 );
  73. mesh.rotation.y = Math.sin( time / 2 );
  74. let i = 0;
  75. const offset = ( amount - 1 ) / 2;
  76. for ( let x = 0; x < amount; x ++ ) {
  77. for ( let y = 0; y < amount; y ++ ) {
  78. for ( let z = 0; z < amount; z ++ ) {
  79. dummy.position.set( offset - x, offset - y, offset - z );
  80. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  81. dummy.rotation.z = dummy.rotation.y * 2;
  82. dummy.updateMatrix();
  83. mesh.setMatrixAt( i ++, dummy.matrix );
  84. }
  85. }
  86. }
  87. mesh.instanceMatrix.needsUpdate = true;
  88. mesh.computeBoundingSphere();
  89. }
  90. renderer.render( scene, camera );
  91. }
  92. </script>
  93. </body>
  94. </html>