SignedDistanceField.hx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package h3d.shader;
  2. class SignedDistanceField extends hxsl.Shader {
  3. static var SRC = {
  4. @:import h3d.shader.Base2d;
  5. // Mode of operation - single-channel or multi-channel.
  6. // 0123 = RGBA, evertyhing else is MSDF.
  7. @const var channel : Int = 0;
  8. /**
  9. Use automatic edge smoothing based on derivatives.
  10. **/
  11. @const var autoSmoothing : Bool = false;
  12. /**
  13. Variable used to determine the edge of the field. ( default : 0.5 )
  14. Can be used to provide cheaper Outline for Text compared to Filter usage.
  15. **/
  16. @param var alphaCutoff : Float = 0.5;
  17. /**
  18. Determines smoothing of the edge. Lower value is sharper.
  19. **/
  20. @param var smoothing : Float = 0.04166666666666666666666666666667; // 1/24
  21. function median(r : Float, g : Float, b : Float) : Float {
  22. return max(min(r, g), min(max(r, g), b));
  23. }
  24. function fragment() {
  25. var textureSample : Vec4 = textureColor;
  26. var distance : Float;
  27. distance = if (channel == 0) textureSample.r;
  28. else if (channel == 1) textureSample.g;
  29. else if (channel == 2) textureSample.b;
  30. else if (channel == 3) textureSample.a;
  31. else median(textureSample.r, textureSample.g, textureSample.b);
  32. var smoothVal = autoSmoothing ? abs(fwidth(distance) * 0.5) : smoothing;
  33. textureColor = vec4(1.0, 1.0, 1.0, smoothstep(alphaCutoff - smoothVal, alphaCutoff + smoothVal, distance));
  34. }
  35. }
  36. }