• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

Adding material into light calculation?

15 posts in this topic

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;
}

/****************************************************/
/**          THE PIXELSHADER        PROGRAM        **/
/****************************************************/

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?

0

Share this post


Link to post
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)

0

Share this post


Link to post
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
// that is coming towards your surface/shaded pixel)
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;

// Your ambient light term
float3 AmbientLight = AmbientLight;

// Now "add" the ambient light to your diffuse composition
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
0

Share this post


Link to post
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

0

Share this post


Link to post
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
0

Share this post


Link to post
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

0

Share this post


Link to post
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.)

0

Share this post


Link to post
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.

0

Share this post


Link to post
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).

0

Share this post


Link to post
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.

0

Share this post


Link to post
Share on other sites

Aah I get it, sorry, didn't see it right away.

Just changed it, and now it works :) thanks!

 

What would you think would be best performance wise, doing the separate calculations or doing it at once?

(like I did up till now)

0

Share this post


Link to post
Share on other sites
As far as I know, and someone correct me if I'm wrong but I don't see how it would create more operations for the hardware to do then doing it in a single line. And as far as the extra variable goes, it could be quite possible that the compiler sees this as redundant anyway and replaces it with the "single-line" approach. I'm not an expert on this though. Worst case scenario is having wasted a few bytes of graphics memory for those extra variables but having a much cleaner and readable code. But that's just me :)
0

Share this post


Link to post
Share on other sites

Thanks,I'm trying it right away.

Do get some errors though when checking it using 'fxc.exe' from the DX sdk:

 

- working.fx(70,28): warning X3206: 'dot': implicit truncation of vector type

- working.fx(73,16): warning X3206: 'implicit truncation of vector type

- error X3501: 'main' entrypoint not found

 

compilation failed; no code producted

 

Could this have to do with DX10/DX9 compilation for the FX/ shader?

I'm using DX9 and read that in the SDK the compilations will be done based on syntax/ compilation of DX10 (and 11?).

 

If I open the same file in effectedit (also DX SDK) I don't get these errors.

Wíll try to figure out if there's a warning level or something in effectedit.

 

By the way strange that D3DXCreateEffectFromFile gives back a D3D_OK and the result is also as expected..

Any hints how to pick this up?

0

Share this post


Link to post
Share on other sites
Sorry, being stupid, forgot to give the /T parameter with fx_2_0 (since i use d3d9). Will keep you posted on the output comparison of the 2 styles of calculations
0

Share this post


Link to post
Share on other sites

Good news, I got the compilation comparison done :)

Here are the 2 results (TXT files):

www.sierracosworth.nl/gamedev/output1.txt

www.sierracosworth.nl/gamedev/output2.txt

 

When I compare the results, both outputs are 100% the same.

Good to know this, so I can tweak the shader to how I can read and work with it best, without affecting the result/ performance.

 

Thanks again for your help.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0