2
0

webgl_buffergeometry_instancing_interleaved.html 5.6 KB

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