webgl_buffergeometry_instancing_dynamic.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 type="module">
  28. import * as THREE from '../build/three.module.js';
  29. import Stats from './jsm/libs/stats.module.js';
  30. var container, stats;
  31. var camera, scene, renderer, mesh;
  32. var offsetAttribute, orientationAttribute;
  33. var instances = 5000;
  34. var lastTime = 0;
  35. var moveQ = new THREE.Quaternion( 0.5, 0.5, 0.5, 0.0 ).normalize();
  36. var tmpQ = new THREE.Quaternion();
  37. var tmpM = new THREE.Matrix4();
  38. var currentM = new THREE.Matrix4();
  39. init();
  40. animate();
  41. function init() {
  42. container = document.getElementById( 'container' );
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0x101010 );
  46. // geometry
  47. var bufferGeometry = new THREE.BoxBufferGeometry( 2, 2, 2 );
  48. // copying data from a simple box geometry, but you can specify a custom geometry if you want
  49. var geometry = new THREE.InstancedBufferGeometry();
  50. geometry.setIndex( bufferGeometry.index );
  51. geometry.setAttribute( 'position', bufferGeometry.attributes.position );
  52. geometry.setAttribute( 'uv', bufferGeometry.attributes.uv );
  53. // material
  54. var material = new THREE.MeshBasicMaterial();
  55. material.map = new THREE.TextureLoader().load( 'textures/crate.gif' );
  56. // per instance data
  57. var matrix = new THREE.Matrix4();
  58. var offset = new THREE.Vector3();
  59. var orientation = new THREE.Quaternion();
  60. var scale = new THREE.Vector3( 1, 1, 1 );
  61. var x, y, z, w;
  62. mesh = new THREE.InstancedMesh( geometry, material, instances );
  63. for ( var i = 0; i < instances; i ++ ) {
  64. // offsets
  65. x = Math.random() * 100 - 50;
  66. y = Math.random() * 100 - 50;
  67. z = Math.random() * 100 - 50;
  68. offset.set( x, y, z ).normalize();
  69. offset.multiplyScalar( 5 ); // move out at least 5 units from center in current direction
  70. offset.set( x + offset.x, y + offset.y, z + offset.z );
  71. // orientations
  72. x = Math.random() * 2 - 1;
  73. y = Math.random() * 2 - 1;
  74. z = Math.random() * 2 - 1;
  75. w = Math.random() * 2 - 1;
  76. orientation.set( x, y, z, w ).normalize();
  77. matrix.compose( offset, orientation, scale );
  78. mesh.setMatrixAt( i, matrix );
  79. }
  80. scene.add( mesh );
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild( renderer.domElement );
  85. if ( renderer.extensions.get( 'ANGLE_instanced_arrays' ) === null ) {
  86. document.getElementById( 'notSupported' ).style.display = '';
  87. return;
  88. }
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. //
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. render();
  102. stats.update();
  103. }
  104. function render() {
  105. var time = performance.now();
  106. mesh.rotation.y = time * 0.00005;
  107. var delta = ( time - lastTime ) / 5000;
  108. tmpQ.set( moveQ.x * delta, moveQ.y * delta, moveQ.z * delta, 1 ).normalize();
  109. tmpM.makeRotationFromQuaternion( tmpQ );
  110. for ( var i = 0, il = instances; i < il; i ++ ) {
  111. mesh.getMatrixAt( i, currentM );
  112. currentM.multiply( tmpM );
  113. mesh.setMatrixAt( i, currentM );
  114. }
  115. mesh.instanceMatrix.needsUpdate = true;
  116. lastTime = time;
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>