2
0

webgpu_tsl_interoperability.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <html lang="en">
  2. <head>
  3. <title>three.js - shadertoy</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 - WGSL/TSL Node Interoperability
  11. <br />CRT Shader adapted from <a href="https://mini.gmshaders.com/p/gm-shaders-mini-crt" target="_blank" rel="noopener"> Xor</a>.
  12. </div>
  13. <div id="container">
  14. <canvas id="c"></canvas>
  15. </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/",
  21. "three/nodes": "./jsm/nodes/Nodes.js"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  28. import WebGL from 'three/addons/capabilities/WebGL.js';
  29. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  30. import { tslFn, sampler, attribute, varyingProperty, timerLocal, uniform, wgslFn, texture, uv, clamp, float, vec2, vec3, fract, floor, MeshBasicNodeMaterial, positionGeometry, sin } from 'three/nodes';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. let renderer, camera, scene;
  33. const dpr = window.devicePixelRatio;
  34. const crtWidthUniform = uniform( 1608 );
  35. const crtHeightUniform = uniform( 1608 );
  36. const canvas = document.getElementById( 'c' );
  37. function onWindowResize() {
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. }
  40. function animate() {
  41. renderer.render( scene, camera );
  42. }
  43. function init() {
  44. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  45. document.body.appendChild( WebGPU.getErrorMessage() );
  46. throw new Error( 'No WebGPU or WebGL2 support' );
  47. }
  48. const vUv = varyingProperty( 'vec2', 'vUv' );
  49. // In WGSL, access varying properties from the varying struct
  50. const wgslVertexShader = wgslFn( `
  51. fn crtVertex(
  52. position: vec3f,
  53. uv: vec2f
  54. ) -> vec3<f32> {
  55. varyings.vUv = uv;
  56. return position;
  57. }
  58. `, [
  59. vUv
  60. ] );
  61. // Only wgsl vertex shaders take varyings arguments when defined.
  62. // For a wgsl fragment shader, pass the varyingProperty node to the
  63. // fragment shader's constructor to acess the varying value computed
  64. // by the vertex shader.
  65. const wgslFragmentShader = wgslFn( `
  66. fn crtFragment(
  67. vUv: vec2f,
  68. tex: texture_2d<f32>,
  69. texSampler: sampler,
  70. crtWidth: f32,
  71. crtHeight: f32,
  72. cellOffset: f32,
  73. cellSize: f32,
  74. borderMask: f32,
  75. time: f32,
  76. speed: f32,
  77. pulseIntensity: f32,
  78. pulseWidth: f32,
  79. pulseRate: f32
  80. ) -> vec3<f32> {
  81. // Convert uv into map of pixels
  82. var pixel = ( vUv * 0.5 + 0.5 ) * vec2<f32>(
  83. crtWidth,
  84. crtHeight
  85. );
  86. // Coordinate for each cell in the pixel map
  87. let coord = pixel / cellSize;
  88. // Three color values for each cell (r, g, b)
  89. let subcoord = coord * vec2f( 3.0, 1.0 );
  90. let offset = vec2<f32>( 0, fract( floor( coord.x ) * cellOffset ) );
  91. let maskCoord = floor( coord + offset ) * cellSize;
  92. var samplePoint = maskCoord / vec2<f32>(crtWidth, crtHeight);
  93. samplePoint.x += fract( time * speed / 20 );
  94. var color = textureSample(
  95. tex,
  96. texSampler,
  97. samplePoint
  98. ).xyz;
  99. // Current implementation does not give an even amount of space to each r, g, b unit of a cell
  100. // Fix/hack this by multiplying subCoord.x by cellSize at cellSizes below 6
  101. let ind = floor( subcoord.x ) % 3;
  102. var maskColor = vec3<f32>(
  103. f32( ind == 0.0 ),
  104. f32( ind == 1.0 ),
  105. f32( ind == 2.0 )
  106. ) * 3.0;
  107. let cellUV = fract( subcoord + offset ) * 2.0 - 1.0;
  108. var border: vec2<f32> = 1.0 - cellUV * cellUV * borderMask;
  109. maskColor *= vec3f( clamp( border.x, 0.0, 1.0 ) * clamp( border.y, 0.0, 1.0) );
  110. color *= maskColor;
  111. color.r *= 1.0 + pulseIntensity * sin( pixel.y / pulseWidth + time * pulseRate );
  112. color.b *= 1.0 + pulseIntensity * sin( pixel.y / pulseWidth + time * pulseRate );
  113. color.g *= 1.0 + pulseIntensity * sin( pixel.y / pulseWidth + time * pulseRate );
  114. return color;
  115. }
  116. ` );
  117. const textureLoader = new THREE.TextureLoader();
  118. const planetTexture = textureLoader.load( 'textures/planets/earth_lights_2048.png' );
  119. planetTexture.wrapS = THREE.RepeatWrapping;
  120. planetTexture.wrapT = THREE.RepeatWrapping;
  121. // Node Uniforms:
  122. // Passed to WGSL Functions.
  123. // Manipulated directly in TSL Functions.
  124. const cellOffsetUniform = uniform( 0.5 );
  125. const cellSizeUniform = uniform( 6 );
  126. const borderMaskUniform = uniform( 1 );
  127. const pulseIntensityUniform = uniform( 0.06 );
  128. const pulseWidthUniform = uniform( 60 );
  129. const pulseRateUniform = uniform( 20 );
  130. const wgslShaderSpeedUniform = uniform( 1.0 );
  131. const tslShaderSpeedUniform = uniform( 1.0 );
  132. //
  133. const wgslShaderMaterial = new MeshBasicNodeMaterial();
  134. // Accessed attributes correspond to a Mesh or BufferGeometry's setAttribute() calls.
  135. wgslShaderMaterial.positionNode = wgslVertexShader( {
  136. position: attribute( 'position' ),
  137. uv: attribute( 'uv' )
  138. } );
  139. wgslShaderMaterial.fragmentNode = wgslFragmentShader( {
  140. vUv: vUv,
  141. tex: texture( planetTexture ),
  142. texSampler: sampler( planetTexture ),
  143. crtWidth: crtWidthUniform,
  144. crtHeight: crtHeightUniform,
  145. cellOffset: cellOffsetUniform,
  146. cellSize: cellSizeUniform,
  147. borderMask: borderMaskUniform,
  148. time: timerLocal(),
  149. speed: wgslShaderSpeedUniform,
  150. pulseIntensity: pulseIntensityUniform,
  151. pulseWidth: pulseWidthUniform,
  152. pulseRate: pulseRateUniform
  153. } );
  154. //
  155. const tslVertexShader = tslFn( () => {
  156. vUv.assign( uv() );
  157. return positionGeometry;
  158. } );
  159. const tslFragmentShader = tslFn( () => {
  160. const dimensions = vec2( crtWidthUniform, crtHeightUniform );
  161. const translatedUV = vUv.mul( 0.5 ).add( 0.5 );
  162. const pixel = translatedUV.mul( dimensions );
  163. const coord = pixel.div( cellSizeUniform );
  164. const subCoord = coord.mul( vec2( 3.0, 1.0 ) );
  165. const cellOffset = vec2(
  166. 0.0,
  167. fract( floor( coord.x ).mul( cellOffsetUniform ) )
  168. );
  169. const maskCoord = floor( coord.add( cellOffset ) ).mul( cellSizeUniform );
  170. const samplePoint = maskCoord.div( dimensions );
  171. const time = timerLocal().mul( tslShaderSpeedUniform );
  172. samplePoint.x = samplePoint.x.add( fract( time.div( 20 ) ) );
  173. samplePoint.y = samplePoint.y.sub( 1.5 );
  174. let color = texture( planetTexture, samplePoint );
  175. const ind = floor( subCoord.x ).mod( 3 );
  176. let maskColor = vec3(
  177. ind.equal( 0.0 ),
  178. ind.equal( 1.0 ),
  179. ind.equal( 2.0 )
  180. ).mul( 3.0 );
  181. const subCoordOffset = fract( subCoord.add( cellOffset ) );
  182. let cellUV = subCoordOffset.mul( 2.0 );
  183. cellUV = cellUV.sub( 1.0 );
  184. const border = float( 1.0 ).sub(
  185. cellUV.mul( cellUV ).mul( borderMaskUniform )
  186. );
  187. const clampX = clamp( border.x, 0.0, 1.0 );
  188. const clampY = clamp( border.y, 0.0, 1.0 );
  189. const borderClamp = clampX.mul( clampY );
  190. maskColor = maskColor.mul( borderClamp );
  191. color = color.mul( maskColor );
  192. const pixelDampen = pixel.y.div( pulseWidthUniform );
  193. let pulse = sin( pixelDampen.add( time.mul( pulseRateUniform ) ) );
  194. pulse = pulse.mul( pulseIntensityUniform );
  195. color = color.mul( float( 1.0 ).add( pulse ) );
  196. return color;
  197. } );
  198. const tslShaderMaterial = new MeshBasicNodeMaterial();
  199. tslShaderMaterial.positionNode = tslVertexShader();
  200. tslShaderMaterial.colorNode = tslFragmentShader();
  201. camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  202. scene = new THREE.Scene();
  203. const geometry = new THREE.PlaneGeometry( 2, 1 );
  204. const wgslQuad = new THREE.Mesh( geometry, wgslShaderMaterial );
  205. wgslQuad.position.y += 0.5;
  206. scene.add( wgslQuad );
  207. const tslQuad = new THREE.Mesh( geometry, tslShaderMaterial );
  208. tslQuad.position.y -= 0.5;
  209. scene.add( tslQuad );
  210. renderer = new WebGPURenderer( { antialias: true, canvas: canvas } );
  211. renderer.setPixelRatio( dpr );
  212. renderer.setSize( window.innerWidth, window.innerHeight );
  213. renderer.setAnimationLoop( animate );
  214. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  215. document.body.appendChild( renderer.domElement );
  216. window.addEventListener( 'resize', onWindowResize );
  217. const gui = new GUI();
  218. gui.add( cellSizeUniform, 'value', 6, 50, 1 ).name( 'Cell Size' );
  219. gui.add( cellOffsetUniform, 'value', 0, 1, 0.1 ).name( 'Cell Offset' );
  220. gui.add( borderMaskUniform, 'value', 0, 5, 0.1 ).name( 'Border Mask' );
  221. gui.add( pulseIntensityUniform, 'value', 0, 0.5, 0.01 ).name( 'Pulse Intensity' );
  222. gui.add( pulseWidthUniform, 'value', 10, 100, 5 ).name( 'Pulse Width' );
  223. gui.add( wgslShaderSpeedUniform, 'value', 1, 10 ).name( 'WGSL Shader Speed' );
  224. gui.add( tslShaderSpeedUniform, 'value', 1, 10 ).name( 'TSL Shader Speed' );
  225. }
  226. init();
  227. </script>
  228. </body>
  229. </html>