2
0

webgl_buffergeometry_instancing_interleaved.html 5.8 KB

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