Segment SDF

Based on work by Inigo Quilez: https://iquilezles.org/articles/distfunctions2d/

Demo

▶ Click to play

The sdf function is imported, but we then need to set up a proxy function. This is because the macro system relies on inlining, and we want to create a function definition, not just inline the body at the point of use.

This shader also uses an imported 'fill' function to calculate a nice colour gradient.

Note that we can't just render the line segment, because at 0.0f the SDF is 0.0f, so we need the annular, i.e. abs(sdf) - thickness-of-border.

inline def fragment =
  Shader[FragmentEnv] { env =>
    import ultraviolet.sdf.*
    import FillColorHelper.*

    def proxy: (vec2, vec2, vec2) => Float =
      (p, a, b) => segment(p, a, b)

    def calculateColour: (vec2, Float) => vec4 = (uv, sdf) => fill(uv, sdf)

    def fragment(color: vec4): vec4 =
      val segSDF = proxy(env.UV, vec2(0.2f), vec2(0.8f))
      calculateColour(env.UV, abs(segSDF) - 0.1f)
  }