GaussianBlurNode.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import { RenderTarget, Vector2, NodeMaterial, RendererUtils, QuadMesh, TempNode, NodeUpdateType } from 'three/webgpu';
  2. import { nodeObject, Fn, float, uv, uniform, convertToTexture, vec2, vec4, passTexture, premultiplyAlpha, unpremultiplyAlpha } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. let _rendererState;
  5. /**
  6. * Post processing node for creating a gaussian blur effect.
  7. *
  8. * @augments TempNode
  9. * @three_import import { gaussianBlur, premultipliedGaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  10. */
  11. class GaussianBlurNode extends TempNode {
  12. static get type() {
  13. return 'GaussianBlurNode';
  14. }
  15. /**
  16. * Constructs a new gaussian blur node.
  17. *
  18. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  19. * @param {Node<vec2|float>} directionNode - Defines the direction and radius of the blur.
  20. * @param {number} sigma - Controls the kernel of the blur filter. Higher values mean a wider blur radius.
  21. */
  22. constructor( textureNode, directionNode = null, sigma = 4 ) {
  23. super( 'vec4' );
  24. /**
  25. * The texture node that represents the input of the effect.
  26. *
  27. * @type {TextureNode}
  28. */
  29. this.textureNode = textureNode;
  30. /**
  31. * Defines the direction and radius of the blur.
  32. *
  33. * @type {Node<vec2|float>}
  34. */
  35. this.directionNode = directionNode;
  36. /**
  37. * Controls the kernel of the blur filter. Higher values mean a wider blur radius.
  38. *
  39. * @type {number}
  40. */
  41. this.sigma = sigma;
  42. /**
  43. * A uniform node holding the inverse resolution value.
  44. *
  45. * @private
  46. * @type {UniformNode<vec2>}
  47. */
  48. this._invSize = uniform( new Vector2() );
  49. /**
  50. * Gaussian blur is applied in two passes (horizontal, vertical).
  51. * This node controls the direction of each pass.
  52. *
  53. * @private
  54. * @type {UniformNode<vec2>}
  55. */
  56. this._passDirection = uniform( new Vector2() );
  57. /**
  58. * The render target used for the horizontal pass.
  59. *
  60. * @private
  61. * @type {RenderTarget}
  62. */
  63. this._horizontalRT = new RenderTarget( 1, 1, { depthBuffer: false } );
  64. this._horizontalRT.texture.name = 'GaussianBlurNode.horizontal';
  65. /**
  66. * The render target used for the vertical pass.
  67. *
  68. * @private
  69. * @type {RenderTarget}
  70. */
  71. this._verticalRT = new RenderTarget( 1, 1, { depthBuffer: false } );
  72. this._verticalRT.texture.name = 'GaussianBlurNode.vertical';
  73. /**
  74. * The result of the effect is represented as a separate texture node.
  75. *
  76. * @private
  77. * @type {PassTextureNode}
  78. */
  79. this._textureNode = passTexture( this, this._verticalRT.texture );
  80. this._textureNode.uvNode = textureNode.uvNode;
  81. /**
  82. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  83. * its effect once per frame in `updateBefore()`.
  84. *
  85. * @type {string}
  86. * @default 'frame'
  87. */
  88. this.updateBeforeType = NodeUpdateType.FRAME;
  89. /**
  90. * Controls the resolution of the effect.
  91. *
  92. * @type {Vector2}
  93. * @default (1,1)
  94. */
  95. this.resolution = new Vector2( 1, 1 );
  96. /**
  97. * Whether the effect should use premultiplied alpha or not. Set this to `true`
  98. * if you are going to blur texture input with transparency.
  99. *
  100. * @type {boolean}
  101. * @default false
  102. */
  103. this.premultipliedAlpha = false;
  104. }
  105. /**
  106. * Sets the given premultiplied alpha value.
  107. *
  108. * @param {boolean} value - Whether the effect should use premultiplied alpha or not.
  109. * @return {GaussianBlurNode} height - A reference to this node.
  110. */
  111. setPremultipliedAlpha( value ) {
  112. this.premultipliedAlpha = value;
  113. return this;
  114. }
  115. /**
  116. * Returns the premultiplied alpha value.
  117. *
  118. * @return {boolean} Whether the effect should use premultiplied alpha or not.
  119. */
  120. getPremultipliedAlpha() {
  121. return this.premultipliedAlpha;
  122. }
  123. /**
  124. * Sets the size of the effect.
  125. *
  126. * @param {number} width - The width of the effect.
  127. * @param {number} height - The height of the effect.
  128. */
  129. setSize( width, height ) {
  130. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  131. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  132. this._invSize.value.set( 1 / width, 1 / height );
  133. this._horizontalRT.setSize( width, height );
  134. this._verticalRT.setSize( width, height );
  135. }
  136. /**
  137. * This method is used to render the effect once per frame.
  138. *
  139. * @param {NodeFrame} frame - The current node frame.
  140. */
  141. updateBefore( frame ) {
  142. const { renderer } = frame;
  143. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  144. //
  145. const textureNode = this.textureNode;
  146. const map = textureNode.value;
  147. const currentTexture = textureNode.value;
  148. _quadMesh.material = this._material;
  149. this.setSize( map.image.width, map.image.height );
  150. const textureType = map.type;
  151. this._horizontalRT.texture.type = textureType;
  152. this._verticalRT.texture.type = textureType;
  153. // horizontal
  154. renderer.setRenderTarget( this._horizontalRT );
  155. this._passDirection.value.set( 1, 0 );
  156. _quadMesh.render( renderer );
  157. // vertical
  158. textureNode.value = this._horizontalRT.texture;
  159. renderer.setRenderTarget( this._verticalRT );
  160. this._passDirection.value.set( 0, 1 );
  161. _quadMesh.render( renderer );
  162. // restore
  163. textureNode.value = currentTexture;
  164. RendererUtils.restoreRendererState( renderer, _rendererState );
  165. }
  166. /**
  167. * Returns the result of the effect as a texture node.
  168. *
  169. * @return {PassTextureNode} A texture node that represents the result of the effect.
  170. */
  171. getTextureNode() {
  172. return this._textureNode;
  173. }
  174. /**
  175. * This method is used to setup the effect's TSL code.
  176. *
  177. * @param {NodeBuilder} builder - The current node builder.
  178. * @return {PassTextureNode}
  179. */
  180. setup( builder ) {
  181. const textureNode = this.textureNode;
  182. //
  183. const uvNode = uv();
  184. const directionNode = vec2( this.directionNode || 1 );
  185. let sampleTexture, output;
  186. if ( this.premultipliedAlpha ) {
  187. // https://lisyarus.github.io/blog/posts/blur-coefficients-generator.html
  188. sampleTexture = ( uv ) => premultiplyAlpha( textureNode.sample( uv ) );
  189. output = ( color ) => unpremultiplyAlpha( color );
  190. } else {
  191. sampleTexture = ( uv ) => textureNode.sample( uv );
  192. output = ( color ) => color;
  193. }
  194. const blur = Fn( () => {
  195. const kernelSize = 3 + ( 2 * this.sigma );
  196. const gaussianCoefficients = this._getCoefficients( kernelSize );
  197. const invSize = this._invSize;
  198. const direction = directionNode.mul( this._passDirection );
  199. const diffuseSum = vec4( sampleTexture( uvNode ).mul( gaussianCoefficients[ 0 ] ) ).toVar();
  200. for ( let i = 1; i < kernelSize; i ++ ) {
  201. const x = float( i );
  202. const w = float( gaussianCoefficients[ i ] );
  203. const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar();
  204. const sample1 = sampleTexture( uvNode.add( uvOffset ) );
  205. const sample2 = sampleTexture( uvNode.sub( uvOffset ) );
  206. diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) );
  207. }
  208. return output( diffuseSum );
  209. } );
  210. //
  211. const material = this._material || ( this._material = new NodeMaterial() );
  212. material.fragmentNode = blur().context( builder.getSharedContext() );
  213. material.name = 'Gaussian_blur';
  214. material.needsUpdate = true;
  215. //
  216. const properties = builder.getNodeProperties( this );
  217. properties.textureNode = textureNode;
  218. //
  219. return this._textureNode;
  220. }
  221. /**
  222. * Frees internal resources. This method should be called
  223. * when the effect is no longer required.
  224. */
  225. dispose() {
  226. this._horizontalRT.dispose();
  227. this._verticalRT.dispose();
  228. }
  229. /**
  230. * Computes gaussian coefficients depending on the given kernel radius.
  231. *
  232. * @private
  233. * @param {number} kernelRadius - The kernel radius.
  234. * @return {Array<number>}
  235. */
  236. _getCoefficients( kernelRadius ) {
  237. const coefficients = [];
  238. const sigma = kernelRadius / 3;
  239. for ( let i = 0; i < kernelRadius; i ++ ) {
  240. coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( sigma * sigma ) ) / sigma );
  241. }
  242. return coefficients;
  243. }
  244. }
  245. export default GaussianBlurNode;
  246. /**
  247. * TSL function for creating a gaussian blur node for post processing.
  248. *
  249. * @tsl
  250. * @function
  251. * @param {Node<vec4>} node - The node that represents the input of the effect.
  252. * @param {Node<vec2|float>} directionNode - Defines the direction and radius of the blur.
  253. * @param {number} sigma - Controls the kernel of the blur filter. Higher values mean a wider blur radius.
  254. * @returns {GaussianBlurNode}
  255. */
  256. export const gaussianBlur = ( node, directionNode, sigma ) => nodeObject( new GaussianBlurNode( convertToTexture( node ), directionNode, sigma ) );
  257. /**
  258. * TSL function for creating a gaussian blur node for post processing with enabled premultiplied alpha.
  259. *
  260. * @tsl
  261. * @function
  262. * @param {Node<vec4>} node - The node that represents the input of the effect.
  263. * @param {Node<vec2|float>} directionNode - Defines the direction and radius of the blur.
  264. * @param {number} sigma - Controls the kernel of the blur filter. Higher values mean a wider blur radius.
  265. * @returns {GaussianBlurNode}
  266. */
  267. export const premultipliedGaussianBlur = ( node, directionNode, sigma ) => nodeObject( new GaussianBlurNode( convertToTexture( node ), directionNode, sigma ).setPremultipliedAlpha( true ) );
粤ICP备19079148号