Sign in to follow this  
Psilobe

HLSL Diffuse shader

Recommended Posts

Psilobe    193
I'm trying to create a basic diffuse shader but the something goes wrong, instead of my little teapot getting diffuse it gets pitch black.
The shader compiles without error. I've compared my code to multiple example diffuse shaders but cant figure out what's wrong. I'm writing the shader in rendermonkey.


[code]float4x4 view_proj_matrix;
float4 Light1_Position;
float4x4 inv_view_matrix;

struct VS_INPUT
{
float4 Position : POSITION0;
float4 Normal: NORMAL0;
};

struct VS_OUTPUT
{
float4 Pos: POSITION;
float4 Nor: TEXCOORD0;
float3 LightVector: TEXCOORD1;
};

VS_OUTPUT vs_main(VS_INPUT Input)
{
VS_OUTPUT Out;

Out.Pos = mul(view_proj_matrix, Input.Position);
Out.Nor = normalize(Input.Normal);
Out.LightVector = normalize(Light1_Position-Input.Position);

return Out;
}
[/code]

[code]float4 Light1_Color;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float4 Nor: TEXCOORD0;
float3 LightVector: TEXCOORD1;
};

float4 ps_main(VS_OUTPUT Input) : COLOR
{

float4 diffuse = dot(Input.Nor,Input.LightVector);
return (Light1_Color*diffuse);

}

[/code]

Share this post


Link to post
Share on other sites
mind in a box    887
[quote name='myers80' timestamp='1311436524' post='4839301']
You need to also transform the input normal by the (3x3) of the view transformation.
[/quote]

No, he doesn't. He could try that, though. That is only needed when he would do the other stuff in view-space as well. But since neither "LightVector" nor "Position" look like viewspace I don't think that he has to.

I think the issue is that you "Input.Nor" is a float4, while "Input.LightVector" is a float3.
Also you seem to be doing alphablending because the results of the dot() go directly into the float4 you output.

I suggest to change the shader to the following:
[code]
float4 ps_main(VS_OUTPUT Input) : COLOR
{

float3 diffuse = dot(Input.Nor.xyz, Input.LightVector);
return (Light1_Color.rgb * diffuse, 1);

}
[/code]

[font="Arial"]You also don't really need to have your normals as a float4. Float3 is sufficient. (Remember that you can use "[/font][font="Arial"].xyz[/font]"[font="Arial"] after the normalize() to get only the first 3 values)
Same goes for "Light1_Color". You don't need the Alpha-Channel there.
[/font]

Share this post


Link to post
Share on other sites
Psilobe    193
Ok so now the model is white all around. It should be more like a beige color if done correctly. I've tried switching things around to test but no matter what I do it stays white. I believe I'm doing something else wrong and missing something basic for shading.

But thanks for the help so far. :)


[code]

float4 Light1_Color;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float4 Nor: TEXCOORD0;
float3 LightVector: TEXCOORD1;
};

float4 ps_main(VS_OUTPUT Input) : COLOR0
{

float3 diffuse = clamp(dot(Input.Nor.xyz,Input.LightVector),0,1);
return (Light1_Color.xyz*diffuse,1);

}

[/code]

Share this post


Link to post
Share on other sites
mind in a box    887
So, for the next steps I would suggest checking your normals and the LightVector.

At first you should try to modify your Dot() so that it uses a hardcoded and know light-vector:
dot(Input.Nor.xyz,float3(1,1,1))If that looks good you know that the problem is you LightVector. If not you should check the normals.
For them you can simply do "return float4(Input.Nor.xyz, 1);" as first line in your shader. x is then red, y is green, z is blue. If that looks somewhat right it's okay (Whole mesh has to be colorful :) ).

PS: I noticed you added that Clamp(). Just a little hint: In HLSL "saturate(x)" is the same as "clamp(x, 0, 1)".

Share this post


Link to post
Share on other sites
Psilobe    193
Thanks for the tip about saturate, anyway I tried both testing the normal and light vector and it seems to be the normal that's wrong.When trying [color=#1C2837][size=2]"return float4(Input.Nor.xyz, 1); it was still white.[/size][/color]

Share this post


Link to post
Share on other sites
Psilobe    193
Thanks for the help,, I think it was that I lacked an entry for the normal in the stream map. Will have to try tomorrow but that was probably the error.

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