webgpu_compute_texture_pingpong.html 4.7 KB

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