webgpu_compute_texture_pingpong.html 5.8 KB

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