1
0

webgpu_performance_renderbundle.html 6.4 KB

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