Repository navigation
Expand file tree
/
Copy pathpeel_color_fragmentC4.shader
More file actions
121 lines (105 loc) · 3.16 KB
/
Copy pathpeel_color_fragmentC4.shader
File metadata and controls
121 lines (105 loc) · 3.16 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#version 300 es
# ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
# else
precision mediump float;
# endif
uniform int light_count;
uniform vec4 light_pos[32];
uniform vec3 light_color[32];
uniform vec3 light_ambient;
#define LP(i) light_pos[i]
#define LC(i) light_color[i]
uniform vec2 canvas_size;
uniform sampler2D texmap; // TEXTURE0 - user texture
uniform sampler2D bumpmap; // TEXTURE1 - user bumpmap
uniform sampler2D D0; // TEXTURE3 - opaque depth map (minormode 5)
uniform sampler2D D3; // TEXTURE9 - depth map (minormode 11)
in vec3 es_position; // eye space surface position
in vec3 es_normal; // eye space surface normal
in vec2 mat_pos; // surface material position in [0,1]^2
in vec4 vcolor;
in vec3 bumpX;
in vec4 parameters; // shininess, emissive, hasTexture, hasBump
#define shininess parameters[0]
#define emissive parameters[1]
#define hasTexture parameters[2]
#define hasBump parameters[3]
out vec4 output_color;
vec3 normal;
vec3 pos;
vec3 diffuse_color;
vec3 specular_color;
vec3 color;
void calc_color(vec4 lpos, vec3 lcolor)
{
vec3 L = lpos.xyz - pos*lpos.w; // w == 0 for distant_light
L = normalize(L);
float N = max(dot(normal,L), 0.0);
color += (lcolor * N)*diffuse_color;
if (shininess > 0.0) {
vec3 R = reflect(L,normal);
color += specular_color * LC(0) * pow(max(dot(R,normalize(pos)),0.0),100.0*shininess);
}
}
// Return lit surface color based on the given surface properties and the lights
// specified by the light_* uniforms.
void lightAt()
{
if (hasTexture != 0.0) {
diffuse_color = diffuse_color * texture(texmap, mat_pos).xyz;
}
if (hasBump != 0.0) {
vec3 Y = cross(normal, bumpX);
vec3 Nb = texture(bumpmap, mat_pos).xyz;
Nb = 2.0*Nb - 1.0;
normal = normalize(Nb.x*bumpX + Nb.y*Y + Nb.z*normal);
}
if (emissive != 0.0) {
// From VPython materials.emissive:
float d = dot(normalize(-pos), normal);
d = pow(d * 1.5, 0.4) * 1.1;
if (d > 1.0) d = 1.0;
color = diffuse_color * d;
return;
}
color = light_ambient * diffuse_color;
for (int i=0; i<light_count; i++) {
calc_color(LP(i), LC(i));
}
}
ivec4 encode(float k) { // assumes k is >= 0
if (k <= 0.0) return ivec4(0, 0, 0, 0);
k = 3.0*128.0*k;
int b1 = int(k);
int b2 = int(256.0*fract(k));
return ivec4(
b1,
b2,
0,
0);
}
int decode(ivec4 d) {
return int(256*d[0] + d[1]);
}
int fdecode(vec4 d) {
return int(255.0*(256.0*d[0] + d[1]));
}
void main(void) {
// create transparency color map - C1 (minormode 6), C2 (8), C3 (19), C4 (12)
ivec4 c = encode(1.0 - gl_FragCoord.z);
int z = decode(c);
vec2 loc = vec2(gl_FragCoord.x/canvas_size.x, gl_FragCoord.y/canvas_size.y);
int zmin = fdecode(texture(D0, loc));
int zmax = fdecode(texture(D3, loc));
normal = normalize(es_normal);
pos = es_position;
diffuse_color = vcolor.rgb;
specular_color = vec3(.8,.8,.8);
lightAt(); // determine color from lighting
if (zmin < z && z < zmax) {
output_color = vec4( color, vcolor.a );
} else {
discard;
}
}