Sign in to follow this  
giugio

shader and matrix transform

Recommended Posts

hy, i have this PART OF shader:
PS_INPUT VS( VS_INPUT input )
{
    
    PS_INPUT output = (PS_INPUT)0;
   

    output.Pos = mul(input.Pos,worldMatrix);
    output.Pos = mul( output.Pos, ViewMatrix );
    output.Pos = mul( output.Pos, projMatrix );
     
    output.normal=mul(input.Normal,worldMatrix);
	output.normal=normalize(mul(output.normal,ViewMatrix));
    output.normal=normalize(mul(output.normal,projMatrix));
      
    return output;
}


float4 PS( PS_INPUT In) : SV_Target
{
   float4 colLight = float4(1, 1, 1, 1);
   float4 MaterialColor = float4(0, 1, 0, 1);
   float3 lightDirection = float3(0,0,-21);
   float3 L = normalize(lightDirection.xyz);
   float4 D = saturate( dot(In.normal,L) * colLight * MaterialColor);
   return D;

 }

i disabled the cull mode but some faces appear black , i think is a normal problem , is correct to multiply the normal in the VS for the word , view and projection matrix? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by giugio
hy, i have this PART OF shader:

*** Source Snippet Removed ***

i disabled the cull mode but some faces appear black , i think is a normal problem , is correct to multiply the normal in the VS for the word , view and projection matrix?

Thanks.


You shouldn't be transforming a normal at all, it's just a directional vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by deadstar

You shouldn't be transforming a normal at all, it's just a directional vector.


That is not true, your normal needs to be transformed by the rotational portion of the transformation matrix. There are many threads on gamedev and else where discussing how to do this.



Share this post


Link to post
Share on other sites
Quote:
Original post by DieterVW
Quote:
Original post by deadstar

You shouldn't be transforming a normal at all, it's just a directional vector.


That is not true, your normal needs to be transformed by the rotational portion of the transformation matrix. There are many threads on gamedev and else where discussing how to do this.


Ah of course, my mistake.

Don't listen to me giugio, it's been a long day ;)

Share this post


Link to post
Share on other sites
output.normal=mul(input.Normal,(float3x3)worldMatrix); <- if you use float3x3 this uses just the rotation part of the matrix, this is what you need

This is all you need. Dont multiply your normal by the view-proj matrix at all, only the world transform is needed. And in case your input normal isnt normalized, normalize it.

Share this post


Link to post
Share on other sites
Quote:
Original post by n3Xus
output.normal=mul(input.Normal,(float3x3)worldMatrix); <- if you use float3x3 this uses just the rotation part of the matrix, this is what you need

This is all you need. Dont multiply your normal by the view-proj matrix at all, only the world transform is needed. And in case your input normal isnt normalized, normalize it.


This only works if your transformation matrix contains no scaling.

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