FilmNode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { TempNode } from 'three/webgpu';
  2. import { rand, Fn, fract, time, uv, clamp, mix, vec4, nodeProxy } from 'three/tsl';
  3. /**
  4. * Post processing node for creating a film grain effect.
  5. *
  6. * @augments TempNode
  7. */
  8. class FilmNode extends TempNode {
  9. static get type() {
  10. return 'FilmNode';
  11. }
  12. /**
  13. * Constructs a new film node.
  14. *
  15. * @param {Node} inputNode - The node that represents the input of the effect.
  16. * @param {?Node<float>} [intensityNode=null] - A node that represents the effect's intensity.
  17. * @param {?Node<vec2>} [uvNode=null] - A node that allows to pass custom (e.g. animated) uv data.
  18. */
  19. constructor( inputNode, intensityNode = null, uvNode = null ) {
  20. super( 'vec4' );
  21. /**
  22. * The node that represents the input of the effect.
  23. *
  24. * @type {Node}
  25. */
  26. this.inputNode = inputNode;
  27. /**
  28. * A node that represents the effect's intensity.
  29. *
  30. * @type {?Node<float>}
  31. * @default null
  32. */
  33. this.intensityNode = intensityNode;
  34. /**
  35. * A node that allows to pass custom (e.g. animated) uv data.
  36. *
  37. * @type {?Node<vec2>}
  38. * @default null
  39. */
  40. this.uvNode = uvNode;
  41. }
  42. /**
  43. * This method is used to setup the effect's TSL code.
  44. *
  45. * @param {NodeBuilder} builder - The current node builder.
  46. * @return {ShaderCallNodeInternal}
  47. */
  48. setup( /* builder */ ) {
  49. const uvNode = this.uvNode || uv();
  50. const film = Fn( () => {
  51. const base = this.inputNode.rgb;
  52. const noise = rand( fract( uvNode.add( time ) ) );
  53. let color = base.add( base.mul( clamp( noise.add( 0.1 ), 0, 1 ) ) );
  54. if ( this.intensityNode !== null ) {
  55. color = mix( base, color, this.intensityNode );
  56. }
  57. return vec4( color, this.inputNode.a );
  58. } );
  59. const outputNode = film();
  60. return outputNode;
  61. }
  62. }
  63. export default FilmNode;
  64. /**
  65. * TSL function for creating a film node for post processing.
  66. *
  67. * @tsl
  68. * @function
  69. * @param {Node<vec4>} inputNode - The node that represents the input of the effect.
  70. * @param {?Node<float>} [intensityNode=null] - A node that represents the effect's intensity.
  71. * @param {?Node<vec2>} [uvNode=null] - A node that allows to pass custom (e.g. animated) uv data.
  72. * @returns {FilmNode}
  73. */
  74. export const film = /*@__PURE__*/ nodeProxy( FilmNode );
粤ICP备19079148号