webgl_instancing_performance.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - performance</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. #info {
  10. background-color: rgba(0,0,0,0.75);
  11. }
  12. .dg .folder .gui-stats {
  13. height: auto;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - instancing - performance
  20. </div>
  21. <div id="container"></div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/dat.gui.module.js';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js';
  28. var container, stats, gui, guiStatsEl;
  29. var camera, controls, scene, renderer, material;
  30. // gui
  31. var Method = {
  32. INSTANCED: 'INSTANCED',
  33. MERGED: 'MERGED',
  34. NAIVE: 'NAIVE'
  35. };
  36. var api = {
  37. method: Method.INSTANCED,
  38. count: 1000
  39. };
  40. //
  41. init();
  42. initMesh();
  43. animate();
  44. //
  45. function clean() {
  46. var meshes = [];
  47. scene.traverse( function ( object ) {
  48. if ( object.isMesh ) meshes.push( object );
  49. } );
  50. for ( var i = 0; i < meshes.length; i ++ ) {
  51. var mesh = meshes[ i ];
  52. mesh.material.dispose();
  53. mesh.geometry.dispose();
  54. scene.remove( mesh );
  55. }
  56. }
  57. var randomizeMatrix = function () {
  58. var position = new THREE.Vector3();
  59. var rotation = new THREE.Euler();
  60. var quaternion = new THREE.Quaternion();
  61. var scale = new THREE.Vector3();
  62. return function ( matrix ) {
  63. position.x = Math.random() * 40 - 20;
  64. position.y = Math.random() * 40 - 20;
  65. position.z = Math.random() * 40 - 20;
  66. rotation.x = Math.random() * 2 * Math.PI;
  67. rotation.y = Math.random() * 2 * Math.PI;
  68. rotation.z = Math.random() * 2 * Math.PI;
  69. quaternion.setFromEuler( rotation );
  70. scale.x = scale.y = scale.z = Math.random() * 1;
  71. matrix.compose( position, quaternion, scale );
  72. };
  73. }();
  74. function initMesh() {
  75. clean();
  76. // make instances
  77. new THREE.BufferGeometryLoader()
  78. .setPath( 'models/json/' )
  79. .load( 'suzanne_buffergeometry.json', function ( geometry ) {
  80. material = new THREE.MeshNormalMaterial();
  81. geometry.computeVertexNormals();
  82. console.time( api.method + ' (build)' );
  83. switch ( api.method ) {
  84. case Method.INSTANCED:
  85. makeInstanced( geometry );
  86. break;
  87. case Method.MERGED:
  88. makeMerged( geometry );
  89. break;
  90. case Method.NAIVE:
  91. makeNaive( geometry );
  92. break;
  93. }
  94. console.timeEnd( api.method + ' (build)' );
  95. } );
  96. }
  97. function makeInstanced( geometry ) {
  98. var matrix = new THREE.Matrix4();
  99. var mesh = new THREE.InstancedMesh( geometry, material, api.count );
  100. for ( var i = 0; i < api.count; i ++ ) {
  101. randomizeMatrix( matrix );
  102. mesh.setMatrixAt( i, matrix );
  103. }
  104. scene.add( mesh );
  105. //
  106. var geometryByteLength = getGeometryByteLength( geometry );
  107. guiStatsEl.innerHTML = [
  108. '<i>GPU draw calls</i>: 1',
  109. '<i>GPU memory</i>: ' + formatBytes( api.count * 16 + geometryByteLength, 2 )
  110. ].join( '<br/>' );
  111. }
  112. function makeMerged( geometry ) {
  113. var instanceGeometry;
  114. var geometries = [];
  115. var matrix = new THREE.Matrix4();
  116. for ( var i = 0; i < api.count; i ++ ) {
  117. randomizeMatrix( matrix );
  118. var instanceGeometry = geometry.clone();
  119. instanceGeometry.applyMatrix4( matrix );
  120. geometries.push( instanceGeometry );
  121. }
  122. var mergedGeometry = BufferGeometryUtils.mergeBufferGeometries( geometries );
  123. scene.add( new THREE.Mesh( mergedGeometry, material ) );
  124. //
  125. guiStatsEl.innerHTML = [
  126. '<i>GPU draw calls</i>: 1',
  127. '<i>GPU memory</i>: ' + formatBytes( getGeometryByteLength( mergedGeometry ), 2 )
  128. ].join( '<br/>' );
  129. }
  130. function makeNaive( geometry ) {
  131. var matrix = new THREE.Matrix4();
  132. for ( var i = 0; i < api.count; i ++ ) {
  133. randomizeMatrix( matrix );
  134. var mesh = new THREE.Mesh( geometry, material );
  135. mesh.applyMatrix4( matrix );
  136. scene.add( mesh );
  137. }
  138. //
  139. var geometryByteLength = getGeometryByteLength( geometry );
  140. guiStatsEl.innerHTML = [
  141. '<i>GPU draw calls</i>: ' + api.count,
  142. '<i>GPU memory</i>: ' + formatBytes( api.count * 16 + geometryByteLength, 2 )
  143. ].join( '<br/>' );
  144. }
  145. function init() {
  146. var width = window.innerWidth;
  147. var height = window.innerHeight;
  148. // camera
  149. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 100 );
  150. camera.position.z = 30;
  151. // renderer
  152. renderer = new THREE.WebGLRenderer( { antialias: true } );
  153. renderer.setPixelRatio( window.devicePixelRatio );
  154. renderer.setSize( width, height );
  155. renderer.outputEncoding = THREE.sRGBEncoding;
  156. container = document.getElementById( 'container' );
  157. container.appendChild( renderer.domElement );
  158. // scene
  159. scene = new THREE.Scene();
  160. scene.background = new THREE.Color( 0xffffff );
  161. // controls
  162. controls = new OrbitControls( camera, renderer.domElement );
  163. controls.autoRotate = true;
  164. // stats
  165. stats = new Stats();
  166. container.appendChild( stats.dom );
  167. // gui
  168. gui = new GUI();
  169. gui.add( api, 'method', Method ).onChange( initMesh );
  170. gui.add( api, 'count', 1, 10000 ).step( 1 ).onChange( initMesh );
  171. var perfFolder = gui.addFolder( 'Performance' );
  172. guiStatsEl = document.createElement( 'li' );
  173. guiStatsEl.classList.add( 'gui-stats' );
  174. perfFolder.__ul.appendChild( guiStatsEl );
  175. perfFolder.open();
  176. // listeners
  177. window.addEventListener( 'resize', onWindowResize, false );
  178. Object.assign(window, {scene});
  179. }
  180. //
  181. function onWindowResize() {
  182. var width = window.innerWidth;
  183. var height = window.innerHeight;
  184. camera.aspect = width / height;
  185. camera.updateProjectionMatrix();
  186. renderer.setSize( width, height );
  187. }
  188. function animate() {
  189. requestAnimationFrame( animate );
  190. controls.update();
  191. stats.update();
  192. render();
  193. }
  194. function render() {
  195. renderer.render( scene, camera );
  196. }
  197. //
  198. function getGeometryByteLength( geometry ) {
  199. var total = 0;
  200. if ( geometry.index ) total += geometry.index.array.byteLength;
  201. for ( var name in geometry.attributes ) {
  202. total += geometry.attributes[ name ].array.byteLength;
  203. }
  204. return total;
  205. }
  206. // Source: https://stackoverflow.com/a/18650828/1314762
  207. function formatBytes( bytes, decimals ) {
  208. if ( bytes === 0 ) return '0 bytes';
  209. var k = 1024;
  210. var dm = decimals < 0 ? 0 : decimals;
  211. var sizes = [ 'bytes', 'KB', 'MB' ];
  212. var i = Math.floor( Math.log( bytes ) / Math.log( k ) );
  213. return parseFloat( ( bytes / Math.pow( k, i ) ).toFixed( dm ) ) + ' ' + sizes[ i ];
  214. }
  215. </script>
  216. </body>
  217. </html>