webgpu_compute_texture_pingpong.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. <br>Texture generated using GPU Compute.
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { texture, textureStore, wgslFn, code, instanceIndex } from 'three/nodes';
  25. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. import StorageTexture from 'three/addons/renderers/common/StorageTexture.js';
  28. let camera, scene, renderer;
  29. let computeToPing, computeToPong;
  30. let pingTexture, pongTexture;
  31. let material;
  32. let phase = true;
  33. init();
  34. render();
  35. function init() {
  36. if ( WebGPU.isAvailable() === false ) {
  37. document.body.appendChild( WebGPU.getErrorMessage() );
  38. throw new Error( 'No WebGPU support' );
  39. }
  40. const aspect = window.innerWidth / window.innerHeight;
  41. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  42. camera.position.z = 1;
  43. scene = new THREE.Scene();
  44. // texture
  45. const width = 512, height = 512;
  46. pingTexture = new StorageTexture( width, height );
  47. pongTexture = new StorageTexture( width, height );
  48. // compute init
  49. const rand2 = code( `
  50. fn rand2( n: vec2f ) -> f32 {
  51. return fract( sin( dot( n, vec2f( 12.9898, 4.1414 ) ) ) * 43758.5453 );
  52. }
  53. ` );
  54. const computeInitWGSL = wgslFn( `
  55. fn computeInitWGSL( writeTex: texture_storage_2d<rgba8unorm, write>, index: u32 ) -> void {
  56. let posX = index % ${ width };
  57. let posY = index / ${ width };
  58. let indexUV = vec2u( posX, posY );
  59. let uv = getUV( posX, posY );
  60. textureStore( writeTex, indexUV, vec4f( vec3f( rand2( uv ) ), 1 ) );
  61. }
  62. fn getUV( posX: u32, posY: u32 ) -> vec2f {
  63. let uv = vec2f( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
  64. return uv;
  65. }
  66. `, [ rand2 ] );
  67. const computeInitNode = computeInitWGSL( { writeTex: textureStore( pingTexture ), index: instanceIndex } ).compute( width * height );
  68. // compute loop
  69. const computePingPongWGSL = wgslFn( `
  70. fn computePingPongWGSL( readTex: texture_2d<f32>, writeTex: texture_storage_2d<rgba8unorm, write>, index: u32 ) -> void {
  71. let posX = index % ${ width };
  72. let posY = index / ${ width };
  73. let indexUV = vec2u( posX, posY );
  74. let color = vec3f( rand2( textureLoad( readTex, indexUV, 0 ).xy ) );
  75. textureStore( writeTex, indexUV, vec4f( color, 1 ) );
  76. }
  77. `, [ rand2 ] );
  78. computeToPong = computePingPongWGSL( { readTex: texture( pingTexture ), writeTex: textureStore( pongTexture ), index: instanceIndex } ).compute( width * height );
  79. computeToPing = computePingPongWGSL( { readTex: texture( pongTexture ), writeTex: textureStore( pingTexture ), index: instanceIndex } ).compute( width * height );
  80. //
  81. material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
  82. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  83. scene.add( plane );
  84. renderer = new WebGPURenderer( { antialias: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.setAnimationLoop( render );
  88. document.body.appendChild( renderer.domElement );
  89. window.addEventListener( 'resize', onWindowResize );
  90. // compute init
  91. renderer.compute( computeInitNode );
  92. }
  93. function onWindowResize() {
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. const aspect = window.innerWidth / window.innerHeight;
  96. const frustumHeight = camera.top - camera.bottom;
  97. camera.left = - frustumHeight * aspect / 2;
  98. camera.right = frustumHeight * aspect / 2;
  99. camera.updateProjectionMatrix();
  100. render();
  101. }
  102. function render() {
  103. // compute step
  104. renderer.compute( phase ? computeToPong : computeToPing );
  105. material.map = phase ? pongTexture : pingTexture;
  106. phase = ! phase;
  107. // render step
  108. // update material texture node
  109. renderer.render( scene, camera );
  110. }
  111. </script>
  112. </body>
  113. </html>