2
0

webgl_buffergeometry_instancing_interleaved_dynamic.html 7.5 KB

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