ColorAdjustmentNode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import TempNode from '../core/TempNode.js';
  2. import { dot, mix } from '../math/MathNode.js';
  3. import { add } from '../math/OperatorNode.js';
  4. import { addNodeClass } from '../core/Node.js';
  5. import { addNodeElement, ShaderNode, nodeProxy, float, vec3, mat3 } from '../shadernode/ShaderNode.js';
  6. const saturationNode = new ShaderNode( ( { color, adjustment } ) => {
  7. return adjustment.mix( luminance( color ), color );
  8. } );
  9. const vibranceNode = new ShaderNode( ( { color, adjustment } ) => {
  10. const average = add( color.r, color.g, color.b ).div( 3.0 );
  11. const mx = color.r.max( color.g.max( color.b ) );
  12. const amt = mx.sub( average ).mul( adjustment ).mul( - 3.0 );
  13. return mix( color, mx, amt );
  14. } );
  15. const hueNode = new ShaderNode( ( { color, adjustment } ) => {
  16. const RGBtoYIQ = mat3( 0.299, 0.587, 0.114, 0.595716, - 0.274453, - 0.321263, 0.211456, - 0.522591, 0.311135 );
  17. const YIQtoRGB = mat3( 1.0, 0.9563, 0.6210, 1.0, - 0.2721, - 0.6474, 1.0, - 1.107, 1.7046 );
  18. const yiq = RGBtoYIQ.mul( color );
  19. const hue = yiq.z.atan2( yiq.y ).add( adjustment );
  20. const chroma = yiq.yz.length();
  21. return YIQtoRGB.mul( vec3( yiq.x, chroma.mul( hue.cos() ), chroma.mul( hue.sin() ) ) );
  22. } );
  23. class ColorAdjustmentNode extends TempNode {
  24. constructor( method, colorNode, adjustmentNode = float( 1 ) ) {
  25. super( 'vec3' );
  26. this.method = method;
  27. this.colorNode = colorNode;
  28. this.adjustmentNode = adjustmentNode;
  29. }
  30. construct() {
  31. const { method, colorNode, adjustmentNode } = this;
  32. const callParams = { color: colorNode, adjustment: adjustmentNode };
  33. let outputNode = null;
  34. if ( method === ColorAdjustmentNode.SATURATION ) {
  35. outputNode = saturationNode.call( callParams );
  36. } else if ( method === ColorAdjustmentNode.VIBRANCE ) {
  37. outputNode = vibranceNode.call( callParams );
  38. } else if ( method === ColorAdjustmentNode.HUE ) {
  39. outputNode = hueNode.call( callParams );
  40. } else {
  41. console.error( `${ this.type }: Method "${ this.method }" not supported!` );
  42. }
  43. return outputNode;
  44. }
  45. }
  46. ColorAdjustmentNode.SATURATION = 'saturation';
  47. ColorAdjustmentNode.VIBRANCE = 'vibrance';
  48. ColorAdjustmentNode.HUE = 'hue';
  49. export default ColorAdjustmentNode;
  50. export const saturation = nodeProxy( ColorAdjustmentNode, ColorAdjustmentNode.SATURATION );
  51. export const vibrance = nodeProxy( ColorAdjustmentNode, ColorAdjustmentNode.VIBRANCE );
  52. export const hue = nodeProxy( ColorAdjustmentNode, ColorAdjustmentNode.HUE );
  53. export const lumaCoeffs = vec3( 0.2125, 0.7154, 0.0721 );
  54. export const luminance = ( color, luma = lumaCoeffs ) => dot( color, luma );
  55. addNodeElement( 'saturation', saturation );
  56. addNodeElement( 'vibrance', vibrance );
  57. addNodeElement( 'hue', hue );
  58. addNodeClass( ColorAdjustmentNode );