GaussianBlurNode.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import TempNode from '../core/TempNode.js';
  2. import { nodeObject, addNodeElement, tslFn, float, vec2, vec4 } from '../shadernode/ShaderNode.js';
  3. import { NodeUpdateType } from '../core/constants.js';
  4. import { mul } from '../math/OperatorNode.js';
  5. import { uv } from '../accessors/UVNode.js';
  6. import { texture } from '../accessors/TextureNode.js';
  7. import { uniform } from '../core/UniformNode.js';
  8. import { Vector2, RenderTarget } from 'three';
  9. import QuadMesh from '../../objects/QuadMesh.js';
  10. // WebGPU: The use of a single QuadMesh for both gaussian blur passes results in a single RenderObject with a SampledTexture binding that
  11. // alternates between source textures and triggers creation of new BindGroups and BindGroupLayouts every frame.
  12. const quadMesh1 = new QuadMesh();
  13. const quadMesh2 = new QuadMesh();
  14. class GaussianBlurNode extends TempNode {
  15. constructor( textureNode, sigma = 2 ) {
  16. super( textureNode );
  17. this.textureNode = textureNode;
  18. this.sigma = sigma;
  19. this.directionNode = vec2( 1 );
  20. this._invSize = uniform( new Vector2() );
  21. this._passDirection = uniform( new Vector2() );
  22. this._horizontalRT = new RenderTarget();
  23. this._horizontalRT.texture.name = 'GaussianBlurNode.horizontal';
  24. this._verticalRT = new RenderTarget();
  25. this._verticalRT.texture.name = 'GaussianBlurNode.vertical';
  26. this.updateBeforeType = NodeUpdateType.RENDER;
  27. this.resolution = new Vector2( 1, 1 );
  28. }
  29. setSize( width, height ) {
  30. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  31. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  32. this._invSize.value.set( 1 / width, 1 / height );
  33. this._horizontalRT.setSize( width, height );
  34. this._verticalRT.setSize( width, height );
  35. }
  36. updateBefore( frame ) {
  37. const { renderer } = frame;
  38. const textureNode = this.textureNode;
  39. const map = textureNode.value;
  40. const currentRenderTarget = renderer.getRenderTarget();
  41. const currentTexture = textureNode.value;
  42. quadMesh1.material = this._material;
  43. quadMesh2.material = this._material;
  44. this.setSize( map.image.width, map.image.height );
  45. // horizontal
  46. renderer.setRenderTarget( this._horizontalRT );
  47. this._passDirection.value.set( 1, 0 );
  48. quadMesh1.render( renderer );
  49. // vertical
  50. textureNode.value = this._horizontalRT.texture;
  51. renderer.setRenderTarget( this._verticalRT );
  52. this._passDirection.value.set( 0, 1 );
  53. quadMesh2.render( renderer );
  54. // restore
  55. renderer.setRenderTarget( currentRenderTarget );
  56. textureNode.value = currentTexture;
  57. }
  58. setup( builder ) {
  59. const textureNode = this.textureNode;
  60. if ( textureNode.isTextureNode !== true ) {
  61. console.error( 'GaussianBlurNode requires a TextureNode.' );
  62. return vec4();
  63. }
  64. //
  65. const uvNode = textureNode.uvNode || uv();
  66. const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
  67. const blur = tslFn( () => {
  68. const kernelSize = 3 + ( 2 * this.sigma );
  69. const gaussianCoefficients = this._getCoefficients( kernelSize );
  70. const invSize = this._invSize;
  71. const direction = vec2( this.directionNode ).mul( this._passDirection );
  72. const weightSum = float( gaussianCoefficients[ 0 ] ).toVar();
  73. const diffuseSum = vec4( sampleTexture( uvNode ).mul( weightSum ) ).toVar();
  74. for ( let i = 1; i < kernelSize; i ++ ) {
  75. const x = float( i );
  76. const w = float( gaussianCoefficients[ i ] );
  77. const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar();
  78. const sample1 = vec4( sampleTexture( uvNode.add( uvOffset ) ) );
  79. const sample2 = vec4( sampleTexture( uvNode.sub( uvOffset ) ) );
  80. diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) );
  81. weightSum.addAssign( mul( 2.0, w ) );
  82. }
  83. return diffuseSum.div( weightSum );
  84. } );
  85. //
  86. const material = this._material || ( this._material = builder.createNodeMaterial() );
  87. material.fragmentNode = blur();
  88. //
  89. const properties = builder.getNodeProperties( this );
  90. properties.textureNode = textureNode;
  91. //
  92. return texture( this._verticalRT.texture );
  93. }
  94. _getCoefficients( kernelRadius ) {
  95. const coefficients = [];
  96. for ( let i = 0; i < kernelRadius; i ++ ) {
  97. coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius );
  98. }
  99. return coefficients;
  100. }
  101. }
  102. export const gaussianBlur = ( node, sigma ) => nodeObject( new GaussianBlurNode( nodeObject( node ), sigma ) );
  103. addNodeElement( 'gaussianBlur', gaussianBlur );
  104. export default GaussianBlurNode;