HueSaturationShader.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /** @module HueSaturationShader */
  2. /**
  3. * Hue and saturation adjustment, {@link https://github.com/evanw/glfx.js}.
  4. *
  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. * @constant
  9. * @type {Object}
  10. */
  11. const HueSaturationShader = {
  12. name: 'HueSaturationShader',
  13. uniforms: {
  14. 'tDiffuse': { value: null },
  15. 'hue': { value: 0 },
  16. 'saturation': { value: 0 }
  17. },
  18. vertexShader: /* glsl */`
  19. varying vec2 vUv;
  20. void main() {
  21. vUv = uv;
  22. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  23. }`,
  24. fragmentShader: /* glsl */`
  25. uniform sampler2D tDiffuse;
  26. uniform float hue;
  27. uniform float saturation;
  28. varying vec2 vUv;
  29. void main() {
  30. gl_FragColor = texture2D( tDiffuse, vUv );
  31. // hue
  32. float angle = hue * 3.14159265;
  33. float s = sin(angle), c = cos(angle);
  34. vec3 weights = (vec3(2.0 * c, -sqrt(3.0) * s - c, sqrt(3.0) * s - c) + 1.0) / 3.0;
  35. float len = length(gl_FragColor.rgb);
  36. gl_FragColor.rgb = vec3(
  37. dot(gl_FragColor.rgb, weights.xyz),
  38. dot(gl_FragColor.rgb, weights.zxy),
  39. dot(gl_FragColor.rgb, weights.yzx)
  40. );
  41. // saturation
  42. float average = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0;
  43. if (saturation > 0.0) {
  44. gl_FragColor.rgb += (average - gl_FragColor.rgb) * (1.0 - 1.0 / (1.001 - saturation));
  45. } else {
  46. gl_FragColor.rgb += (average - gl_FragColor.rgb) * (-saturation);
  47. }
  48. }`
  49. };
  50. export { HueSaturationShader };
粤ICP备19079148号