webgpu_test_memory.html 6.9 KB

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