material.frag 818 B

1234567891011121314151617181920212223242526272829303132
  1. #version 310 es
  2. // SPDX-License-Identifier: Unlicense OR MIT
  3. precision mediump float;
  4. layout(binding = 0) uniform sampler2D tex;
  5. layout(location = 0) in highp vec2 vUV;
  6. layout(location = 0) out vec4 fragColor;
  7. layout(push_constant) uniform Color {
  8. // If emulateSRGB is set (!= 0), the input texels are sRGB encoded. We save the
  9. // conversion step below, at the cost of texture filtering in sRGB space.
  10. layout(offset=16) float emulateSRGB;
  11. } _color;
  12. vec3 RGBtosRGB(vec3 rgb) {
  13. bvec3 cutoff = greaterThanEqual(rgb, vec3(0.0031308));
  14. vec3 below = vec3(12.92)*rgb;
  15. vec3 above = vec3(1.055)*pow(rgb, vec3(0.41666)) - vec3(0.055);
  16. return mix(below, above, cutoff);
  17. }
  18. void main() {
  19. vec4 texel = texture(tex, vUV);
  20. if (_color.emulateSRGB == 0.0) {
  21. texel.rgb = RGBtosRGB(texel.rgb);
  22. }
  23. fragColor = texel;
  24. }