|
| 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 | + |
| 11 | +const quadMesh = new QuadMesh(); |
| 12 | + |
| 13 | +class GaussianBlurNode extends TempNode { |
| 14 | + |
| 15 | + constructor( textureNode, sigma = 2 ) { |
| 16 | + |
| 17 | + super( textureNode ); |
| 18 | + |
| 19 | + this.textureNode = textureNode; |
| 20 | + this.sigma = sigma; |
| 21 | + |
| 22 | + this.directionNode = vec2( 1 ); |
| 23 | + |
| 24 | + this._invSize = uniform( new Vector2() ); |
| 25 | + this._passDirection = uniform( new Vector2() ); |
| 26 | + |
| 27 | + this._horizontalRT = new RenderTarget(); |
| 28 | + this._verticalRT = new RenderTarget(); |
| 29 | + |
| 30 | + this.updateBeforeType = NodeUpdateType.RENDER; |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + setSize( width, height ) { |
| 35 | + |
| 36 | + this._invSize.value.set( 1 / width, 1 / height ); |
| 37 | + this._horizontalRT.setSize( width, height ); |
| 38 | + this._verticalRT.setSize( width, height ); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + updateBefore( frame ) { |
| 43 | + |
| 44 | + const { renderer } = frame; |
| 45 | + |
| 46 | + const textureNode = this.textureNode; |
| 47 | + const map = textureNode.value; |
| 48 | + |
| 49 | + const currentRenderTarget = renderer.getRenderTarget(); |
| 50 | + const currentTexture = textureNode.value; |
| 51 | + |
| 52 | + quadMesh.material = this._material; |
| 53 | + |
| 54 | + this.setSize( map.image.width, map.image.height ); |
| 55 | + |
| 56 | + // horizontal |
| 57 | + |
| 58 | + renderer.setRenderTarget( this._horizontalRT ); |
| 59 | + |
| 60 | + this._passDirection.value.set( 1, 0 ); |
| 61 | + |
| 62 | + quadMesh.render( renderer ); |
| 63 | + |
| 64 | + // vertical |
| 65 | + |
| 66 | + textureNode.value = this._horizontalRT.texture; |
| 67 | + renderer.setRenderTarget( this._verticalRT ); |
| 68 | + |
| 69 | + this._passDirection.value.set( 0, 1 ); |
| 70 | + |
| 71 | + quadMesh.render( renderer ); |
| 72 | + |
| 73 | + // restore |
| 74 | + |
| 75 | + renderer.setRenderTarget( currentRenderTarget ); |
| 76 | + textureNode.value = currentTexture; |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + setup( builder ) { |
| 81 | + |
| 82 | + const textureNode = this.textureNode; |
| 83 | + |
| 84 | + if ( textureNode.isTextureNode !== true ) { |
| 85 | + |
| 86 | + console.error( 'GaussianBlurNode requires a TextureNode.' ); |
| 87 | + |
| 88 | + return vec4(); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + // |
| 93 | + |
| 94 | + const uvNode = textureNode.uvNode || uv(); |
| 95 | + |
| 96 | + const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } ); |
| 97 | + |
| 98 | + const blur = tslFn( () => { |
| 99 | + |
| 100 | + const kernelSize = 3 + ( 2 * this.sigma ); |
| 101 | + const gaussianCoefficients = this._getCoefficients( kernelSize ); |
| 102 | + |
| 103 | + const invSize = this._invSize; |
| 104 | + const direction = vec2( this.directionNode ).mul( this._passDirection ); |
| 105 | + |
| 106 | + const weightSum = float( gaussianCoefficients[ 0 ] ).toVar(); |
| 107 | + const diffuseSum = vec3( sampleTexture( uvNode ).mul( weightSum ) ).toVar(); |
| 108 | + |
| 109 | + for ( let i = 1; i < kernelSize; i ++ ) { |
| 110 | + |
| 111 | + const x = float( i ); |
| 112 | + const w = float( gaussianCoefficients[ i ] ); |
| 113 | + |
| 114 | + const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar(); |
| 115 | + |
| 116 | + const sample1 = vec3( sampleTexture( uvNode.add( uvOffset ) ) ); |
| 117 | + const sample2 = vec3( sampleTexture( uvNode.sub( uvOffset ) ) ); |
| 118 | + |
| 119 | + diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) ); |
| 120 | + weightSum.addAssign( mul( 2.0, w ) ); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + return vec4( diffuseSum.div( weightSum ), 1.0 ); |
| 125 | + |
| 126 | + } ); |
| 127 | + |
| 128 | + // |
| 129 | + |
| 130 | + const material = this._material || ( this._material = builder.createNodeMaterial( 'MeshBasicNodeMaterial' ) ); |
| 131 | + material.fragmentNode = blur(); |
| 132 | + |
| 133 | + // |
| 134 | + |
| 135 | + const properties = builder.getNodeProperties( this ); |
| 136 | + properties.textureNode = textureNode; |
| 137 | + |
| 138 | + // |
| 139 | + |
| 140 | + return texture( this._verticalRT.texture ); |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + _getCoefficients( kernelRadius ) { |
| 145 | + |
| 146 | + const coefficients = []; |
| 147 | + |
| 148 | + for ( let i = 0; i < kernelRadius; i ++ ) { |
| 149 | + |
| 150 | + coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius ); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + return coefficients; |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
| 159 | + |
| 160 | +export const gaussianBlur = ( node, sigma ) => nodeObject( new GaussianBlurNode( nodeObject( node ), sigma ) ); |
| 161 | + |
| 162 | +addNodeElement( 'gaussianBlur', gaussianBlur ); |
| 163 | + |
| 164 | +export default GaussianBlurNode; |
| 165 | + |
0 commit comments