Functions.glsl 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SHADERS_FUNCTIONS_GLSL
  6. #define ANKI_SHADERS_FUNCTIONS_GLSL
  7. vec3 dither(in vec3 col, in float C)
  8. {
  9. vec3 vDither = vec3(dot(vec2(171.0, 231.0), gl_FragCoord.xy));
  10. vDither.rgb = fract(vDither.rgb / vec3(103.0, 71.0, 97.0));
  11. col = col * (255.0 / C) + vDither.rgb;
  12. col = floor(col) / 255.0;
  13. col *= C;
  14. return col;
  15. }
  16. float dither(in float col, in float C)
  17. {
  18. float vDither = dot(vec2(171.0, 231.0), gl_FragCoord.xy);
  19. vDither = fract(vDither / 103.0);
  20. col = col * (255.0 / C) + vDither;
  21. col = floor(col) / 255.0;
  22. col *= C;
  23. return col;
  24. }
  25. // Convert to linear depth
  26. float linearizeDepth(in float depth, in float zNear, in float zFar)
  27. {
  28. return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
  29. }
  30. // This is the optimal linearizeDepth where a=(f+n)/2n and b=(n-f)/2n
  31. float linearizeDepthOptimal(in float depth, in float a, in float b)
  32. {
  33. return 1.0 / (a + depth * b);
  34. }
  35. #endif