GaussianBlurNode.js 3.9 KB

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