webgpu_compute_texture.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. <meta property="og:title" content="three.js webgpu - compute texture">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/webgpu_compute_texture.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_compute_texture.jpg">
  10. <link type="text/css" rel="stylesheet" href="example.css">
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  15. <div class="title-wrapper">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a>
  17. <span>Compute Texture</span>
  18. </div>
  19. <small>
  20. Compute texture using GPU.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { texture, textureStore, Fn, instanceIndex, float, uvec2, vec4 } from 'three/tsl';
  36. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  37. let camera, scene, renderer;
  38. init().then( render );
  39. async function init() {
  40. if ( WebGPU.isAvailable() === false ) {
  41. document.body.appendChild( WebGPU.getErrorMessage() );
  42. throw new Error( 'No WebGPU support' );
  43. }
  44. const aspect = window.innerWidth / window.innerHeight;
  45. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  46. camera.position.z = 1;
  47. scene = new THREE.Scene();
  48. // texture
  49. const width = 512, height = 512;
  50. const storageTexture = new THREE.StorageTexture( width, height );
  51. //storageTexture.minFilter = THREE.LinearMipMapLinearFilter;
  52. // create function
  53. const computeTexture = Fn( ( { storageTexture } ) => {
  54. const posX = instanceIndex.mod( width );
  55. const posY = instanceIndex.div( width );
  56. const indexUV = uvec2( posX, posY );
  57. // https://www.shadertoy.com/view/Xst3zN
  58. const x = float( posX ).div( 50.0 );
  59. const y = float( posY ).div( 50.0 );
  60. const v1 = x.sin();
  61. const v2 = y.sin();
  62. const v3 = x.add( y ).sin();
  63. const v4 = x.mul( x ).add( y.mul( y ) ).sqrt().add( 5.0 ).sin();
  64. const v = v1.add( v2, v3, v4 );
  65. const r = v.sin();
  66. const g = v.add( Math.PI ).sin();
  67. const b = v.add( Math.PI ).sub( 0.5 ).sin();
  68. textureStore( storageTexture, indexUV, vec4( r, g, b, 1 ) ).toWriteOnly();
  69. } );
  70. // compute
  71. const computeNode = computeTexture( { storageTexture } ).compute( width * height );
  72. const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
  73. material.colorNode = texture( storageTexture );
  74. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  75. scene.add( plane );
  76. renderer = new THREE.WebGPURenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. document.body.appendChild( renderer.domElement );
  80. await renderer.init();
  81. // compute texture
  82. renderer.compute( computeNode );
  83. window.addEventListener( 'resize', onWindowResize );
  84. }
  85. function onWindowResize() {
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. const aspect = window.innerWidth / window.innerHeight;
  88. const frustumHeight = camera.top - camera.bottom;
  89. camera.left = - frustumHeight * aspect / 2;
  90. camera.right = frustumHeight * aspect / 2;
  91. camera.updateProjectionMatrix();
  92. render();
  93. }
  94. function render() {
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>
粤ICP备19079148号