Glad to see you’re looking into this as well. I’m also stuck…
So far I’ve looked at different packages, and they either avoid fwidth and make you set a smoothness amount manually for each shape (e.g. MPUIkit, DTT Procedural UI, my own private package and probably more), which is inconsistent across different resolutions (not good!), or they use fwidth and have to deal with this weirdness.
It doesn’t even look that bad when the box is decently sized, but if you make it smaller, it gets really bad.
I even looked at Unity’s UGUI shader graph samples (they released a video about it today, lol) and their solution has the same issue. They’re using something like saturate(sdf / length(float2(ddx(sdf), ddy(sdf)) - 0.5) in shader graph, resulting in the same issues.
I also tried looking at how TMP handles their AA, but I’m not enough of a shader/math wizard to understand what’s going on and I don’t know how many things are handled differently due to using textures and other parameters instead. But from what I’m seeing it’s using some sort of scale factor to multiply the distance with at the end - no fwidth nor smoothstep (although it might do some smoothstep-y things somewhere along the way, it’s just not using the instrinsic function).
Oh and, as I was typing this, I tested some more with my SDF library made in shader graph for use outside of UI, and it does seem to have mostly similar issues with the box SDF, so I suppose it’s not the fault of the UI itself. This makes me think it’s not really “solvable” with just fwidth.
This is just rendered to a quad (as it’s usually used in VFX Graph). It’s less noticable and it’s on the top-right corner instead (UVs are different? Idk), but it’s there:
Edit: I just realized it’s “less noticable” because the background in the screenshot is gray, oops. So it’s pretty much the same effect that happens in UI space.
This whole thing wouldn’t be such a big deal if boxes weren’t such an integral part of UI design. Looking at UI Toolkit, visual elements by themselves mostly render boxes and they seem to have it figured out. Perfect looking sub-pixel AA that seemingly works at any resolution. I’d love to see their shader code to see how it’s done, but I’d expect a lot of magic that wouldn’t be of much use to me, lmao.
So yeah… if I figure something out I’ll post it here. I hope some shader wizard (or witch) can chime in to grant us some insight. In the meantime we just gotta deal with it I guess.