FXAANode.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import { Vector2, TempNode } from 'three/webgpu';
  2. import { nodeObject, Fn, uniformArray, select, float, NodeUpdateType, uv, dot, clamp, uniform, convertToTexture, smoothstep, bool, vec2, vec3, If, Loop, max, min, Break, abs } from 'three/tsl';
  3. /**
  4. * Post processing node for applying FXAA. This node requires sRGB input
  5. * so tone mapping and color space conversion must happen before the anti-aliasing.
  6. *
  7. * @augments TempNode
  8. */
  9. class FXAANode extends TempNode {
  10. static get type() {
  11. return 'FXAANode';
  12. }
  13. /**
  14. * Constructs a new FXAA node.
  15. *
  16. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  17. */
  18. constructor( textureNode ) {
  19. super( 'vec4' );
  20. /**
  21. * The texture node that represents the input of the effect.
  22. *
  23. * @type {TextureNode}
  24. */
  25. this.textureNode = textureNode;
  26. /**
  27. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node updates
  28. * its internal uniforms once per frame in `updateBefore()`.
  29. *
  30. * @type {string}
  31. * @default 'frame'
  32. */
  33. this.updateBeforeType = NodeUpdateType.FRAME;
  34. /**
  35. * A uniform node holding the inverse resolution value.
  36. *
  37. * @private
  38. * @type {UniformNode<vec2>}
  39. */
  40. this._invSize = uniform( new Vector2() );
  41. }
  42. /**
  43. * This method is used to update the effect's uniforms once per frame.
  44. *
  45. * @param {NodeFrame} frame - The current node frame.
  46. */
  47. updateBefore( /* frame */ ) {
  48. const map = this.textureNode.value;
  49. this._invSize.value.set( 1 / map.image.width, 1 / map.image.height );
  50. }
  51. /**
  52. * This method is used to setup the effect's TSL code.
  53. *
  54. * @param {NodeBuilder} builder - The current node builder.
  55. * @return {ShaderCallNodeInternal}
  56. */
  57. setup( /* builder */ ) {
  58. const textureNode = this.textureNode.bias( - 100 );
  59. const uvNode = textureNode.uvNode || uv();
  60. const EDGE_STEP_COUNT = float( 6 );
  61. const EDGE_GUESS = float( 8.0 );
  62. const EDGE_STEPS = uniformArray( [ 1.0, 1.5, 2.0, 2.0, 2.0, 4.0 ] );
  63. const _ContrastThreshold = float( 0.0312 );
  64. const _RelativeThreshold = float( 0.063 );
  65. const _SubpixelBlending = float( 1.0 );
  66. const Sample = Fn( ( [ uv ] ) => {
  67. return textureNode.sample( uv );
  68. } );
  69. const SampleLuminance = Fn( ( [ uv ] ) => {
  70. return dot( Sample( uv ).rgb, vec3( 0.3, 0.59, 0.11 ) );
  71. } );
  72. const SampleLuminanceOffset = Fn( ( [ texSize, uv, uOffset, vOffset ] ) => {
  73. const shiftedUv = uv.add( texSize.mul( vec2( uOffset, vOffset ) ) );
  74. return SampleLuminance( shiftedUv );
  75. } );
  76. const ShouldSkipPixel = ( l ) => {
  77. const threshold = max( _ContrastThreshold, _RelativeThreshold.mul( l.highest ) );
  78. return l.contrast.lessThan( threshold );
  79. };
  80. const SampleLuminanceNeighborhood = ( texSize, uv ) => {
  81. const m = SampleLuminance( uv );
  82. const n = SampleLuminanceOffset( texSize, uv, 0.0, - 1.0 );
  83. const e = SampleLuminanceOffset( texSize, uv, 1.0, 0.0 );
  84. const s = SampleLuminanceOffset( texSize, uv, 0.0, 1.0 );
  85. const w = SampleLuminanceOffset( texSize, uv, - 1.0, 0.0 );
  86. const ne = SampleLuminanceOffset( texSize, uv, 1.0, - 1.0 );
  87. const nw = SampleLuminanceOffset( texSize, uv, - 1.0, - 1.0 );
  88. const se = SampleLuminanceOffset( texSize, uv, 1.0, 1.0 );
  89. const sw = SampleLuminanceOffset( texSize, uv, - 1.0, 1.0 );
  90. const highest = max( max( max( max( s, e ), n ), w ), m );
  91. const lowest = min( min( min( min( s, e ), n ), w ), m );
  92. const contrast = highest.sub( lowest );
  93. return { m, n, e, s, w, ne, nw, se, sw, highest, lowest, contrast };
  94. };
  95. const DeterminePixelBlendFactor = ( l ) => {
  96. let f = float( 2.0 ).mul( l.s.add( l.e ).add( l.n ).add( l.w ) );
  97. f = f.add( l.se.add( l.sw ).add( l.ne ).add( l.nw ) );
  98. f = f.mul( 1.0 / 12.0 );
  99. f = abs( f.sub( l.m ) );
  100. f = clamp( f.div( max( l.contrast, 0 ) ), 0.0, 1.0 );
  101. const blendFactor = smoothstep( 0.0, 1.0, f );
  102. return blendFactor.mul( blendFactor ).mul( _SubpixelBlending );
  103. };
  104. const DetermineEdge = ( texSize, l ) => {
  105. const horizontal =
  106. abs( l.s.add( l.n ).sub( l.m.mul( 2.0 ) ) ).mul( 2.0 ).add(
  107. abs( l.se.add( l.ne ).sub( l.e.mul( 2.0 ) ) ).add(
  108. abs( l.sw.add( l.nw ).sub( l.w.mul( 2.0 ) ) )
  109. )
  110. );
  111. const vertical =
  112. abs( l.e.add( l.w ).sub( l.m.mul( 2.0 ) ) ).mul( 2.0 ).add(
  113. abs( l.se.add( l.sw ).sub( l.s.mul( 2.0 ) ) ).add(
  114. abs( l.ne.add( l.nw ).sub( l.n.mul( 2.0 ) ) )
  115. )
  116. );
  117. const isHorizontal = horizontal.greaterThanEqual( vertical );
  118. const pLuminance = select( isHorizontal, l.s, l.e );
  119. const nLuminance = select( isHorizontal, l.n, l.w );
  120. const pGradient = abs( pLuminance.sub( l.m ) );
  121. const nGradient = abs( nLuminance.sub( l.m ) );
  122. const pixelStep = select( isHorizontal, texSize.y, texSize.x ).toVar();
  123. const oppositeLuminance = float().toVar();
  124. const gradient = float().toVar();
  125. If( pGradient.lessThan( nGradient ), () => {
  126. pixelStep.assign( pixelStep.negate() );
  127. oppositeLuminance.assign( nLuminance );
  128. gradient.assign( nGradient );
  129. } ).Else( () => {
  130. oppositeLuminance.assign( pLuminance );
  131. gradient.assign( pGradient );
  132. } );
  133. return { isHorizontal, pixelStep, oppositeLuminance, gradient };
  134. };
  135. const DetermineEdgeBlendFactor = ( texSize, l, e, uv ) => {
  136. const uvEdge = uv.toVar();
  137. const edgeStep = vec2().toVar();
  138. If( e.isHorizontal, () => {
  139. uvEdge.y.addAssign( e.pixelStep.mul( 0.5 ) );
  140. edgeStep.assign( vec2( texSize.x, 0.0 ) );
  141. } ).Else( () => {
  142. uvEdge.x.addAssign( e.pixelStep.mul( 0.5 ) );
  143. edgeStep.assign( vec2( 0.0, texSize.y ) );
  144. } );
  145. const edgeLuminance = l.m.add( e.oppositeLuminance ).mul( 0.5 );
  146. const gradientThreshold = e.gradient.mul( 0.25 );
  147. const puv = uvEdge.add( edgeStep.mul( EDGE_STEPS.element( 0 ) ) ).toVar();
  148. const pLuminanceDelta = SampleLuminance( puv ).sub( edgeLuminance ).toVar();
  149. const pAtEnd = abs( pLuminanceDelta ).greaterThanEqual( gradientThreshold ).toVar();
  150. Loop( { start: 1, end: EDGE_STEP_COUNT }, ( { i } ) => {
  151. If( pAtEnd, () => {
  152. Break();
  153. } );
  154. puv.addAssign( edgeStep.mul( EDGE_STEPS.element( i ) ) );
  155. pLuminanceDelta.assign( SampleLuminance( puv ).sub( edgeLuminance ) );
  156. pAtEnd.assign( abs( pLuminanceDelta ).greaterThanEqual( gradientThreshold ) );
  157. } );
  158. If( pAtEnd.not(), () => {
  159. puv.addAssign( edgeStep.mul( EDGE_GUESS ) );
  160. } );
  161. const nuv = uvEdge.sub( edgeStep.mul( EDGE_STEPS.element( 0 ) ) ).toVar();
  162. const nLuminanceDelta = SampleLuminance( nuv ).sub( edgeLuminance ).toVar();
  163. const nAtEnd = abs( nLuminanceDelta ).greaterThanEqual( gradientThreshold ).toVar();
  164. Loop( { start: 1, end: EDGE_STEP_COUNT }, ( { i } ) => {
  165. If( nAtEnd, () => {
  166. Break();
  167. } );
  168. nuv.subAssign( edgeStep.mul( EDGE_STEPS.element( i ) ) );
  169. nLuminanceDelta.assign( SampleLuminance( nuv ).sub( edgeLuminance ) );
  170. nAtEnd.assign( abs( nLuminanceDelta ).greaterThanEqual( gradientThreshold ) );
  171. } );
  172. If( nAtEnd.not(), () => {
  173. nuv.subAssign( edgeStep.mul( EDGE_GUESS ) );
  174. } );
  175. const pDistance = float().toVar();
  176. const nDistance = float().toVar();
  177. If( e.isHorizontal, () => {
  178. pDistance.assign( puv.x.sub( uv.x ) );
  179. nDistance.assign( uv.x.sub( nuv.x ) );
  180. } ).Else( () => {
  181. pDistance.assign( puv.y.sub( uv.y ) );
  182. nDistance.assign( uv.y.sub( nuv.y ) );
  183. } );
  184. const shortestDistance = float().toVar();
  185. const deltaSign = bool().toVar();
  186. If( pDistance.lessThanEqual( nDistance ), () => {
  187. shortestDistance.assign( pDistance );
  188. deltaSign.assign( pLuminanceDelta.greaterThanEqual( 0.0 ) );
  189. } ).Else( () => {
  190. shortestDistance.assign( nDistance );
  191. deltaSign.assign( nLuminanceDelta.greaterThanEqual( 0.0 ) );
  192. } );
  193. const blendFactor = float().toVar();
  194. If( deltaSign.equal( l.m.sub( edgeLuminance ).greaterThanEqual( 0.0 ) ), () => {
  195. blendFactor.assign( 0.0 );
  196. } ).Else( () => {
  197. blendFactor.assign( float( 0.5 ).sub( shortestDistance.div( pDistance.add( nDistance ) ) ) );
  198. } );
  199. return blendFactor;
  200. };
  201. const ApplyFXAA = Fn( ( [ uv, texSize ] ) => {
  202. const luminance = SampleLuminanceNeighborhood( texSize, uv );
  203. If( ShouldSkipPixel( luminance ), () => {
  204. return Sample( uv );
  205. } );
  206. const pixelBlend = DeterminePixelBlendFactor( luminance );
  207. const edge = DetermineEdge( texSize, luminance );
  208. const edgeBlend = DetermineEdgeBlendFactor( texSize, luminance, edge, uv );
  209. const finalBlend = max( pixelBlend, edgeBlend );
  210. const finalUv = uv.toVar();
  211. If( edge.isHorizontal, () => {
  212. finalUv.y.addAssign( edge.pixelStep.mul( finalBlend ) );
  213. } ).Else( () => {
  214. finalUv.x.addAssign( edge.pixelStep.mul( finalBlend ) );
  215. } );
  216. return Sample( finalUv );
  217. } ).setLayout( {
  218. name: 'FxaaPixelShader',
  219. type: 'vec4',
  220. inputs: [
  221. { name: 'uv', type: 'vec2' },
  222. { name: 'texSize', type: 'vec2' },
  223. ]
  224. } );
  225. const fxaa = Fn( () => {
  226. return ApplyFXAA( uvNode, this._invSize );
  227. } );
  228. const outputNode = fxaa();
  229. return outputNode;
  230. }
  231. }
  232. export default FXAANode;
  233. /**
  234. * TSL function for creating a FXAA node for anti-aliasing via post processing.
  235. *
  236. * @tsl
  237. * @function
  238. * @param {Node<vec4>} node - The node that represents the input of the effect.
  239. * @returns {FXAANode}
  240. */
  241. export const fxaa = ( node ) => nodeObject( new FXAANode( convertToTexture( node ) ) );
粤ICP备19079148号