Sign in to follow this  
tokaplan

Default.fx 3ds Max vertex shader

Recommended Posts

tokaplan    122
Hi everyone, I've been browsing through 3ds Max 9 shaders found in maps/fx folder, and here is the vertex shader from default.fx effect file. It's pretty basic: // light direction (view space) float3 lightDir : Direction < string UIName = "Light Direction"; string Object = "TargetLight"; > = {-0.577, -0.577, 0.577}; // transformations float4x4 World : WORLD; float4x4 View : VIEW; float4x4 Projection : PROJECTION; float4x4 WorldViewProj : WORLDVIEWPROJ; float4x4 WorldView : WORLDVIEW; struct VS_OUTPUT { float4 Pos : POSITION; float4 col : COLOR0; }; VS_OUTPUT VS( float3 Pos : POSITION, float3 col : COLOR, float3 Norm : NORMAL, float2 Tex : TEXCOORD0) { VS_OUTPUT Out = (VS_OUTPUT)0; float3 L = lightDir; float3 P = mul(float4(Pos, 1),(float4x4)World); // position (view space) float3 N = normalize(mul(Norm,(float3x3)World)); // normal (view space) float3 R = normalize(2 * dot(N, L) * N - L);// reflection vector (view space) float3 V = normalize(P); // view direction (view space) Out.Pos = mul(float4(Pos,1),WorldViewProj); // position (projected) float4 Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient float4 Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4); // specular Out.col = Diff + Spec; return Out; } Well, I just don't understand. First we do this: float3 P = mul(float4(Pos, 1),(float4x4)World); And the comment says: // position (view space) How come we calculate view space vertex position by multiplying the model position by the world matrix? Wouldn't we get a world space position? Ok, maybe I'm wrong. Maybe we somehow do get a view space position through this transformation. But then how come Out.Pos = mul(float4(Pos,1),WorldViewProj); // position (projected) Now we multiply Pos by WorldViewProj matrix and get projection space position. This doesn't check out, because we already decided that World matrix gets us to view space, and we only have to further multiply by projection matrix to get to rojected space from there. Maybe the comment is wrong! Maybe what we meant to say was float3 P = mul(float4(Pos, 1),(float4x4)World); // position (WORLD space) But then how come we do this: float3 V = normalize(P); // view direction (view space) So that first comment about P couldn't possibly be wrong, becuase there's no way we can get a view direction based solely on world space vertex position, so we really DO mean to calculate view space position through World matrix alone. Can anyone please explain? I though I understood vertex shaders until now.

Share this post


Link to post
Share on other sites
Molle85    172
it looks kinda interesting...

my explanation would be,
either the effect is an old one, or that the positions and normals are already transformed before sent to the shader

Share this post


Link to post
Share on other sites
tokaplan    122
Well, it may be old, but it still has to work ;)

If Pos were transformed, there would be no need to transform it again, right?
Out.Pos = mul(float4(Pos,1),WorldViewProj); // position (projected)

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