# Adding material into light calculation?

This topic is 2116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

Happy as I was in getting my first VS/PS working with ambient and diffuse lighting, I'm now trying to add the ambient and diffuse material into the calculation. Been trying a lot of variants but now yet with succes.

Here's the calculation I'm doing:

float4      MatAmb : MATERIALAMBIENT;
float4      MatDiff: MATERIALDIFFUSE;

float3 DiffLightDir <  string UIDirectional = "Light Direction"; > = {1.0, 0.0, 0.0};
float4 DiffLightColor = { 1.0f, 1.0f, 1.0f, 1.0f };
float  DiffLightIntensity = 0.0f;

float4      AmbientColor;
float       AmbientIntensity;

VS_OUTPUT VS_function(VS_INPUT input)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

float4 worldPosition = mul(input.Pos, World);
Out.Pos = mul(worldPosition, ViewProj);

float4 normal = mul(input.Normal, WorldInvTransp);
float lightIntensity = dot(normal, DiffLightDir);

Out.Color = saturate(DiffLightColor * DiffLightIntensity * lightIntensity * MatDiff);
Out.Normal = normal;
Out.TexCoord = input.TexCoord;

return Out;
}

/****************************************************/
/****************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
textureColor.a = 1;

return saturate((input.Color + AmbientColor * AmbientIntensity) * textureColor);
}


What I thought was the solution for ambient, was:

return saturate((input.Color + AmbientColor * AmbientIntensity) * textureColor * MatAmb);

But the endresult is for each material 'black'/ dark, I'm 100% sure all vars are filled with valid values.

Can you give me some pointers or help me out?

##### Share on other sites

I have tried this, giving a bit better result but not correct:

float amb = AmbientColor * AmbientIntensity;

float diff = input.Color;

return (amb * MatAmb + diff * MatDiff) * textureColor;

Now I get the material color taken into account, but for example red light also lights up a material which is green

(i.e. light: RGB = 1, 0, 0, material ambient = 0,1,0)

##### Share on other sites

I think what you want to do is something like this

// Incoming radiance (this would be the color and intensity of the light energy
float3 Li = LightColor * LightIntensity;

// This would correspond to your LightIntensity (the cosine factor)
float NdotL = saturate(dot(Normal, LightDirection));

// Your final diffuse light output (or composition)
float3 Diffuse = NdotL * Li;

float3 AmbientLight = AmbientLight;

Diffuse += AmbientColor * AmbientIntensity;

// And at last multiply the result of your light computation by your material's
// diffuse color (albedo) -> basically the regular texture color
float3 output = Diffuse * Albedo;

Edited by lipsryme

##### Share on other sites

Hi, thanks. I got it, with the difference that I have a different diffuse and ambient material.
Part of the lighting calculation (diffuse light) is done in te Vertex shader, the rest in the pixel shader.

The working result now is:

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
textureColor.a = 1;

return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);
}


The only thing I don't understand is that splitting the formula up, doesn't give the correct result.
I tried it like this:

float amb = AmbientColor * AmbientIntensity * MatAmb
float diff = input.Color * MatDiff
return saturate(diff + amb) * textureColor

This doesn't work though, resulting pixels are black/ not as expected.
I tried to fund this by doing a pseudo formula:

return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);
equals
result = ((A * B + C * D * E) * F)

amb = C * D * E
diff = A * B

result = (A + B) * F

When I fill in fictive numbers for A though F, I get the same result calculating it in one formula as when I do it in with the amb/diff step in between.
Strange? or am I missing something

##### Share on other sites
Am I correct in thinking MatDiff is just a diffuse color coming from the material you've defined ?
If it is then you should multiply this with the texture color before you combine it with the lighting (input.Color).

Basically MatDiff and textureColor is the same "variable", being the final diffuse color of your material(not surface/pixel).
Which is then multiplied by the lighting.

If that's no different then maybe try to remove the saturate and see what happens...

The only way for it to be black is if your ambient term equals zero and your cosine factor (N dot L) or the material's diffuse color equals zero. Try to check if they are > 0.0f if not then either there's something wrong or it being completely black is the correct outcome :) Edited by lipsryme

##### Share on other sites
The only thing I don't understand is that splitting the formula up, doesn't give the correct result.

I tried it like this:

float amb = AmbientColor * AmbientIntensity * MatAmb
float diff = input.Color * MatDiff
return saturate(diff + amb) * textureColor

I think you mean for those types to be float3

##### Share on other sites

They're all actually float4 vars.

@Lipsryme; I understand what you mean, tried this:

// return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);

float amb = AmbientColor * AmbientIntensity * MatAmb * textureColor;

float diff = input.Color * MatDiff * textureColor;

return saturate(diff + amb);

This gives a nice black & white scene (looks really cool :)) but not the same result as the total formula.

Will play around to see if I can find it out.

By the way, wouldn't it in the end be faster performance wise, to have the 1 complete calculation? (instead of 2 and extra vars etc.)

##### Share on other sites
They're all actually float4 vars.

Then can you paste your *actual* code? Because what you've written there only says 'float', which is not a float4.

##### Share on other sites

Sorry, here it is.

Where the not commented calculation of the output color in the PixelShader works perfect and the commented one doesn't give the correct result:

float3 DiffLightDir <  string UIDirectional = "Light Direction"; > = {1.0, 0.0, 0.0};
float4 DiffLightColor = { 1.0f, 1.0f, 1.0f, 1.0f };
float  DiffLightIntensity = 0.5f;

float4x4    World         : WORLD;
float4x4    WorldInvTransp: WORLDINVTRANSP;
float4x4    ViewProj      : VIEWPROJECTION;

float4      AmbientColor;
float       AmbientIntensity;

float4      MatAmb : MATERIALAMBIENT;
float4      MatDiff: MATERIALDIFFUSE;

texture     Tex0 < string name = "textures\\wdplanks.png"; >;

VS_OUTPUT VS_function(VS_INPUT input)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

float4 worldPosition = mul(input.Pos, World);
Out.Pos = mul(worldPosition, ViewProj);

float4 normal = mul(input.Normal, WorldInvTransp);
float lightIntensity = dot(normal, DiffLightDir);

Out.Color = saturate(DiffLightColor * DiffLightIntensity * lightIntensity);
Out.Normal = normal;
Out.TexCoord = input.TexCoord;

return Out;
}

float4 PS_function(VS_OUTPUT input): COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TexCoord);
textureColor.a = 1;

return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);

float amb = AmbientColor * AmbientIntensity * MatAmb;
float diff = input.Color * MatDiff;

//    return saturate((diff + amb) * textureColor);
}


Input.color in de PX is the diffuse color component, as output of the VS.

The result when I uncomment the line is that the Material colors (MatAmb and MatDiff) are not visible in the result (just texture colours).

##### Share on other sites

So the problem is exactly what I suggested. Your 'amb' and 'diffuse' variables should be float4's, but they're floats.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632926
• Total Posts
3009239
• ### Who's Online (See full list)

There are no registered users currently online

×