2022-01-20 21:58:50 +08:00
|
|
|
#version 330
|
|
|
|
|
2022-02-21 15:11:13 +08:00
|
|
|
uniform sampler2D gPos;
|
2022-01-29 00:43:11 +08:00
|
|
|
uniform sampler2D tex;
|
2022-01-20 21:58:50 +08:00
|
|
|
uniform sampler2D shadowmap;
|
|
|
|
uniform vec3 viewPos;
|
|
|
|
uniform vec3 sun;
|
|
|
|
uniform vec4 fogColor;
|
2022-02-21 15:11:13 +08:00
|
|
|
uniform vec2 screenSize;
|
2022-01-20 21:58:50 +08:00
|
|
|
|
2022-01-29 00:43:11 +08:00
|
|
|
uniform float alpha; // Alpha of the semi-transparant layer
|
2022-01-20 21:58:50 +08:00
|
|
|
|
2022-01-29 00:43:11 +08:00
|
|
|
// Fragment information
|
|
|
|
in vec4 fragPos;
|
2022-02-21 15:11:13 +08:00
|
|
|
in vec4 fragPosView;
|
|
|
|
in vec4 fragPosScreen;
|
2022-01-29 00:43:11 +08:00
|
|
|
in vec4 fragPosLightspace;
|
|
|
|
in vec3 fragNormal;
|
|
|
|
in vec2 fragTexCoord;
|
|
|
|
vec4 fragColor;
|
2022-01-20 21:58:50 +08:00
|
|
|
|
|
|
|
out vec4 outputColor;
|
|
|
|
|
|
|
|
|
|
|
|
const float gamma = 2.2;
|
|
|
|
|
2022-01-29 00:43:11 +08:00
|
|
|
const float ambient = 0.3, specularStrength = 1.6, specularShininess = 128;
|
2022-01-20 21:58:50 +08:00
|
|
|
const float fogDensity = .00003;
|
|
|
|
|
|
|
|
|
2022-02-21 15:11:13 +08:00
|
|
|
float sunalpha = 0;
|
2022-01-20 21:58:50 +08:00
|
|
|
float light;
|
2022-02-21 14:01:09 +08:00
|
|
|
vec3 color = vec3(0);
|
2022-01-20 21:58:50 +08:00
|
|
|
void lightSun();
|
|
|
|
float lightSunShadow();
|
|
|
|
void lightPoint(int i);
|
|
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
2022-02-21 14:13:24 +08:00
|
|
|
fragColor = vec4(texture(tex, fragTexCoord).rgb, 1.0f);
|
2022-01-20 21:58:50 +08:00
|
|
|
|
|
|
|
light = ambient;
|
|
|
|
|
|
|
|
lightSun();
|
|
|
|
|
2022-02-21 14:01:09 +08:00
|
|
|
color += fragColor.rgb * light;
|
2022-01-20 21:58:50 +08:00
|
|
|
|
2022-02-21 14:01:09 +08:00
|
|
|
//float z = gl_FragCoord.z / gl_FragCoord.w;
|
|
|
|
//float fog = clamp(exp(-fogDensity * z * z), 0.2, 1);
|
2022-01-20 21:58:50 +08:00
|
|
|
|
2022-02-21 15:11:13 +08:00
|
|
|
float waterDepth = (fragPosView.z-texture(gPos, gl_FragCoord.xy/screenSize).a);
|
|
|
|
|
|
|
|
float waterFactor = pow(alpha, waterDepth);
|
|
|
|
outputColor = vec4(color*waterFactor, min(1-waterFactor + sunalpha, 1));
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void lightSun() {
|
|
|
|
/* Diffuse */
|
|
|
|
vec3 lightDir = sun;
|
|
|
|
float diffuse = max(dot(fragNormal, lightDir), 0.0f);
|
|
|
|
|
|
|
|
/* Specular */
|
|
|
|
vec3 viewDir = normalize(viewPos - fragPos.xyz);
|
|
|
|
vec3 reflectDir = reflect(-lightDir, fragNormal);
|
|
|
|
float specular = specularStrength * pow(max(dot(viewDir, reflectDir), 0.0), specularShininess);
|
|
|
|
|
|
|
|
float shadow = lightSunShadow();
|
|
|
|
light += diffuse * shadow;
|
2022-02-21 14:01:09 +08:00
|
|
|
color += vec3(specular) * shadow;
|
2022-01-29 22:29:27 +08:00
|
|
|
|
2022-02-21 15:11:13 +08:00
|
|
|
if (specular*shadow > 1.0f)
|
|
|
|
sunalpha += specular*shadow - 1.0f;
|
|
|
|
sunalpha += 0.1f*shadow;
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float lightSunShadow() {
|
|
|
|
/* Shadow */
|
2022-01-22 23:06:41 +08:00
|
|
|
float bias = max(0.0013 * (1.0 - dot(fragNormal, sun)), 0.0001);
|
2022-01-20 21:58:50 +08:00
|
|
|
vec3 projCoords = fragPosLightspace.xyz / fragPosLightspace.w;
|
|
|
|
projCoords = projCoords*0.5 + 0.5;
|
2022-01-22 23:06:41 +08:00
|
|
|
float closestDepth = texture(shadowmap, projCoords.xy).r;
|
2022-01-29 00:43:11 +08:00
|
|
|
float currentDepth = projCoords.z;
|
2022-01-20 21:58:50 +08:00
|
|
|
float shadow = 0;
|
|
|
|
|
|
|
|
if (currentDepth > 1.0f || currentDepth < 0.0f)
|
|
|
|
return 1.0f;
|
|
|
|
|
2022-01-22 23:06:41 +08:00
|
|
|
//vec2 texelSize = clamp((currentDepth+bias-closestDepth)*100.0f, 0.05f, 1.5f) / textureSize(shadowmap, 0);
|
|
|
|
vec2 texelSize = 0.4 / textureSize(shadowmap, 0);
|
|
|
|
for (int x=-4; x<=4; ++x)
|
|
|
|
for (int y=-4; y<=4; ++y) {
|
2022-01-20 21:58:50 +08:00
|
|
|
float pcfDepth = texture(shadowmap, projCoords.xy + vec2(x,y)*texelSize).r;
|
|
|
|
shadow += currentDepth-bias < pcfDepth ? 1.0f : 0.0f;
|
|
|
|
}
|
2022-01-22 23:06:41 +08:00
|
|
|
return min(shadow/81.0f, 1.0f);
|
2022-01-20 21:58:50 +08:00
|
|
|
}
|