webgl_buffergeometry_instancing_interleaved.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - indexed instancing (single box), interleaved buffers</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. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - indexed instancing (single box), interleaved buffers
  13. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. let container, stats;
  27. let camera, scene, renderer, mesh;
  28. const instances = 5000;
  29. let lastTime = 0;
  30. const moveQ = new THREE.Quaternion( 0.5, 0.5, 0.5, 0.0 ).normalize();
  31. const tmpQ = new THREE.Quaternion();
  32. const tmpM = new THREE.Matrix4();
  33. const currentM = new THREE.Matrix4();
  34. init();
  35. function init() {
  36. container = document.getElementById( 'container' );
  37. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0x101010 );
  40. // geometry
  41. const geometry = new THREE.InstancedBufferGeometry();
  42. // per mesh data x,y,z,w,u,v,s,t for 4-element alignment
  43. // only use x,y,z and u,v; but x, y, z, nx, ny, nz, u, v would be a good layout
  44. const vertexBuffer = new THREE.InterleavedBuffer( new Float32Array( [
  45. // Front
  46. - 1, 1, 1, 0, 0, 0, 0, 0,
  47. 1, 1, 1, 0, 1, 0, 0, 0,
  48. - 1, - 1, 1, 0, 0, 1, 0, 0,
  49. 1, - 1, 1, 0, 1, 1, 0, 0,
  50. // Back
  51. 1, 1, - 1, 0, 1, 0, 0, 0,
  52. - 1, 1, - 1, 0, 0, 0, 0, 0,
  53. 1, - 1, - 1, 0, 1, 1, 0, 0,
  54. - 1, - 1, - 1, 0, 0, 1, 0, 0,
  55. // Left
  56. - 1, 1, - 1, 0, 1, 1, 0, 0,
  57. - 1, 1, 1, 0, 1, 0, 0, 0,
  58. - 1, - 1, - 1, 0, 0, 1, 0, 0,
  59. - 1, - 1, 1, 0, 0, 0, 0, 0,
  60. // Right
  61. 1, 1, 1, 0, 1, 0, 0, 0,
  62. 1, 1, - 1, 0, 1, 1, 0, 0,
  63. 1, - 1, 1, 0, 0, 0, 0, 0,
  64. 1, - 1, - 1, 0, 0, 1, 0, 0,
  65. // Top
  66. - 1, 1, 1, 0, 0, 0, 0, 0,
  67. 1, 1, 1, 0, 1, 0, 0, 0,
  68. - 1, 1, - 1, 0, 0, 1, 0, 0,
  69. 1, 1, - 1, 0, 1, 1, 0, 0,
  70. // Bottom
  71. 1, - 1, 1, 0, 1, 0, 0, 0,
  72. - 1, - 1, 1, 0, 0, 0, 0, 0,
  73. 1, - 1, - 1, 0, 1, 1, 0, 0,
  74. - 1, - 1, - 1, 0, 0, 1, 0, 0
  75. ] ), 8 );
  76. // Use vertexBuffer, starting at offset 0, 3 items in position attribute
  77. const positions = new THREE.InterleavedBufferAttribute( vertexBuffer, 3, 0 );
  78. geometry.setAttribute( 'position', positions );
  79. // Use vertexBuffer, starting at offset 4, 2 items in uv attribute
  80. const uvs = new THREE.InterleavedBufferAttribute( vertexBuffer, 2, 4 );
  81. geometry.setAttribute( 'uv', uvs );
  82. const indices = new Uint16Array( [
  83. 0, 2, 1,
  84. 2, 3, 1,
  85. 4, 6, 5,
  86. 6, 7, 5,
  87. 8, 10, 9,
  88. 10, 11, 9,
  89. 12, 14, 13,
  90. 14, 15, 13,
  91. 16, 17, 18,
  92. 18, 17, 19,
  93. 20, 21, 22,
  94. 22, 21, 23
  95. ] );
  96. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  97. // material
  98. const material = new THREE.MeshBasicMaterial();
  99. material.map = new THREE.TextureLoader().load( 'textures/crate.gif' );
  100. material.map.colorSpace = THREE.SRGBColorSpace;
  101. material.map.flipY = false;
  102. // per instance data
  103. const matrix = new THREE.Matrix4();
  104. const offset = new THREE.Vector3();
  105. const orientation = new THREE.Quaternion();
  106. const scale = new THREE.Vector3( 1, 1, 1 );
  107. let x, y, z, w;
  108. mesh = new THREE.InstancedMesh( geometry, material, instances );
  109. for ( let i = 0; i < instances; i ++ ) {
  110. // offsets
  111. x = Math.random() * 100 - 50;
  112. y = Math.random() * 100 - 50;
  113. z = Math.random() * 100 - 50;
  114. offset.set( x, y, z ).normalize();
  115. offset.multiplyScalar( 5 ); // move out at least 5 units from center in current direction
  116. offset.set( x + offset.x, y + offset.y, z + offset.z );
  117. // orientations
  118. x = Math.random() * 2 - 1;
  119. y = Math.random() * 2 - 1;
  120. z = Math.random() * 2 - 1;
  121. w = Math.random() * 2 - 1;
  122. orientation.set( x, y, z, w ).normalize();
  123. matrix.compose( offset, orientation, scale );
  124. mesh.setMatrixAt( i, matrix );
  125. }
  126. scene.add( mesh );
  127. renderer = new THREE.WebGLRenderer();
  128. renderer.setPixelRatio( window.devicePixelRatio );
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. renderer.setAnimationLoop( animate );
  131. container.appendChild( renderer.domElement );
  132. stats = new Stats();
  133. container.appendChild( stats.dom );
  134. window.addEventListener( 'resize', onWindowResize );
  135. }
  136. function onWindowResize() {
  137. camera.aspect = window.innerWidth / window.innerHeight;
  138. camera.updateProjectionMatrix();
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. }
  141. //
  142. function animate() {
  143. const time = performance.now();
  144. mesh.rotation.y = time * 0.00005;
  145. const delta = ( time - lastTime ) / 5000;
  146. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  147. tmpM.makeRotationFromQuaternion( tmpQ );
  148. for ( let i = 0, il = instances; i < il; i ++ ) {
  149. mesh.getMatrixAt( i, currentM );
  150. currentM.multiply( tmpM );
  151. mesh.setMatrixAt( i, currentM );
  152. }
  153. mesh.instanceMatrix.needsUpdate = true;
  154. mesh.computeBoundingSphere();
  155. lastTime = time;
  156. renderer.render( scene, camera );
  157. stats.update();
  158. }
  159. </script>
  160. </body>
  161. </html>