2
0

webgl_buffergeometry_instancing_dynamic.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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">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;
  77. var orientations;
  78. function init() {
  79. container = document.getElementById( 'container' );
  80. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  81. //camera.position.z = 20;
  82. renderer = new THREE.WebGLRenderer();
  83. scene = new THREE.Scene();
  84. // geometry
  85. var instances = 5000;
  86. var geometry = new THREE.InstancedBufferGeometry();
  87. // per mesh data
  88. var vertices = new THREE.BufferAttribute( new Float32Array( [
  89. // Front
  90. -1, 1, 1,
  91. 1, 1, 1,
  92. -1, -1, 1,
  93. 1, -1, 1,
  94. // Back
  95. 1, 1, -1,
  96. -1, 1, -1,
  97. 1, -1, -1,
  98. -1, -1, -1,
  99. // Left
  100. -1, 1, -1,
  101. -1, 1, 1,
  102. -1, -1, -1,
  103. -1, -1, 1,
  104. // Right
  105. 1, 1, 1,
  106. 1, 1, -1,
  107. 1, -1, 1,
  108. 1, -1, -1,
  109. // Top
  110. -1, 1, 1,
  111. 1, 1, 1,
  112. -1, 1, -1,
  113. 1, 1, -1,
  114. // Bottom
  115. 1, -1, 1,
  116. -1, -1, 1,
  117. 1, -1, -1,
  118. -1, -1, -1
  119. ] ), 3 );
  120. geometry.addAttribute( 'position', vertices );
  121. var uvs = new THREE.BufferAttribute( new Float32Array( [
  122. //x y z
  123. // Front
  124. 0, 0,
  125. 1, 0,
  126. 0, 1,
  127. 1, 1,
  128. // Back
  129. 1, 0,
  130. 0, 0,
  131. 1, 1,
  132. 0, 1,
  133. // Left
  134. 1, 1,
  135. 1, 0,
  136. 0, 1,
  137. 0, 0,
  138. // Right
  139. 1, 0,
  140. 1, 1,
  141. 0, 0,
  142. 0, 1,
  143. // Top
  144. 0, 0,
  145. 1, 0,
  146. 0, 1,
  147. 1, 1,
  148. // Bottom
  149. 1, 0,
  150. 0, 0,
  151. 1, 1,
  152. 0, 1
  153. ] ), 2 );
  154. geometry.addAttribute( 'uv', uvs );
  155. var indices = new Uint16Array( [
  156. 0, 1, 2,
  157. 2, 1, 3,
  158. 4, 5, 6,
  159. 6, 5, 7,
  160. 8, 9, 10,
  161. 10, 9, 11,
  162. 12, 13, 14,
  163. 14, 13, 15,
  164. 16, 17, 18,
  165. 18, 17, 19,
  166. 20, 21, 22,
  167. 22, 21, 23
  168. ] );
  169. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  170. // per instance data
  171. var offsets = new THREE.InstancedBufferAttribute( new Float32Array( instances * 3 ), 3, 1 );
  172. var vector = new THREE.Vector4();
  173. for ( var i = 0, ul = offsets.count; i < ul; i++ ) {
  174. var x = Math.random() * 100 - 50;
  175. var y = Math.random() * 100 - 50;
  176. var z = Math.random() * 100 - 50;
  177. vector.set( x, y, z, 0 ).normalize();
  178. // move out at least 5 units from center in current direction
  179. offsets.setXYZ( i, x + vector.x * 5, y + vector.y * 5, z + vector.z * 5 );
  180. }
  181. geometry.addAttribute( 'offset', offsets ); // per mesh translation
  182. orientations = new THREE.InstancedBufferAttribute( new Float32Array( instances * 4 ), 4, 1 ).setDynamic( true );
  183. for ( var i = 0, ul = orientations.count; i < ul; i++ ) {
  184. vector.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  185. vector.normalize();
  186. orientations.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  187. }
  188. geometry.addAttribute( 'orientation', orientations ); // per mesh orientation
  189. // material
  190. var texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
  191. texture.anisotropy = renderer.getMaxAnisotropy();
  192. var material = new THREE.RawShaderMaterial( {
  193. uniforms: {
  194. map: { value: texture }
  195. },
  196. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  197. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  198. side: THREE.DoubleSide,
  199. transparent: false
  200. } );
  201. var mesh = new THREE.Mesh( geometry, material );
  202. scene.add( mesh );
  203. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === false ) {
  204. document.getElementById( "notSupported" ).style.display = "";
  205. return;
  206. }
  207. renderer.setClearColor( 0x101010 );
  208. renderer.setPixelRatio( window.devicePixelRatio );
  209. renderer.setSize( window.innerWidth, window.innerHeight );
  210. container.appendChild( renderer.domElement );
  211. stats = new Stats();
  212. container.appendChild( stats.dom );
  213. window.addEventListener( 'resize', onWindowResize, false );
  214. }
  215. function onWindowResize( event ) {
  216. camera.aspect = window.innerWidth / window.innerHeight;
  217. camera.updateProjectionMatrix();
  218. renderer.setSize( window.innerWidth, window.innerHeight );
  219. }
  220. //
  221. function animate() {
  222. requestAnimationFrame( animate );
  223. render();
  224. stats.update();
  225. }
  226. var lastTime = 0;
  227. var moveQ = ( new THREE.Quaternion( .5, .5, .5, 0.0 ) ).normalize();
  228. var tmpQ = new THREE.Quaternion();
  229. var currentQ = new THREE.Quaternion();
  230. function render() {
  231. var time = performance.now();
  232. var object = scene.children[0];
  233. object.rotation.y = time * 0.00005;
  234. renderer.render( scene, camera );
  235. var delta = ( time - lastTime ) / 5000;
  236. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  237. for ( var i = 0, ul = orientations.count; i < ul; i++ ) {
  238. var index = i * 4;
  239. currentQ.set( orientations.array[index], orientations.array[index + 1], orientations.array[index + 2], orientations.array[index + 3] );
  240. currentQ.multiply( tmpQ );
  241. orientations.setXYZW( i, currentQ.x, currentQ.y, currentQ.z, currentQ.w );
  242. }
  243. orientations.needsUpdate = true;
  244. lastTime = time;
  245. }
  246. init();
  247. animate();
  248. </script>
  249. </body>
  250. </html>