2024-01-31 16:09:43 +01:00
|
|
|
#version 430 core
|
2024-02-03 21:03:12 +01:00
|
|
|
#define PI 3.14159265359
|
2024-01-31 16:09:43 +01:00
|
|
|
|
2024-03-18 14:21:18 +01:00
|
|
|
float AMBIENT = 0.2;
|
2024-01-31 16:09:43 +01:00
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
uniform vec3 albedo;
|
|
|
|
uniform float metallic;
|
|
|
|
uniform float roughness;
|
2024-01-31 16:09:43 +01:00
|
|
|
uniform vec3 color;
|
|
|
|
uniform vec3 lightPos;
|
2024-02-03 21:03:12 +01:00
|
|
|
uniform vec3 viewPos;
|
|
|
|
uniform vec3 ambientColor;
|
2024-01-31 16:09:43 +01:00
|
|
|
uniform sampler2D colorTexture;
|
|
|
|
|
|
|
|
in vec3 fragNormal;
|
|
|
|
in vec3 fragPosition;
|
|
|
|
in vec2 texCoords;
|
|
|
|
|
|
|
|
out vec4 outColor;
|
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
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-01-31 16:09:43 +01:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec3 normal = normalize(fragNormal);
|
2024-02-03 21:03:12 +01:00
|
|
|
vec3 viewDir = normalize(viewPos - fragPosition);
|
|
|
|
vec3 lightDir = normalize(lightPos - fragPosition);
|
2024-01-31 16:09:43 +01:00
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
vec3 F0 = vec3(0.04);
|
|
|
|
F0 = mix(F0, albedo, metallic);
|
2024-01-31 16:09:43 +01:00
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
float NdotL = max(dot(normal, lightDir), 0.0);
|
|
|
|
float NdotV = max(dot(normal, viewDir), 0.0);
|
2024-01-31 16:09:43 +01:00
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
vec3 H = normalize(lightDir + viewDir);
|
|
|
|
float NdotH = max(dot(normal, H), 0.0);
|
|
|
|
float LdotH = max(dot(lightDir, H), 0.0);
|
|
|
|
vec3 albedo = texture(colorTexture, texCoords).rgb;
|
2024-03-18 14:21:18 +01:00
|
|
|
|
|
|
|
float saturationFactor = 2.0;
|
|
|
|
albedo = mix(vec3(dot(albedo, vec3(0.2126, 0.7152, 0.0722))), albedo, saturationFactor);
|
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
albedo *= 2.0;
|
|
|
|
vec3 specular = F0 * (specularD(NdotH, roughness) * specularF(LdotH, F0) * specularG(NdotL, NdotV, roughness)) / (4.0 * NdotL * NdotV);
|
2024-01-31 16:09:43 +01:00
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
vec3 diffuse = (1.0 - F0) * albedo / PI;
|
2024-01-31 16:09:43 +01:00
|
|
|
|
|
|
|
vec3 ambient = ambientColor * AMBIENT;
|
|
|
|
|
2024-02-03 21:03:12 +01:00
|
|
|
vec3 finalColor = ambient + (diffuse + specular) * NdotL;
|
2024-01-31 16:09:43 +01:00
|
|
|
|
|
|
|
outColor = vec4(finalColor, 1.0);
|
|
|
|
}
|