RGBShiftNode.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. */
  9. class RGBShiftNode extends TempNode {
  10. static get type() {
  11. return 'RGBShiftNode';
  12. }
  13. /**
  14. * Constructs a new RGB shift node.
  15. *
  16. * @param {TextureNode} textureNode - The texture node that represents the input of the effect.
  17. * @param {number} [amount=0.005] - The amount of the RGB shift.
  18. * @param {number} [angle=0] - Defines the orientation in which colors are shifted.
  19. */
  20. constructor( textureNode, amount = 0.005, angle = 0 ) {
  21. super( 'vec4' );
  22. /**
  23. * The texture node that represents the input of the effect.
  24. *
  25. * @type {TextureNode}
  26. */
  27. this.textureNode = textureNode;
  28. /**
  29. * The amount of the RGB shift.
  30. *
  31. * @type {UniformNode<float>}
  32. */
  33. this.amount = uniform( amount );
  34. /**
  35. * Defines in which direction colors are shifted.
  36. *
  37. * @type {UniformNode<float>}
  38. */
  39. this.angle = uniform( angle );
  40. }
  41. /**
  42. * This method is used to setup the effect's TSL code.
  43. *
  44. * @param {NodeBuilder} builder - The current node builder.
  45. * @return {ShaderCallNodeInternal}
  46. */
  47. setup( /* builder */ ) {
  48. const { textureNode } = this;
  49. const uvNode = textureNode.uvNode || uv();
  50. const sampleTexture = ( uv ) => textureNode.sample( uv );
  51. const rgbShift = Fn( () => {
  52. const offset = vec2( cos( this.angle ), sin( this.angle ) ).mul( this.amount );
  53. const cr = sampleTexture( uvNode.add( offset ) );
  54. const cga = sampleTexture( uvNode );
  55. const cb = sampleTexture( uvNode.sub( offset ) );
  56. return vec4( cr.r, cga.g, cb.b, cga.a );
  57. } );
  58. return rgbShift();
  59. }
  60. }
  61. export default RGBShiftNode;
  62. /**
  63. * TSL function for creating a RGB shift or split effect for post processing.
  64. *
  65. * @tsl
  66. * @function
  67. * @param {Node<vec4>} node - The node that represents the input of the effect.
  68. * @param {number} [amount=0.005] - The amount of the RGB shift.
  69. * @param {number} [angle=0] - Defines in which direction colors are shifted.
  70. * @returns {RGBShiftNode}
  71. */
  72. export const rgbShift = ( node, amount, angle ) => nodeObject( new RGBShiftNode( convertToTexture( node ), amount, angle ) );
粤ICP备19079148号