HueSaturationShader.js 1.5 KB

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