webgpu_performance_renderbundle.html 6.8 KB

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