webgpu_performance_renderbundle.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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, instancedMesh;
  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. const instanceMatrix = new THREE.Matrix4();
  48. const rotationMatrix = new THREE.Matrix4();
  49. //
  50. const api = {
  51. webgpu: true,
  52. renderBundle: true,
  53. count: 4000,
  54. opacity: 1,
  55. dynamic: false
  56. };
  57. init();
  58. //
  59. function randomizeMatrix( matrix ) {
  60. position.x = Math.random() * 80 - 40;
  61. position.y = Math.random() * 80 - 40;
  62. position.z = Math.random() * 80 - 40;
  63. rotation.x = Math.random() * 2 * Math.PI;
  64. rotation.y = Math.random() * 2 * Math.PI;
  65. rotation.z = Math.random() * 2 * Math.PI;
  66. quaternion.setFromEuler( rotation );
  67. scale.x = scale.y = scale.z = 0.35 + ( Math.random() * 0.5 );
  68. return matrix.compose( position, quaternion, scale );
  69. }
  70. function randomizeRotationSpeed( rotation ) {
  71. rotation.x = Math.random() * .05;
  72. rotation.y = Math.random() * .05;
  73. rotation.z = Math.random() * .05;
  74. return rotation;
  75. }
  76. function initGeometries() {
  77. geometries = [
  78. new THREE.ConeGeometry( 1.0, 2.0, 3, 1 ),
  79. new THREE.BoxGeometry( 2.0, 2.0, 2.0 ),
  80. new THREE.PlaneGeometry( 2.0, 2, 1, 1 ),
  81. new THREE.CapsuleGeometry( ),
  82. new THREE.CircleGeometry( 1.0, 3 ),
  83. new THREE.CylinderGeometry( 1.0, 1.0, 2.0, 3, 1 ),
  84. new THREE.DodecahedronGeometry( 1.0, 0 ),
  85. new THREE.IcosahedronGeometry( 1.0, 0 ),
  86. new THREE.OctahedronGeometry( 1.0, 0 ),
  87. new THREE.PolyhedronGeometry( [ 0, 0, 0 ], [ 0, 0, 0 ], 1, 0 ),
  88. new THREE.RingGeometry( 1.0, 1.5, 3 ),
  89. new THREE.SphereGeometry( 1.0, 3, 2 ),
  90. new THREE.TetrahedronGeometry( 1.0, 0 ),
  91. new THREE.TorusGeometry( 1.0, 0.5, 3, 3 ),
  92. new THREE.TorusKnotGeometry( 1.0, 0.5, 20, 3, 1, 1 ),
  93. ];
  94. }
  95. function initMesh( count ) {
  96. group = api.renderBundle ? new THREE.BundleGroup() : new THREE.Group();
  97. for ( let i = 0; i < count; i ++ ) {
  98. const material = new THREE.MeshToonNodeMaterial( {
  99. color: new THREE.Color( Math.random() * 0xffffff ),
  100. side: THREE.DoubleSide,
  101. } );
  102. const child = new THREE.Mesh( geometries[ i % geometries.length ], material );
  103. randomizeMatrix( child.matrix );
  104. child.matrix.decompose( child.position, child.quaternion, child.scale );
  105. child.userData.rotationSpeed = randomizeRotationSpeed( new THREE.Euler() );
  106. child.frustumCulled = false;
  107. group.add( child );
  108. }
  109. // instanced meshes are supported inside a bundle group, too
  110. instancedMesh = new THREE.InstancedMesh( geometries[ 0 ], new THREE.MeshToonNodeMaterial(), 10 );
  111. instancedMesh.userData.rotationSpeeds = [];
  112. const matrix = new THREE.Matrix4();
  113. for ( let i = 0; i < instancedMesh.count; i ++ ) {
  114. randomizeMatrix( matrix );
  115. instancedMesh.setMatrixAt( i, matrix );
  116. instancedMesh.userData.rotationSpeeds.push( randomizeRotationSpeed( new THREE.Euler() ) );
  117. }
  118. group.add( instancedMesh );
  119. //
  120. scene.add( group );
  121. }
  122. function init() {
  123. const searchParams = new URLSearchParams( window.location.search );
  124. api.webgpu = searchParams.get( 'backend' ) !== 'webgl';
  125. api.renderBundle = searchParams.get( 'renderBundle' ) !== 'false';
  126. api.count = parseFloat( searchParams.get( 'count' ) || 4000 );
  127. const count = api.count;
  128. // camera
  129. const aspect = window.innerWidth / window.innerHeight;
  130. camera = new THREE.PerspectiveCamera( 70, aspect, 1, 100 );
  131. camera.position.z = 50;
  132. // renderer
  133. renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: ! api.webgpu } );
  134. renderer.setPixelRatio( window.devicePixelRatio );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. renderer.inspector = new Inspector();
  137. renderer.setAnimationLoop( animate );
  138. // scene
  139. scene = new THREE.Scene();
  140. scene.background = new THREE.Color( 0xc1c1c1 );
  141. const light = new THREE.DirectionalLight( 0xffffff, 3.4 );
  142. scene.add( light );
  143. document.body.appendChild( renderer.domElement );
  144. initGeometries();
  145. initMesh( count );
  146. controls = new OrbitControls( camera, renderer.domElement );
  147. controls.autoRotate = true;
  148. controls.autoRotateSpeed = 1.0;
  149. // gui
  150. gui = renderer.inspector.createParameters( 'Settings' );
  151. gui.add( api, 'renderBundle' ).name( 'render bundle' ).onChange( reload );
  152. gui.add( api, 'webgpu' ).onChange( reload );
  153. gui.add( api, 'dynamic' ).onChange( () => {
  154. group.static = ! group.static;
  155. } );
  156. // listeners
  157. window.addEventListener( 'resize', onWindowResize );
  158. function onWindowResize() {
  159. const width = window.innerWidth;
  160. const height = window.innerHeight;
  161. camera.aspect = width / height;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( width, height );
  164. group.needsUpdate = true;
  165. }
  166. function reload() {
  167. const backendParam = 'backend=' + ( api.webgpu ? 'webgpu' : 'webgl' );
  168. const renderBundleParam = '&renderBundle=' + ( api.renderBundle ? 'true' : 'false' );
  169. const countParam = '&count=' + api.count;
  170. location.href = location.pathname + '?' + backendParam + renderBundleParam + countParam; // relative redirect with parameters
  171. }
  172. function animate() {
  173. animateMeshes();
  174. controls.update();
  175. renderer.render( scene, camera );
  176. }
  177. function animateMeshes() {
  178. if ( api.dynamic === false ) return;
  179. // rotate the regular meshes
  180. for ( let i = 0; i < api.count; i ++ ) {
  181. const child = group.children[ i ];
  182. const rotationSpeed = child.userData.rotationSpeed;
  183. child.rotation.set(
  184. child.rotation.x + rotationSpeed.x,
  185. child.rotation.y + rotationSpeed.y,
  186. child.rotation.z + rotationSpeed.z
  187. );
  188. }
  189. // rotate each instance of the instanced mesh individually
  190. const rotationSpeeds = instancedMesh.userData.rotationSpeeds;
  191. for ( let i = 0; i < instancedMesh.count; i ++ ) {
  192. rotationMatrix.makeRotationFromEuler( rotationSpeeds[ i ] );
  193. instancedMesh.getMatrixAt( i, instanceMatrix );
  194. instanceMatrix.multiply( rotationMatrix );
  195. instancedMesh.setMatrixAt( i, instanceMatrix );
  196. }
  197. instancedMesh.instanceMatrix.needsUpdate = true;
  198. }
  199. }
  200. </script>
  201. </body>
  202. </html>
粤ICP备19079148号