2
0

webgl_buffergeometry_instancing_interleaved.html 5.9 KB

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