GaussianBlurNode.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import TempNode from '../core/TempNode.js';
  2. import { nodeObject, addNodeElement, tslFn, float, vec2, vec3, 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. const quadMesh = new QuadMesh();
  11. class GaussianBlurNode extends TempNode {
  12. constructor( textureNode, sigma = 2 ) {
  13. super( textureNode );
  14. this.textureNode = textureNode;
  15. this.sigma = sigma;
  16. this.directionNode = vec2( 1 );
  17. this._invSize = uniform( new Vector2() );
  18. this._passDirection = uniform( new Vector2() );
  19. this._horizontalRT = new RenderTarget();
  20. this._verticalRT = new RenderTarget();
  21. this.updateBeforeType = NodeUpdateType.RENDER;
  22. this.resolution = new Vector2( 1, 1 );
  23. }
  24. setSize( width, height ) {
  25. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  26. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  27. this._invSize.value.set( 1 / width, 1 / height );
  28. this._horizontalRT.setSize( width, height );
  29. this._verticalRT.setSize( width, height );
  30. }
  31. updateBefore( frame ) {
  32. const { renderer } = frame;
  33. const textureNode = this.textureNode;
  34. const map = textureNode.value;
  35. const currentRenderTarget = renderer.getRenderTarget();
  36. const currentTexture = textureNode.value;
  37. quadMesh.material = this._material;
  38. this.setSize( map.image.width, map.image.height );
  39. // horizontal
  40. renderer.setRenderTarget( this._horizontalRT );
  41. this._passDirection.value.set( 1, 0 );
  42. quadMesh.render( renderer );
  43. // vertical
  44. textureNode.value = this._horizontalRT.texture;
  45. renderer.setRenderTarget( this._verticalRT );
  46. this._passDirection.value.set( 0, 1 );
  47. quadMesh.render( renderer );
  48. // restore
  49. renderer.setRenderTarget( currentRenderTarget );
  50. textureNode.value = currentTexture;
  51. }
  52. setup( builder ) {
  53. const textureNode = this.textureNode;
  54. if ( textureNode.isTextureNode !== true ) {
  55. console.error( 'GaussianBlurNode requires a TextureNode.' );
  56. return vec4();
  57. }
  58. //
  59. const uvNode = textureNode.uvNode || uv();
  60. const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
  61. const blur = tslFn( () => {
  62. const kernelSize = 3 + ( 2 * this.sigma );
  63. const gaussianCoefficients = this._getCoefficients( kernelSize );
  64. const invSize = this._invSize;
  65. const direction = vec2( this.directionNode ).mul( this._passDirection );
  66. const weightSum = float( gaussianCoefficients[ 0 ] ).toVar();
  67. const diffuseSum = vec3( sampleTexture( uvNode ).mul( weightSum ) ).toVar();
  68. for ( let i = 1; i < kernelSize; i ++ ) {
  69. const x = float( i );
  70. const w = float( gaussianCoefficients[ i ] );
  71. const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar();
  72. const sample1 = vec3( sampleTexture( uvNode.add( uvOffset ) ) );
  73. const sample2 = vec3( sampleTexture( uvNode.sub( uvOffset ) ) );
  74. diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) );
  75. weightSum.addAssign( mul( 2.0, w ) );
  76. }
  77. return vec4( diffuseSum.div( weightSum ), 1.0 );
  78. } );
  79. //
  80. const material = this._material || ( this._material = builder.createNodeMaterial( 'MeshBasicNodeMaterial' ) );
  81. material.fragmentNode = blur();
  82. //
  83. const properties = builder.getNodeProperties( this );
  84. properties.textureNode = textureNode;
  85. //
  86. return texture( this._verticalRT.texture );
  87. }
  88. _getCoefficients( kernelRadius ) {
  89. const coefficients = [];
  90. for ( let i = 0; i < kernelRadius; i ++ ) {
  91. coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius );
  92. }
  93. return coefficients;
  94. }
  95. }
  96. export const gaussianBlur = ( node, sigma ) => nodeObject( new GaussianBlurNode( nodeObject( node ), sigma ) );
  97. addNodeElement( 'gaussianBlur', gaussianBlur );
  98. export default GaussianBlurNode;