AfterImageNode.js 5.7 KB

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