webgpu_performance_renderbundle.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - Render Bundle</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. <div id="info" class="invert">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a>
  14. <span>Render Bundle</span>
  15. </div>
  16. <small>
  17. Performance Optimization with Render Bundles. Only supported with WebGPU.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/",
  27. "stats-gl": "https://cdn.jsdelivr.net/npm/stats-gl@3.6.0/dist/main.js"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. let camera, scene, renderer;
  36. let controls;
  37. let gui;
  38. let geometries, group;
  39. //
  40. const position = new THREE.Vector3();
  41. const rotation = new THREE.Euler();
  42. const quaternion = new THREE.Quaternion();
  43. const scale = new THREE.Vector3();
  44. //
  45. const api = {
  46. webgpu: true,
  47. renderBundle: true,
  48. count: 4000,
  49. opacity: 1,
  50. dynamic: false
  51. };
  52. init();
  53. //
  54. function randomizeMatrix( matrix ) {
  55. position.x = Math.random() * 80 - 40;
  56. position.y = Math.random() * 80 - 40;
  57. position.z = Math.random() * 80 - 40;
  58. rotation.x = Math.random() * 2 * Math.PI;
  59. rotation.y = Math.random() * 2 * Math.PI;
  60. rotation.z = Math.random() * 2 * Math.PI;
  61. quaternion.setFromEuler( rotation );
  62. scale.x = scale.y = scale.z = 0.35 + ( Math.random() * 0.5 );
  63. return matrix.compose( position, quaternion, scale );
  64. }
  65. function randomizeRotationSpeed( rotation ) {
  66. rotation.x = Math.random() * .05;
  67. rotation.y = Math.random() * .05;
  68. rotation.z = Math.random() * .05;
  69. return rotation;
  70. }
  71. function initGeometries() {
  72. geometries = [
  73. new THREE.ConeGeometry( 1.0, 2.0, 3, 1 ),
  74. new THREE.BoxGeometry( 2.0, 2.0, 2.0 ),
  75. new THREE.PlaneGeometry( 2.0, 2, 1, 1 ),
  76. new THREE.CapsuleGeometry( ),
  77. new THREE.CircleGeometry( 1.0, 3 ),
  78. new THREE.CylinderGeometry( 1.0, 1.0, 2.0, 3, 1 ),
  79. new THREE.DodecahedronGeometry( 1.0, 0 ),
  80. new THREE.IcosahedronGeometry( 1.0, 0 ),
  81. new THREE.OctahedronGeometry( 1.0, 0 ),
  82. new THREE.PolyhedronGeometry( [ 0, 0, 0 ], [ 0, 0, 0 ], 1, 0 ),
  83. new THREE.RingGeometry( 1.0, 1.5, 3 ),
  84. new THREE.SphereGeometry( 1.0, 3, 2 ),
  85. new THREE.TetrahedronGeometry( 1.0, 0 ),
  86. new THREE.TorusGeometry( 1.0, 0.5, 3, 3 ),
  87. new THREE.TorusKnotGeometry( 1.0, 0.5, 20, 3, 1, 1 ),
  88. ];
  89. }
  90. function initMesh( count ) {
  91. initRegularMesh( count );
  92. }
  93. function initRegularMesh( count ) {
  94. group = api.renderBundle ? new THREE.BundleGroup() : new THREE.Group();
  95. for ( let i = 0; i < count; i ++ ) {
  96. const material = new THREE.MeshToonNodeMaterial( {
  97. color: new THREE.Color( Math.random() * 0xffffff ),
  98. side: THREE.DoubleSide,
  99. } );
  100. const child = new THREE.Mesh( geometries[ i % geometries.length ], material );
  101. randomizeMatrix( child.matrix );
  102. child.matrix.decompose( child.position, child.quaternion, child.scale );
  103. child.userData.rotationSpeed = randomizeRotationSpeed( new THREE.Euler() );
  104. child.frustumCulled = false;
  105. group.add( child );
  106. }
  107. scene.add( group );
  108. }
  109. function init() {
  110. const searchParams = new URLSearchParams( window.location.search );
  111. api.webgpu = searchParams.get( 'backend' ) !== 'webgl';
  112. api.renderBundle = searchParams.get( 'renderBundle' ) !== 'false';
  113. api.count = parseFloat( searchParams.get( 'count' ) || 4000 );
  114. const count = api.count;
  115. // camera
  116. const aspect = window.innerWidth / window.innerHeight;
  117. camera = new THREE.PerspectiveCamera( 70, aspect, 1, 100 );
  118. camera.position.z = 50;
  119. // renderer
  120. renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: ! api.webgpu } );
  121. renderer.setPixelRatio( window.devicePixelRatio );
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. renderer.inspector = new Inspector();
  124. renderer.setAnimationLoop( animate );
  125. // scene
  126. scene = new THREE.Scene();
  127. scene.background = new THREE.Color( 0xc1c1c1 );
  128. const light = new THREE.DirectionalLight( 0xffffff, 3.4 );
  129. scene.add( light );
  130. document.body.appendChild( renderer.domElement );
  131. initGeometries();
  132. initMesh( count );
  133. controls = new OrbitControls( camera, renderer.domElement );
  134. controls.autoRotate = true;
  135. controls.autoRotateSpeed = 1.0;
  136. // gui
  137. gui = renderer.inspector.createParameters( 'Settings' );
  138. gui.add( api, 'renderBundle' ).name( 'render bundle' ).onChange( reload );
  139. gui.add( api, 'webgpu' ).onChange( reload );
  140. gui.add( api, 'dynamic' ).onChange( () => {
  141. group.static = ! group.static;
  142. } );
  143. // listeners
  144. window.addEventListener( 'resize', onWindowResize );
  145. function onWindowResize() {
  146. const width = window.innerWidth;
  147. const height = window.innerHeight;
  148. camera.aspect = width / height;
  149. camera.updateProjectionMatrix();
  150. renderer.setSize( width, height );
  151. group.needsUpdate = true;
  152. }
  153. function reload() {
  154. const backendParam = 'backend=' + ( api.webgpu ? 'webgpu' : 'webgl' );
  155. const renderBundleParam = '&renderBundle=' + ( api.renderBundle ? 'true' : 'false' );
  156. const countParam = '&count=' + api.count;
  157. location.href = location.pathname + '?' + backendParam + renderBundleParam + countParam; // relative redirect with parameters
  158. }
  159. function animate() {
  160. animateMeshes();
  161. controls.update();
  162. renderer.render( scene, camera );
  163. }
  164. function animateMeshes() {
  165. const count = api.count;
  166. const loopNum = api.dynamic ? count : 0;
  167. for ( let i = 0; i < loopNum; i ++ ) {
  168. const child = group.children[ i ];
  169. const rotationSpeed = child.userData.rotationSpeed;
  170. child.rotation.set(
  171. child.rotation.x + rotationSpeed.x,
  172. child.rotation.y + rotationSpeed.y,
  173. child.rotation.z + rotationSpeed.z
  174. );
  175. }
  176. }
  177. }
  178. </script>
  179. </body>
  180. </html>
粤ICP备19079148号