webgpu_test_memory.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - memory test I</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 - memory test I">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_test_memory.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_test_memory.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><span>WebGPU Memory Test I</span>
  18. </div>
  19. <small>
  20. This example tests memory management with WebGPU renderer.
  21. <br /> Spheres are created, rendered, and disposed in each frame.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/webgpu": "../build/three.webgpu.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { pass, mrt, packNormalToRGB, normalView, screenUV, context, sample, unpackRGBToNormal } from 'three/tsl';
  37. import { outline } from 'three/addons/tsl/display/OutlineNode.js';
  38. import { ao } from 'three/addons/tsl/display/GTAONode.js';
  39. import { Inspector } from 'three/addons/inspector/Inspector.js';
  40. let camera, scene, renderer, light;
  41. let generateMeshes = true;
  42. let mesh;
  43. let renderPipeline;
  44. let aoNode, outlineNode, scenePass, prePass;
  45. const selectedObjects = [];
  46. const params = {
  47. castShadow: true,
  48. enablePP: false,
  49. enableOutline: true,
  50. enableAO: true,
  51. recreateLight: () => {
  52. // Remove existing light
  53. if ( light ) {
  54. scene.remove( light );
  55. light.dispose();
  56. light = null;
  57. }
  58. // Create new directional light
  59. light = new THREE.DirectionalLight( 0xffffff, 1 );
  60. light.position.set( Math.random() * 200 - 100, 100, Math.random() * 200 - 100 );
  61. light.castShadow = params.castShadow;
  62. scene.add( light );
  63. },
  64. start: () => {
  65. generateMeshes = true;
  66. },
  67. stop: () => {
  68. generateMeshes = false;
  69. }
  70. };
  71. init();
  72. async function init() {
  73. const container = document.createElement( 'div' );
  74. document.body.appendChild( container );
  75. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  76. camera.position.z = 200;
  77. scene = new THREE.Scene();
  78. scene.background = new THREE.Color( 0xffffff );
  79. renderer = new THREE.WebGPURenderer();
  80. renderer.shadowMap.enabled = true;
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setAnimationLoop( animate );
  84. renderer.inspector = new Inspector();
  85. container.appendChild( renderer.domElement );
  86. await renderer.init();
  87. light = new THREE.DirectionalLight( 0xffffff, 1 );
  88. light.position.set( 0, 100, 0 );
  89. light.castShadow = true;
  90. scene.add( light );
  91. const planeGeometry = new THREE.PlaneGeometry( 1000, 1000 );
  92. const planeMaterial = new THREE.MeshLambertMaterial( { color: 0xcccccc } );
  93. const plane = new THREE.Mesh( planeGeometry, planeMaterial );
  94. plane.rotation.x = - Math.PI / 2;
  95. plane.position.y = - 100;
  96. plane.receiveShadow = true;
  97. scene.add( plane );
  98. // Inspector UI
  99. const gui = renderer.inspector.createParameters( 'Settings' );
  100. gui.add( params, 'castShadow' ).name( 'Cast Shadow' ).onChange( ( value ) => {
  101. if ( light ) light.castShadow = value;
  102. } );
  103. const ppFolder = gui.addFolder( 'Post Processing' );
  104. ppFolder.add( params, 'enablePP' ).name( 'Enable' ).onChange( updatePostProcessing );
  105. ppFolder.add( params, 'enableOutline' ).name( 'Outline' ).onChange( updatePostProcessing );
  106. ppFolder.add( params, 'enableAO' ).name( 'AO' ).onChange( updatePostProcessing );
  107. gui.add( params, 'recreateLight' ).name( 'Recreate Directional Light' );
  108. gui.add( params, 'start' ).name( 'Start Creating Meshes' );
  109. gui.add( params, 'stop' ).name( 'Stop Creating Meshes' );
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function updatePostProcessing() {
  113. if ( renderPipeline ) {
  114. renderPipeline.dispose();
  115. renderPipeline = null;
  116. }
  117. if ( scenePass ) {
  118. scenePass.dispose();
  119. scenePass = null;
  120. }
  121. if ( prePass ) {
  122. prePass.dispose();
  123. prePass = null;
  124. }
  125. if ( aoNode ) {
  126. aoNode.dispose();
  127. aoNode = null;
  128. }
  129. if ( outlineNode ) {
  130. outlineNode.dispose();
  131. outlineNode = null;
  132. }
  133. if ( params.enablePP ) {
  134. renderPipeline = new THREE.RenderPipeline( renderer );
  135. scenePass = pass( scene, camera );
  136. let colorNode = scenePass;
  137. if ( params.enableAO ) {
  138. prePass = pass( scene, camera );
  139. prePass.setMRT( mrt( {
  140. output: packNormalToRGB( normalView )
  141. } ) );
  142. const prePassNormal = sample( ( uv ) => {
  143. return unpackRGBToNormal( prePass.getTextureNode().sample( uv ) );
  144. } );
  145. const prePassDepth = prePass.getTextureNode( 'depth' );
  146. aoNode = ao( prePassDepth, prePassNormal, camera );
  147. scenePass.contextNode = context( {
  148. ao: aoNode.getTextureNode().sample( screenUV ).r
  149. } );
  150. }
  151. if ( params.enableOutline ) {
  152. outlineNode = outline( scene, camera, {
  153. selectedObjects: selectedObjects
  154. } );
  155. colorNode = colorNode.add( outlineNode );
  156. }
  157. renderPipeline.outputNode = colorNode;
  158. }
  159. }
  160. function onWindowResize() {
  161. const width = window.innerWidth;
  162. const height = window.innerHeight;
  163. camera.aspect = width / height;
  164. camera.updateProjectionMatrix();
  165. renderer.setSize( width, height );
  166. }
  167. function createImage() {
  168. const canvas = document.createElement( 'canvas' );
  169. canvas.width = 256;
  170. canvas.height = 256;
  171. const canvas2DContext = canvas.getContext( '2d' );
  172. canvas2DContext.fillStyle = 'rgb(' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ')';
  173. canvas2DContext.fillRect( 0, 0, 256, 256 );
  174. return canvas;
  175. }
  176. //
  177. function animate() {
  178. if ( generateMeshes ) {
  179. if ( mesh ) {
  180. scene.remove( mesh );
  181. mesh.geometry.dispose();
  182. mesh.material.map.dispose();
  183. mesh.material.dispose();
  184. }
  185. const geometry = new THREE.SphereGeometry( 50, Math.random() * 64, Math.random() * 32 );
  186. const texture = new THREE.CanvasTexture( createImage() );
  187. const material = new THREE.MeshLambertMaterial( { map: texture } );
  188. mesh = new THREE.Mesh( geometry, material );
  189. mesh.castShadow = true;
  190. scene.add( mesh );
  191. if ( outlineNode ) {
  192. selectedObjects[ 0 ] = mesh;
  193. }
  194. }
  195. if ( renderPipeline ) {
  196. renderPipeline.render();
  197. } else {
  198. renderer.render( scene, camera );
  199. }
  200. }
  201. </script>
  202. </body>
  203. </html>
粤ICP备19079148号