BrightnessContrastShader.js 1.1 KB

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