HueSaturationShader.js 1.5 KB

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