antialias.glsl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Procedural anti-aliasing by blurring two colors that meet at a sharp edge.
  3. *
  4. * @name czm_antialias
  5. * @glslFunction
  6. *
  7. * @param {vec4} color1 The color on one side of the edge.
  8. * @param {vec4} color2 The color on the other side of the edge.
  9. * @param {vec4} currentcolor The current color, either <code>color1</code> or <code>color2</code>.
  10. * @param {float} dist The distance to the edge in texture coordinates.
  11. * @param {float} [fuzzFactor=0.1] Controls the blurriness between the two colors.
  12. * @returns {vec4} The anti-aliased color.
  13. *
  14. * @example
  15. * // GLSL declarations
  16. * vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist, float fuzzFactor);
  17. * vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist);
  18. *
  19. * // get the color for a material that has a sharp edge at the line y = 0.5 in texture space
  20. * float dist = abs(textureCoordinates.t - 0.5);
  21. * vec4 currentColor = mix(bottomColor, topColor, step(0.5, textureCoordinates.t));
  22. * vec4 color = czm_antialias(bottomColor, topColor, currentColor, dist, 0.1);
  23. */
  24. vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist, float fuzzFactor)
  25. {
  26. float val1 = clamp(dist / fuzzFactor, 0.0, 1.0);
  27. float val2 = clamp((dist - 0.5) / fuzzFactor, 0.0, 1.0);
  28. val1 = val1 * (1.0 - val2);
  29. val1 = val1 * val1 * (3.0 - (2.0 * val1));
  30. val1 = pow(val1, 0.5); //makes the transition nicer
  31. vec4 midColor = (color1 + color2) * 0.5;
  32. return mix(midColor, currentColor, val1);
  33. }
  34. vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist)
  35. {
  36. return czm_antialias(color1, color2, currentColor, dist, 0.1);
  37. }