webgl_buffergeometry_instancing_interleaved_dynamic.html 7.6 KB

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