webgpu_compute_texture_pingpong.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute Ping/Pong Texture
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/webgpu": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.tsl.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three/webgpu';
  24. import { storageTexture, wgslFn, code, instanceIndex, uniform, NodeAccess } from 'three/tsl';
  25. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  26. let camera, scene, renderer;
  27. let computeInitNode, computeToPing, computeToPong;
  28. let pingTexture, pongTexture;
  29. let material;
  30. let phase = true;
  31. let lastUpdate = - 1;
  32. const seed = uniform( new THREE.Vector2() );
  33. init();
  34. function init() {
  35. if ( WebGPU.isAvailable() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw new Error( 'No WebGPU support' );
  38. }
  39. const aspect = window.innerWidth / window.innerHeight;
  40. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  41. camera.position.z = 1;
  42. scene = new THREE.Scene();
  43. // texture
  44. const hdr = true;
  45. const width = 512, height = 512;
  46. pingTexture = new THREE.StorageTexture( width, height );
  47. pongTexture = new THREE.StorageTexture( width, height );
  48. if ( hdr ) {
  49. pingTexture.type = THREE.HalfFloatType;
  50. pongTexture.type = THREE.HalfFloatType;
  51. }
  52. const wgslFormat = hdr ? 'rgba16float' : 'rgba8unorm';
  53. const readPing = storageTexture( pingTexture ).setAccess( NodeAccess.READ_ONLY );
  54. const writePing = storageTexture( pingTexture ).setAccess( NodeAccess.WRITE_ONLY );
  55. const readPong = storageTexture( pongTexture ).setAccess( NodeAccess.READ_ONLY );
  56. const writePong = storageTexture( pongTexture ).setAccess( NodeAccess.WRITE_ONLY );
  57. // compute init
  58. const rand2 = code( `
  59. fn rand2( n: vec2f ) -> f32 {
  60. return fract( sin( dot( n, vec2f( 12.9898, 4.1414 ) ) ) * 43758.5453 );
  61. }
  62. fn blur( image : texture_storage_2d<${wgslFormat}, read>, uv : vec2i ) -> vec4f {
  63. var color = vec4f( 0.0 );
  64. color += textureLoad( image, uv + vec2i( - 1, 1 ));
  65. color += textureLoad( image, uv + vec2i( - 1, - 1 ));
  66. color += textureLoad( image, uv + vec2i( 0, 0 ));
  67. color += textureLoad( image, uv + vec2i( 1, - 1 ));
  68. color += textureLoad( image, uv + vec2i( 1, 1 ));
  69. return color / 5.0;
  70. }
  71. fn getUV( posX: u32, posY: u32 ) -> vec2f {
  72. let uv = vec2f( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
  73. return uv;
  74. }
  75. ` );
  76. const computeInitWGSL = wgslFn( `
  77. fn computeInitWGSL( writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32, seed: vec2f ) -> void {
  78. let posX = index % ${ width };
  79. let posY = index / ${ width };
  80. let indexUV = vec2u( posX, posY );
  81. let uv = getUV( posX, posY );
  82. let r = rand2( uv + seed * 100 ) - rand2( uv + seed * 300 );
  83. let g = rand2( uv + seed * 200 ) - rand2( uv + seed * 300 );
  84. let b = rand2( uv + seed * 200 ) - rand2( uv + seed * 100 );
  85. textureStore( writeTex, indexUV, vec4( r, g, b, 1 ) );
  86. }
  87. `, [ rand2 ] );
  88. computeInitNode = computeInitWGSL( { writeTex: storageTexture( pingTexture ), index: instanceIndex, seed } ).compute( width * height );
  89. // compute loop
  90. const computePingPongWGSL = wgslFn( `
  91. fn computePingPongWGSL( readTex: texture_storage_2d<${wgslFormat}, read>, writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32 ) -> void {
  92. let posX = index % ${ width };
  93. let posY = index / ${ width };
  94. let indexUV = vec2i( i32( posX ), i32( posY ) );
  95. let color = blur( readTex, indexUV ).rgb;
  96. textureStore( writeTex, indexUV, vec4f( color * 1.05, 1 ) );
  97. }
  98. `, [ rand2 ] );
  99. //
  100. computeToPong = computePingPongWGSL( { readTex: readPing, writeTex: writePong, index: instanceIndex } ).compute( width * height );
  101. computeToPing = computePingPongWGSL( { readTex: readPong, writeTex: writePing, index: instanceIndex } ).compute( width * height );
  102. //
  103. material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
  104. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  105. scene.add( plane );
  106. renderer = new THREE.WebGPURenderer( { antialias: true } );
  107. renderer.setPixelRatio( window.devicePixelRatio );
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. renderer.setAnimationLoop( render );
  110. document.body.appendChild( renderer.domElement );
  111. window.addEventListener( 'resize', onWindowResize );
  112. // compute init
  113. renderer.computeAsync( 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号