1
0

webgpu_compute_texture.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <html lang="en">
  2. <head>
  3. <title>three.js - WebGPU - Compute 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 Texture
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/tsl": "../build/three.webgpu.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { texture, textureStore, Fn, instanceIndex, float, uvec2, vec4 } from 'three/tsl';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. let camera, scene, renderer;
  26. init();
  27. render();
  28. function init() {
  29. if ( WebGPU.isAvailable() === false ) {
  30. document.body.appendChild( WebGPU.getErrorMessage() );
  31. throw new Error( 'No WebGPU support' );
  32. }
  33. const aspect = window.innerWidth / window.innerHeight;
  34. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  35. camera.position.z = 1;
  36. scene = new THREE.Scene();
  37. // texture
  38. const width = 512, height = 512;
  39. const storageTexture = new THREE.StorageTexture( width, height );
  40. //storageTexture.minFilter = THREE.LinearMipMapLinearFilter;
  41. // create function
  42. const computeTexture = Fn( ( { storageTexture } ) => {
  43. const posX = instanceIndex.modInt( width );
  44. const posY = instanceIndex.div( width );
  45. const indexUV = uvec2( posX, posY );
  46. // https://www.shadertoy.com/view/Xst3zN
  47. const x = float( posX ).div( 50.0 );
  48. const y = float( posY ).div( 50.0 );
  49. const v1 = x.sin();
  50. const v2 = y.sin();
  51. const v3 = x.add( y ).sin();
  52. const v4 = x.mul( x ).add( y.mul( y ) ).sqrt().add( 5.0 ).sin();
  53. const v = v1.add( v2, v3, v4 );
  54. const r = v.sin();
  55. const g = v.add( Math.PI ).sin();
  56. const b = v.add( Math.PI ).sub( 0.5 ).sin();
  57. textureStore( storageTexture, indexUV, vec4( r, g, b, 1 ) ).toWriteOnly();
  58. } );
  59. // compute
  60. const computeNode = computeTexture( { storageTexture } ).compute( width * height );
  61. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  62. material.colorNode = texture( storageTexture );
  63. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  64. scene.add( plane );
  65. renderer = new THREE.WebGPURenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. document.body.appendChild( renderer.domElement );
  69. // compute texture
  70. renderer.compute( computeNode );
  71. window.addEventListener( 'resize', onWindowResize );
  72. }
  73. function onWindowResize() {
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. const aspect = window.innerWidth / window.innerHeight;
  76. const frustumHeight = camera.top - camera.bottom;
  77. camera.left = - frustumHeight * aspect / 2;
  78. camera.right = frustumHeight * aspect / 2;
  79. camera.updateProjectionMatrix();
  80. render();
  81. }
  82. function render() {
  83. renderer.renderAsync( scene, camera );
  84. }
  85. </script>
  86. </body>
  87. </html>