webgpu_tsl_interoperability.html 9.6 KB

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