2024-02-07 02:59:34 +01:00
|
|
|
#version 430 core
|
|
|
|
#define PI 3.14159265359
|
2024-03-18 14:21:18 +01:00
|
|
|
|
2024-02-08 10:10:49 +01:00
|
|
|
layout (location = 0) out vec4 outColor;
|
|
|
|
layout (location = 1) out vec4 BrightColor;
|
2024-03-18 14:21:18 +01:00
|
|
|
|
|
|
|
in vec3 fragNormal;
|
|
|
|
in vec3 fragPosition;
|
|
|
|
in vec2 texCoords;
|
|
|
|
|
|
|
|
uniform sampler2D textures[4];
|
2024-02-07 02:59:34 +01:00
|
|
|
|
|
|
|
uniform vec3 albedo;
|
|
|
|
uniform float metallic;
|
|
|
|
uniform float roughness;
|
|
|
|
uniform vec3 lightPos;
|
|
|
|
uniform vec3 viewPos;
|
|
|
|
uniform vec3 ambientColor;
|
|
|
|
|
|
|
|
float specularD(float NdotH, float roughness)
|
|
|
|
{
|
|
|
|
float alpha = roughness * roughness;
|
|
|
|
float alpha2 = alpha * alpha;
|
|
|
|
float cos2ThetaH = NdotH * NdotH;
|
|
|
|
|
|
|
|
float expTerm = (cos2ThetaH - 1.0) / (alpha2 * cos2ThetaH);
|
|
|
|
return exp(expTerm) / (PI * alpha2 * pow(cos2ThetaH + alpha2 - 1.0, 2.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 specularF(float LdotH, vec3 F0)
|
|
|
|
{
|
|
|
|
return F0 + (vec3(1.0) - F0) * pow(1.0 - LdotH, 5.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
float specularG(float NdotL, float NdotV, float roughness)
|
|
|
|
{
|
|
|
|
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;
|
|
|
|
|
|
|
|
float GL = NdotL / (NdotL * (1.0 - k) + k);
|
|
|
|
float GV = NdotV / (NdotV * (1.0 - k) + k);
|
|
|
|
|
|
|
|
return GL * GV;
|
|
|
|
}
|
|
|
|
|
2024-03-18 14:21:18 +01:00
|
|
|
void main() {
|
2024-02-07 02:59:34 +01:00
|
|
|
vec3 normal = normalize(fragNormal);
|
|
|
|
vec3 viewDir = normalize(viewPos - fragPosition);
|
|
|
|
vec3 lightDir = normalize(lightPos - fragPosition);
|
|
|
|
|
2024-03-18 14:21:18 +01:00
|
|
|
vec3 F0 = mix(vec3(0.04), albedo, metallic);
|
2024-02-07 02:59:34 +01:00
|
|
|
|
|
|
|
float NdotL = max(dot(normal, lightDir), 0.0);
|
|
|
|
float NdotV = max(dot(normal, viewDir), 0.0);
|
|
|
|
|
|
|
|
vec3 H = normalize(lightDir + viewDir);
|
|
|
|
float NdotH = max(dot(normal, H), 0.0);
|
|
|
|
float LdotH = max(dot(lightDir, H), 0.0);
|
2024-03-18 14:21:18 +01:00
|
|
|
|
2024-03-18 16:48:46 +01:00
|
|
|
int texIndex = 0;
|
2024-03-18 14:21:18 +01:00
|
|
|
vec3 albedoTex = texture(textures[texIndex], texCoords).rgb;
|
|
|
|
albedoTex = vec3(smoothstep(0.0, 1.0, albedoTex));
|
2024-02-07 02:59:34 +01:00
|
|
|
|
2024-03-18 14:21:18 +01:00
|
|
|
float luminance = dot(albedoTex, vec3(0.2126, 0.7152, 0.0722));
|
|
|
|
albedoTex *= 4.0;
|
|
|
|
float saturationFactor = 4.0;
|
|
|
|
albedoTex = mix(vec3(luminance), albedoTex, saturationFactor);
|
2024-02-07 02:59:34 +01:00
|
|
|
|
2024-03-18 14:21:18 +01:00
|
|
|
vec3 specular = F0 * (specularD(NdotH, roughness) * specularF(LdotH, F0) * specularG(NdotL, NdotV, roughness));
|
|
|
|
vec3 diffuse = (1.0 - F0) * albedoTex / PI;
|
2024-02-07 02:59:34 +01:00
|
|
|
|
2024-03-18 14:21:18 +01:00
|
|
|
vec3 ambient = ambientColor * 0.02;
|
|
|
|
|
2024-02-07 02:59:34 +01:00
|
|
|
vec3 finalColor = ambient + (diffuse + specular) * NdotL;
|
|
|
|
|
2024-03-18 17:34:06 +01:00
|
|
|
|
|
|
|
float minBrightness = 0.001;
|
|
|
|
if (dot(finalColor, vec3(0.2126, 0.7152, 0.0722)) < minBrightness)
|
|
|
|
finalColor = mix(finalColor, vec3(1.0), minBrightness);
|
2024-03-18 14:21:18 +01:00
|
|
|
|
2024-03-18 17:34:06 +01:00
|
|
|
BrightColor = vec4(finalColor, 1.0);
|
2024-02-07 02:59:34 +01:00
|
|
|
outColor = vec4(finalColor, 1.0);
|
|
|
|
}
|