PDA

View Full Version : GLSL black ring around object when using PBR lighting

Atemboi
05-14-2017, 02:18 PM
Hi, I'm creating a physically based lighting solution in my engine, and I've come across an issue where there's a black ring around the object from the light.

I'm using this code in the fragment shader:

vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);

}

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 nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;

return nom / denom;
}

float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;

float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;

return nom / 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;
}

float CalculateAttenuation(vec3 lp, float lr, vec3 FragPos) {
float distance = distance(lp, FragPos);

float saturated = pow(clamp(1 - pow(distance / lr, 4), 0.0, 1.0), 2);

return saturated / (pow(distance, 2) + 1);
}
void main()
{
vec3 N = normalize(Normal);
vec3 V = normalize(camPos - FragPos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
vec3 L = normalize(lightPos - FragPos);
vec3 H = normalize(V + L);
vec3 Lo = vec3(0.0);
float distance = length(lightPos - FragPos);
//float attenuation = 1 / (distance * distance);
//vec3 radiance = vec3(500.0f,500.0f,500.0f) * attenuation;
vec3 radiance = (vec3(500.0f,500.0f,500.0f) * CalculateAttenuation(lightPos,100,FragPos) * 5);

float NDF = DistributionGGX(N,H,roughness);
float G = GeometrySmith(N,V,L,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 nominator = NDF * G * F;
float denominator = 4 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.01;
vec3 specular = nominator / denominator;