webgl_buffergeometry_instancing_dynamic.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. <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), dynamic updates
  25. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  26. </div>
  27. <script src="../build/three.js"></script>
  28. <script src="js/WebGL.js"></script>
  29. <script src="js/libs/stats.min.js"></script>
  30. <script id="vertexShader" type="x-shader/x-vertex">
  31. precision highp float;
  32. uniform mat4 modelViewMatrix;
  33. uniform mat4 projectionMatrix;
  34. attribute vec3 position;
  35. attribute vec3 offset;
  36. attribute vec2 uv;
  37. attribute vec4 orientation;
  38. varying vec2 vUv;
  39. // http://www.geeks3d.com/20141201/how-to-rotate-a-vertex-by-a-quaternion-in-glsl/
  40. vec3 applyQuaternionToVector( vec4 q, vec3 v ){
  41. return v + 2.0 * cross( q.xyz, cross( q.xyz, v ) + q.w * v );
  42. }
  43. void main() {
  44. vec3 vPosition = applyQuaternionToVector( orientation, position );
  45. vUv = uv;
  46. gl_Position = projectionMatrix * modelViewMatrix * vec4( offset + vPosition, 1.0 );
  47. }
  48. </script>
  49. <script id="fragmentShader" type="x-shader/x-fragment">
  50. precision highp float;
  51. uniform sampler2D map;
  52. varying vec2 vUv;
  53. void main() {
  54. gl_FragColor = texture2D( map, vUv );
  55. }
  56. </script>
  57. <script>
  58. if ( WEBGL.isWebGLAvailable() === false ) {
  59. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  60. }
  61. var container, stats;
  62. var camera, scene, renderer, mesh;
  63. var offsetAttribute, orientationAttribute;
  64. var lastTime = 0;
  65. var moveQ = new THREE.Quaternion( 0.5, 0.5, 0.5, 0.0 ).normalize();
  66. var tmpQ = new THREE.Quaternion();
  67. var currentQ = new THREE.Quaternion();
  68. init();
  69. animate();
  70. function init() {
  71. container = document.getElementById( 'container' );
  72. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  73. scene = new THREE.Scene();
  74. scene.background = new THREE.Color( 0x101010 );
  75. // geometry
  76. var instances = 5000;
  77. var bufferGeometry = new THREE.BoxBufferGeometry( 2, 2, 2 );
  78. // copying data from a simple box geometry, but you can specify a custom geometry if you want
  79. var geometry = new THREE.InstancedBufferGeometry();
  80. geometry.index = bufferGeometry.index;
  81. geometry.attributes.position = bufferGeometry.attributes.position;
  82. geometry.attributes.uv = bufferGeometry.attributes.uv;
  83. // per instance data
  84. var offsets = [];
  85. var orientations = [];
  86. var vector = new THREE.Vector4();
  87. var x, y, z, w;
  88. for ( var i = 0; i < instances; i ++ ) {
  89. // offsets
  90. x = Math.random() * 100 - 50;
  91. y = Math.random() * 100 - 50;
  92. z = Math.random() * 100 - 50;
  93. vector.set( x, y, z, 0 ).normalize();
  94. vector.multiplyScalar( 5 ); // move out at least 5 units from center in current direction
  95. offsets.push( x + vector.x, y + vector.y, z + vector.z );
  96. // orientations
  97. x = Math.random() * 2 - 1;
  98. y = Math.random() * 2 - 1;
  99. z = Math.random() * 2 - 1;
  100. w = Math.random() * 2 - 1;
  101. vector.set( x, y, z, w ).normalize();
  102. orientations.push( vector.x, vector.y, vector.z, vector.w );
  103. }
  104. offsetAttribute = new THREE.InstancedBufferAttribute( new Float32Array( offsets ), 3 );
  105. orientationAttribute = new THREE.InstancedBufferAttribute( new Float32Array( orientations ), 4 ).setDynamic( true );
  106. geometry.addAttribute( 'offset', offsetAttribute );
  107. geometry.addAttribute( 'orientation', orientationAttribute );
  108. // material
  109. var material = new THREE.RawShaderMaterial( {
  110. uniforms: {
  111. map: { value: new THREE.TextureLoader().load( 'textures/crate.gif' ) }
  112. },
  113. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  114. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  115. } );
  116. mesh = new THREE.Mesh( geometry, material );
  117. scene.add( mesh );
  118. renderer = new THREE.WebGLRenderer();
  119. renderer.setPixelRatio( window.devicePixelRatio );
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. container.appendChild( renderer.domElement );
  122. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  123. document.getElementById( 'notSupported' ).style.display = '';
  124. return;
  125. }
  126. stats = new Stats();
  127. container.appendChild( stats.dom );
  128. window.addEventListener( 'resize', onWindowResize, false );
  129. }
  130. function onWindowResize( event ) {
  131. camera.aspect = window.innerWidth / window.innerHeight;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. //
  136. function animate() {
  137. requestAnimationFrame( animate );
  138. render();
  139. stats.update();
  140. }
  141. function render() {
  142. var time = performance.now();
  143. mesh.rotation.y = time * 0.00005;
  144. var delta = ( time - lastTime ) / 5000;
  145. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  146. for ( var i = 0, il = orientationAttribute.count; i < il; i ++ ) {
  147. currentQ.fromArray( orientationAttribute.array, ( i * 4 ) );
  148. currentQ.multiply( tmpQ );
  149. orientationAttribute.setXYZW( i, currentQ.x, currentQ.y, currentQ.z, currentQ.w );
  150. }
  151. orientationAttribute.needsUpdate = true;
  152. lastTime = time;
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>