Class Sdf
java.lang.Object
lol.trq.helios.sdf.Sdf
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic floatarc(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 floatclampRadius(float radius, float halfWidth, float halfHeight) Clamps a corner radius so it fits the rectangle.static floatcoverage(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 floatglow(float distance, float width) Inner glow: 1 at the edge, fading to 0 atwidthinside it.static floatroundedRect(float px, float py, float halfWidth, float halfHeight, float radius) Distance from a point to a rounded rectangle centred on the origin.static floatsegment(float px, float py, float ax, float ay, float bx, float by) Distance from a point to a line segment.static floatshadow(float distance, float softness) Drop shadow falloff: 1 well inside, 0 atsoftnessoutside the edge.static floatsmoothstep(float edge0, float edge1, float x) GLSLsmoothstep.static floatstroke(float distance, float width) Turns a filled shape into an outline: a band of the given width just inside its edge.
-
Field Details
-
TAU
public static final float TAUA full turn in radians.- See Also:
-
-
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 centrepy- point y, relative to the centrehalfWidth- half the rectangle widthhalfHeight- half the rectangle heightradius- 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 centrepixel- 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 radiushalfWidth- half the rectangle widthhalfHeight- 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 shapewidth- 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 xpy- point yax- segment start xay- segment start ybx- segment end xby- 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 xpy- point yradius- the outer radiusthickness- the ring thickness, inward from the outer radiusstart- the start angle in radians, 0 along +xsweep- 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 atsoftnessoutside the edge.- Parameters:
distance- the signed distancesoftness- 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 atwidthinside it.- Parameters:
distance- the signed distancewidth- how far the glow reaches inward- Returns:
- the glow strength in
[0, 1]
-
smoothstep
public static float smoothstep(float edge0, float edge1, float x) GLSLsmoothstep.- Parameters:
edge0- lower edgeedge1- upper edgex- the value- Returns:
- the Hermite interpolation in
[0, 1]
-