-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragShader.glsl
130 lines (98 loc) · 3.76 KB
/
fragShader.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#version 430 core
out vec4 FragColor;
uniform sampler2D heightsTexture;
uniform sampler2D texture2;
uniform sampler2D normalTexture;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform int parFlag = 0;
uniform int binaryIter = 5;
uniform mat4 model;
in GS_OUT {
vec3 FragPos;
vec2 TexCoords;
vec3 TangentLightPos;
vec3 TangentViewPos;
vec3 TangentFragPos;
vec3 normal;
float hBase;
vec3 T;
vec3 B;
vec3 N;
float incUV;
} te_out;
vec2 parallaxBinarySearch(vec2 texCoords, vec3 viewDir){
vec2 P = normalize(viewDir.xy * 0.3);
vec3 front;
vec3 back;
vec3 mid;
vec2 prevCoords = texCoords;
front = vec3(texCoords + (0.05 * viewDir.xy), te_out.hBase + 0.3);
back = vec3(texCoords - (0.05 * viewDir.xy), te_out.hBase - 0.3);
vec2 currentCoords;
for (int i = 0; i < 10; i++) {
vec3 mid = (front + back) / 2; // middle
float hTex = texture(heightsTexture, mid.xy).r;
if (mid.z < hTex) {
back = mid;
} else {
front = mid;
}
currentCoords = front.xy * mid.z + back.xy * (1 - mid.z);
}
return currentCoords;
}
vec2 parallaxMapping(vec2 texCoords, vec3 viewDir){
const float numLayers = 60;
float layerDepth = 1.0 / numLayers;
float currentLayerDepth = te_out.hBase;
vec2 P = viewDir.xy * 0.3;
vec2 deltaTexCoords = P / numLayers;
texCoords = texCoords + P;
vec2 currentTexCoords = texCoords;
float currentDepthMapValue = (1 - (texture(heightsTexture, currentTexCoords).r));
vec2 finalCoords;
while(currentLayerDepth < currentDepthMapValue) {
currentTexCoords -= deltaTexCoords;
currentDepthMapValue = 1 - (texture(heightsTexture, currentTexCoords).r);
currentLayerDepth += layerDepth;
}
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
float afterDepth = currentDepthMapValue - currentLayerDepth;
float beforeDepth = (1 - (texture(heightsTexture, prevTexCoords).r)) - currentLayerDepth + layerDepth;
float weight = afterDepth / (afterDepth - beforeDepth);
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
return finalTexCoords;
}
void main() {
vec3 newPos;
vec3 newNormal;
vec2 texCoords = te_out.TexCoords;
vec3 viewDir = normalize(te_out.TangentViewPos - te_out.TangentFragPos);
if(texCoords.x < 0.0 || texCoords.y < 0.0) discard;
else if(parFlag == 1)
texCoords = parallaxMapping(te_out.TexCoords, viewDir);
else if(parFlag == 2)
texCoords = parallaxBinarySearch(te_out.TexCoords, viewDir);
if(texCoords.x > 1.0 || texCoords.y > 1.0 || texCoords.x < 0.0 || texCoords.y < 0.0)
discard;
newNormal = texture(normalTexture, vec2(texCoords.x, 0 + (1 - texCoords.y))).rgb;
newNormal = normalize(newNormal * 2.0 - 1.0);
//vec3 norm = normalize(te_out.normal);
vec3 norm = newNormal;
vec3 lightColor = texture(texture2, vec2(texCoords)).rgb;
vec3 lightDir = normalize(lightPos - te_out.FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
float specularStrength = 0.5;
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * lightColor;
float ambientStrength = 0.05;
vec3 ambient = ambientStrength * lightColor;
vec4 result = vec4(specular + diffuse + ambient, 1.0);
//vec4 result = vec4(texture(heightsTexture, te_out.TexCoords).r, texture(heightsTexture, te_out.TexCoords).r, texture(heightsTexture, te_out.TexCoords).r, 1.0);
//vec4 result = vec4(texture(normalTexture, te_out.TexCoords).rgb, 1.0);
//vec4 result = vec4(0, 0, te_out.hBase/10, 1.0);
FragColor = result;
}