HueSaturationShader.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Hue and saturation adjustment
  3. * https://github.com/evanw/glfx.js
  4. * hue: -1 to 1 (-1 is 180 degrees in the negative direction, 0 is no change, etc.
  5. * saturation: -1 to 1 (-1 is solid gray, 0 is no change, and 1 is maximum contrast)
  6. */
  7. const HueSaturationShader = {
  8. uniforms: {
  9. 'tDiffuse': { value: null },
  10. 'hue': { value: 0 },
  11. 'saturation': { value: 0 }
  12. },
  13. vertexShader: /* glsl */`
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  18. }`,
  19. fragmentShader: /* glsl */`
  20. uniform sampler2D tDiffuse;
  21. uniform float hue;
  22. uniform float saturation;
  23. varying vec2 vUv;
  24. void main() {
  25. gl_FragColor = texture2D( tDiffuse, vUv );
  26. // hue
  27. float angle = hue * 3.14159265;
  28. float s = sin(angle), c = cos(angle);
  29. vec3 weights = (vec3(2.0 * c, -sqrt(3.0) * s - c, sqrt(3.0) * s - c) + 1.0) / 3.0;
  30. float len = length(gl_FragColor.rgb);
  31. gl_FragColor.rgb = vec3(
  32. dot(gl_FragColor.rgb, weights.xyz),
  33. dot(gl_FragColor.rgb, weights.zxy),
  34. dot(gl_FragColor.rgb, weights.yzx)
  35. );
  36. // saturation
  37. float average = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0;
  38. if (saturation > 0.0) {
  39. gl_FragColor.rgb += (average - gl_FragColor.rgb) * (1.0 - 1.0 / (1.001 - saturation));
  40. } else {
  41. gl_FragColor.rgb += (average - gl_FragColor.rgb) * (-saturation);
  42. }
  43. }`
  44. };
  45. export { HueSaturationShader };