AnamorphicNode.js 6.2 KB

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