webgpu_compute_texture_pingpong.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.
  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, wgslFn, code, instanceIndex, uniform, 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 seed = uniform( new THREE.Vector2() );
  40. init();
  41. async function init() {
  42. if ( WebGPU.isAvailable() === false ) {
  43. document.body.appendChild( WebGPU.getErrorMessage() );
  44. throw new Error( 'No WebGPU support' );
  45. }
  46. const aspect = window.innerWidth / window.innerHeight;
  47. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  48. camera.position.z = 1;
  49. scene = new THREE.Scene();
  50. // texture
  51. const hdr = true;
  52. const width = 512, height = 512;
  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 wgslFormat = hdr ? 'rgba16float' : 'rgba8unorm';
  60. const readPing = storageTexture( pingTexture ).setAccess( NodeAccess.READ_ONLY );
  61. const writePing = storageTexture( pingTexture ).setAccess( NodeAccess.WRITE_ONLY );
  62. const readPong = storageTexture( pongTexture ).setAccess( NodeAccess.READ_ONLY );
  63. const writePong = storageTexture( pongTexture ).setAccess( NodeAccess.WRITE_ONLY );
  64. // compute init
  65. const rand2 = code( `
  66. fn rand2( n: vec2f ) -> f32 {
  67. return fract( sin( dot( n, vec2f( 12.9898, 4.1414 ) ) ) * 43758.5453 );
  68. }
  69. fn blur( image : texture_storage_2d<${wgslFormat}, read>, uv : vec2i ) -> vec4f {
  70. var color = vec4f( 0.0 );
  71. color += textureLoad( image, uv + vec2i( - 1, 1 ));
  72. color += textureLoad( image, uv + vec2i( - 1, - 1 ));
  73. color += textureLoad( image, uv + vec2i( 0, 0 ));
  74. color += textureLoad( image, uv + vec2i( 1, - 1 ));
  75. color += textureLoad( image, uv + vec2i( 1, 1 ));
  76. return color / 5.0;
  77. }
  78. fn getUV( posX: u32, posY: u32 ) -> vec2f {
  79. let uv = vec2f( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
  80. return uv;
  81. }
  82. ` );
  83. const computeInitWGSL = wgslFn( `
  84. fn computeInitWGSL( writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32, seed: vec2f ) -> void {
  85. let posX = index % ${ width };
  86. let posY = index / ${ width };
  87. let indexUV = vec2u( posX, posY );
  88. let uv = getUV( posX, posY );
  89. let r = rand2( uv + seed * 100 ) - rand2( uv + seed * 300 );
  90. let g = rand2( uv + seed * 200 ) - rand2( uv + seed * 300 );
  91. let b = rand2( uv + seed * 200 ) - rand2( uv + seed * 100 );
  92. textureStore( writeTex, indexUV, vec4( r, g, b, 1 ) );
  93. }
  94. `, [ rand2 ] );
  95. computeInitNode = computeInitWGSL( { writeTex: storageTexture( pingTexture ), index: instanceIndex, seed } ).compute( width * height );
  96. // compute loop
  97. const computePingPongWGSL = wgslFn( `
  98. fn computePingPongWGSL( readTex: texture_storage_2d<${wgslFormat}, read>, writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32 ) -> void {
  99. let posX = index % ${ width };
  100. let posY = index / ${ width };
  101. let indexUV = vec2i( i32( posX ), i32( posY ) );
  102. let color = blur( readTex, indexUV ).rgb;
  103. textureStore( writeTex, indexUV, vec4f( color * 1.05, 1 ) );
  104. }
  105. `, [ rand2 ] );
  106. //
  107. computeToPong = computePingPongWGSL( { readTex: readPing, writeTex: writePong, index: instanceIndex } ).compute( width * height );
  108. computeToPing = computePingPongWGSL( { readTex: readPong, writeTex: writePing, index: instanceIndex } ).compute( width * height );
  109. //
  110. material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
  111. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  112. scene.add( plane );
  113. renderer = new THREE.WebGPURenderer( { antialias: true } );
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. renderer.setAnimationLoop( render );
  117. document.body.appendChild( renderer.domElement );
  118. await renderer.init();
  119. window.addEventListener( 'resize', onWindowResize );
  120. // compute init
  121. renderer.compute( computeInitNode );
  122. }
  123. function onWindowResize() {
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. const aspect = window.innerWidth / window.innerHeight;
  126. const frustumHeight = camera.top - camera.bottom;
  127. camera.left = - frustumHeight * aspect / 2;
  128. camera.right = frustumHeight * aspect / 2;
  129. camera.updateProjectionMatrix();
  130. }
  131. function render() {
  132. const time = performance.now();
  133. const seconds = Math.floor( time / 1000 );
  134. // reset every second
  135. if ( phase && seconds !== lastUpdate ) {
  136. seed.value.set( Math.random(), Math.random() );
  137. renderer.compute( computeInitNode );
  138. lastUpdate = seconds;
  139. }
  140. // compute step
  141. renderer.compute( phase ? computeToPong : computeToPing );
  142. material.map = phase ? pongTexture : pingTexture;
  143. phase = ! phase;
  144. // render step
  145. // update material texture node
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>
粤ICP备19079148号