2D Signed Distance Functions

(iquilezles.org)

57 points | by nickswalker 4 days ago ago

6 comments

  • jesse__ a minute ago ago

    I owe this guy so much. A living legend.

  • Lerc 2 hours ago ago

    Can you freely compose signed distance functions? Obviously people use them for + and - regularly. My intuition says you should be able to apply *, / and more as well.

    and sqrt( sdEquilateralTriangle(pos.xy, 10)**2 + sdCircle(pos.xz,10)**2 )

    seems like there's scope for a nice little domain specific language to.

    I think it would be interesting to have some composite operations that did probabilistic branching based upon a hashing RNG to conditionally combine shapes

    something like

        float thingy(pos,r) {
          float more = infinity
          float pseudoRandom = HashToUnit(pos) 
          if (pseudoRandom >0.5) {
            float direction=randomAngleFromSeed(pseudoRandom+r)
            more = thingy(pos+direction*r, r*0.75) 
          }
          return  min(circle(pos,r),more)
        }
    • sebastianmestre an hour ago ago

      You can take the minimum of two SDFs, which more or less gives you an SDF for their union. The maximum is the intersection. A few years ago I wrote a DSL that writes the SDFs for you, for my university programming languages course. https://github.com/SebastianMestre/school/tree/master/univer...

    • Sharlin 23 minutes ago ago

      f * g is a symmetric difference (all zeros remain zeros, the new internal points are those that are inside exactly one of f and g: (-, +) -> -, (+, -) -> -, (+, +) -> +, (-, -) -> +).

      f * g + x for some small constant x makes the symdiff smoother, depending on the sign of x it makes the components either meld together or "repel" each other. If the original components are disjoint (or if it's 3D solids and the internal surfaces are irrelevant) and x < 0, it functions as a smooth union.

      f / g has the same inside/zero/outside behavior as f * g, but is of course very pathological for all values of g close to zero. I don't think it has any good uses.

  • on_the_train 2 hours ago ago

    I come back every couple of months when I have a new project involving sdfs. And almost every time it's a bit of trial and error figuring out the parameters. It's workable, but a minor pet peeve that they're not described or named better.

    It's a priceless resource nevertheless.

    • RogerL 2 hours ago ago

      I was just about to say the same thing. This is bad code/documentation. Single letter variable names is almost always wrong if it isn't i for an index or such (and even then, would typing 'idx' kill you?). And as parameters, so much worse. Don't make me guess how to call your function please.