Circle 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.

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

    def proxy: (vec2, Float) => Float =
      (p, r) => circle(p, r)

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

    def fragment(color: vec4): vec4 =
      calculateColour(env.UV, proxy(env.UV - 0.5f, 0.4f))
  }