Class Sdf

java.lang.Object
lol.trq.helios.sdf.Sdf

public final class Sdf extends Object
Signed distance functions and anti-aliasing coverage, the same formulas the shaders in lol/trq/helios/shaders/lib/sdf.glsl use. Kept in Java so they can be tested off the GL thread, and so the harness can predict every rendered pixel. Screen y points down, so angles grow clockwise on screen.

Distances are negative inside a shape, zero on its edge and positive outside.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final float
    A full turn in radians.
  • Method Summary

    Modifier and Type
    Method
    Description
    static float
    arc(float px, float py, float radius, float thickness, float start, float sweep)
    Distance from a point to a ring segment with butt caps, centred on the origin.
    static float
    clampRadius(float radius, float halfWidth, float halfHeight)
    Clamps a corner radius so it fits the rectangle.
    static float
    coverage(float distance, float pixel)
    Coverage of a pixel by a shape: 1 well inside, 0 well outside, and a linear ramp across one pixel centred on the edge, so the feather is half a pixel either side.
    static float
    glow(float distance, float width)
    Inner glow: 1 at the edge, fading to 0 at width inside it.
    static float
    roundedRect(float px, float py, float halfWidth, float halfHeight, float radius)
    Distance from a point to a rounded rectangle centred on the origin.
    static float
    segment(float px, float py, float ax, float ay, float bx, float by)
    Distance from a point to a line segment.
    static float
    shadow(float distance, float softness)
    Drop shadow falloff: 1 well inside, 0 at softness outside the edge.
    static float
    smoothstep(float edge0, float edge1, float x)
    GLSL smoothstep.
    static float
    stroke(float distance, float width)
    Turns a filled shape into an outline: a band of the given width just inside its edge.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • roundedRect

      public static float roundedRect(float px, float py, float halfWidth, float halfHeight, float radius)
      Distance from a point to a rounded rectangle centred on the origin.
      Parameters:
      px - point x, relative to the centre
      py - point y, relative to the centre
      halfWidth - half the rectangle width
      halfHeight - half the rectangle height
      radius - the corner radius, clamped to [0, min(halfWidth, halfHeight)]
      Returns:
      the signed distance
    • coverage

      public static float coverage(float distance, float pixel)
      Coverage of a pixel by a shape: 1 well inside, 0 well outside, and a linear ramp across one pixel centred on the edge, so the feather is half a pixel either side.
      Parameters:
      distance - the signed distance at the pixel centre
      pixel - the length of one pixel in distance units (the gradient length of the distance across a pixel)
      Returns:
      the coverage in [0, 1]
    • clampRadius

      public static float clampRadius(float radius, float halfWidth, float halfHeight)
      Clamps a corner radius so it fits the rectangle.
      Parameters:
      radius - the requested radius
      halfWidth - half the rectangle width
      halfHeight - half the rectangle height
      Returns:
      the radius used
    • stroke

      public static float stroke(float distance, float width)
      Turns a filled shape into an outline: a band of the given width just inside its edge.
      Parameters:
      distance - the distance to the filled shape
      width - the outline width
      Returns:
      the distance to the outline
    • segment

      public static float segment(float px, float py, float ax, float ay, float bx, float by)
      Distance from a point to a line segment.
      Parameters:
      px - point x
      py - point y
      ax - segment start x
      ay - segment start y
      bx - segment end x
      by - segment end y
      Returns:
      the distance, never negative
    • arc

      public static float arc(float px, float py, float radius, float thickness, float start, float sweep)
      Distance from a point to a ring segment with butt caps, centred on the origin.
      Parameters:
      px - point x
      py - point y
      radius - the outer radius
      thickness - the ring thickness, inward from the outer radius
      start - the start angle in radians, 0 along +x
      sweep - the swept angle in radians, clockwise on screen; 2π or more is a full ring
      Returns:
      the signed distance
    • shadow

      public static float shadow(float distance, float softness)
      Drop shadow falloff: 1 well inside, 0 at softness outside the edge.
      Parameters:
      distance - the signed distance
      softness - half the width of the falloff band
      Returns:
      the shadow opacity in [0, 1]
    • glow

      public static float glow(float distance, float width)
      Inner glow: 1 at the edge, fading to 0 at width inside it.
      Parameters:
      distance - the signed distance
      width - how far the glow reaches inward
      Returns:
      the glow strength in [0, 1]
    • smoothstep

      public static float smoothstep(float edge0, float edge1, float x)
      GLSL smoothstep.
      Parameters:
      edge0 - lower edge
      edge1 - upper edge
      x - the value
      Returns:
      the Hermite interpolation in [0, 1]