webgl_instancing_dynamic.html 3.4 KB

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