Sign in to follow this  

[HLSL] Passing through vertex data

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm quite new to vertex shaders and so on, so forgive me if this is a stupid question. I'm trying to write a cel-shading vertex shader that modulates a cel-shading texture in texture unit 1 with an optional mesh texture in texture unit 0. The problem I have is with that word "optional" -- I haven't really understood/worked out how you cope with the fact that some meshes will have tex0 co-ordinates and some won't. Currently my vertex shader looks like this:
struct VS_INPUT
{
    float4 vPosition        : POSITION;
    float3 vNormal          : NORMAL;    
};

struct VS_OUTPUT
{
    float4 vPosition    : POSITION;
    float3 vDiffuse     : COLOR0;    
    float2 fTex1	: TEXCOORD1;
};

// Declare the required matrices with the appropriate semantics
//   so that the viewer can supply the necessary matrix information.
const float4x4 mWorldViewProj;

const float4 vLightDir;		// Light direction
const float4 vMatDiff;		// Material's diffuse component

//----------------------------------------------------------------------------
// Shader body 
//----------------------------------------------------------------------------
VS_OUTPUT main(const VS_INPUT v)
{
	VS_OUTPUT Output;
	
	// Project position
	Output.vPosition = mul(mWorldViewProj, v.vPosition);  

    // Do lighting calculation (Light.w = 0 for directional light, 1 for positional light)
    float fColor1 = dot(v.vNormal.xyz, normalize(vLightDir.w * v.vPosition.xyz - vLightDir.xyz));

    // Final color   
    Output.fTex1    = float2(fColor1, 0.5);
    Output.vDiffuse = vMatDiff;

    return Output;
}

That works fine if there are no textures, but the tex0 co-ordinates seem to be lost on meshes that had that information. I can also edit this shader so that VS_INPUT and VS_OUTPUT both contain tex0 co-ordinates that I explicitly copy across, but then it ceases to work for meshes that don't have that information. Is there an easy way to catch both cases? Surely I don't *really* have to write two separate vertex shaders, do I?? Is there no way to make it implicitly pass through any data that I don't actively alter? Thanks for any help, Andy

Share this post


Link to post
Share on other sites
Quote:
Original post by AndyGeers
Surely I don't *really* have to write two separate vertex shaders, do I??



Yes, I believe that's your only answer in this case.

It's easy for vertex shaders to ignore data that is present in the stream that they don't NEED.

However, if something is declared in your input structure as being present and it turns out that it's NOT present in the stream -- DirectX will complain and the DrawIndexPrimitve call will fail.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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