webgl_buffergeometry_instancing_dynamic.html 5.7 KB

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