-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3. Anime4k ComputeGradient.effect
82 lines (65 loc) · 1.86 KB
/
3. Anime4k ComputeGradient.effect
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
uniform float4x4 ViewProj;
uniform texture2d image;
uniform float2 uv_scale;
uniform float2 uv_offset;
uniform float2 uv_size;
uniform texture2d PreviousPass<
string name = "Previous Pass";
string field_type = "input";
>;
#define parens(x) ((x))
#define dx parens(1/(float)uv_size.x)
#define dy parens(1/(float)uv_size.y)
// ---------- Shader Code
sampler_state def_sampler {
Filter = Linear;
AddressU = Wrap;
AddressV = Wrap;
BorderColor = 00000000;
};
struct VertFragData {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
VertFragData VSDefault(VertFragData vtx) {
vtx.pos = mul(float4(vtx.pos.xyz, 1.0), ViewProj);
vtx.uv = vtx.uv * uv_scale + uv_offset;
return vtx;
}
float4 getSample(float2 pos) {
return float4(image.Sample(def_sampler, pos).rgb, PreviousPass.Sample(def_sampler, pos).a);
}
float4 PSDefault(VertFragData vtx, texture2d PreviousPass) : TARGET {
float4 c0 = getSample(vtx.uv);
//[tl t tr]
//[ l r]
//[bl b br]
float t = getSample(vtx.uv + float2(0, -dy))[3];
float tl = getSample(vtx.uv + float2(-dx, -dy))[3];
float tr = getSample(vtx.uv + float2(dx, -dy))[3];
float l = getSample(vtx.uv + float2(-dx, 0))[3];
float r = getSample(vtx.uv + float2(dx, 0))[3];
float b = getSample(vtx.uv + float2(0, dy))[3];
float bl = getSample(vtx.uv + float2(-dx, dy))[3];
float br = getSample(vtx.uv + float2(dx, dy))[3];
//Horizontal Gradient
//[-1 0 1]
//[-2 0 2]
//[-1 0 1]
float xgrad = (-tl + tr - l - l + r + r - bl + br);
//Vertical Gradient
//[-1 -2 -1]
//[ 0 0 0]
//[ 1 2 1]
float ygrad = (-tl - t - t - tr + bl + b + b + br);
//Computes the luminance's gradient and saves it in the unused alpha channel
return float4(c0[0], c0[1], c0[2], 1 - clamp(sqrt(xgrad * xgrad + ygrad * ygrad), 0, 1));
}
technique Draw
{
pass
{
vertex_shader = VSDefault(vtx);
pixel_shader = PSDefault(vtx, PreviousPass);
}
}