AnamorphicNode.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { RenderTarget, Vector2, TempNode, QuadMesh, NodeMaterial, RendererUtils } from 'three/webgpu';
  2. import { nodeObject, Fn, float, NodeUpdateType, uv, passTexture, uniform, convertToTexture, vec2, vec3, Loop, mix, luminance } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. let _rendererState;
  5. /**
  6. * Post processing node for adding an anamorphic flare effect.
  7. *
  8. * @augments TempNode
  9. * @three_import import { anamorphic } from 'three/addons/tsl/display/AnamorphicNode.js';
  10. */
  11. class AnamorphicNode extends TempNode {
  12. static get type() {
  13. return 'AnamorphicNode';
  14. }
  15. /**
  16. * Constructs a new anamorphic node.
  17. *
  18. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  19. * @param {Node<float>} thresholdNode - The threshold is one option to control the intensity and size of the effect.
  20. * @param {Node<float>} scaleNode - Defines the vertical scale of the flares.
  21. * @param {number} samples - More samples result in larger flares and a more expensive runtime behavior.
  22. */
  23. constructor( textureNode, thresholdNode, scaleNode, samples ) {
  24. super( 'vec4' );
  25. /**
  26. * The texture node that represents the input of the effect.
  27. *
  28. * @type {TextureNode}
  29. */
  30. this.textureNode = textureNode;
  31. /**
  32. * The threshold is one option to control the intensity and size of the effect.
  33. *
  34. * @type {Node<float>}
  35. */
  36. this.thresholdNode = thresholdNode;
  37. /**
  38. * Defines the vertical scale of the flares.
  39. *
  40. * @type {Node<float>}
  41. */
  42. this.scaleNode = scaleNode;
  43. /**
  44. * The color of the flares.
  45. *
  46. * @type {Node<vec3>}
  47. */
  48. this.colorNode = vec3( 0.1, 0.0, 1.0 );
  49. /**
  50. * More samples result in larger flares and a more expensive runtime behavior.
  51. *
  52. * @type {Node<float>}
  53. */
  54. this.samples = samples;
  55. /**
  56. * The resolution scale.
  57. *
  58. * @type {Vector2}
  59. */
  60. this.resolution = new Vector2( 1, 1 );
  61. /**
  62. * The internal render target of the effect.
  63. *
  64. * @private
  65. * @type {RenderTarget}
  66. */
  67. this._renderTarget = new RenderTarget( 1, 1, { depthBuffer: false } );
  68. this._renderTarget.texture.name = 'anamorphic';
  69. /**
  70. * A uniform node holding the inverse resolution value.
  71. *
  72. * @private
  73. * @type {UniformNode<vec2>}
  74. */
  75. this._invSize = uniform( new Vector2() );
  76. /**
  77. * The result of the effect is represented as a separate texture node.
  78. *
  79. * @private
  80. * @type {PassTextureNode}
  81. */
  82. this._textureNode = passTexture( this, this._renderTarget.texture );
  83. /**
  84. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  85. * its effect once per frame in `updateBefore()`.
  86. *
  87. * @type {string}
  88. * @default 'frame'
  89. */
  90. this.updateBeforeType = NodeUpdateType.FRAME;
  91. }
  92. /**
  93. * Returns the result of the effect as a texture node.
  94. *
  95. * @return {PassTextureNode} A texture node that represents the result of the effect.
  96. */
  97. getTextureNode() {
  98. return this._textureNode;
  99. }
  100. /**
  101. * Sets the size of the effect.
  102. *
  103. * @param {number} width - The width of the effect.
  104. * @param {number} height - The height of the effect.
  105. */
  106. setSize( width, height ) {
  107. this._invSize.value.set( 1 / width, 1 / height );
  108. width = Math.max( Math.round( width * this.resolution.x ), 1 );
  109. height = Math.max( Math.round( height * this.resolution.y ), 1 );
  110. this._renderTarget.setSize( width, height );
  111. }
  112. /**
  113. * This method is used to render the effect once per frame.
  114. *
  115. * @param {NodeFrame} frame - The current node frame.
  116. */
  117. updateBefore( frame ) {
  118. const { renderer } = frame;
  119. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  120. //
  121. const textureNode = this.textureNode;
  122. const map = textureNode.value;
  123. this._renderTarget.texture.type = map.type;
  124. const currentTexture = textureNode.value;
  125. _quadMesh.material = this._material;
  126. this.setSize( map.image.width, map.image.height );
  127. // render
  128. renderer.setRenderTarget( this._renderTarget );
  129. _quadMesh.render( renderer );
  130. // restore
  131. textureNode.value = currentTexture;
  132. RendererUtils.restoreRendererState( renderer, _rendererState );
  133. }
  134. /**
  135. * This method is used to setup the effect's TSL code.
  136. *
  137. * @param {NodeBuilder} builder - The current node builder.
  138. * @return {PassTextureNode}
  139. */
  140. setup( builder ) {
  141. const textureNode = this.textureNode;
  142. const uvNode = textureNode.uvNode || uv();
  143. const sampleTexture = ( uv ) => textureNode.sample( uv );
  144. const threshold = ( color, threshold ) => mix( vec3( 0.0 ), color, luminance( color ).sub( threshold ).max( 0 ) );
  145. const anamorph = Fn( () => {
  146. const samples = this.samples;
  147. const halfSamples = Math.floor( samples / 2 );
  148. const total = vec3( 0 ).toVar();
  149. Loop( { start: - halfSamples, end: halfSamples }, ( { i } ) => {
  150. const softness = float( i ).abs().div( halfSamples ).oneMinus();
  151. const uv = vec2( uvNode.x.add( this._invSize.x.mul( i ).mul( this.scaleNode ) ), uvNode.y );
  152. const color = sampleTexture( uv );
  153. const pass = threshold( color, this.thresholdNode ).mul( softness );
  154. total.addAssign( pass );
  155. } );
  156. return total.mul( this.colorNode );
  157. } );
  158. //
  159. const material = this._material || ( this._material = new NodeMaterial() );
  160. material.name = 'Anamorphic';
  161. material.fragmentNode = anamorph();
  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._renderTarget.dispose();
  174. }
  175. }
  176. /**
  177. * TSL function for creating an anamorphic flare effect.
  178. *
  179. * @tsl
  180. * @function
  181. * @param {TextureNode} node - The node that represents the input of the effect.
  182. * @param {Node<float> | number} [threshold=0.9] - The threshold is one option to control the intensity and size of the effect.
  183. * @param {Node<float> | number} [scale=3] - Defines the vertical scale of the flares.
  184. * @param {number} [samples=32] - More samples result in larger flares and a more expensive runtime behavior.
  185. * @returns {AnamorphicNode}
  186. */
  187. export const anamorphic = ( node, threshold = .9, scale = 3, samples = 32 ) => nodeObject( new AnamorphicNode( convertToTexture( node ), nodeObject( threshold ), nodeObject( scale ), samples ) );
  188. export default AnamorphicNode;