webgl_buffergeometry_instancing_dynamic.html 5.9 KB

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