LensflareNode.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { RenderTarget, Vector2, TempNode, NodeUpdateType, QuadMesh, RendererUtils, NodeMaterial } from 'three/webgpu';
  2. import { convertToTexture, nodeObject, Fn, passTexture, uv, vec2, vec3, vec4, max, float, sub, int, Loop, fract, pow, distance } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. const _size = /*@__PURE__*/ new Vector2();
  5. let _rendererState;
  6. /**
  7. * Post processing node for adding a bloom-based lens flare effect. This effect
  8. * requires that you extract the bloom of the scene via a bloom pass first.
  9. *
  10. * References:
  11. * - {@link https://john-chapman-graphics.blogspot.com/2013/02/pseudo-lens-flare.html}.
  12. * - {@link https://john-chapman.github.io/2017/11/05/pseudo-lens-flare.html}.
  13. *
  14. * @augments TempNode
  15. * @three_import import { lensflare } from 'three/addons/tsl/display/LensflareNode.js';
  16. */
  17. class LensflareNode extends TempNode {
  18. static get type() {
  19. return 'LensflareNode';
  20. }
  21. /**
  22. * Constructs a new lens flare node.
  23. *
  24. * @param {TextureNode} textureNode - The texture node that represents the scene's bloom.
  25. * @param {Object} params - The parameter object for configuring the effect.
  26. * @param {Node<vec3> | Color} [params.ghostTint=vec3(1, 1, 1)] - Defines the tint of the flare/ghosts.
  27. * @param {Node<float> | number} [params.threshold=float(0.5)] - Controls the size and strength of the effect. A higher threshold results in smaller flares.
  28. * @param {Node<float> | number} [params.ghostSamples=float(4)] - Represents the number of flares/ghosts per bright spot which pivot around the center.
  29. * @param {Node<float> | number} [params.ghostSpacing=float(0.25)] - Defines the spacing of the flares/ghosts.
  30. * @param {Node<float> | number} [params.ghostAttenuationFactor=float(25)] - Defines the attenuation factor of flares/ghosts.
  31. * @param {number} [params.downSampleRatio=4] - Defines how downsampling since the effect is usually not rendered at full resolution.
  32. */
  33. constructor( textureNode, params = {} ) {
  34. super( 'vec4' );
  35. /**
  36. * The texture node that represents the scene's bloom.
  37. *
  38. * @type {TextureNode}
  39. */
  40. this.textureNode = textureNode;
  41. const {
  42. ghostTint = vec3( 1, 1, 1 ),
  43. threshold = float( 0.5 ),
  44. ghostSamples = float( 4 ),
  45. ghostSpacing = float( 0.25 ),
  46. ghostAttenuationFactor = float( 25 ),
  47. downSampleRatio = 4
  48. } = params;
  49. /**
  50. * Defines the tint of the flare/ghosts.
  51. *
  52. * @type {Node<vec3>}
  53. */
  54. this.ghostTintNode = nodeObject( ghostTint );
  55. /**
  56. * Controls the size and strength of the effect. A higher threshold results in smaller flares.
  57. *
  58. * @type {Node<float>}
  59. */
  60. this.thresholdNode = nodeObject( threshold );
  61. /**
  62. * Represents the number of flares/ghosts per bright spot which pivot around the center.
  63. *
  64. * @type {Node<float>}
  65. */
  66. this.ghostSamplesNode = nodeObject( ghostSamples );
  67. /**
  68. * Defines the spacing of the flares/ghosts.
  69. *
  70. * @type {Node<float>}
  71. */
  72. this.ghostSpacingNode = nodeObject( ghostSpacing );
  73. /**
  74. * Defines the attenuation factor of flares/ghosts.
  75. *
  76. * @type {Node<float>}
  77. */
  78. this.ghostAttenuationFactorNode = nodeObject( ghostAttenuationFactor );
  79. /**
  80. * Defines how downsampling since the effect is usually not rendered at full resolution.
  81. *
  82. * @type {number}
  83. */
  84. this.downSampleRatio = downSampleRatio;
  85. /**
  86. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  87. * its effect once per frame in `updateBefore()`.
  88. *
  89. * @type {string}
  90. * @default 'frame'
  91. */
  92. this.updateBeforeType = NodeUpdateType.FRAME;
  93. /**
  94. * The internal render target of the effect.
  95. *
  96. * @private
  97. * @type {RenderTarget}
  98. */
  99. this._renderTarget = new RenderTarget( 1, 1, { depthBuffer: false } );
  100. this._renderTarget.texture.name = 'LensflareNode';
  101. /**
  102. * The node material that holds the effect's TSL code.
  103. *
  104. * @private
  105. * @type {NodeMaterial}
  106. */
  107. this._material = new NodeMaterial();
  108. this._material.name = 'LensflareNode';
  109. /**
  110. * The result of the effect is represented as a separate texture node.
  111. *
  112. * @private
  113. * @type {PassTextureNode}
  114. */
  115. this._textureNode = passTexture( this, this._renderTarget.texture );
  116. }
  117. /**
  118. * Returns the result of the effect as a texture node.
  119. *
  120. * @return {PassTextureNode} A texture node that represents the result of the effect.
  121. */
  122. getTextureNode() {
  123. return this._textureNode;
  124. }
  125. /**
  126. * Sets the size of the effect.
  127. *
  128. * @param {number} width - The width of the effect.
  129. * @param {number} height - The height of the effect.
  130. */
  131. setSize( width, height ) {
  132. const resx = Math.round( width / this.downSampleRatio );
  133. const resy = Math.round( height / this.downSampleRatio );
  134. this._renderTarget.setSize( resx, resy );
  135. }
  136. /**
  137. * This method is used to render the effect once per frame.
  138. *
  139. * @param {NodeFrame} frame - The current node frame.
  140. */
  141. updateBefore( frame ) {
  142. const { renderer } = frame;
  143. const size = renderer.getDrawingBufferSize( _size );
  144. this.setSize( size.width, size.height );
  145. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  146. _quadMesh.material = this._material;
  147. // clear
  148. renderer.setMRT( null );
  149. // lensflare
  150. renderer.setRenderTarget( this._renderTarget );
  151. _quadMesh.render( renderer );
  152. // restore
  153. RendererUtils.restoreRendererState( renderer, _rendererState );
  154. }
  155. /**
  156. * This method is used to setup the effect's TSL code.
  157. *
  158. * @param {NodeBuilder} builder - The current node builder.
  159. * @return {PassTextureNode}
  160. */
  161. setup( builder ) {
  162. const lensflare = Fn( () => {
  163. // flip uvs so lens flare pivot around the image center
  164. const texCoord = uv().oneMinus().toVar();
  165. // ghosts are positioned along this vector
  166. const ghostVec = sub( vec2( 0.5 ), texCoord ).mul( this.ghostSpacingNode ).toVar();
  167. // sample ghosts
  168. const result = vec4().toVar();
  169. Loop( { start: int( 0 ), end: int( this.ghostSamplesNode ), type: 'int', condition: '<' }, ( { i } ) => {
  170. // use fract() to ensure that the texture coordinates wrap around
  171. const sampleUv = fract( texCoord.add( ghostVec.mul( float( i ) ) ) ).toVar();
  172. // reduce contributions from samples at the screen edge
  173. const d = distance( sampleUv, vec2( 0.5 ) );
  174. const weight = pow( d.oneMinus(), this.ghostAttenuationFactorNode );
  175. // accumulate
  176. let sample = this.textureNode.sample( sampleUv ).rgb;
  177. sample = max( sample.sub( this.thresholdNode ), vec3( 0 ) ).mul( this.ghostTintNode );
  178. result.addAssign( sample.mul( weight ) );
  179. } );
  180. return result;
  181. } );
  182. this._material.fragmentNode = lensflare().context( builder.getSharedContext() );
  183. this._material.needsUpdate = true;
  184. return this._textureNode;
  185. }
  186. /**
  187. * Frees internal resources. This method should be called
  188. * when the effect is no longer required.
  189. */
  190. dispose() {
  191. this._renderTarget.dispose();
  192. this._material.dispose();
  193. }
  194. }
  195. export default LensflareNode;
  196. /**
  197. * TSL function for creating a bloom-based lens flare effect.
  198. *
  199. * @tsl
  200. * @function
  201. * @param {TextureNode} node - The node that represents the scene's bloom.
  202. * @param {Object} params - The parameter object for configuring the effect.
  203. * @param {Node<vec3> | Color} [params.ghostTint=vec3(1, 1, 1)] - Defines the tint of the flare/ghosts.
  204. * @param {Node<float> | number} [params.threshold=float(0.5)] - Controls the size and strength of the effect. A higher threshold results in smaller flares.
  205. * @param {Node<float> | number} [params.ghostSamples=float(4)] - Represents the number of flares/ghosts per bright spot which pivot around the center.
  206. * @param {Node<float> | number} [params.ghostSpacing=float(0.25)] - Defines the spacing of the flares/ghosts.
  207. * @param {Node<float> | number} [params.ghostAttenuationFactor=float(25)] - Defines the attenuation factor of flares/ghosts.
  208. * @param {number} [params.downSampleRatio=4] - Defines how downsampling since the effect is usually not rendered at full resolution.
  209. * @returns {LensflareNode}
  210. */
  211. export const lensflare = ( node, params ) => nodeObject( new LensflareNode( convertToTexture( node ), params ) );