HueSaturationShader.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/#manual/en/introduction/Installation." );
  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. THREE.HueSaturationShader = {
  9. uniforms: {
  10. "tDiffuse": { value: null },
  11. "hue": { value: 0 },
  12. "saturation": { value: 0 }
  13. },
  14. vertexShader: [
  15. "varying vec2 vUv;",
  16. "void main() {",
  17. " vUv = uv;",
  18. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  19. "}"
  20. ].join( "\n" ),
  21. fragmentShader: [
  22. "uniform sampler2D tDiffuse;",
  23. "uniform float hue;",
  24. "uniform float saturation;",
  25. "varying vec2 vUv;",
  26. "void main() {",
  27. " gl_FragColor = texture2D( tDiffuse, vUv );",
  28. // hue
  29. " float angle = hue * 3.14159265;",
  30. " float s = sin(angle), c = cos(angle);",
  31. " vec3 weights = (vec3(2.0 * c, -sqrt(3.0) * s - c, sqrt(3.0) * s - c) + 1.0) / 3.0;",
  32. " float len = length(gl_FragColor.rgb);",
  33. " gl_FragColor.rgb = vec3(",
  34. " dot(gl_FragColor.rgb, weights.xyz),",
  35. " dot(gl_FragColor.rgb, weights.zxy),",
  36. " dot(gl_FragColor.rgb, weights.yzx)",
  37. " );",
  38. // saturation
  39. " float average = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0;",
  40. " if (saturation > 0.0) {",
  41. " gl_FragColor.rgb += (average - gl_FragColor.rgb) * (1.0 - 1.0 / (1.001 - saturation));",
  42. " } else {",
  43. " gl_FragColor.rgb += (average - gl_FragColor.rgb) * (-saturation);",
  44. " }",
  45. "}"
  46. ].join( "\n" )
  47. };