0%

sobel

sobel

经常用来卡通动画描边使用

上链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//=============================================================================
// Performs edge detection using Sobel operator.
//=============================================================================

Texture2D gInput : register(t0);
RWTexture2D<float4> gOutput : register(u0);


// Approximates luminance ("brightness") from an RGB value. These weights are derived from
// experiment based on eye sensitivity to different wavelengths of light.
float CalcLuminance(float3 color)
{
return dot(color, float3(0.299f, 0.587f, 0.114f));
}

[numthreads(16, 16, 1)]
void SobelCS(int3 dispatchThreadID : SV_DispatchThreadID)
{
// Sample the pixels in the neighborhood of this pixel.
float4 c[3][3];
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
int2 xy = dispatchThreadID.xy + int2(-1 + j, -1 + i);
c[i][j] = gInput[xy];
}
}

// For each color channel, estimate partial x derivative using Sobel scheme.
float4 Gx = -1.0f*c[0][0] - 2.0f*c[1][0] - 1.0f*c[2][0] + 1.0f*c[0][2] + 2.0f*c[1][2] + 1.0f*c[2][2];

// For each color channel, estimate partial y derivative using Sobel scheme.
float4 Gy = -1.0f*c[2][0] - 2.0f*c[2][1] - 1.0f*c[2][1] + 1.0f*c[0][0] + 2.0f*c[0][1] + 1.0f*c[0][2];

// Gradient is (Gx, Gy). For each color channel, compute magnitude to get maximum rate of change.
float4 mag = sqrt(Gx*Gx + Gy*Gy);

// Make edges black, and nonedges white.
mag = 1.0f - saturate(CalcLuminance(mag.rgb));

gOutput[dispatchThreadID.xy] = mag;
}