2
0

webgl_instancing_dynamic.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. animate();
  29. function init() {
  30. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  31. camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
  32. camera.lookAt( 0, 0, 0 );
  33. scene = new THREE.Scene();
  34. const loader = new THREE.BufferGeometryLoader();
  35. loader.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  36. geometry.computeVertexNormals();
  37. geometry.scale( 0.5, 0.5, 0.5 );
  38. const material = new THREE.MeshNormalMaterial();
  39. // check overdraw
  40. // let material = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.1, transparent: true } );
  41. mesh = new THREE.InstancedMesh( geometry, material, count );
  42. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  43. scene.add( mesh );
  44. //
  45. const gui = new GUI();
  46. gui.add( mesh, 'count', 0, count );
  47. } );
  48. //
  49. renderer = new THREE.WebGLRenderer( { antialias: true } );
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  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. requestAnimationFrame( animate );
  67. render();
  68. stats.update();
  69. }
  70. function render() {
  71. if ( mesh ) {
  72. const time = Date.now() * 0.001;
  73. mesh.rotation.x = Math.sin( time / 4 );
  74. mesh.rotation.y = Math.sin( time / 2 );
  75. let i = 0;
  76. const offset = ( amount - 1 ) / 2;
  77. for ( let x = 0; x < amount; x ++ ) {
  78. for ( let y = 0; y < amount; y ++ ) {
  79. for ( let z = 0; z < amount; z ++ ) {
  80. dummy.position.set( offset - x, offset - y, offset - z );
  81. dummy.rotation.y = ( Math.sin( x / 4 + time ) + Math.sin( y / 4 + time ) + Math.sin( z / 4 + time ) );
  82. dummy.rotation.z = dummy.rotation.y * 2;
  83. dummy.updateMatrix();
  84. mesh.setMatrixAt( i ++, dummy.matrix );
  85. }
  86. }
  87. }
  88. mesh.instanceMatrix.needsUpdate = true;
  89. mesh.computeBoundingSphere();
  90. }
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>