AfterImageNode.js 5.9 KB

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