webgpu_compute_texture.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.module.js",
  16. "three/addons/": "./jsm/",
  17. "three/nodes": "./jsm/nodes/Nodes.js"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { texture, textureStore, wgslFn, instanceIndex, MeshBasicNodeMaterial } from 'three/nodes';
  24. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  25. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  26. import StorageTexture from 'three/addons/renderers/common/StorageTexture.js';
  27. let camera, scene, renderer;
  28. init();
  29. render();
  30. function init() {
  31. if ( WebGPU.isAvailable() === false ) {
  32. document.body.appendChild( WebGPU.getErrorMessage() );
  33. throw new Error( 'No WebGPU support' );
  34. }
  35. const aspect = window.innerWidth / window.innerHeight;
  36. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  37. camera.position.z = 1;
  38. scene = new THREE.Scene();
  39. // texture
  40. const width = 512, height = 512;
  41. const storageTexture = new StorageTexture( width, height );
  42. // create function
  43. const computeWGSL = wgslFn( `
  44. fn computeWGSL( storageTex: texture_storage_2d<rgba8unorm, write>, index: u32 ) -> void {
  45. let posX = index % ${ width };
  46. let posY = index / ${ width };
  47. let indexUV = vec2u( posX, posY );
  48. // https://www.shadertoy.com/view/Xst3zN
  49. let x = f32( posX ) / 50.0;
  50. let y = f32( posY ) / 50.0;
  51. let v1 = sin( x );
  52. let v2 = sin( y );
  53. let v3 = sin( x + y );
  54. let v4 = sin( sqrt( x * x + y * y ) + 5.0 );
  55. let v = v1 + v2 + v3 + v4;
  56. let PI = 3.14159265359;
  57. let r = sin( v );
  58. let g = sin( v + PI );
  59. let b = sin( v + PI - 0.5 );
  60. textureStore( storageTex, indexUV, vec4f( r, g, b, 1 ) );
  61. }
  62. ` );
  63. // compute
  64. const computeWGSLCall = computeWGSL( { storageTex: textureStore( storageTexture ), index: instanceIndex } );
  65. const computeNode = computeWGSLCall.compute( width * height );
  66. const material = new MeshBasicNodeMaterial( { color: 0x00ff00 } );
  67. material.colorNode = texture( storageTexture );
  68. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  69. scene.add( plane );
  70. renderer = new WebGPURenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. document.body.appendChild( renderer.domElement );
  74. // compute texture
  75. renderer.compute( computeNode );
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. const aspect = window.innerWidth / window.innerHeight;
  81. const frustumHeight = camera.top - camera.bottom;
  82. camera.left = - frustumHeight * aspect / 2;
  83. camera.right = frustumHeight * aspect / 2;
  84. camera.updateProjectionMatrix();
  85. render();
  86. }
  87. function render() {
  88. renderer.render( scene, camera );
  89. }
  90. </script>
  91. </body>
  92. </html>