AfterImageNode.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 material for the composite pass.
  70. *
  71. * @private
  72. * @type {?NodeMaterial}
  73. */
  74. this._materialComposed = null;
  75. /**
  76. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  77. * its effect once per frame in `updateBefore()`.
  78. *
  79. * @type {string}
  80. * @default 'frame'
  81. */
  82. this.updateBeforeType = NodeUpdateType.FRAME;
  83. }
  84. /**
  85. * Returns the result of the effect as a texture node.
  86. *
  87. * @return {PassTextureNode} A texture node that represents the result of the effect.
  88. */
  89. getTextureNode() {
  90. return this._textureNode;
  91. }
  92. /**
  93. * Sets the size of the effect.
  94. *
  95. * @param {number} width - The width of the effect.
  96. * @param {number} height - The height of the effect.
  97. */
  98. setSize( width, height ) {
  99. this._compRT.setSize( width, height );
  100. this._oldRT.setSize( width, height );
  101. }
  102. /**
  103. * This method is used to render the effect once per frame.
  104. *
  105. * @param {NodeFrame} frame - The current node frame.
  106. */
  107. updateBefore( frame ) {
  108. const { renderer } = frame;
  109. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  110. //
  111. const textureNode = this.textureNode;
  112. const map = textureNode.value;
  113. const textureType = map.type;
  114. this._compRT.texture.type = textureType;
  115. this._oldRT.texture.type = textureType;
  116. renderer.getDrawingBufferSize( _size );
  117. this.setSize( _size.x, _size.y );
  118. // make sure texture nodes point to correct render targets
  119. this._textureNode.value = this._compRT.texture;
  120. this._textureNodeOld.value = this._oldRT.texture;
  121. // composite
  122. _quadMesh.material = this._materialComposed;
  123. _quadMesh.name = 'AfterImage';
  124. renderer.setRenderTarget( this._compRT );
  125. _quadMesh.render( renderer );
  126. // swap
  127. const temp = this._oldRT;
  128. this._oldRT = this._compRT;
  129. this._compRT = temp;
  130. //
  131. RendererUtils.restoreRendererState( renderer, _rendererState );
  132. }
  133. /**
  134. * This method is used to setup the effect's TSL code.
  135. *
  136. * @param {NodeBuilder} builder - The current node builder.
  137. * @return {PassTextureNode}
  138. */
  139. setup( builder ) {
  140. const textureNode = this.textureNode;
  141. const textureNodeOld = this._textureNodeOld;
  142. //
  143. textureNodeOld.uvNode = textureNode.uvNode || uv();
  144. const afterImg = Fn( () => {
  145. const texelOld = textureNodeOld.sample().toVar();
  146. const texelNew = textureNode.sample().toVar();
  147. const threshold = float( 0.1 ).toConst();
  148. // m acts as a mask. It's 1 if the previous pixel was "bright enough" (above the threshold) and 0 if it wasn't.
  149. const m = max( sign( texelOld.sub( threshold ) ), 0.0 );
  150. // This is where the after-image fades:
  151. //
  152. // - If m is 0, texelOld is multiplied by 0, effectively clearing the after-image for that pixel.
  153. // - If m is 1, texelOld is multiplied by "damp". Since "damp" is between 0 and 1, this reduces the color value of
  154. // texelOld, making it darker and causing it to fade.
  155. texelOld.mulAssign( this.damp.mul( m ) );
  156. return max( texelNew, texelOld );
  157. } );
  158. //
  159. const materialComposed = this._materialComposed || ( this._materialComposed = new NodeMaterial() );
  160. materialComposed.name = 'AfterImage';
  161. materialComposed.fragmentNode = afterImg();
  162. //
  163. const properties = builder.getNodeProperties( this );
  164. properties.textureNode = textureNode;
  165. //
  166. return this._textureNode;
  167. }
  168. /**
  169. * Frees internal resources. This method should be called
  170. * when the effect is no longer required.
  171. */
  172. dispose() {
  173. this._compRT.dispose();
  174. this._oldRT.dispose();
  175. if ( this._materialComposed !== null ) this._materialComposed.dispose();
  176. }
  177. }
  178. /**
  179. * TSL function for creating an after image node for post processing.
  180. *
  181. * @tsl
  182. * @function
  183. * @param {Node<vec4>} node - The node that represents the input of the effect.
  184. * @param {(Node<float>|number)} [damp=0.96] - The damping intensity. A higher value means a stronger after image effect.
  185. * @returns {AfterImageNode}
  186. */
  187. export const afterImage = ( node, damp ) => new AfterImageNode( convertToTexture( node ), nodeObject( damp ) );
  188. export default AfterImageNode;
粤ICP备19079148号