AnamorphicNode.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 material for the anamorphic pass.
  85. *
  86. * @private
  87. * @type {?NodeMaterial}
  88. */
  89. this._material = null;
  90. /**
  91. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  92. * its effect once per frame in `updateBefore()`.
  93. *
  94. * @type {string}
  95. * @default 'frame'
  96. */
  97. this.updateBeforeType = NodeUpdateType.FRAME;
  98. }
  99. /**
  100. * Returns the result of the effect as a texture node.
  101. *
  102. * @return {PassTextureNode} A texture node that represents the result of the effect.
  103. */
  104. getTextureNode() {
  105. return this._textureNode;
  106. }
  107. /**
  108. * Sets the size of the effect.
  109. *
  110. * @param {number} width - The width of the effect.
  111. * @param {number} height - The height of the effect.
  112. */
  113. setSize( width, height ) {
  114. this._invSize.value.set( 1 / width, 1 / height );
  115. width = Math.max( Math.round( width * this.resolutionScale ), 1 );
  116. height = Math.max( Math.round( height * this.resolutionScale ), 1 );
  117. this._renderTarget.setSize( width, height );
  118. }
  119. /**
  120. * This method is used to render the effect once per frame.
  121. *
  122. * @param {NodeFrame} frame - The current node frame.
  123. */
  124. updateBefore( frame ) {
  125. const { renderer } = frame;
  126. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  127. //
  128. const textureNode = this.textureNode;
  129. const map = textureNode.value;
  130. this._renderTarget.texture.type = map.type;
  131. const currentTexture = textureNode.value;
  132. _quadMesh.material = this._material;
  133. _quadMesh.name = 'Anamorphic';
  134. this.setSize( map.image.width, map.image.height );
  135. // render
  136. renderer.setRenderTarget( this._renderTarget );
  137. _quadMesh.render( renderer );
  138. // restore
  139. textureNode.value = currentTexture;
  140. RendererUtils.restoreRendererState( renderer, _rendererState );
  141. }
  142. /**
  143. * This method is used to setup the effect's TSL code.
  144. *
  145. * @param {NodeBuilder} builder - The current node builder.
  146. * @return {PassTextureNode}
  147. */
  148. setup( builder ) {
  149. const textureNode = this.textureNode;
  150. const uvNode = textureNode.uvNode || uv();
  151. const sampleTexture = ( uv ) => textureNode.sample( uv );
  152. const threshold = ( color, threshold ) => mix( vec3( 0.0 ), color, luminance( color ).sub( threshold ).max( 0 ) );
  153. const anamorph = Fn( () => {
  154. const samples = this.samples;
  155. const halfSamples = Math.floor( samples / 2 );
  156. const total = vec3( 0 ).toVar();
  157. Loop( { start: - halfSamples, end: halfSamples }, ( { i } ) => {
  158. const softness = float( i ).abs().div( halfSamples ).oneMinus();
  159. const uv = vec2( uvNode.x.add( this._invSize.x.mul( i ).mul( this.scaleNode ) ), uvNode.y );
  160. const color = sampleTexture( uv );
  161. const pass = threshold( color, this.thresholdNode ).mul( softness );
  162. total.addAssign( pass );
  163. } );
  164. return total.mul( this.colorNode );
  165. } );
  166. //
  167. const material = this._material || ( this._material = new NodeMaterial() );
  168. material.name = 'Anamorphic';
  169. material.fragmentNode = anamorph();
  170. //
  171. const properties = builder.getNodeProperties( this );
  172. properties.textureNode = textureNode;
  173. //
  174. return this._textureNode;
  175. }
  176. /**
  177. * Frees internal resources. This method should be called
  178. * when the effect is no longer required.
  179. */
  180. dispose() {
  181. this._renderTarget.dispose();
  182. if ( this._material !== null ) this._material.dispose();
  183. }
  184. /**
  185. * The resolution scale.
  186. *
  187. * @deprecated
  188. * @type {Vector2}
  189. * @default {(1,1)}
  190. */
  191. get resolution() {
  192. console.warn( 'THREE.AnamorphicNode: The "resolution" property has been renamed to "resolutionScale" and is now of type `number`.' ); // @deprecated r180
  193. return new Vector2( this.resolutionScale, this.resolutionScale );
  194. }
  195. set resolution( value ) {
  196. console.warn( 'THREE.AnamorphicNode: The "resolution" property has been renamed to "resolutionScale" and is now of type `number`.' ); // @deprecated r180
  197. this.resolutionScale = value.x;
  198. }
  199. }
  200. /**
  201. * TSL function for creating an anamorphic flare effect.
  202. *
  203. * @tsl
  204. * @function
  205. * @param {TextureNode} node - The node that represents the input of the effect.
  206. * @param {Node<float> | number} [threshold=0.9] - The threshold is one option to control the intensity and size of the effect.
  207. * @param {Node<float> | number} [scale=3] - Defines the vertical scale of the flares.
  208. * @param {number} [samples=32] - More samples result in larger flares and a more expensive runtime behavior.
  209. * @returns {AnamorphicNode}
  210. */
  211. export const anamorphic = ( node, threshold = .9, scale = 3, samples = 32 ) => new AnamorphicNode( convertToTexture( node ), nodeObject( threshold ), nodeObject( scale ), samples );
  212. export default AnamorphicNode;
粤ICP备19079148号