Jump to content
  • Advertisement
Sign in to follow this  

[HLSL] Passing through vertex data

This topic is 4505 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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!