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