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