-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathExample.frag
65 lines (55 loc) · 1.7 KB
/
Example.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
// GLSL fragment shader
#version 400 core
// Input constant data
layout(std140) uniform Scene
{
mat4 vpMatrix;
mat4 vMatrix;
mat4 wMatrix;
vec3 lightVec;
float texScale;
float tessLevelInner;
float tessLevelOuter;
float maxHeightFactor;
float shininessPower;
};
// Input and output attributes
in vec3 teViewPos;
in vec3 teNormal;
in vec3 teTangent;
in vec3 teBitangent;
in vec2 teTexCoord;
out vec4 fragColor;
// Input textures
uniform sampler2D colorMap;
uniform sampler2D normalMap;
uniform sampler2D specularMap;
vec4 PhongShading(vec4 albedo, vec3 normal, float shininess, vec3 viewVec)
{
// Diffuse lighting
float lambertian = max(0.0, dot(normal, lightVec));
vec3 diffuse = albedo.rgb * lambertian;
// Specular lighting
vec3 reflectVec = normalize(reflect(lightVec, normal));
vec3 halfVec = normalize(lightVec - viewVec);
float specAngle = max(0.0, dot(normal, halfVec));
vec3 specular = vec3(shininess * pow(specAngle, shininessPower));
return vec4(diffuse + specular, albedo.a);
}
void main()
{
// Sample color, normal, and specular maps
vec4 albedo = texture(colorMap, teTexCoord);
vec3 normal = normalize(texture(normalMap, teTexCoord).rgb * 2.0 - 1.0);
float roughness = texture(specularMap, teTexCoord).r;
float shininess = 1.0 - roughness;
// Transform normal from world-space into tangent-space
mat3 tangentSpace = mat3(
normalize(teTangent),
normalize(teBitangent),
normalize(teNormal)
);
normal = normal * tangentSpace;
// Compute simple phong shading
fragColor = PhongShading(albedo, normal, shininess, normalize(teViewPos));
}