SharpenNode.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { HalfFloatType, RenderTarget, Vector2, NodeMaterial, RendererUtils, QuadMesh, TempNode, NodeUpdateType } from 'three/webgpu';
  2. import { Fn, float, vec3, vec4, ivec2, int, uv, floor, abs, max, min, exp2, nodeObject, passTexture, textureSize, textureLoad, convertToTexture } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. const _size = /*@__PURE__*/ new Vector2();
  5. let _rendererState;
  6. /**
  7. * Post processing node for contrast-adaptive sharpening (RCAS).
  8. *
  9. * Reference: {@link https://gpuopen.com/fidelityfx-superresolution/}.
  10. *
  11. * @augments TempNode
  12. * @three_import import { sharpen } from 'three/addons/tsl/display/SharpenNode.js';
  13. */
  14. class SharpenNode extends TempNode {
  15. static get type() {
  16. return 'SharpenNode';
  17. }
  18. /**
  19. * Constructs a new sharpen node.
  20. *
  21. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  22. * @param {Node<float>} [sharpness=0.2] - Sharpening strength. 0 = maximum sharpening, 2 = no sharpening.
  23. * @param {Node<bool>} [denoise=false] - Whether to attenuate sharpening in noisy areas.
  24. */
  25. constructor( textureNode, sharpness = 0.2, denoise = false ) {
  26. super( 'vec4' );
  27. /**
  28. * This flag can be used for type testing.
  29. *
  30. * @type {boolean}
  31. * @readonly
  32. * @default true
  33. */
  34. this.isSharpenNode = true;
  35. /**
  36. * The texture node that represents the input of the effect.
  37. *
  38. * @type {TextureNode}
  39. */
  40. this.textureNode = textureNode;
  41. /**
  42. * Sharpening strength. 0 = maximum, 2 = none.
  43. *
  44. * @type {Node<float>}
  45. * @default 0.2
  46. */
  47. this.sharpness = nodeObject( sharpness );
  48. /**
  49. * Whether to attenuate sharpening in noisy areas.
  50. *
  51. * @type {Node<bool>}
  52. * @default false
  53. */
  54. this.denoise = nodeObject( denoise );
  55. /**
  56. * The render target for the sharpening pass.
  57. *
  58. * @private
  59. * @type {RenderTarget}
  60. */
  61. this._renderTarget = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType } );
  62. this._renderTarget.texture.name = 'SharpenNode.output';
  63. /**
  64. * The result of the effect as a texture node.
  65. *
  66. * @private
  67. * @type {PassTextureNode}
  68. */
  69. this._textureNode = passTexture( this, this._renderTarget.texture );
  70. /**
  71. * The material for the sharpening pass.
  72. *
  73. * @private
  74. * @type {?NodeMaterial}
  75. */
  76. this._material = null;
  77. /**
  78. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  79. * its effect once per frame in `updateBefore()`.
  80. *
  81. * @type {string}
  82. * @default 'frame'
  83. */
  84. this.updateBeforeType = NodeUpdateType.FRAME;
  85. }
  86. /**
  87. * Sets the output size of the effect.
  88. *
  89. * @param {number} width - The width in pixels.
  90. * @param {number} height - The height in pixels.
  91. */
  92. setSize( width, height ) {
  93. this._renderTarget.setSize( width, height );
  94. }
  95. /**
  96. * This method is used to render the effect once per frame.
  97. *
  98. * @param {NodeFrame} frame - The current node frame.
  99. */
  100. updateBefore( frame ) {
  101. const { renderer } = frame;
  102. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  103. //
  104. renderer.getDrawingBufferSize( _size );
  105. this.setSize( _size.x, _size.y );
  106. renderer.setRenderTarget( this._renderTarget );
  107. _quadMesh.material = this._material;
  108. _quadMesh.name = 'Sharpen [ RCAS ]';
  109. _quadMesh.render( renderer );
  110. //
  111. RendererUtils.restoreRendererState( renderer, _rendererState );
  112. }
  113. /**
  114. * Returns the result of the effect as a texture node.
  115. *
  116. * @return {PassTextureNode} A texture node that represents the result of the effect.
  117. */
  118. getTextureNode() {
  119. return this._textureNode;
  120. }
  121. /**
  122. * This method is used to setup the effect's TSL code.
  123. *
  124. * @param {NodeBuilder} builder - The current node builder.
  125. * @return {PassTextureNode}
  126. */
  127. setup( builder ) {
  128. const textureNode = this.textureNode;
  129. const inputTex = textureNode.value;
  130. // RCAS: Robust Contrast-Adaptive Sharpening.
  131. //
  132. // Ported from AMD FidelityFX FSR 1 (ffx_fsr1.h). Uses a 5-tap
  133. // cross pattern (center + up/down/left/right) to compute a
  134. // per-pixel sharpening weight that is automatically limited by
  135. // local contrast to avoid ringing. An optional noise-attenuation
  136. // factor reduces sharpening in noisy areas.
  137. const rcas = Fn( () => {
  138. const targetUV = uv();
  139. const texSize = textureSize( textureLoad( inputTex ) );
  140. const p = ivec2( int( floor( targetUV.x.mul( texSize.x ) ) ), int( floor( targetUV.y.mul( texSize.y ) ) ) ).toConst();
  141. const e = textureLoad( inputTex, p );
  142. const b = textureLoad( inputTex, p.add( ivec2( 0, - 1 ) ) );
  143. const d = textureLoad( inputTex, p.add( ivec2( - 1, 0 ) ) );
  144. const f = textureLoad( inputTex, p.add( ivec2( 1, 0 ) ) );
  145. const h = textureLoad( inputTex, p.add( ivec2( 0, 1 ) ) );
  146. // Approximate luminance (luma times 2).
  147. const luma = ( s ) => s.g.add( s.b.add( s.r ).mul( 0.5 ) );
  148. const bL = luma( b );
  149. const dL = luma( d );
  150. const eL = luma( e );
  151. const fL = luma( f );
  152. const hL = luma( h );
  153. // Sharpening amount from user parameter.
  154. const con = exp2( this.sharpness.negate() ).toConst();
  155. // Min and max of ring.
  156. const mn4 = min( min( b.rgb, d.rgb ), min( f.rgb, h.rgb ) ).toConst();
  157. const mx4 = max( max( b.rgb, d.rgb ), max( f.rgb, h.rgb ) ).toConst();
  158. // Compute adaptive lobe weight.
  159. // Limiters based on how much sharpening the local contrast can tolerate.
  160. const RCAS_LIMIT = float( 0.25 - 1.0 / 16.0 ).toConst();
  161. const hitMin = min( mn4, e.rgb ).div( mx4.mul( 4.0 ) ).toConst();
  162. const hitMax = vec3( 1.0 ).sub( max( mx4, e.rgb ) ).div( mn4.mul( 4.0 ).sub( 4.0 ) ).toConst();
  163. const lobeRGB = max( hitMin.negate(), hitMax ).toConst();
  164. const lobe = max(
  165. RCAS_LIMIT.negate(),
  166. min( max( lobeRGB.r, max( lobeRGB.g, lobeRGB.b ) ), float( 0.0 ) )
  167. ).mul( con ).toConst();
  168. // Noise attenuation.
  169. const nz = bL.add( dL ).add( fL ).add( hL ).mul( 0.25 ).sub( eL ).toConst();
  170. const nzRange = max( max( bL, dL ), max( eL, max( fL, hL ) ) ).sub( min( min( bL, dL ), min( eL, min( fL, hL ) ) ) ).toConst();
  171. const nzFactor = float( 1.0 ).sub( abs( nz ).div( max( nzRange, float( 1.0 / 65536.0 ) ) ).saturate().mul( 0.5 ) ).toConst();
  172. const effectiveLobe = this.denoise.equal( true ).select( lobe.mul( nzFactor ), lobe ).toConst();
  173. // Resolve: weighted blend of cross neighbors and center.
  174. const result = b.rgb.add( d.rgb ).add( f.rgb ).add( h.rgb ).mul( effectiveLobe ).add( e.rgb )
  175. .div( effectiveLobe.mul( 4.0 ).add( 1.0 ) ).toConst();
  176. return vec4( result, e.a );
  177. } );
  178. //
  179. const context = builder.getSharedContext();
  180. const material = this._material || ( this._material = new NodeMaterial() );
  181. material.fragmentNode = rcas().context( context );
  182. material.name = 'Sharpen_RCAS';
  183. material.needsUpdate = true;
  184. //
  185. const properties = builder.getNodeProperties( this );
  186. properties.textureNode = textureNode;
  187. //
  188. return this._textureNode;
  189. }
  190. /**
  191. * Frees internal resources. This method should be called
  192. * when the effect is no longer required.
  193. */
  194. dispose() {
  195. this._renderTarget.dispose();
  196. if ( this._material !== null ) this._material.dispose();
  197. }
  198. }
  199. export default SharpenNode;
  200. /**
  201. * TSL function for creating a sharpen node for post processing.
  202. *
  203. * @tsl
  204. * @function
  205. * @param {Node<vec4>} node - The node that represents the input of the effect.
  206. * @param {(number|Node<float>)} [sharpness=0.2] - Sharpening strength. 0 = maximum, 2 = none.
  207. * @param {(boolean|Node<bool>)} [denoise=false] - Whether to attenuate sharpening in noisy areas.
  208. * @returns {SharpenNode}
  209. */
  210. export const sharpen = ( node, sharpness, denoise ) => new SharpenNode( convertToTexture( node ), sharpness, denoise );
粤ICP备19079148号