webgl_buffergeometry_instancing_interleaved_dynamic.html 7.4 KB

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