AfterImageNode.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { RenderTarget, Vector2, QuadMesh, NodeMaterial, RendererUtils, TempNode, NodeUpdateType } from 'three/webgpu';
  2. import { nodeObject, Fn, float, uv, texture, passTexture, uniform, sign, max, convertToTexture } from 'three/tsl';
  3. const _size = /*@__PURE__*/ new Vector2();
  4. const _quadMeshComp = /*@__PURE__*/ new QuadMesh();
  5. let _rendererState;
  6. /**
  7. * Post processing node for creating an after image effect.
  8. *
  9. * @augments TempNode
  10. * @three_import import { afterImage } from 'three/addons/tsl/display/AfterImageNode.js';
  11. */
  12. class AfterImageNode extends TempNode {
  13. static get type() {
  14. return 'AfterImageNode';
  15. }
  16. /**
  17. * Constructs a new after image node.
  18. *
  19. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  20. * @param {number} [damp=0.96] - The damping intensity. A higher value means a stronger after image effect.
  21. */
  22. constructor( textureNode, damp = 0.96 ) {
  23. super( 'vec4' );
  24. /**
  25. * The texture node that represents the input of the effect.
  26. *
  27. * @type {TextureNode}
  28. */
  29. this.textureNode = textureNode;
  30. /**
  31. * The texture represents the pervious frame.
  32. *
  33. * @type {TextureNode}
  34. */
  35. this.textureNodeOld = texture( null );
  36. /**
  37. * How quickly the after-image fades. A higher value means the after-image
  38. * persists longer, while a lower value means it fades faster. Should be in
  39. * the range `[0, 1]`.
  40. *
  41. * @type {UniformNode<float>}
  42. */
  43. this.damp = uniform( damp );
  44. /**
  45. * The render target used for compositing the effect.
  46. *
  47. * @private
  48. * @type {RenderTarget}
  49. */
  50. this._compRT = new RenderTarget( 1, 1, { depthBuffer: false } );
  51. this._compRT.texture.name = 'AfterImageNode.comp';
  52. /**
  53. * The render target that represents the previous frame.
  54. *
  55. * @private
  56. * @type {RenderTarget}
  57. */
  58. this._oldRT = new RenderTarget( 1, 1, { depthBuffer: false } );
  59. this._oldRT.texture.name = 'AfterImageNode.old';
  60. /**
  61. * The result of the effect is represented as a separate texture node.
  62. *
  63. * @private
  64. * @type {PassTextureNode}
  65. */
  66. this._textureNode = passTexture( this, this._compRT.texture );
  67. /**
  68. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  69. * its effect once per frame in `updateBefore()`.
  70. *
  71. * @type {string}
  72. * @default 'frame'
  73. */
  74. this.updateBeforeType = NodeUpdateType.FRAME;
  75. }
  76. /**
  77. * Returns the result of the effect as a texture node.
  78. *
  79. * @return {PassTextureNode} A texture node that represents the result of the effect.
  80. */
  81. getTextureNode() {
  82. return this._textureNode;
  83. }
  84. /**
  85. * Sets the size of the effect.
  86. *
  87. * @param {number} width - The width of the effect.
  88. * @param {number} height - The height of the effect.
  89. */
  90. setSize( width, height ) {
  91. this._compRT.setSize( width, height );
  92. this._oldRT.setSize( width, height );
  93. }
  94. /**
  95. * This method is used to render the effect once per frame.
  96. *
  97. * @param {NodeFrame} frame - The current node frame.
  98. */
  99. updateBefore( frame ) {
  100. const { renderer } = frame;
  101. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  102. //
  103. const textureNode = this.textureNode;
  104. const map = textureNode.value;
  105. const textureType = map.type;
  106. this._compRT.texture.type = textureType;
  107. this._oldRT.texture.type = textureType;
  108. renderer.getDrawingBufferSize( _size );
  109. this.setSize( _size.x, _size.y );
  110. const currentTexture = textureNode.value;
  111. this.textureNodeOld.value = this._oldRT.texture;
  112. // comp
  113. _quadMeshComp.material = this._materialComposed;
  114. renderer.setRenderTarget( this._compRT );
  115. _quadMeshComp.render( renderer );
  116. // Swap the textures
  117. const temp = this._oldRT;
  118. this._oldRT = this._compRT;
  119. this._compRT = temp;
  120. //
  121. textureNode.value = currentTexture;
  122. RendererUtils.restoreRendererState( renderer, _rendererState );
  123. }
  124. /**
  125. * This method is used to setup the effect's TSL code.
  126. *
  127. * @param {NodeBuilder} builder - The current node builder.
  128. * @return {PassTextureNode}
  129. */
  130. setup( builder ) {
  131. const textureNode = this.textureNode;
  132. const textureNodeOld = this.textureNodeOld;
  133. //
  134. textureNodeOld.uvNode = textureNode.uvNode || uv();
  135. const afterImg = Fn( () => {
  136. const texelOld = textureNodeOld.sample().toVar();
  137. const texelNew = textureNode.sample().toVar();
  138. const threshold = float( 0.1 ).toConst();
  139. // m acts as a mask. It's 1 if the previous pixel was "bright enough" (above the threshold) and 0 if it wasn't.
  140. const m = max( sign( texelOld.sub( threshold ) ), 0.0 );
  141. // This is where the after-image fades:
  142. //
  143. // - If m is 0, texelOld is multiplied by 0, effectively clearing the after-image for that pixel.
  144. // - If m is 1, texelOld is multiplied by "damp". Since "damp" is between 0 and 1, this reduces the color value of
  145. // texelOld, making it darker and causing it to fade.
  146. texelOld.mulAssign( this.damp.mul( m ) );
  147. return max( texelNew, texelOld );
  148. } );
  149. //
  150. const materialComposed = this._materialComposed || ( this._materialComposed = new NodeMaterial() );
  151. materialComposed.name = 'AfterImage';
  152. materialComposed.fragmentNode = afterImg();
  153. //
  154. const properties = builder.getNodeProperties( this );
  155. properties.textureNode = textureNode;
  156. //
  157. return this._textureNode;
  158. }
  159. /**
  160. * Frees internal resources. This method should be called
  161. * when the effect is no longer required.
  162. */
  163. dispose() {
  164. this._compRT.dispose();
  165. this._oldRT.dispose();
  166. }
  167. }
  168. /**
  169. * TSL function for creating an after image node for post processing.
  170. *
  171. * @tsl
  172. * @function
  173. * @param {Node<vec4>} node - The node that represents the input of the effect.
  174. * @param {number} [damp=0.96] - The damping intensity. A higher value means a stronger after image effect.
  175. * @returns {AfterImageNode}
  176. */
  177. export const afterImage = ( node, damp ) => nodeObject( new AfterImageNode( convertToTexture( node ), damp ) );
  178. export default AfterImageNode;