2

I was having no end of trouble with this function, emitted by the shader designer in VS2012. If you look at the code below, you can see the difference between the VS Version and MY Version.

In the VS version, they do not take into account the texture, so the "shady" sides of any object lit only by ambient is just grey. In MY version, you can see I added the pixelcolor (which is from the texture) and then it works great. Since they take the pixelcolor into account for the diffuse lighting, I can't figure out why they wouldn't for the ambient.

Since I'm very new to 3D I don't want to assume I'm so clever and the VS team never tested this. And since its so fundamental, I'm wondering if I'm just missing something. Thoughts?

float3 LambertLighting(
float3 lightNormal,
float3 surfaceNormal,
float3 materialAmbient,
float3 lightAmbient,
float3 lightColor,
float3 pixelColor
 )
{
    // compute amount of contribution per light
    float diffuseAmount = saturate(dot(lightNormal, surfaceNormal));
    float3 diffuse = diffuseAmount * lightColor * pixelColor;

    // combine ambient with diffuse

   // VS Version:
   return saturate((materialAmbient * lightAmbient) + diffuse);

   // MY Version:
   return saturate((materialAmbient * lightAmbient * pixelColor) + diffuse);

}
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Dave
  • 1,521
  • 17
  • 31

1 Answers1

0

This line should already take in account the color from the texture:

float3 diffuse = diffuseAmount * lightColor * pixelColor;

Which is later mixed with the ambient lighting to get the correct result. In your version you are adding the texture color twice (once in diffuse and once in ambient). The VS version corresponds to various online examples of Lambertian lighting I have seen so that is definitely correct.

What might go wrong is that in your test setup the diffuse color is black or gray, which would make the pixel color look gray too.

Luca_Scorpion
  • 371
  • 4
  • 15