New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quick idea for improving text antialiasing #137
Comments
Thanks for the suggestion! I'll definitely look into this, as I've noticed those blurry edges at oblique angles too and would like to eliminate them. Theoretically I think both the change in |
Great! The change is as simple as that one line (and passing in that distance argument to acommodate it). If you still would like the raw code, I can send it when I'm working tomorrow.
I don't exactly understand how the derivatives would be the same. |
Thanks for the discussion, this stuff breaks my brain sometimes. ;) When I said they are "equivalent" I meant that they both represent a distance in the same units, but you're right they're not exactly the same. Backing up, what we want here is the potential rate of change between fragments. Using the change in the So how can we determine the potential rate of change without using But, as you've seen, where it breaks down is at oblique angles; in those cases the rate of potential x/y change in the vertical is much different than that of the horizontal. And so we end up using a single value for the potential rate of change that is too large in one direction and too small in the other, giving blurry and/or choppy lines depending on the SDF's actual direction of change at that fragment. So yeah, there's definitely room for improvement in the oblique case, but just using the derivative of |
I'm noticing that this modification significantly improves the result for custom text transformations. Here is some text, modified to follow a curve using Here is the same screenshot, but with @asbjornlystrup's modification to In the second shot, you can see the extremely stretched "GREAT!" is quite a bit crisper. |
Anecdotally: I've also found that tweaking the constant to 0.25 (from 0.5) makes a nice crisp edge as well:
|
Thanks @canadaduane, I can definitely see the improvement in the stretched parts. I can also see the ugly artifacts popping up there between the L's in HELLO, which we'd definitely need to figure out before adopting this change. |
I'm not sure if you've updated the antialiasing code recently, as I'm using an older version, but after finding a nice way to do antialiasing of shader-drawn circles, I wanted to try it out on your text as well, because the text is SDF-based, and the circle's antialiasing is distance-based as well.
In my current version of Troika, you do
return length(fwidth(vTroikaGlyphUV * vTroikaGlyphDimensions)) * 0.5;
for the aa distance. This has the artifact that when viewing at an angle, the leftmost and rightmost edges get a somewhat visible blur:I changed it to a scalar; the distance, just like in my circle code:
return fwidth(distance) * 0.5;
With the distance passed in fromtroikaGetFragDistValue
like this:float distance = troikaGetFragDistValue(); float aaDist = troikaGetAADist(distance);
Doing it this way I don't get the blurring artifact:
The
0.5
factor can be increased for a smoother antialiasing. E.g.0.8
:I thought maybe you were unaware of this method just like me, so thought I'd share it. My "implementation" does have some strange artifacts though (see below), and maybe this is why you opted for the other method. Also, I'm not exactly sure how you use the aaDist variable in the shader code, so you might be better equipped at implementing this. Maybe there's some code that should be changed or removed because of this change for example, that could improve my "implementation" further.
I found this method of doing it in bgolus's post here: https://forum.unity.com/threads/antialiasing-circle-shader.432119/ where there's some more info. He also says that you can do for example
length(vec2(dFdx(distance), dFdy(distance)))
instead offwidth(distance)
for even greater precision, although I'm not sure if this is negligible or not.The text was updated successfully, but these errors were encountered: