webgpu_compute_texture.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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="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 Texture</span>
  14. </div>
  15. <small>
  16. Compute 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 { texture, textureStore, Fn, instanceIndex, float, uvec2, vec4 } from 'three/tsl';
  32. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  33. let camera, scene, renderer;
  34. init().then( render );
  35. async 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. const storageTexture = new THREE.StorageTexture( width, height );
  47. //storageTexture.minFilter = THREE.LinearMipMapLinearFilter;
  48. // create function
  49. const computeTexture = Fn( ( { storageTexture } ) => {
  50. const posX = instanceIndex.mod( width );
  51. const posY = instanceIndex.div( width );
  52. const indexUV = uvec2( posX, posY );
  53. // https://www.shadertoy.com/view/Xst3zN
  54. const x = float( posX ).div( 50.0 );
  55. const y = float( posY ).div( 50.0 );
  56. const v1 = x.sin();
  57. const v2 = y.sin();
  58. const v3 = x.add( y ).sin();
  59. const v4 = x.mul( x ).add( y.mul( y ) ).sqrt().add( 5.0 ).sin();
  60. const v = v1.add( v2, v3, v4 );
  61. const r = v.sin();
  62. const g = v.add( Math.PI ).sin();
  63. const b = v.add( Math.PI ).sub( 0.5 ).sin();
  64. textureStore( storageTexture, indexUV, vec4( r, g, b, 1 ) ).toWriteOnly();
  65. } );
  66. // compute
  67. const computeNode = computeTexture( { storageTexture } ).compute( width * height );
  68. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  69. material.colorNode = texture( storageTexture );
  70. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  71. scene.add( plane );
  72. renderer = new THREE.WebGPURenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. document.body.appendChild( renderer.domElement );
  76. await renderer.init();
  77. // compute texture
  78. renderer.compute( computeNode );
  79. window.addEventListener( 'resize', onWindowResize );
  80. }
  81. function onWindowResize() {
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. const aspect = window.innerWidth / window.innerHeight;
  84. const frustumHeight = camera.top - camera.bottom;
  85. camera.left = - frustumHeight * aspect / 2;
  86. camera.right = frustumHeight * aspect / 2;
  87. camera.updateProjectionMatrix();
  88. render();
  89. }
  90. function render() {
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>
粤ICP备19079148号