2
0

webgl_instancing_dynamic.html 3.5 KB

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