webgpu_tsl_interoperability.html 9.3 KB

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