3 ms·
2D Signed Distance Functions
- on_the_train 9mo agoI 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 9mo agoI 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.
- vegabook 9mo agoor maybe terseness helps put your brain into pure algorithmic mode? After all that's how mathematical notation works and SDFs are pretty mathematical.
- Lerc 9mo agoCan 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 9mo agoYou 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/university/sdf-dsl https://github.com/SebastianMestre/school/tree/master/univer...
- Sharlin 9mo agof * 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.
- jesse__ 9mo agof*g is good to mask one SDF by another. I use it all the time in my little voxel engine.
- jesse__ 9mo agoThere's a bit to unpack here. There are two things one might care about when computing an SDF .. the isosurface, or the SDF itself. If you only care about the isosurface (ie. where the function is 0), you can do any ridiculous operations you can think of, and it'll work just fine. Add, sub, multiply, exp .. whatever you want. Voxel engines do this trick a lot. Then it becomes more of a density field, as apposed to a distance field. If you care about having a correct SDF, for something like raymarching, then you have to be somewhat more careful. Adding two SDFs does not result in a valid SDF, but taking the min or max of two SDFs does. Additionally, computing an analytical derivative of an SDF breaks if you add them, but you can use the analytical derivative if you take a min or max. Same applies for smooth min/max.
- jesse__ 9mo agoI owe iq so much; a living legend. Inigo, if you happen to ever read this, thanks so much for all the work you've published. Your Youtube videos (not to mention shadertoy) sparked an interest in graphics I never knew I had. For anyone that's unfamiliar, his Youtube videos are extremely well put together, and well worth the handful of hours to watch. https://www.youtube.com/c/InigoQuilez https://www.youtube.com/c/InigoQuilez
- tarnith 9mo agoI can definitely say I wouldn't know half of what I do and probably wouldn't have kept at it with writing GLSL and learning more about how GPUs really work without a lot of his freely shared knowledge over the years. His articles on his website are very much worth a deep read too!
- Jach 9mo agoAnother useful page from the author is the one on bounding boxes for SDFs: https://iquilezles.org/articles/bboxes2d/ https://iquilezles.org/articles/bboxes2d/
- SideQuark 9mo agoSomeone at work recently copied the ellipse SDF from that page into production code without checking it, and shipped a crash to a ton of people. If you simply glance at it, you’ll see it divides by zero on circles. Then, if you try to check for that, you’ll immediately hit overflow cases for common values in shaders. I replaced it with correctly designed, numerically robust code. Don’t use these routines; they’re all similarly land mines of bad numerics. They’re pretty but not robust.