RGBShiftNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { TempNode } from 'three/webgpu';
  2. import { nodeObject, Fn, uv, uniform, vec2, sin, cos, vec4, convertToTexture } from 'three/tsl';
  3. /**
  4. * Post processing node for shifting/splitting RGB color channels. The effect
  5. * separates color channels and offsets them from each other.
  6. *
  7. * @augments TempNode
  8. * @three_import import { rgbShift } from 'three/addons/tsl/display/RGBShiftNode.js';
  9. */
  10. class RGBShiftNode extends TempNode {
  11. static get type() {
  12. return 'RGBShiftNode';
  13. }
  14. /**
  15. * Constructs a new RGB shift node.
  16. *
  17. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  18. * @param {number} [amount=0.005] - The amount of the RGB shift.
  19. * @param {number} [angle=0] - Defines the orientation in which colors are shifted.
  20. */
  21. constructor( textureNode, amount = 0.005, angle = 0 ) {
  22. super( 'vec4' );
  23. /**
  24. * The texture node that represents the input of the effect.
  25. *
  26. * @type {TextureNode}
  27. */
  28. this.textureNode = textureNode;
  29. /**
  30. * The amount of the RGB shift.
  31. *
  32. * @type {UniformNode<float>}
  33. */
  34. this.amount = uniform( amount );
  35. /**
  36. * Defines in which direction colors are shifted.
  37. *
  38. * @type {UniformNode<float>}
  39. */
  40. this.angle = uniform( angle );
  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 { textureNode } = this;
  50. const uvNode = textureNode.uvNode || uv();
  51. const sampleTexture = ( uv ) => textureNode.sample( uv );
  52. const rgbShift = Fn( () => {
  53. const offset = vec2( cos( this.angle ), sin( this.angle ) ).mul( this.amount );
  54. const cr = sampleTexture( uvNode.add( offset ) );
  55. const cga = sampleTexture( uvNode );
  56. const cb = sampleTexture( uvNode.sub( offset ) );
  57. return vec4( cr.r, cga.g, cb.b, cga.a );
  58. } );
  59. return rgbShift();
  60. }
  61. }
  62. export default RGBShiftNode;
  63. /**
  64. * TSL function for creating a RGB shift or split effect for post processing.
  65. *
  66. * @tsl
  67. * @function
  68. * @param {Node<vec4>} node - The node that represents the input of the effect.
  69. * @param {number} [amount=0.005] - The amount of the RGB shift.
  70. * @param {number} [angle=0] - Defines in which direction colors are shifted.
  71. * @returns {RGBShiftNode}
  72. */
  73. export const rgbShift = ( node, amount, angle ) => nodeObject( new RGBShiftNode( convertToTexture( node ), amount, angle ) );