webgl_buffergeometry_instancing_dynamic.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - indexed instancing (single box), 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), 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, mesh;
  81. var offsetAttribute, orientationAttribute;
  82. var lastTime = 0;
  83. var moveQ = new THREE.Quaternion( 0.5, 0.5, 0.5, 0.0 ).normalize();
  84. var tmpQ = new THREE.Quaternion();
  85. var currentQ = new THREE.Quaternion();
  86. init();
  87. animate();
  88. function init() {
  89. container = document.getElementById( 'container' );
  90. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  91. scene = new THREE.Scene();
  92. scene.background = new THREE.Color( 0x101010 );
  93. // geometry
  94. var instances = 5000;
  95. var bufferGeometry = new THREE.BoxBufferGeometry( 2, 2, 2 );
  96. // copying data from a simple box geometry, but you can specify a custom geometry if you want
  97. var geometry = new THREE.InstancedBufferGeometry();
  98. geometry.index = bufferGeometry.index;
  99. geometry.attributes.position = bufferGeometry.attributes.position;
  100. geometry.attributes.uv = bufferGeometry.attributes.uv;
  101. // per instance data
  102. var offsets = [];
  103. var orientations = [];
  104. var vector = new THREE.Vector4();
  105. var x, y, z, w;
  106. for ( var i = 0; i < instances; i ++ ) {
  107. // offsets
  108. x = Math.random() * 100 - 50;
  109. y = Math.random() * 100 - 50;
  110. z = Math.random() * 100 - 50;
  111. vector.set( x, y, z, 0 ).normalize();
  112. vector.multiplyScalar( 5 ); // move out at least 5 units from center in current direction
  113. offsets.push( x + vector.x, y + vector.y, z + vector.z );
  114. // orientations
  115. x = Math.random() * 2 - 1;
  116. y = Math.random() * 2 - 1;
  117. z = Math.random() * 2 - 1;
  118. w = Math.random() * 2 - 1;
  119. vector.set( x, y, z, w ).normalize();
  120. orientations.push( vector.x, vector.y, vector.z, vector.w );
  121. }
  122. offsetAttribute = new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 );
  123. orientationAttribute = new THREE.InstancedBufferAttribute( new Float32Array( orientations ), 4 ).setDynamic( true );
  124. geometry.addAttribute( 'offset', offsetAttribute );
  125. geometry.addAttribute( 'orientation', orientationAttribute );
  126. // material
  127. var material = new THREE.RawShaderMaterial( {
  128. uniforms: {
  129. map: { value: new THREE.TextureLoader().load( 'textures/crate.gif' ) }
  130. },
  131. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  132. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  133. } );
  134. mesh = new THREE.Mesh( geometry, material );
  135. scene.add( mesh );
  136. renderer = new THREE.WebGLRenderer();
  137. renderer.setPixelRatio( window.devicePixelRatio );
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. container.appendChild( renderer.domElement );
  140. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  141. document.getElementById( 'notSupported' ).style.display = '';
  142. return;
  143. }
  144. stats = new Stats();
  145. container.appendChild( stats.dom );
  146. window.addEventListener( 'resize', onWindowResize, false );
  147. }
  148. function onWindowResize( event ) {
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. //
  154. function animate() {
  155. requestAnimationFrame( animate );
  156. render();
  157. stats.update();
  158. }
  159. function render() {
  160. var time = performance.now();
  161. mesh.rotation.y = time * 0.00005;
  162. var delta = ( time - lastTime ) / 5000;
  163. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  164. for ( var i = 0, il = orientationAttribute.count; i < il; i ++ ) {
  165. currentQ.fromArray( orientationAttribute.array, ( i * 4 ) );
  166. currentQ.multiply( tmpQ );
  167. orientationAttribute.setXYZW( i, currentQ.x, currentQ.y, currentQ.z, currentQ.w );
  168. }
  169. orientationAttribute.needsUpdate = true;
  170. lastTime = time;
  171. renderer.render( scene, camera );
  172. }
  173. </script>
  174. </body>
  175. </html>