84 lines
2.3 KiB
GLSL
84 lines
2.3 KiB
GLSL
#version 430 core
|
|
|
|
uniform vec3 albedoColor; // Kolor podstawowy (albedo)
|
|
uniform float metallic; // Wsp??czynnik metaliczno?ci
|
|
uniform float roughness; // Wsp??czynnik chropowato?ci
|
|
|
|
uniform vec3 lightPos;
|
|
uniform vec3 viewPos;
|
|
|
|
in vec3 FragPos; // Pozycja fragmentu
|
|
in vec3 Normal; // Normalna fragmentu
|
|
out vec4 FragColor;
|
|
|
|
// Obliczenie faktora odbicia ?wietlnego dla modelu PBR
|
|
float DistributionGGX(vec3 N, vec3 H, float roughness);
|
|
float GeometrySchlickGGX(float NdotV, float roughness);
|
|
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
|
|
|
|
void main()
|
|
{
|
|
vec3 N = normalize(Normal);
|
|
vec3 V = normalize(viewPos - FragPos); // Wektor widzenia
|
|
vec3 L = normalize(lightPos - FragPos); // Wektor do ?wiat?a
|
|
vec3 H = normalize(V + L); // Wektor po?owy
|
|
|
|
// Obliczenie faktora odbicia dla modelu PBR
|
|
float NDF = DistributionGGX(N, H, roughness);
|
|
float G = GeometrySmith(N, V, L, roughness);
|
|
vec3 F = vec3(0.04); // Aproksymacja Fresnel
|
|
|
|
// Obliczenie sk?adnika o?wietlenia dyfuzyjnego
|
|
vec3 kD = albedoColor.rgb / PI;
|
|
|
|
// Obliczenie sk?adnika o?wietlenia specularnego
|
|
vec3 kS = F * G * NDF / (4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0));
|
|
|
|
// Obliczenie o?wietlenia bezpo?redniego
|
|
vec3 irradiance = vec3(1.0); // Przyk?adowe o?wietlenie otoczenia
|
|
|
|
// Model o?wietlenia oparty na PBR
|
|
vec3 diffuse = kD * irradiance;
|
|
vec3 specular = kS * irradiance;
|
|
|
|
vec3 color = diffuse + specular;
|
|
FragColor = vec4(color, 1.0);
|
|
}
|
|
|
|
// Funkcje do oblicze? PBR
|
|
|
|
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
|
{
|
|
float a = roughness * roughness;
|
|
float a2 = a * a;
|
|
float NdotH = max(dot(N, H), 0.0);
|
|
float NdotH2 = NdotH * NdotH;
|
|
|
|
float num = a2;
|
|
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
|
denom = PI * denom * denom;
|
|
|
|
return num / denom;
|
|
}
|
|
|
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
|
{
|
|
float r = (roughness + 1.0);
|
|
float k = (r * r) / 8.0;
|
|
|
|
float num = NdotV;
|
|
float denom = NdotV * (1.0 - k) + k;
|
|
|
|
return num / denom;
|
|
}
|
|
|
|
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
|
{
|
|
float NdotV = max(dot(N, V), 0.0);
|
|
float NdotL = max(dot(N, L), 0.0);
|
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
|
|
|
return ggx1 * ggx2;
|
|
}
|