SDF2D.hlsl 611 B

12345678910111213141516171819202122232425262728
  1. #ifndef UNITY_SDF2D_INCLUDED
  2. #define UNITY_SDF2D_INCLUDED
  3. // Ref: https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
  4. float CircleSDF(float2 position, float radius)
  5. {
  6. return length(position) - radius;
  7. }
  8. float RectangleSDF(float2 position, float2 bound)
  9. {
  10. float2 d = abs(position) - bound;
  11. return length(max(d, float2(0, 0))) + min(max(d.x, d.y), 0.0);
  12. }
  13. float EllipseSDF(float2 position, float2 r)
  14. {
  15. float2 p = position;
  16. float2 r2 = r*r;
  17. float k0 = length(p/r);
  18. float k1 = length(p/r2);
  19. return k0*(k0 - 1.0)/k1;
  20. }
  21. #endif // UNITY_SDF2D_INCLUDED