# What is wrong with my metallic PBR shader?

I feel like my light conservation math isn't correct, but I am not sure which part is wrong.

Is reflected light supposed to influence the diffuse light?

Does metalness only affect the reflected light or does it also affect ambient and diffuse light?

vec4 falbedo; // surface color
vec3 fnormal; // surface normal

vec3 ambientLight;

vec3 directionToCamera; // Camera normal
vec3 directionToLight; // Directional light normal
vec3 lightColor; // Directional light color

float metalness; // 0-1
float shininess; // 1-256
float subsurface; // 0-1

// Diffuse light
float lightNormalDot = dot(fnormal, directionToLight);
float lightIntensity = max(.0, lightNormalDot);

// Blinn–Phong reflection
vec3 halfway = normalize(directionToCamera + directionToLight);
float reflectionFactor = max(.0, dot(halfway, fnormal));

float reflection = pow(reflectionFactor, shininess);

vec3 res = falbedo.rgb * ambientLight;
res += subsurface * lightIntensity * lightColor * falbedo.rgb;
res += (1. - subsurface) * reflection * mix(lightColor, lightColor * falbedo.rgb, metalness);

gl_FragColor = vec4(res, falbedo.a);

• i'm not sure what you mean by "reflected light". however, ambient light is just a simple approximation for global illumination. it should be a simple value added without anny effect of reflection or viewer position taken into account. therefore, metalness does not affect the ambient light (unless your metalness affects your albedo, which i can't see int he code)
– Tare
Apr 21, 2020 at 12:23
• @Tare I should edit the question so it's clear that I'm talking about directional light. The ambient light is complementary Apr 22, 2020 at 15:56