RGBShiftNode.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import TempNode from '../core/TempNode.js';
  2. import { nodeObject, addNodeElement, tslFn, vec2, vec4 } from '../shadernode/ShaderNode.js';
  3. import { uniform } from '../core/UniformNode.js';
  4. import { NodeUpdateType } from '../core/constants.js';
  5. import { uv } from '../accessors/UVNode.js';
  6. import { sin, cos } from '../math/MathNode.js';
  7. class RGBShiftNode extends TempNode {
  8. constructor( textureNode, amount = 0.005, angle = 0 ) {
  9. super( 'vec4' );
  10. this.textureNode = textureNode;
  11. this.amount = uniform( amount );
  12. this.angle = uniform( angle );
  13. this.updateBeforeType = NodeUpdateType.RENDER;
  14. }
  15. updateBefore() {
  16. }
  17. setup() {
  18. const { textureNode } = this;
  19. const uvNode = textureNode.uvNode || uv();
  20. const sampleTexture = ( uv ) => this.textureNode.uv( uv );
  21. const rgbShift = tslFn( () => {
  22. const offset = vec2( cos( this.angle ), sin( this.angle ) ).mul( this.amount );
  23. const cr = sampleTexture( uvNode.add( offset ) );
  24. const cga = sampleTexture( uvNode );
  25. const cb = sampleTexture( uvNode.sub( offset ) );
  26. return vec4( cr.r, cga.g, cb.b, cga.a );
  27. } );
  28. return rgbShift();
  29. }
  30. }
  31. export const rgbShift = ( node, amount, angle ) => nodeObject( new RGBShiftNode( nodeObject( node ), amount, angle ) );
  32. addNodeElement( 'rgbShift', rgbShift );
  33. export default RGBShiftNode;