webgl_buffergeometry_instancing_interleaved.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. <meta property="og:title" content="three.js webgl - indexed instancing (single box), interleaved buffers">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry_instancing_interleaved.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry_instancing_interleaved.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - indexed instancing (single box), interleaved buffers
  17. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. let container, stats;
  31. let camera, scene, renderer, mesh;
  32. const instances = 5000;
  33. let lastTime = 0;
  34. const moveQ = new THREE.Quaternion( 0.5, 0.5, 0.5, 0.0 ).normalize();
  35. const tmpQ = new THREE.Quaternion();
  36. const tmpM = new THREE.Matrix4();
  37. const currentM = new THREE.Matrix4();
  38. init();
  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.colorSpace = THREE.SRGBColorSpace;
  105. material.map.flipY = false;
  106. // per instance data
  107. const matrix = new THREE.Matrix4();
  108. const offset = new THREE.Vector3();
  109. const orientation = new THREE.Quaternion();
  110. const scale = new THREE.Vector3( 1, 1, 1 );
  111. let x, y, z, w;
  112. mesh = new THREE.InstancedMesh( geometry, material, instances );
  113. for ( let 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. renderer.setAnimationLoop( animate );
  135. container.appendChild( renderer.domElement );
  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. const time = performance.now();
  148. mesh.rotation.y = time * 0.00005;
  149. const delta = ( time - lastTime ) / 5000;
  150. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  151. tmpM.makeRotationFromQuaternion( tmpQ );
  152. for ( let i = 0, il = instances; i < il; i ++ ) {
  153. mesh.getMatrixAt( i, currentM );
  154. currentM.multiply( tmpM );
  155. mesh.setMatrixAt( i, currentM );
  156. }
  157. mesh.instanceMatrix.needsUpdate = true;
  158. mesh.computeBoundingSphere();
  159. lastTime = time;
  160. renderer.render( scene, camera );
  161. stats.update();
  162. }
  163. </script>
  164. </body>
  165. </html>
粤ICP备19079148号