-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault140.frag
113 lines (88 loc) · 3.38 KB
/
default140.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
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
#version 300 es
precision highp float;
// Outputs colors in RGBA
out vec4 FragColor;
// Imports the current position from the Vertex Shader
in vec3 crntPos;
// Imports the normal from the Vertex Shader
in vec3 Normal;
// Imports the color from the Vertex Shader
in vec3 color;
// Imports the texture coordinates from the Vertex Shader
in vec2 texCoord;
// Gets the Texture Units from the main function
uniform sampler2D diffuse0;
uniform sampler2D specular0;
// Gets the color of the light from the main function
uniform vec4 lightColor;
// Gets the position of the light from the main function
uniform vec3 lightPos;
// Gets the position of the camera from the main function
uniform vec3 camPos;
vec4 pointLight()
{
// used in two variables so I calculate it here to not have to do it twice
vec3 lightVec = lightPos - crntPos;
// intensity of light with respect to distance
float dist = length(lightVec);
float a = 3.0;
float b = 0.7;
float inten = 1.0f / (a * dist * dist + b * dist + 1.0f);
// ambient lighting
float ambient = 0.20f;
// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(lightVec);
float diffuse = max(dot(normal, lightDirection), 0.0f);
// specular lighting
float specularLight = 0.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 16.0);
float specular = specAmount * specularLight;
return (texture(diffuse0, texCoord) * (diffuse * inten + ambient) + texture(specular0, texCoord).r * specular * inten) * lightColor;
}
vec4 direcLight()
{
// ambient lighting
float ambient = 0.20f;
// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(vec3(1.0f, 1.0f, 0.0f));
float diffuse = max(dot(normal, lightDirection), 0.0f);
// specular lighting
float specularLight = 0.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 16.0);
float specular = specAmount * specularLight;
return (texture(diffuse0, texCoord) * (diffuse + ambient) + texture(specular0, texCoord).r * specular) * lightColor;
}
vec4 spotLight()
{
// controls how big the area that is lit up is
float outerCone = 0.90f;
float innerCone = 0.95f;
// ambient lighting
float ambient = 0.20f;
// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(lightPos - crntPos);
float diffuse = max(dot(normal, lightDirection), 0.0f);
// specular lighting
float specularLight = 0.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 16.0);
float specular = specAmount * specularLight;
// calculates the intensity of the crntPos based on its angle to the center of the light cone
float angle = dot(vec3(0.0f, -1.0f, 0.0f), -lightDirection);
float inten = clamp((angle - outerCone) / (innerCone - outerCone), 0.0f, 1.0f);
return (texture(diffuse0, texCoord) * (diffuse * inten + ambient) + texture(specular0, texCoord).r * specular * inten) * lightColor;
}
void main()
{
// outputs final color
FragColor = direcLight();
//FragColor = vec4(1.0,0.0,0.0,1.0);
}