webgl_buffergeometry_instancing_interleaved_dynamic.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - indexed instancing (single box), interleaved buffers, dynamic updates</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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - indexed instancing (single box)<br/>interleaved buffers, dynamic updates
  25. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  26. </div>
  27. <script id="vertexShader" type="x-shader/x-vertex">
  28. precision highp float;
  29. uniform mat4 modelViewMatrix;
  30. uniform mat4 projectionMatrix;
  31. attribute vec3 position;
  32. attribute vec3 offset;
  33. attribute vec2 uv;
  34. attribute vec4 orientation;
  35. varying vec2 vUv;
  36. // http://www.geeks3d.com/20141201/how-to-rotate-a-vertex-by-a-quaternion-in-glsl/
  37. vec3 applyQuaternionToVector( vec4 q, vec3 v ){
  38. return v + 2.0 * cross( q.xyz, cross( q.xyz, v ) + q.w * v );
  39. }
  40. void main() {
  41. vec3 vPosition = applyQuaternionToVector( orientation, position );
  42. vUv = uv;
  43. gl_Position = projectionMatrix * modelViewMatrix * vec4( offset + vPosition, 1.0 );
  44. }
  45. </script>
  46. <script id="fragmentShader" type="x-shader/x-fragment">
  47. precision highp float;
  48. uniform sampler2D map;
  49. varying vec2 vUv;
  50. void main() {
  51. gl_FragColor = texture2D(map, vUv);
  52. }
  53. </script>
  54. <script type="module">
  55. import * as THREE from '../build/three.module.js';
  56. import Stats from './jsm/libs/stats.module.js';
  57. var container, stats;
  58. var camera, scene, renderer;
  59. var orientations, instanceBuffer;
  60. function init() {
  61. container = document.getElementById( 'container' );
  62. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  63. scene = new THREE.Scene();
  64. scene.background = new THREE.Color( 0x101010 );
  65. renderer = new THREE.WebGLRenderer();
  66. // geometry
  67. var instances = 5000;
  68. var geometry = new THREE.InstancedBufferGeometry();
  69. // per mesh data x,y,z,w,u,v,s,t for 4-element alignment
  70. // only use x,y,z and u,v; but x, y, z, nx, ny, nz, u, v would be a good layout
  71. var vertexBuffer = new THREE.InterleavedBuffer( new Float32Array( [
  72. // Front
  73. - 1, 1, 1, 0, 0, 0, 0, 0,
  74. 1, 1, 1, 0, 1, 0, 0, 0,
  75. - 1, - 1, 1, 0, 0, 1, 0, 0,
  76. 1, - 1, 1, 0, 1, 1, 0, 0,
  77. // Back
  78. 1, 1, - 1, 0, 1, 0, 0, 0,
  79. - 1, 1, - 1, 0, 0, 0, 0, 0,
  80. 1, - 1, - 1, 0, 1, 1, 0, 0,
  81. - 1, - 1, - 1, 0, 0, 1, 0, 0,
  82. // Left
  83. - 1, 1, - 1, 0, 1, 1, 0, 0,
  84. - 1, 1, 1, 0, 1, 0, 0, 0,
  85. - 1, - 1, - 1, 0, 0, 1, 0, 0,
  86. - 1, - 1, 1, 0, 0, 0, 0, 0,
  87. // Right
  88. 1, 1, 1, 0, 1, 0, 0, 0,
  89. 1, 1, - 1, 0, 1, 1, 0, 0,
  90. 1, - 1, 1, 0, 0, 0, 0, 0,
  91. 1, - 1, - 1, 0, 0, 1, 0, 0,
  92. // Top
  93. - 1, 1, 1, 0, 0, 0, 0, 0,
  94. 1, 1, 1, 0, 1, 0, 0, 0,
  95. - 1, 1, - 1, 0, 0, 1, 0, 0,
  96. 1, 1, - 1, 0, 1, 1, 0, 0,
  97. // Bottom
  98. 1, - 1, 1, 0, 1, 0, 0, 0,
  99. - 1, - 1, 1, 0, 0, 0, 0, 0,
  100. 1, - 1, - 1, 0, 1, 1, 0, 0,
  101. - 1, - 1, - 1, 0, 0, 1, 0, 0
  102. ] ), 8 );
  103. // Use vertexBuffer, starting at offset 0, 3 items in position attribute
  104. var positions = new THREE.InterleavedBufferAttribute( vertexBuffer, 3, 0 );
  105. geometry.setAttribute( 'position', positions );
  106. // Use vertexBuffer, starting at offset 4, 2 items in uv attribute
  107. var uvs = new THREE.InterleavedBufferAttribute( vertexBuffer, 2, 4 );
  108. geometry.setAttribute( 'uv', uvs );
  109. var indices = new Uint16Array( [
  110. 0, 1, 2,
  111. 2, 1, 3,
  112. 4, 5, 6,
  113. 6, 5, 7,
  114. 8, 9, 10,
  115. 10, 9, 11,
  116. 12, 13, 14,
  117. 14, 13, 15,
  118. 16, 17, 18,
  119. 18, 17, 19,
  120. 20, 21, 22,
  121. 22, 21, 23
  122. ] );
  123. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  124. // per instance data
  125. instanceBuffer = new THREE.InstancedInterleavedBuffer( new Float32Array( instances * 8 ), 8, 1 ).setUsage( THREE.DynamicDrawUsage );
  126. var offsets = new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 0 );
  127. var vector = new THREE.Vector4();
  128. for ( var i = 0, ul = offsets.count; i < ul; i ++ ) {
  129. var x = Math.random() * 100 - 50;
  130. var y = Math.random() * 100 - 50;
  131. var z = Math.random() * 100 - 50;
  132. vector.set( x, y, z, 0 ).normalize();
  133. // move out at least 5 units from center in current direction
  134. offsets.setXYZ( i, x + vector.x * 5, y + vector.y * 5, z + vector.z * 5 );
  135. }
  136. geometry.setAttribute( 'offset', offsets ); // per mesh translation
  137. orientations = new THREE.InterleavedBufferAttribute( instanceBuffer, 4, 4 );
  138. for ( var i = 0, ul = orientations.count; i < ul; i ++ ) {
  139. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  140. vector.normalize();
  141. orientations.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  142. }
  143. geometry.setAttribute( 'orientation', orientations ); // per mesh orientation
  144. // material
  145. var texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
  146. texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
  147. var material = new THREE.RawShaderMaterial( {
  148. uniforms: {
  149. map: { value: texture }
  150. },
  151. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  152. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  153. side: THREE.DoubleSide,
  154. transparent: false
  155. } );
  156. var mesh = new THREE.Mesh( geometry, material );
  157. mesh.frustumCulled = false;
  158. scene.add( mesh );
  159. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  160. document.getElementById( "notSupported" ).style.display = "";
  161. return;
  162. }
  163. renderer.setPixelRatio( window.devicePixelRatio );
  164. renderer.setSize( window.innerWidth, window.innerHeight );
  165. container.appendChild( renderer.domElement );
  166. stats = new Stats();
  167. container.appendChild( stats.dom );
  168. window.addEventListener( 'resize', onWindowResize, false );
  169. }
  170. function onWindowResize() {
  171. camera.aspect = window.innerWidth / window.innerHeight;
  172. camera.updateProjectionMatrix();
  173. renderer.setSize( window.innerWidth, window.innerHeight );
  174. }
  175. //
  176. function animate() {
  177. requestAnimationFrame( animate );
  178. render();
  179. stats.update();
  180. }
  181. var lastTime = 0;
  182. var moveQ = ( new THREE.Quaternion( .5, .5, .5, 0.0 ) ).normalize();
  183. var tmpQ = new THREE.Quaternion();
  184. var currentQ = new THREE.Quaternion();
  185. function render() {
  186. var time = performance.now();
  187. var object = scene.children[ 0 ];
  188. object.rotation.y = time * 0.00005;
  189. renderer.render( scene, camera );
  190. var delta = ( time - lastTime ) / 5000;
  191. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  192. for ( var i = 0, ul = orientations.count; i < ul; i ++ ) {
  193. var index = i * instanceBuffer.stride + orientations.offset;
  194. currentQ.set( instanceBuffer.array[ index ], instanceBuffer.array[ index + 1 ], instanceBuffer.array[ index + 2 ], instanceBuffer.array[ index + 3 ] );
  195. currentQ.multiply( tmpQ );
  196. orientations.setXYZW( i, currentQ.x, currentQ.y, currentQ.z, currentQ.w );
  197. }
  198. instanceBuffer.needsUpdate = true;
  199. lastTime = time;
  200. }
  201. init();
  202. animate();
  203. </script>
  204. </body>
  205. </html>