-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspecialized.frag
More file actions
89 lines (83 loc) · 2.64 KB
/
Copy pathspecialized.frag
File metadata and controls
89 lines (83 loc) · 2.64 KB
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
83
84
85
86
87
88
89
#version 450
#define NORMAL 0
#define FACE_NORMAL 1
#define DIFFUSE 2
#define SPECULAR 3
#define CELL_SHADE 4
layout(location = 0) in vec3 viewPos;
layout(location = 1) in vec3 viewNormal;
layout(location = 0) out vec4 oColor;
layout(constant_id = 0) const bool colorFill = false;
layout(constant_id = 1) const int shadingType = 0;
float stepmix(float edge0, float edge1, float e, float x)
{
float t = clamp(.5 * (x - edge0 + e)/e, 0.0, 1.0);
return mix(edge0, edge1, t);
}
void main()
{
vec3 albedo = vec3(0., 0.5, 1.);
if (colorFill)
{
oColor.rgb = albedo;
}
else
{
vec3 lightPos = vec3(0.);
if (NORMAL == shadingType)
{
oColor.rgb = normalize(viewNormal);
}
else if (FACE_NORMAL == shadingType)
{
vec3 ddx = dFdx(viewPos);
vec3 ddy = dFdy(viewPos);
vec3 normal = cross(ddy, ddx);
oColor.rgb = normalize(normal);
}
else if (DIFFUSE == shadingType)
{
vec3 N = normalize(viewNormal);
vec3 L = normalize(lightPos - viewPos);
float NdL = max(0., dot(N, L));
oColor.rgb = albedo * NdL;
}
else
{
vec3 N = normalize(viewNormal);
vec3 L = normalize(lightPos - viewPos);
vec3 V = normalize(-viewPos);
vec3 R = reflect(-L, N);
float NdL = max(0., dot(N, L));
float RdV = max(0., dot(R, V));
float spec = pow(RdV, 32.);
if (SPECULAR == shadingType)
{
oColor.rgb = albedo * NdL + spec;
}
else if (CELL_SHADE == shadingType)
{
// cell-shading code taken from http://prideout.net/blog/?p=22
float a = 0.4, b = 0.6, c = 0.9, d = 1.;
float e = fwidth(NdL);
if ((NdL > a - e) && (NdL < a + e))
NdL = stepmix(a, b, d, NdL);
else if ((NdL > b - e) && (NdL < b + e))
NdL = stepmix(b, c, d, NdL);
else if ((NdL > c - e) && (NdL < c + e))
NdL = stepmix(c, d, d, NdL);
else if (NdL < a) NdL = 0.0;
else if (NdL < b) NdL = b;
else if (NdL < c) NdL = c;
else NdL = d;
e = fwidth(spec);
if (spec > 0.5 - e && spec < 0.5 + e)
spec = smoothstep(0.5 - e, 0.5 + e, spec);
else
spec = step(0.5, spec);
oColor.rgb = albedo * NdL + spec;
}
}
}
oColor.a = 1.;
}