1
0

webgpu_performance_renderbundle.html 7.1 KB

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