BrightnessContrastShader.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Brightness and contrast adjustment
  3. * https://github.com/evanw/glfx.js
  4. * brightness: -1 to 1 (-1 is solid black, 0 is no change, and 1 is solid white)
  5. * contrast: -1 to 1 (-1 is solid gray, 0 is no change, and 1 is maximum contrast)
  6. */
  7. const BrightnessContrastShader = {
  8. uniforms: {
  9. 'tDiffuse': { value: null },
  10. 'brightness': { value: 0 },
  11. 'contrast': { value: 0 }
  12. },
  13. vertexShader: /* glsl */`
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  18. }`,
  19. fragmentShader: /* glsl */`
  20. uniform sampler2D tDiffuse;
  21. uniform float brightness;
  22. uniform float contrast;
  23. varying vec2 vUv;
  24. void main() {
  25. gl_FragColor = texture2D( tDiffuse, vUv );
  26. gl_FragColor.rgb += brightness;
  27. if (contrast > 0.0) {
  28. gl_FragColor.rgb = (gl_FragColor.rgb - 0.5) / (1.0 - contrast) + 0.5;
  29. } else {
  30. gl_FragColor.rgb = (gl_FragColor.rgb - 0.5) * (1.0 + contrast) + 0.5;
  31. }
  32. }`
  33. };
  34. export { BrightnessContrastShader };