AnamorphicNode.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 {number}
  59. */
  60. this.resolutionScale = 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.resolutionScale ), 1 );
  109. height = Math.max( Math.round( height * this.resolutionScale ), 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. _quadMesh.name = 'Anamorphic';
  127. this.setSize( map.image.width, map.image.height );
  128. // render
  129. renderer.setRenderTarget( this._renderTarget );
  130. _quadMesh.render( renderer );
  131. // restore
  132. textureNode.value = currentTexture;
  133. RendererUtils.restoreRendererState( renderer, _rendererState );
  134. }
  135. /**
  136. * This method is used to setup the effect's TSL code.
  137. *
  138. * @param {NodeBuilder} builder - The current node builder.
  139. * @return {PassTextureNode}
  140. */
  141. setup( builder ) {
  142. const textureNode = this.textureNode;
  143. const uvNode = textureNode.uvNode || uv();
  144. const sampleTexture = ( uv ) => textureNode.sample( uv );
  145. const threshold = ( color, threshold ) => mix( vec3( 0.0 ), color, luminance( color ).sub( threshold ).max( 0 ) );
  146. const anamorph = Fn( () => {
  147. const samples = this.samples;
  148. const halfSamples = Math.floor( samples / 2 );
  149. const total = vec3( 0 ).toVar();
  150. Loop( { start: - halfSamples, end: halfSamples }, ( { i } ) => {
  151. const softness = float( i ).abs().div( halfSamples ).oneMinus();
  152. const uv = vec2( uvNode.x.add( this._invSize.x.mul( i ).mul( this.scaleNode ) ), uvNode.y );
  153. const color = sampleTexture( uv );
  154. const pass = threshold( color, this.thresholdNode ).mul( softness );
  155. total.addAssign( pass );
  156. } );
  157. return total.mul( this.colorNode );
  158. } );
  159. //
  160. const material = this._material || ( this._material = new NodeMaterial() );
  161. material.name = 'Anamorphic';
  162. material.fragmentNode = anamorph();
  163. //
  164. const properties = builder.getNodeProperties( this );
  165. properties.textureNode = textureNode;
  166. //
  167. return this._textureNode;
  168. }
  169. /**
  170. * Frees internal resources. This method should be called
  171. * when the effect is no longer required.
  172. */
  173. dispose() {
  174. this._renderTarget.dispose();
  175. }
  176. /**
  177. * The resolution scale.
  178. *
  179. * @deprecated
  180. * @type {Vector2}
  181. * @default {(1,1)}
  182. */
  183. get resolution() {
  184. console.warn( 'THREE.AnamorphicNode: The "resolution" property has been renamed to "resolutionScale" and is now of type `number`.' ); // @deprecated r180
  185. return new Vector2( this.resolutionScale, this.resolutionScale );
  186. }
  187. set resolution( value ) {
  188. console.warn( 'THREE.AnamorphicNode: The "resolution" property has been renamed to "resolutionScale" and is now of type `number`.' ); // @deprecated r180
  189. this.resolutionScale = value.x;
  190. }
  191. }
  192. /**
  193. * TSL function for creating an anamorphic flare effect.
  194. *
  195. * @tsl
  196. * @function
  197. * @param {TextureNode} node - The node that represents the input of the effect.
  198. * @param {Node<float> | number} [threshold=0.9] - The threshold is one option to control the intensity and size of the effect.
  199. * @param {Node<float> | number} [scale=3] - Defines the vertical scale of the flares.
  200. * @param {number} [samples=32] - More samples result in larger flares and a more expensive runtime behavior.
  201. * @returns {AnamorphicNode}
  202. */
  203. export const anamorphic = ( node, threshold = .9, scale = 3, samples = 32 ) => nodeObject( new AnamorphicNode( convertToTexture( node ), nodeObject( threshold ), nodeObject( scale ), samples ) );
  204. export default AnamorphicNode;
粤ICP备19079148号