webgpu_performance_renderbundle.html 7.2 KB

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