/
ToonUtil.cginc
67 lines (57 loc) · 1.69 KB
/
ToonUtil.cginc
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
#ifndef TOON_UTIL_INCLUDED
#define TOON_UTIL_INCLUDED
#include "UnityCG.cginc"
float4x4 contrastMatrix(float c)
{
float t = (1.0 - c) * 0.5;
return float4x4(c, 0, 0, 0, 0, c, 0, 0, 0, 0, c, 0, t, t, t, 1);
}
float3 RGBToHSV(float3 c)
{
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
float3 HSVToRGB(float3 c)
{
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
}
float3 Hue(float3 p, float v)
{
p.x *= v;
return p;
}
float3 Saturation(float3 p, float v)
{
p.y *= v;
return p;
}
float3 Contrast(float3 p, float v)
{
return mul(float4(p, 1.0), contrastMatrix(v)).rgb;
}
// Returns > 0 if not clipped, < 0 if clipped based
// on the dither
// For use with the "clip" function
// pos is the fragment position in screen space from [0,1]
float isDithered(float2 pos, float alpha) {
pos *= _ScreenParams.xy;
// Define a dither threshold matrix which can
// be used to define how a 4x4 set of pixels
// will be dithered
float DITHER_THRESHOLDS[16] =
{
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
};
int index = (int(pos.x) % 4) * 4 + int(pos.y) % 4;
return alpha - DITHER_THRESHOLDS[index];
}
#endif