LensflareNode.js 7.9 KB

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