webgl_buffergeometry_instancing_interleaved_dynamic.html 7.3 KB

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