-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.frag
80 lines (57 loc) · 1.95 KB
/
shader.frag
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
precision highp float;
uniform mat4 mView; // view transformation
uniform mat4 mViewNormals; // view transformation for vectors
uniform bool uUseNormals;
varying vec3 fNormal;
varying vec3 fPosition;
const int MAX_LIGHTS = 8;
struct LightInfo {
vec3 pos;
vec3 Ia;
vec3 Id;
vec3 Is;
bool isDirectional;
bool isActive;
};
struct MaterialInfo {
vec3 Ka;
vec3 Kd;
vec3 Ks;
float shininess;
};
uniform int uNLights; // Effective number of lights used
uniform LightInfo uLight[MAX_LIGHTS]; // The array of lights present in the scene
uniform MaterialInfo uMaterial; // The material of the object being drawn
void main()
{
vec3 L;
vec3 globalLight;
for(int i=0; i<MAX_LIGHTS; i++){
if(i == uNLights){
break;
} else {
if(uLight[i].isActive){
if(uLight[i].isDirectional)
L = normalize((mViewNormals * vec4(uLight[i].pos, 0)).xyz);
else
L = normalize((mView * vec4(uLight[i].pos, 1)).xyz - fPosition);
vec3 V = normalize(-fPosition);
vec3 H = normalize(L + V);
vec3 N = normalize(fNormal);
vec3 R = reflect(-L, N);
vec3 ambientColor = uMaterial.Ka * uLight[i].Ia;
vec3 diffuseColor = uMaterial.Kd * uLight[i].Id;
float diffuseFactor = max(dot(L, N), 0.0);
vec3 diffuse = diffuseFactor * diffuseColor;
vec3 specularColor = uMaterial.Ks * uLight[i].Is;
float specularFactor = pow(max(dot(R, V), 0.0), uMaterial.shininess);
vec3 specular = specularFactor * specularColor;
if(dot(L,N) < 0.0){
specular = vec3(0.0, 0.0, 0.0);
}
globalLight += ambientColor + diffuse + specular;
}
}
}
gl_FragColor = vec4(globalLight, 1.0);
}