webgpu_tsl_interoperability.html 9.0 KB

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