-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderColorFragment.glsl
50 lines (42 loc) · 1.4 KB
/
shaderColorFragment.glsl
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
#define PROCESSING_LIGHT_SHADER
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform int lightCount;
uniform vec4 lightPosition[8];
uniform vec3 lightDiffuse[8];
uniform vec3 lightAmbient[8];
uniform vec3 lightSpecular[8];
varying vec4 vertColor;
varying vec4 vertEmissive;
varying float vertShininess;
varying vec3 ecNormal;
varying vec3 ecPosition;
float lambertFactor(vec3 lightDir, vec3 vecNormal) {
return max(0.0, dot(lightDir, vecNormal));
}
float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shininess) {
vec3 np = normalize(vertPos);
vec3 ldp = normalize(lightDir - np);
return pow(max(0.0, dot(ldp, vecNormal)), shininess);
}
void main() {
vec3 dfColor = vec3(0);
vec3 amColor = vec3(0);
vec3 spColor = vec3(0);
vec3 normal = normalize(ecNormal);
if(vertEmissive.x > 0 || vertEmissive.y > 0 || vertEmissive.z > 0) {
gl_FragColor = vertEmissive;
} else {
for(int i=0; i<lightCount; i++) {
vec3 lightDir = normalize(lightPosition[i].xyz - ecPosition);
float intensity = lambertFactor(lightDir, normal);
float spec = blinnPhongFactor(lightDir, ecPosition, normal, vertShininess);
dfColor += vertColor.rgb * lightDiffuse[i] * intensity;
spColor += lightSpecular[i] * spec;
amColor += lightAmbient[i];
}
gl_FragColor = vec4(dfColor + amColor + spColor, vertColor.a);
}
}