Sunday 13 December 2015

Temporal Supersampling Anti-aliasing in Unity

I was looking for nice anti-aliasing solution for project I'm working on, and after some search i found presentation of Temporal AA made by Brian Karis for Unreal Engine 4 Infiltrator demo.

The basic idea is simple. We jitter the camera projection in subpixel scale to get frames with aliasing variations and blend them between.
So, first I had to do jittering. I could simply split the pixel into section grid for sample pattern, or use a random number, but I decided to try the same Halton Sequence method as mentioned in the presentation.
The result was something like this:




And after that, I had to blend the frames to get anti-aliased image:

Wow. I didn't really expect that result will be so smooth, so I decided to compare it with other anti-aliasing methods.
I took unity's default FXAA, SMAA made by Thomas @Chman, and for reference I took screenshot with 4x resolution and scaled down to original size in photoshop.

And this is what I got:
SMAA and FXAA worked out on polygon edges pretty well, but in overall it's almost nothing compared to reference and temporal SSAA. In some areas even reference wasn't so good as temporal SSAA.

For now we have a nice AA for static scenery, and here comes the tricky part.
If we try move our camera we will get ghosting from previous frames:


So, I had to reproject previous frame to the current. The easiest way to do that is generate velocity buffer from camera movement and depth buffer.

After some more coding, I got my velocity buffer:
(I adjusted some colors on image to make it more representative)


And applied it to UV for previous frame:

Okay, it's better, but it still have ghosting in areas where there is no useful information for the new frame. 

Let's use there a data from new frame instead:














And Voila! It still have some ghosting, but it's almost not noticable.
So, since we have our velocity buffers only from depth and camera movement, it becomes useless when we have dynamic objects in scene, and ghosting will be appear on them.
I will investigate this issue a bit later, but for now temporal SSAA is pretty usable for static scenery with movable camera, like architeture visualisation for example.