2023-02-10 16:01:48 +01:00
|
|
|
|
#version 430 core
|
2023-02-08 12:36:24 +01:00
|
|
|
|
|
|
|
|
|
float AMBIENT = 0.03;
|
|
|
|
|
float PI = 3.14;
|
|
|
|
|
|
|
|
|
|
uniform sampler2D depthMap;
|
|
|
|
|
|
|
|
|
|
uniform vec3 cameraPos;
|
|
|
|
|
|
|
|
|
|
uniform vec3 color;
|
|
|
|
|
|
|
|
|
|
uniform vec3 sunDir;
|
|
|
|
|
uniform vec3 sunColor;
|
|
|
|
|
|
|
|
|
|
uniform vec3 lightPos;
|
|
|
|
|
uniform vec3 lightColor;
|
|
|
|
|
|
|
|
|
|
uniform vec3 spotlightPos;
|
|
|
|
|
uniform vec3 spotlightColor;
|
|
|
|
|
uniform vec3 spotlightConeDir;
|
|
|
|
|
uniform vec3 spotlightPhi;
|
|
|
|
|
|
|
|
|
|
uniform float metallic;
|
|
|
|
|
uniform float roughness;
|
|
|
|
|
|
|
|
|
|
uniform float exposition;
|
|
|
|
|
|
|
|
|
|
in vec3 vecNormal;
|
|
|
|
|
in vec3 worldPos;
|
|
|
|
|
|
|
|
|
|
out vec4 outColor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in vec3 viewDirTS;
|
|
|
|
|
in vec3 lightDirTS;
|
|
|
|
|
in vec3 spotlightDirTS;
|
|
|
|
|
in vec3 sunDirTS;
|
|
|
|
|
|
|
|
|
|
in vec3 test;
|
|
|
|
|
|
2023-02-10 16:01:48 +01:00
|
|
|
|
// wektor przechowujący punkt widzenia światła
|
|
|
|
|
in vec4 sunSpacePos;
|
|
|
|
|
|
|
|
|
|
// mapa przechowująca wartość cieni
|
|
|
|
|
uniform sampler2D shadowMap;
|
|
|
|
|
|
|
|
|
|
// zmienne przechowujące szerokość i wysokość mapy cieni
|
|
|
|
|
uniform float shadowMapWidth;
|
|
|
|
|
uniform float shadowMapHeight;
|
|
|
|
|
|
|
|
|
|
// zmienne do PCF - Percentage Closer Filtering
|
|
|
|
|
|
|
|
|
|
// promień pixeli - np. 2 oznacza badanie na szerokość 5 bo 2 + 1 + 2
|
|
|
|
|
const int pcfCount = 8;
|
|
|
|
|
|
|
|
|
|
// ilość wszystkich pixeli - (2 * promień + 1) ^ 2
|
|
|
|
|
const float totalTexels = (pcfCount * 2.0 + 1.0) * (pcfCount * 2.0 + 1.0);
|
|
|
|
|
|
|
|
|
|
|
2023-02-08 12:36:24 +01:00
|
|
|
|
float DistributionGGX(vec3 normal, vec3 H, float roughness){
|
|
|
|
|
float a = roughness*roughness;
|
|
|
|
|
float a2 = a*a;
|
|
|
|
|
float NdotH = max(dot(normal, 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 normal, vec3 V, vec3 lightDir, float roughness){
|
|
|
|
|
float NdotV = max(dot(normal, V), 0.0);
|
|
|
|
|
float NdotL = max(dot(normal, lightDir), 0.0);
|
|
|
|
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
|
|
|
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
|
|
|
|
|
|
|
|
|
return ggx1 * ggx2;
|
|
|
|
|
}
|
|
|
|
|
vec3 fresnelSchlick(float cosTheta, vec3 F0){
|
|
|
|
|
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){
|
|
|
|
|
float diffuse=max(0,dot(normal,lightDir));
|
|
|
|
|
|
|
|
|
|
//vec3 V = normalize(cameraPos-worldPos);
|
|
|
|
|
vec3 F0 = vec3(0.04);
|
|
|
|
|
F0 = mix(F0, color, metallic);
|
|
|
|
|
|
|
|
|
|
vec3 H = normalize(V + lightDir);
|
|
|
|
|
|
|
|
|
|
// cook-torrance brdf
|
|
|
|
|
float NDF = DistributionGGX(normal, H, roughness);
|
|
|
|
|
float G = GeometrySmith(normal, V, lightDir, roughness);
|
|
|
|
|
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
|
|
|
|
|
|
|
|
|
vec3 kS = F;
|
|
|
|
|
vec3 kD = vec3(1.0) - kS;
|
|
|
|
|
kD *= 1.0 - metallic;
|
|
|
|
|
|
|
|
|
|
vec3 numerator = NDF * G * F;
|
|
|
|
|
float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001;
|
|
|
|
|
vec3 specular = numerator / denominator;
|
|
|
|
|
|
|
|
|
|
// add to outgoing radiance Lo
|
|
|
|
|
float NdotL = max(dot(normal, lightDir), 0.0);
|
|
|
|
|
return (kD * color / PI + specular) * radiance * NdotL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 16:01:48 +01:00
|
|
|
|
// metoda obliczająca cień za pomocą PCF
|
|
|
|
|
|
|
|
|
|
float calculateShadow(vec4 lightPos, sampler2D Shadow_Map){
|
|
|
|
|
// ujednorodnienie pozycji światła
|
|
|
|
|
vec3 _lightPos = lightPos.xyz/lightPos.w;
|
|
|
|
|
|
|
|
|
|
// skalowanie z wartości (-1, 1) do wartości (0, 1)
|
|
|
|
|
_lightPos = _lightPos * 0.5 + 0.5;
|
|
|
|
|
|
|
|
|
|
// ustalenie wielości texeli
|
|
|
|
|
double TexelWidth = 1.0/shadowMapWidth;
|
|
|
|
|
double TexelHeight = 1.0/shadowMapHeight;
|
|
|
|
|
|
|
|
|
|
vec2 TexelSize = vec2(TexelWidth, TexelHeight);
|
|
|
|
|
|
|
|
|
|
// zmienna przechowująca wartość cienia
|
|
|
|
|
float shadowSum = 0.0;
|
|
|
|
|
|
|
|
|
|
// algorytm PCF
|
|
|
|
|
for (int y = -pcfCount ; y <= pcfCount ; y++) {
|
|
|
|
|
for (int x = -pcfCount ; x <= pcfCount ; x++) {
|
|
|
|
|
// liczymi wartości (x, y) z przesunięciem
|
|
|
|
|
vec2 Offset = vec2(x, y) * TexelSize;
|
|
|
|
|
|
|
|
|
|
// sprawdzenie wartości z dpeth map oraz przypisanie odpowiedniej wartości dla shadow
|
|
|
|
|
float shadow = _lightPos.z > texture(Shadow_Map, _lightPos.xy + Offset).r + 0.001 ? 1.0 : 0.0;
|
|
|
|
|
|
|
|
|
|
// dodanie wartości do shadow
|
|
|
|
|
shadowSum += shadow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// zwrócenie średniej wartości cieni z wszystkich pixeli wokół
|
|
|
|
|
return ((shadowSum/totalTexels)/lightPos.w);
|
|
|
|
|
}
|
2023-02-08 12:36:24 +01:00
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
//vec3 normal = vec3(0,0,1);
|
|
|
|
|
vec3 normal = normalize(vecNormal);
|
|
|
|
|
|
|
|
|
|
//vec3 viewDir = normalize(viewDirTS);
|
|
|
|
|
vec3 viewDir = normalize(cameraPos-worldPos);
|
|
|
|
|
|
|
|
|
|
//vec3 lightDir = normalize(lightDirTS);
|
|
|
|
|
vec3 lightDir = normalize(lightPos-worldPos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vec3 ambient = AMBIENT*color;
|
|
|
|
|
vec3 attenuatedlightColor = lightColor/pow(length(lightPos-worldPos),2);
|
|
|
|
|
vec3 ilumination;
|
|
|
|
|
ilumination = ambient+PBRLight(lightDir,attenuatedlightColor,normal,viewDir);
|
|
|
|
|
|
|
|
|
|
//flashlight
|
|
|
|
|
//vec3 spotlightDir= normalize(spotlightDirTS);
|
|
|
|
|
vec3 spotlightDir= normalize(spotlightPos-worldPos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1);
|
|
|
|
|
attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2);
|
|
|
|
|
ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir);
|
|
|
|
|
|
2023-02-10 16:01:48 +01:00
|
|
|
|
// obliczanie cienia
|
|
|
|
|
float shadow = calculateShadow(sunSpacePos, shadowMap);
|
2023-02-08 12:36:24 +01:00
|
|
|
|
|
2023-02-10 16:01:48 +01:00
|
|
|
|
//sun
|
|
|
|
|
// zaaplikowanie cienia do koloru światła.
|
|
|
|
|
ilumination = ilumination + PBRLight(sunDir,sunColor * (1.0 - shadow), normal, viewDir);
|
2023-02-08 12:36:24 +01:00
|
|
|
|
|
|
|
|
|
outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1);
|
|
|
|
|
//outColor = vec4(roughness,metallic,0,1);
|
|
|
|
|
//outColor = vec4(test;
|
|
|
|
|
}
|