webgl_buffergeometry_instancing_interleaved.html 5.8 KB

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