webgl_buffergeometry_instancing_dynamic.html 5.6 KB

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