GaussianBlurNode.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 { texturePass } from './PassNode.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( 'vec4' );
  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._textureNode = texturePass( this, this._verticalRT.texture );
  27. this.updateBeforeType = NodeUpdateType.RENDER;
  28. this.resolution = new Vector2( 1, 1 );
  29. }
  30. setSize( width, height ) {
  31. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  32. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  33. this._invSize.value.set( 1 / width, 1 / height );
  34. this._horizontalRT.setSize( width, height );
  35. this._verticalRT.setSize( width, height );
  36. }
  37. updateBefore( frame ) {
  38. const { renderer } = frame;
  39. const textureNode = this.textureNode;
  40. const map = textureNode.value;
  41. const currentRenderTarget = renderer.getRenderTarget();
  42. const currentTexture = textureNode.value;
  43. quadMesh1.material = this._material;
  44. quadMesh2.material = this._material;
  45. this.setSize( map.image.width, map.image.height );
  46. const textureType = map.type;
  47. this._horizontalRT.texture.type = textureType;
  48. this._verticalRT.texture.type = textureType;
  49. // horizontal
  50. renderer.setRenderTarget( this._horizontalRT );
  51. this._passDirection.value.set( 1, 0 );
  52. quadMesh1.render( renderer );
  53. // vertical
  54. textureNode.value = this._horizontalRT.texture;
  55. renderer.setRenderTarget( this._verticalRT );
  56. this._passDirection.value.set( 0, 1 );
  57. quadMesh2.render( renderer );
  58. // restore
  59. renderer.setRenderTarget( currentRenderTarget );
  60. textureNode.value = currentTexture;
  61. }
  62. getTextureNode() {
  63. return this._textureNode;
  64. }
  65. setup( builder ) {
  66. const textureNode = this.textureNode;
  67. if ( textureNode.isTextureNode !== true ) {
  68. console.error( 'GaussianBlurNode requires a TextureNode.' );
  69. return vec4();
  70. }
  71. //
  72. const uvNode = textureNode.uvNode || uv();
  73. const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
  74. const blur = tslFn( () => {
  75. const kernelSize = 3 + ( 2 * this.sigma );
  76. const gaussianCoefficients = this._getCoefficients( kernelSize );
  77. const invSize = this._invSize;
  78. const direction = vec2( this.directionNode ).mul( this._passDirection );
  79. const weightSum = float( gaussianCoefficients[ 0 ] ).toVar();
  80. const diffuseSum = vec4( sampleTexture( uvNode ).mul( weightSum ) ).toVar();
  81. for ( let i = 1; i < kernelSize; i ++ ) {
  82. const x = float( i );
  83. const w = float( gaussianCoefficients[ i ] );
  84. const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar();
  85. const sample1 = vec4( sampleTexture( uvNode.add( uvOffset ) ) );
  86. const sample2 = vec4( sampleTexture( uvNode.sub( uvOffset ) ) );
  87. diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) );
  88. weightSum.addAssign( mul( 2.0, w ) );
  89. }
  90. return diffuseSum.div( weightSum );
  91. } );
  92. //
  93. const material = this._material || ( this._material = builder.createNodeMaterial() );
  94. material.fragmentNode = blur();
  95. //
  96. const properties = builder.getNodeProperties( this );
  97. properties.textureNode = textureNode;
  98. //
  99. return this._textureNode;
  100. }
  101. _getCoefficients( kernelRadius ) {
  102. const coefficients = [];
  103. for ( let i = 0; i < kernelRadius; i ++ ) {
  104. coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius );
  105. }
  106. return coefficients;
  107. }
  108. }
  109. export const gaussianBlur = ( node, sigma ) => nodeObject( new GaussianBlurNode( nodeObject( node ), sigma ) );
  110. addNodeElement( 'gaussianBlur', gaussianBlur );
  111. export default GaussianBlurNode;