123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- import { RenderTarget, Vector2, QuadMesh, NodeMaterial, RendererUtils, TempNode, NodeUpdateType } from 'three/webgpu';
- import { nodeObject, Fn, float, uv, texture, passTexture, uniform, sign, max, convertToTexture } from 'three/tsl';
- const _size = /*@__PURE__*/ new Vector2();
- const _quadMeshComp = /*@__PURE__*/ new QuadMesh();
- let _rendererState;
- /**
- * Post processing node for creating an after image effect.
- *
- * @augments TempNode
- * @three_import import { afterImage } from 'three/addons/tsl/display/AfterImageNode.js';
- */
- class AfterImageNode extends TempNode {
- static get type() {
- return 'AfterImageNode';
- }
- /**
- * Constructs a new after image node.
- *
- * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
- * @param {number} [damp=0.96] - The damping intensity. A higher value means a stronger after image effect.
- */
- constructor( textureNode, damp = 0.96 ) {
- super( 'vec4' );
- /**
- * The texture node that represents the input of the effect.
- *
- * @type {TextureNode}
- */
- this.textureNode = textureNode;
- /**
- * The texture represents the pervious frame.
- *
- * @type {TextureNode}
- */
- this.textureNodeOld = texture( null );
- /**
- * How quickly the after-image fades. A higher value means the after-image
- * persists longer, while a lower value means it fades faster. Should be in
- * the range `[0, 1]`.
- *
- * @type {UniformNode<float>}
- */
- this.damp = uniform( damp );
- /**
- * The render target used for compositing the effect.
- *
- * @private
- * @type {RenderTarget}
- */
- this._compRT = new RenderTarget( 1, 1, { depthBuffer: false } );
- this._compRT.texture.name = 'AfterImageNode.comp';
- /**
- * The render target that represents the previous frame.
- *
- * @private
- * @type {RenderTarget}
- */
- this._oldRT = new RenderTarget( 1, 1, { depthBuffer: false } );
- this._oldRT.texture.name = 'AfterImageNode.old';
- /**
- * The result of the effect is represented as a separate texture node.
- *
- * @private
- * @type {PassTextureNode}
- */
- this._textureNode = passTexture( this, this._compRT.texture );
- /**
- * 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;
- }
- /**
- * 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;
- }
- /**
- * 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 ) {
- this._compRT.setSize( width, height );
- this._oldRT.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 textureType = map.type;
- this._compRT.texture.type = textureType;
- this._oldRT.texture.type = textureType;
- renderer.getDrawingBufferSize( _size );
- this.setSize( _size.x, _size.y );
- const currentTexture = textureNode.value;
- this.textureNodeOld.value = this._oldRT.texture;
- // comp
- _quadMeshComp.material = this._materialComposed;
- renderer.setRenderTarget( this._compRT );
- _quadMeshComp.render( renderer );
- // Swap the textures
- const temp = this._oldRT;
- this._oldRT = this._compRT;
- this._compRT = temp;
- //
- textureNode.value = currentTexture;
- RendererUtils.restoreRendererState( renderer, _rendererState );
- }
- /**
- * 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 textureNodeOld = this.textureNodeOld;
- //
- textureNodeOld.uvNode = textureNode.uvNode || uv();
- const afterImg = Fn( () => {
- const texelOld = textureNodeOld.sample().toVar();
- const texelNew = textureNode.sample().toVar();
- const threshold = float( 0.1 ).toConst();
- // m acts as a mask. It's 1 if the previous pixel was "bright enough" (above the threshold) and 0 if it wasn't.
- const m = max( sign( texelOld.sub( threshold ) ), 0.0 );
- // This is where the after-image fades:
- //
- // - If m is 0, texelOld is multiplied by 0, effectively clearing the after-image for that pixel.
- // - If m is 1, texelOld is multiplied by "damp". Since "damp" is between 0 and 1, this reduces the color value of
- // texelOld, making it darker and causing it to fade.
- texelOld.mulAssign( this.damp.mul( m ) );
- return max( texelNew, texelOld );
- } );
- //
- const materialComposed = this._materialComposed || ( this._materialComposed = new NodeMaterial() );
- materialComposed.name = 'AfterImage';
- materialComposed.fragmentNode = afterImg();
- //
- 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._compRT.dispose();
- this._oldRT.dispose();
- }
- }
- /**
- * TSL function for creating an after image node for post processing.
- *
- * @tsl
- * @function
- * @param {Node<vec4>} node - The node that represents the input of the effect.
- * @param {number} [damp=0.96] - The damping intensity. A higher value means a stronger after image effect.
- * @returns {AfterImageNode}
- */
- export const afterImage = ( node, damp ) => nodeObject( new AfterImageNode( convertToTexture( node ), damp ) );
- export default AfterImageNode;
|