I’m working on a shader to procedurally generate eye (iris) textures. Up close it looks pretty good…
…though you can see a bit of aliasing around the edges of the iris and the pupil. But further away, it gets worse:
If I used a “real” texture here, it would do some blending which would produce soft edges around those circles. But since I’m just generating a color on the fly based on the UV coordinates of each pixel, I get these aliased edges that are pretty noticeable.
How can I anti-alias those edges? I’ve thought about trying to use smoothstep to generate an interpolated color on the edges, but exactly how to apply it is not obvious to me.
Instead of doing those ifs, you would generate masks.
So you would so something like (code will not work, I’m writing from the top of my head, but hopefully you’ll get the idea)
fixed maskiris = smoothstep(irisr, irisr+0.1, r);
fixed maskpupil = smoothstep(pupilR, pupilR+0.1, r);
fixed4 iris = (whatever you do to get your iris)
fixed4 col = lerp(float4(1,1,1,1), iris, maskiris);
col = lerp(col, float(0,0,0,0), maskpupil);
You could also use a texture just for the masks? There are a bunch of different methods, I’m not sure why you want to do everything in shader, but I guess you have your reasons.
Well, so obviously one option is to generate an actual texture, and then just apply that with a standard shader. I’m not necessarily opposed to that. But then I have to think carefully about how close somebody might get to these eyes, so they don’t go all pixely prematurely, but at the same time we don’t waste texture RAM needlessly… I’d probably need to generate LOD textures. It just seemed simpler to throw the code onto the GPU and let it do it there.
(I imagine most of those GPU processors sitting around bored out of their skulls, when the most challenging thing anybody asks them to do all day is lerp a color or maybe compute a bit of lighting.)
The trouble with lerping over an extra 0.1 (or whatever I pick) is that, close up, we’d see the edge fading out; and far away, we’d still have aliasing. There’d be a sweet spot where the extra 0.1 is roughly a pixel, where it would probably look great.
I suppose I could compute that extra 0.1 dynamically based on… er… some sort of math applied to values available to the shader.
Well, how about applying some TAA on top or something
The issue here I think, is that while doing an antialiased circle in shader is doable (there are a bunch of methods), it will still be antialiased in UV space, not in screenspace, which is what you want.