123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- import { RenderTarget, Vector2, NodeMaterial, RendererUtils, QuadMesh, TempNode, NodeUpdateType } from 'three/webgpu';
- import { nodeObject, Fn, If, float, uv, uniform, convertToTexture, vec2, vec4, passTexture, mul } from 'three/tsl';
- const _quadMesh = /*@__PURE__*/ new QuadMesh();
- let _rendererState;
- const premult = /*@__PURE__*/ Fn( ( [ color ] ) => {
- return vec4( color.rgb.mul( color.a ), color.a );
- } ).setLayout( {
- name: 'premult',
- type: 'vec4',
- inputs: [
- { name: 'color', type: 'vec4' }
- ]
- } );
- const unpremult = /*@__PURE__*/ Fn( ( [ color ] ) => {
- If( color.a.equal( 0.0 ), () => vec4( 0.0 ) );
- return vec4( color.rgb.div( color.a ), color.a );
- } ).setLayout( {
- name: 'unpremult',
- type: 'vec4',
- inputs: [
- { name: 'color', type: 'vec4' }
- ]
- } );
- /**
- * Post processing node for creating a gaussian blur effect.
- *
- * @augments TempNode
- * @three_import import { gaussianBlur, premultipliedGaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
- */
- class GaussianBlurNode extends TempNode {
- static get type() {
- return 'GaussianBlurNode';
- }
- /**
- * Constructs a new gaussian blur node.
- *
- * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
- * @param {Node<vec2|float>} directionNode - Defines the direction and radius of the blur.
- * @param {number} sigma - Controls the kernel of the blur filter. Higher values mean a wider blur radius.
- */
- constructor( textureNode, directionNode = null, sigma = 2 ) {
- super( 'vec4' );
- /**
- * The texture node that represents the input of the effect.
- *
- * @type {TextureNode}
- */
- this.textureNode = textureNode;
- /**
- * Defines the direction and radius of the blur.
- *
- * @type {Node<vec2|float>}
- */
- this.directionNode = directionNode;
- /**
- * Controls the kernel of the blur filter. Higher values mean a wider blur radius.
- *
- * @type {number}
- */
- this.sigma = sigma;
- /**
- * A uniform node holding the inverse resolution value.
- *
- * @private
- * @type {UniformNode<vec2>}
- */
- this._invSize = uniform( new Vector2() );
- /**
- * Gaussian blur is applied in two passes (horizontal, vertical).
- * This node controls the direction of each pass.
- *
- * @private
- * @type {UniformNode<vec2>}
- */
- this._passDirection = uniform( new Vector2() );
- /**
- * The render target used for the horizontal pass.
- *
- * @private
- * @type {RenderTarget}
- */
- this._horizontalRT = new RenderTarget( 1, 1, { depthBuffer: false } );
- this._horizontalRT.texture.name = 'GaussianBlurNode.horizontal';
- /**
- * The render target used for the vertical pass.
- *
- * @private
- * @type {RenderTarget}
- */
- this._verticalRT = new RenderTarget( 1, 1, { depthBuffer: false } );
- this._verticalRT.texture.name = 'GaussianBlurNode.vertical';
- /**
- * The result of the effect is represented as a separate texture node.
- *
- * @private
- * @type {PassTextureNode}
- */
- this._textureNode = passTexture( this, this._verticalRT.texture );
- this._textureNode.uvNode = textureNode.uvNode;
- /**
- * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
- * its effect once per frame in `updateBefore()`.
- *
- * @type {string}
- * @default 'frame'
- */
- this.updateBeforeType = NodeUpdateType.FRAME;
- /**
- * Controls the resolution of the effect.
- *
- * @type {Vector2}
- * @default (1,1)
- */
- this.resolution = new Vector2( 1, 1 );
- /**
- * Whether the effect should use premultiplied alpha or not. Set this to `true`
- * if you are going to blur texture input with transparency.
- *
- * @type {boolean}
- * @default false
- */
- this.premultipliedAlpha = false;
- }
- /**
- * Sets the given premultiplied alpha value.
- *
- * @param {boolean} value - Whether the effect should use premultiplied alpha or not.
- * @return {GaussianBlurNode} height - A reference to this node.
- */
- setPremultipliedAlpha( value ) {
- this.premultipliedAlpha = value;
- return this;
- }
- /**
- * Returns the premultiplied alpha value.
- *
- * @return {boolean} Whether the effect should use premultiplied alpha or not.
- */
- getPremultipliedAlpha() {
- return this.premultipliedAlpha;
- }
- /**
- * Sets the size of the effect.
- *
- * @param {number} width - The width of the effect.
- * @param {number} height - The height of the effect.
- */
- setSize( width, height ) {
- width = Math.max( Math.round( width * this.resolution.x ), 1 );
- height = Math.max( Math.round( height * this.resolution.y ), 1 );
- this._invSize.value.set( 1 / width, 1 / height );
- this._horizontalRT.setSize( width, height );
- this._verticalRT.setSize( width, height );
- }
- /**
- * This method is used to render the effect once per frame.
- *
- * @param {NodeFrame} frame - The current node frame.
- */
- updateBefore( frame ) {
- const { renderer } = frame;
- _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
- //
- const textureNode = this.textureNode;
- const map = textureNode.value;
- const currentTexture = textureNode.value;
- _quadMesh.material = this._material;
- this.setSize( map.image.width, map.image.height );
- const textureType = map.type;
- this._horizontalRT.texture.type = textureType;
- this._verticalRT.texture.type = textureType;
- // horizontal
- renderer.setRenderTarget( this._horizontalRT );
- this._passDirection.value.set( 1, 0 );
- _quadMesh.render( renderer );
- // vertical
- textureNode.value = this._horizontalRT.texture;
- renderer.setRenderTarget( this._verticalRT );
- this._passDirection.value.set( 0, 1 );
- _quadMesh.render( renderer );
- // restore
- textureNode.value = currentTexture;
- RendererUtils.restoreRendererState( renderer, _rendererState );
- }
- /**
- * Returns the result of the effect as a texture node.
- *
- * @return {PassTextureNode} A texture node that represents the result of the effect.
- */
- getTextureNode() {
- return this._textureNode;
- }
- /**
- * This method is used to setup the effect's TSL code.
- *
- * @param {NodeBuilder} builder - The current node builder.
- * @return {PassTextureNode}
- */
- setup( builder ) {
- const textureNode = this.textureNode;
- //
- const uvNode = uv();
- const directionNode = vec2( this.directionNode || 1 );
- let sampleTexture, output;
- if ( this.premultipliedAlpha ) {
- // https://lisyarus.github.io/blog/posts/blur-coefficients-generator.html
- sampleTexture = ( uv ) => premult( textureNode.sample( uv ) );
- output = ( color ) => unpremult( color );
- } else {
- sampleTexture = ( uv ) => textureNode.sample( uv );
- output = ( color ) => color;
- }
- const blur = Fn( () => {
- const kernelSize = 3 + ( 2 * this.sigma );
- const gaussianCoefficients = this._getCoefficients( kernelSize );
- const invSize = this._invSize;
- const direction = directionNode.mul( this._passDirection );
- const weightSum = float( gaussianCoefficients[ 0 ] ).toVar();
- const diffuseSum = vec4( sampleTexture( uvNode ).mul( weightSum ) ).toVar();
- for ( let i = 1; i < kernelSize; i ++ ) {
- const x = float( i );
- const w = float( gaussianCoefficients[ i ] );
- const uvOffset = vec2( direction.mul( invSize.mul( x ) ) ).toVar();
- const sample1 = sampleTexture( uvNode.add( uvOffset ) );
- const sample2 = sampleTexture( uvNode.sub( uvOffset ) );
- diffuseSum.addAssign( sample1.add( sample2 ).mul( w ) );
- weightSum.addAssign( mul( 2.0, w ) );
- }
- return output( diffuseSum.div( weightSum ) );
- } );
- //
- const material = this._material || ( this._material = new NodeMaterial() );
- material.fragmentNode = blur().context( builder.getSharedContext() );
- material.name = 'Gaussian_blur';
- material.needsUpdate = true;
- //
- const properties = builder.getNodeProperties( this );
- properties.textureNode = textureNode;
- //
- return this._textureNode;
- }
- /**
- * Frees internal resources. This method should be called
- * when the effect is no longer required.
- */
- dispose() {
- this._horizontalRT.dispose();
- this._verticalRT.dispose();
- }
- /**
- * Computes gaussian coefficients depending on the given kernel radius.
- *
- * @private
- * @param {number} kernelRadius - The kernel radius.
- * @return {Array<number>}
- */
- _getCoefficients( kernelRadius ) {
- const coefficients = [];
- for ( let i = 0; i < kernelRadius; i ++ ) {
- coefficients.push( 0.39894 * Math.exp( - 0.5 * i * i / ( kernelRadius * kernelRadius ) ) / kernelRadius );
- }
- return coefficients;
- }
- }
- export default GaussianBlurNode;
- /**
- * TSL function for creating a gaussian blur node for post processing.
- *
- * @tsl
- * @function
- * @param {Node<vec4>} node - The node that represents the input of the effect.
- * @param {Node<vec2|float>} directionNode - Defines the direction and radius of the blur.
- * @param {number} sigma - Controls the kernel of the blur filter. Higher values mean a wider blur radius.
- * @returns {GaussianBlurNode}
- */
- export const gaussianBlur = ( node, directionNode, sigma ) => nodeObject( new GaussianBlurNode( convertToTexture( node ), directionNode, sigma ) );
- /**
- * TSL function for creating a gaussian blur node for post processing with enabled premultiplied alpha.
- *
- * @tsl
- * @function
- * @param {Node<vec4>} node - The node that represents the input of the effect.
- * @param {Node<vec2|float>} directionNode - Defines the direction and radius of the blur.
- * @param {number} sigma - Controls the kernel of the blur filter. Higher values mean a wider blur radius.
- * @returns {GaussianBlurNode}
- */
- export const premultipliedGaussianBlur = ( node, directionNode, sigma ) => nodeObject( new GaussianBlurNode( convertToTexture( node ), directionNode, sigma ).setPremultipliedAlpha( true ) );
|