webgpu_compute_texture_pingpong.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - compute ping/pong texture</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <meta property="og:title" content="three.js webgpu - compute ping/pong texture">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_compute_texture_pingpong.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_compute_texture_pingpong.jpg">
  10. <link type="text/css" rel="stylesheet" href="example.css">
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  15. <div class="title-wrapper">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a>
  17. <span>Compute Ping/Pong Texture</span>
  18. </div>
  19. <small>
  20. Compute ping/pong texture using GPU (pure TSL).
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { storageTexture, textureStore, Fn, instanceIndex, uniform, float, vec2, vec4, uvec2, ivec2, int, NodeAccess } from 'three/tsl';
  36. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  37. let camera, scene, renderer;
  38. let computeInitNode, computeToPing, computeToPong;
  39. let pingTexture, pongTexture;
  40. let material;
  41. let phase = true;
  42. let lastUpdate = - 1;
  43. const width = 512, height = 512;
  44. const seed = uniform( new THREE.Vector2() );
  45. init();
  46. async function init() {
  47. if ( WebGPU.isAvailable() === false ) {
  48. document.body.appendChild( WebGPU.getErrorMessage() );
  49. throw new Error( 'No WebGPU support' );
  50. }
  51. const aspect = window.innerWidth / window.innerHeight;
  52. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  53. camera.position.z = 1;
  54. scene = new THREE.Scene();
  55. // texture
  56. const hdr = true;
  57. pingTexture = new THREE.StorageTexture( width, height );
  58. pongTexture = new THREE.StorageTexture( width, height );
  59. if ( hdr ) {
  60. pingTexture.type = THREE.HalfFloatType;
  61. pongTexture.type = THREE.HalfFloatType;
  62. }
  63. const rand2 = Fn( ( [ n ] ) => {
  64. return n.dot( vec2( 12.9898, 4.1414 ) ).sin().mul( 43758.5453 ).fract();
  65. } );
  66. // Create storage texture nodes with proper access
  67. const writePing = storageTexture( pingTexture ).setAccess( NodeAccess.WRITE_ONLY );
  68. const readPing = storageTexture( pingTexture ).setAccess( NodeAccess.READ_ONLY );
  69. const writePong = storageTexture( pongTexture ).setAccess( NodeAccess.WRITE_ONLY );
  70. const readPong = storageTexture( pongTexture ).setAccess( NodeAccess.READ_ONLY );
  71. const computeInit = Fn( () => {
  72. const posX = instanceIndex.mod( width );
  73. const posY = instanceIndex.div( width );
  74. const indexUV = uvec2( posX, posY );
  75. const uv = vec2( float( posX ).div( width ), float( posY ).div( height ) );
  76. const r = rand2( uv.add( seed.mul( 100 ) ) ).sub( rand2( uv.add( seed.mul( 300 ) ) ) );
  77. const g = rand2( uv.add( seed.mul( 200 ) ) ).sub( rand2( uv.add( seed.mul( 300 ) ) ) );
  78. const b = rand2( uv.add( seed.mul( 200 ) ) ).sub( rand2( uv.add( seed.mul( 100 ) ) ) );
  79. textureStore( writePing, indexUV, vec4( r, g, b, 1 ) );
  80. } );
  81. computeInitNode = computeInit().compute( width * height );
  82. // compute ping-pong: blur function using .load() for textureLoad
  83. const blur = Fn( ( [ readTex, uv ] ) => {
  84. const c0 = readTex.load( uv.add( ivec2( - 1, 1 ) ) );
  85. const c1 = readTex.load( uv.add( ivec2( - 1, - 1 ) ) );
  86. const c2 = readTex.load( uv.add( ivec2( 0, 0 ) ) );
  87. const c3 = readTex.load( uv.add( ivec2( 1, - 1 ) ) );
  88. const c4 = readTex.load( uv.add( ivec2( 1, 1 ) ) );
  89. return c0.add( c1 ).add( c2 ).add( c3 ).add( c4 ).div( 5.0 );
  90. } );
  91. // compute loop: read from one texture, blur, write to another
  92. const computePingPong = Fn( ( [ readTex, writeTex ] ) => {
  93. const posX = instanceIndex.mod( width );
  94. const posY = instanceIndex.div( width );
  95. const indexUV = ivec2( int( posX ), int( posY ) );
  96. const color = blur( readTex, indexUV );
  97. textureStore( writeTex, indexUV, vec4( color.rgb.mul( 1.05 ), 1 ) );
  98. } );
  99. computeToPong = computePingPong( readPing, writePong ).compute( width * height );
  100. computeToPing = computePingPong( readPong, writePing ).compute( width * height );
  101. //
  102. material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
  103. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  104. scene.add( plane );
  105. renderer = new THREE.WebGPURenderer( { antialias: true } );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. renderer.setAnimationLoop( render );
  109. document.body.appendChild( renderer.domElement );
  110. await renderer.init();
  111. window.addEventListener( 'resize', onWindowResize );
  112. // compute init
  113. renderer.compute( computeInitNode );
  114. }
  115. function onWindowResize() {
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. const aspect = window.innerWidth / window.innerHeight;
  118. const frustumHeight = camera.top - camera.bottom;
  119. camera.left = - frustumHeight * aspect / 2;
  120. camera.right = frustumHeight * aspect / 2;
  121. camera.updateProjectionMatrix();
  122. }
  123. function render() {
  124. const time = performance.now();
  125. const seconds = Math.floor( time / 1000 );
  126. // reset every second
  127. if ( phase && seconds !== lastUpdate ) {
  128. seed.value.set( Math.random(), Math.random() );
  129. renderer.compute( computeInitNode );
  130. lastUpdate = seconds;
  131. }
  132. // compute step
  133. renderer.compute( phase ? computeToPong : computeToPing );
  134. material.map = phase ? pongTexture : pingTexture;
  135. phase = ! phase;
  136. // render step
  137. // update material texture node
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>
粤ICP备19079148号