forked from GlPortal/glportal-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffuse.frag
63 lines (51 loc) · 1.68 KB
/
diffuse.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
#version 130
struct Light {
vec3 position;
float distance;
vec3 color;
float energy;
float specular;
};
uniform sampler2D diffuse;
uniform sampler2D normalMap;
uniform sampler2D specularMap;
uniform Light lights[64];
uniform int numLights;
in vec3 pass_position;
in vec2 pass_texCoord;
in vec3 pass_normal;
in vec3 pass_tangent;
invariant in vec3 pass_viewerPos;
invariant in mat3 pass_surf2world;
in vec3 pass_worldPos;
out vec4 out_Color;
void main(void) {
// Normals
// Use MAD (Multiply And Add), like below
vec3 localCoords = (texture(normalMap, pass_texCoord).xyz * 2) - 1;
vec3 normal = normalize(pass_surf2world * localCoords);
vec3 viewDirection = normalize(pass_viewerPos - pass_worldPos);
float gloss = texture(specularMap, pass_texCoord).r * 255.0;
float normalization = (gloss + 8) * 0.125;
vec3 diffuseColor = texture(diffuse, pass_texCoord).rgb;
vec3 refl = .3 * diffuseColor;
for (int i = 0; i < numLights; ++i) {
// Calculate the vector from this pixels surface to the light source
vec3 lightDist = lights[i].position - pass_worldPos;
float lightLength = length(lightDist);
if (lightLength < lights[i].distance) {
// Diffuse reflection + normal map
float fAtt = max(0, 1 - sqrt(lightLength / lights[i].distance));
vec3 lightDir = lightDist/lightLength;
float fDiffuse = max(dot(normal, lightDir), 0);
// Specular reflection
vec3 halfDir = normalize(lightDir + viewDirection);
float fSpecular = normalization * pow(max(dot(halfDir, normal), 0), gloss) *
0.04 + 0.96*pow(1-dot(halfDir, lightDir), 5);
refl += fAtt * (
(diffuseColor + vec3(fSpecular)) * fDiffuse * lights[i].energy * lights[i].color
);
}
}
out_Color = vec4(refl, 1.0);
}