HueSaturationShader.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. console.warn( "THREE.HueSaturationShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author tapio / http://tapio.github.com/
  4. *
  5. * Hue and saturation adjustment
  6. * https://github.com/evanw/glfx.js
  7. * hue: -1 to 1 (-1 is 180 degrees in the negative direction, 0 is no change, etc.
  8. * saturation: -1 to 1 (-1 is solid gray, 0 is no change, and 1 is maximum contrast)
  9. */
  10. THREE.HueSaturationShader = {
  11. uniforms: {
  12. "tDiffuse": { value: null },
  13. "hue": { value: 0 },
  14. "saturation": { value: 0 }
  15. },
  16. vertexShader: [
  17. "varying vec2 vUv;",
  18. "void main() {",
  19. " vUv = uv;",
  20. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  21. "}"
  22. ].join( "\n" ),
  23. fragmentShader: [
  24. "uniform sampler2D tDiffuse;",
  25. "uniform float hue;",
  26. "uniform float saturation;",
  27. "varying vec2 vUv;",
  28. "void main() {",
  29. " gl_FragColor = texture2D( tDiffuse, vUv );",
  30. // hue
  31. " float angle = hue * 3.14159265;",
  32. " float s = sin(angle), c = cos(angle);",
  33. " vec3 weights = (vec3(2.0 * c, -sqrt(3.0) * s - c, sqrt(3.0) * s - c) + 1.0) / 3.0;",
  34. " float len = length(gl_FragColor.rgb);",
  35. " gl_FragColor.rgb = vec3(",
  36. " dot(gl_FragColor.rgb, weights.xyz),",
  37. " dot(gl_FragColor.rgb, weights.zxy),",
  38. " dot(gl_FragColor.rgb, weights.yzx)",
  39. " );",
  40. // saturation
  41. " float average = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0;",
  42. " if (saturation > 0.0) {",
  43. " gl_FragColor.rgb += (average - gl_FragColor.rgb) * (1.0 - 1.0 / (1.001 - saturation));",
  44. " } else {",
  45. " gl_FragColor.rgb += (average - gl_FragColor.rgb) * (-saturation);",
  46. " }",
  47. "}"
  48. ].join( "\n" )
  49. };