ColorAdjustmentNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { FunctionNode } from '../core/FunctionNode.js';
  6. import { LuminanceNode } from './LuminanceNode.js';
  7. function ColorAdjustmentNode( rgb, adjustment, method ) {
  8. TempNode.call( this, 'v3' );
  9. this.rgb = rgb;
  10. this.adjustment = adjustment;
  11. this.method = method || ColorAdjustmentNode.SATURATION;
  12. };
  13. ColorAdjustmentNode.Nodes = (function() {
  14. var hue = new FunctionNode( [
  15. "vec3 hue(vec3 rgb, float adjustment) {",
  16. " const mat3 RGBtoYIQ = mat3(0.299, 0.587, 0.114, 0.595716, -0.274453, -0.321263, 0.211456, -0.522591, 0.311135);",
  17. " const mat3 YIQtoRGB = mat3(1.0, 0.9563, 0.6210, 1.0, -0.2721, -0.6474, 1.0, -1.107, 1.7046);",
  18. " vec3 yiq = RGBtoYIQ * rgb;",
  19. " float hue = atan(yiq.z, yiq.y) + adjustment;",
  20. " float chroma = sqrt(yiq.z * yiq.z + yiq.y * yiq.y);",
  21. " return YIQtoRGB * vec3(yiq.x, chroma * cos(hue), chroma * sin(hue));",
  22. "}"
  23. ].join( "\n" ) );
  24. var saturation = new FunctionNode( [
  25. // Algorithm from Chapter 16 of OpenGL Shading Language
  26. "vec3 saturation(vec3 rgb, float adjustment) {",
  27. " vec3 intensity = vec3( luminance( rgb ) );",
  28. " return mix( intensity, rgb, adjustment );",
  29. "}"
  30. ].join( "\n" ), [ LuminanceNode.Nodes.luminance ] ); // include LuminanceNode function
  31. var vibrance = new FunctionNode( [
  32. // Shader by Evan Wallace adapted by @lo-th
  33. "vec3 vibrance(vec3 rgb, float adjustment) {",
  34. " float average = (rgb.r + rgb.g + rgb.b) / 3.0;",
  35. " float mx = max(rgb.r, max(rgb.g, rgb.b));",
  36. " float amt = (mx - average) * (-3.0 * adjustment);",
  37. " return mix(rgb.rgb, vec3(mx), amt);",
  38. "}"
  39. ].join( "\n" ) );
  40. return {
  41. hue: hue,
  42. saturation: saturation,
  43. vibrance: vibrance
  44. };
  45. })();
  46. ColorAdjustmentNode.SATURATION = 'saturation';
  47. ColorAdjustmentNode.HUE = 'hue';
  48. ColorAdjustmentNode.VIBRANCE = 'vibrance';
  49. ColorAdjustmentNode.BRIGHTNESS = 'brightness';
  50. ColorAdjustmentNode.CONTRAST = 'contrast';
  51. ColorAdjustmentNode.prototype = Object.create( TempNode.prototype );
  52. ColorAdjustmentNode.prototype.constructor = ColorAdjustmentNode;
  53. ColorAdjustmentNode.prototype.nodeType = "ColorAdjustment";
  54. ColorAdjustmentNode.prototype.generate = function ( builder, output ) {
  55. var rgb = this.rgb.build( builder, 'v3' ),
  56. adjustment = this.adjustment.build( builder, 'fv1' );
  57. switch ( this.method ) {
  58. case ColorAdjustmentNode.BRIGHTNESS:
  59. return builder.format( '( ' + rgb + ' + ' + adjustment + ' )', this.getType( builder ), output );
  60. break;
  61. case ColorAdjustmentNode.CONTRAST:
  62. return builder.format( '( ' + rgb + ' * ' + adjustment + ' )', this.getType( builder ), output );
  63. break;
  64. }
  65. var method = builder.include( ColorAdjustmentNode.Nodes[this.method] );
  66. return builder.format( method + '( ' + rgb + ', ' + adjustment + ' )', this.getType( builder ), output );
  67. };
  68. ColorAdjustmentNode.prototype.copy = function ( source ) {
  69. TempNode.prototype.copy.call( this, source );
  70. this.rgb = source.rgb;
  71. this.adjustment = source.adjustment;
  72. this.method = source.method;
  73. };
  74. ColorAdjustmentNode.prototype.toJSON = function ( meta ) {
  75. var data = this.getJSONNode( meta );
  76. if ( ! data ) {
  77. data = this.createJSONNode( meta );
  78. data.rgb = this.rgb.toJSON( meta ).uuid;
  79. data.adjustment = this.adjustment.toJSON( meta ).uuid;
  80. data.method = this.method;
  81. }
  82. return data;
  83. };
  84. export { ColorAdjustmentNode };