webgl_buffergeometry_instancing_dynamic.html 5.8 KB

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