webgpu_tsl_interoperability.html 9.3 KB

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