webgpu_tsl_interoperability.html 9.0 KB

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