1
0

webgpu_performance_renderbundle.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - renderbundle</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. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - renderbundle
  17. <br />
  18. (WebGL uses 10 times fewer meshes to prevent performance issues.)
  19. </div>
  20. <div id="backend" style="position: absolute; top: 200px; left: 0; color: #fff; background-color: rgba(0,0,0,0.75); padding: 5px;">
  21. Draw Calls: 0
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.webgpu.js",
  28. "three/addons/": "./jsm/",
  29. "stats-gl": "https://cdn.jsdelivr.net/npm/stats-gl@2.2.7/dist/main.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import Stats from 'stats-gl';
  36. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. let camera, scene, renderer;
  39. let controls, stats;
  40. let gui;
  41. let geometries, group;
  42. let renderTimeAverages = [];
  43. //
  44. const position = new THREE.Vector3();
  45. const rotation = new THREE.Euler();
  46. const quaternion = new THREE.Quaternion();
  47. const scale = new THREE.Vector3();
  48. //
  49. const MAX_GEOMETRY_COUNT = 4000;
  50. const api = {
  51. webgpu: true,
  52. renderBundle: true,
  53. count: MAX_GEOMETRY_COUNT,
  54. opacity: 1,
  55. dynamic: false
  56. };
  57. init( ! api.webgpu );
  58. //
  59. function randomizeMatrix( matrix ) {
  60. position.x = Math.random() * 80 - 40;
  61. position.y = Math.random() * 80 - 40;
  62. position.z = Math.random() * 80 - 40;
  63. rotation.x = Math.random() * 2 * Math.PI;
  64. rotation.y = Math.random() * 2 * Math.PI;
  65. rotation.z = Math.random() * 2 * Math.PI;
  66. quaternion.setFromEuler( rotation );
  67. const factorScale = api.webgpu ? 1 : 2.0;
  68. scale.x = scale.y = scale.z = 0.35 * factorScale + ( Math.random() * 0.5 * factorScale );
  69. return matrix.compose( position, quaternion, scale );
  70. }
  71. function randomizeRotationSpeed( rotation ) {
  72. rotation.x = Math.random() * .05;
  73. rotation.y = Math.random() * .05;
  74. rotation.z = Math.random() * .05;
  75. return rotation;
  76. }
  77. function initGeometries() {
  78. geometries = [
  79. new THREE.ConeGeometry( 1.0, 2.0, 3, 1 ),
  80. new THREE.BoxGeometry( 2.0, 2.0, 2.0 ),
  81. new THREE.PlaneGeometry( 2.0, 2, 1, 1 ),
  82. new THREE.CapsuleGeometry( ),
  83. new THREE.CircleGeometry( 1.0, 3 ),
  84. new THREE.CylinderGeometry( 1.0, 1.0, 2.0, 3, 1 ),
  85. new THREE.DodecahedronGeometry( 1.0, 0 ),
  86. new THREE.IcosahedronGeometry( 1.0, 0 ),
  87. new THREE.OctahedronGeometry( 1.0, 0 ),
  88. new THREE.PolyhedronGeometry( [ 0, 0, 0 ], [ 0, 0, 0 ], 1, 0 ),
  89. new THREE.RingGeometry( 1.0, 1.5, 3 ),
  90. new THREE.SphereGeometry( 1.0, 3, 2 ),
  91. new THREE.TetrahedronGeometry( 1.0, 0 ),
  92. new THREE.TorusGeometry( 1.0, 0.5, 3, 3 ),
  93. new THREE.TorusKnotGeometry( 1.0, 0.5, 20, 3, 1, 1 ),
  94. ];
  95. }
  96. function cleanup() {
  97. if ( group ) {
  98. group.parent.remove( group );
  99. if ( group.dispose ) {
  100. group.dispose();
  101. }
  102. }
  103. }
  104. function initMesh( count ) {
  105. cleanup();
  106. initRegularMesh( count );
  107. }
  108. function initRegularMesh( count ) {
  109. group = api.renderBundle ? new THREE.BundleGroup() : new THREE.Group();
  110. for ( let i = 0; i < count; i ++ ) {
  111. const material = new THREE.MeshToonNodeMaterial( {
  112. color: new THREE.Color( Math.random() * 0xffffff ),
  113. side: THREE.DoubleSide,
  114. } );
  115. const child = new THREE.Mesh( geometries[ i % geometries.length ], material );
  116. randomizeMatrix( child.matrix );
  117. child.matrix.decompose( child.position, child.quaternion, child.scale );
  118. child.userData.rotationSpeed = randomizeRotationSpeed( new THREE.Euler() );
  119. child.frustumCulled = false;
  120. group.add( child );
  121. }
  122. scene.add( group );
  123. }
  124. async function init( forceWebGL = false ) {
  125. const count = api.count / ( api.webgpu ? 1 : 10 );
  126. renderTimeAverages = [];
  127. if ( renderer ) {
  128. renderer.dispose();
  129. controls.dispose();
  130. document.body.removeChild( stats.dom );
  131. document.body.removeChild( renderer.domElement );
  132. }
  133. // camera
  134. const aspect = window.innerWidth / window.innerHeight;
  135. camera = new THREE.PerspectiveCamera( 70, aspect, 1, 100 );
  136. camera.position.z = 50;
  137. // renderer
  138. renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL } );
  139. renderer.setPixelRatio( window.devicePixelRatio );
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. renderer.setAnimationLoop( animate );
  142. // scene
  143. scene = new THREE.Scene();
  144. scene.background = new THREE.Color( 0xc1c1c1 );
  145. const light = new THREE.DirectionalLight( 0xffffff, 3.4 );
  146. scene.add( light );
  147. document.body.appendChild( renderer.domElement );
  148. await renderer.init();
  149. initGeometries();
  150. initMesh( count );
  151. controls = new OrbitControls( camera, renderer.domElement );
  152. controls.autoRotate = true;
  153. controls.autoRotateSpeed = 1.0;
  154. // stats
  155. stats = new Stats( {
  156. precision: 3,
  157. horizontal: false
  158. } );
  159. // stats.init( renderer );
  160. document.body.appendChild( stats.dom );
  161. stats.dom.style.position = 'absolute';
  162. // gui
  163. gui = new GUI();
  164. gui.add( api, 'renderBundle' ).onChange( () => {
  165. init( ! api.webgpu );
  166. } );
  167. gui.add( api, 'webgpu' ).onChange( () => {
  168. init( ! api.webgpu );
  169. } );
  170. gui.add( api, 'dynamic' ).onChange( () => {
  171. group.static = ! group.static;
  172. } );
  173. // listeners
  174. window.addEventListener( 'resize', onWindowResize );
  175. function onWindowResize() {
  176. const width = window.innerWidth;
  177. const height = window.innerHeight;
  178. camera.aspect = width / height;
  179. camera.updateProjectionMatrix();
  180. renderer.setSize( width, height );
  181. group.needsUpdate = true;
  182. }
  183. async function animate() {
  184. animateMeshes();
  185. controls.update();
  186. const renderTimeAverage = performance.now();
  187. renderer.render( scene, camera );
  188. // push only the last 60 render times
  189. renderTimeAverages.push( performance.now() - renderTimeAverage );
  190. if ( renderTimeAverages.length > 60 ) renderTimeAverages.shift();
  191. const average = renderTimeAverages.reduce( ( a, b ) => a + b, 0 ) / renderTimeAverages.length;
  192. stats.update();
  193. document.getElementById( 'backend' ).innerText = `Average Render Time ${api.renderBundle ? '(Bundle)' : ''}: ` + average.toFixed( 2 ) + 'ms';
  194. }
  195. function animateMeshes() {
  196. const count = api.count / ( api.webgpu ? 1 : 10 );
  197. const loopNum = api.dynamic ? count : 0;
  198. for ( let i = 0; i < loopNum; i ++ ) {
  199. const child = group.children[ i ];
  200. const rotationSpeed = child.userData.rotationSpeed;
  201. child.rotation.set(
  202. child.rotation.x + rotationSpeed.x,
  203. child.rotation.y + rotationSpeed.y,
  204. child.rotation.z + rotationSpeed.z
  205. );
  206. }
  207. }
  208. }
  209. </script>
  210. </body>
  211. </html>