Unity3d Super Sampling Anti-sawtooth Super Sampling Anti-Aliasing, unity3d Anti-sawtooth
Super Sampling Anti-Aliasing
SSAA is one of the most expensive anti-aliasing algorithms. It has been a long time, but the method is relatively simple,
The two steps are summarized as follows:
1. Search for edges
2. blur the edge
This is a post processing method,
Next, let's look at how to implement it.
The reason for searching the edge is also because the consumption is reduced, so that super sampling can be performed only at the edge, without the need to sample the entire graph.
In previous articles, we described three methods for edge search: Robert ts, Sobel, and Canny. Among them, sobel is the best, so we use sobel to find the edge.
The difference in edge search is that the filters are different, but they are both horizontal and vertical sampling.
The two filters of the Sobel operator calculate the horizontal and vertical gray scales respectively.
GX is a horizontal filter, GY is a vertical filter, and vertical filter is a horizontal filter that rotates 90 degrees.
A matrix with a filter of 3x3 is convolution with the image.
If there is no edge, the two points are very similar. The filter returns a smaller value. Otherwise, the edge can be determined.
After finding the edge, you can blur the edge.
Fuzzy edge the fuzzy edge is used for super sampling, and the surrounding pixels are used for color mixing.
We often see, for example, SSAAx2, SSAAx4, x8 .... The following number is the number of sampling points.
I have translated a wiki-based supersample.
There are many fuzzy edge methods (sampling methods) and several popular methods are:
Grid sampling, random sampling, poisson disc sampling, Jitter algorithm sampling, and rotating grid sampling
The sampling range of some methods is set to the gray scale of their edge, that is, the G in edge detection. Because the larger the G, the deeper the edge, the more obvious the sawtooth, the greater the sampling range.
Let's try grid, random, and Rotating sampling.
Grid sampling is relatively simple, but the blur effect may not be good because it is too regular,
Obtain the surrounding vertices and then mix the colors.
float4 c0 = tex2D(_MainTex, i.uv_MainTex + fixed2(0.5, 1) / _Size);float4 c1 = tex2D(_MainTex, i.uv_MainTex + fixed2(-0.5, 1) / _Size);float4 c2 = tex2D(_MainTex, i.uv_MainTex + fixed2(0.5, -1) / _Size);float4 c3 = tex2D(_MainTex, i.uv_MainTex + fixed2(-0.5, -1) / _Size);
Then there is random. I personally think the random effect is not very good, because, like the edge is noisy, there is a trace of pixel diffusion.
float2 randUV = 0;randUV = rand(float2(n.x, n.y));float4 c0 = tex2D(_MainTex, i.uv_MainTex + float2(randUV.x / 2, randUV.y) / _Size);randUV = rand(float2(-n.x, n.y));float4 c1 = tex2D(_MainTex, i.uv_MainTex + float2(randUV.x / 2, randUV.y) / _Size);randUV = rand(float2(n.x, -n.y));float4 c2 = tex2D(_MainTex, i.uv_MainTex + float2(randUV.x / 2, randUV.y) / _Size);randUV = rand(float2(-n.x, -n.y));float4 c3 = tex2D(_MainTex, i.uv_MainTex + float2(randUV.x / 2, randUV.y) / _Size);
Then there is rotating grid sampling. The best rotation angle is arctan (1/2) (about 26.6 °). Here, it is a lazy task, which also saves the consumption of rotation calculation and samples at an approximate position, the effect is good.
<span style="font-size:14px;">float4 c0 = tex2D(_MainTex, i.uv_MainTex + fixed2(0.2 / 2, 0.8) / _Size);float4 c1 = tex2D(_MainTex, i.uv_MainTex + fixed2(0.8 / 2, -0.2) / _Size);float4 c2 = tex2D(_MainTex, i.uv_MainTex + fixed2(-0.2 / 2, -0.8) / _Size);float4 c3 = tex2D(_MainTex, i.uv_MainTex + fixed2(-0.8 / 2, 0.2) / _Size);</span>
Result comparison
Performance
In terms of performance, SSAA is weaker than other AA, mainly because the method is so violent sampling.
The loss between SSAA sampling methods is similar.
SSAAx4
No anti-aliasing
Grid sampling
Random Sampling
Rotating sampling
In unity, the SSAA consumption of image effect is similar to that in this article.
I also looked at the anti-sawtooth methods of other unity, and found that the FXAA consumption was at least about 2. 8 ms.
All code has been shared to GitHub
----- By wolf96 http://blog.csdn.net/wolf96/