Jump to content
  • Advertisement
Sign in to follow this  

[SOLVED] Textures with animation meshes and pixel shaders in HLSL

This topic is 3630 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

Hi ya, I've just got animation working with HLSL in C#/DirectX (Thanks to the awesome responses on this forum) and now what I'm trying to do is get it working with a custom pixel shader. The example I have uses the default pixel shader. What i've previously done with a static mesh is use a sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;}; Then reference it in my pixel shader like: Output.Color = (tex2D(ColoredTextureSampler, PSIn.TexCoords)*(some custom code to shade my model correctly to my light); Now I've tried looking into it and all I can work out is that the animated vertex shader is pulling in the texture colour via BlendWeights or/and BlendIndices in the VS_INPUT struct: struct VS_INPUT { float4 Pos : POSITION; float4 BlendWeights : BLENDWEIGHT; float4 BlendIndices : BLENDINDICES; float3 Normal : NORMAL; float3 Tex0 : TEXCOORD0; }; struct VS_OUTPUT { float4 Pos : POSITION; float4 Diffuse : COLOR; float2 Tex0 : TEXCOORD0; }; Nothing really offers a beginners explanation as to what they actually are and how I could effect the colour of the mesh with my light position and colour. So my question is, how does the pixel shader know what the texture is or what the pixel colour value is in an animated mesh and how could I edit that value? Thanks guys! [Edited by - hotpixel on July 16, 2008 7:37:35 AM]

Share this post

Link to post
Share on other sites
Sorry guys I'm being a prat!

I added a;

Texture ColoredTexture;

In the HLSL, stuck the meshes texture in it then used the:

Output.Color = (tex2D(ColoredTextureSampler, PSIn.Tex0);

To display the animated meshes texture and it works a dream ;)!

Share this post

Link to post
Share on other sites

[Edit] I really gotta learn to type faster; feel free to disregard all the following [grin]

I'm not entirely sure what you're asking, but I think you're referring to that sample by Steve Lanuit I linked to. It doesn't use a pixel shader, so it depends on the fixed function pipeline to apply the texture on the per-vertex lit geometry. This fixed function pipeline (FFP) predates shaders and early cards had limited pixel shader functionality, hence the hybrid approach. I'm afraid I can't tell you much about that approach, since I got started on shader model 2, by which time this wasn't used much anymore.

Anyway, the key lies in the technique definition, where the vertex- & pixelshaders are defined for the effect (line 110-117). Here you'll see the pixel shader is commented out, which is equivalent to setting it to NULL and thus telling DirectX to use the FFP. The shader does come with a rudimentaty pixel shader, but even though it accepts the VS_OUTPUT, it only outputs a solid color (lines 99-105). To make it sample a texture, uncomment the PixelShader line in the technique and replace the ps_skinned function with something like this:

[source lang=hlsl]
texture Texture;

sampler Sampler = sampler_state
Texture = (Texture);

MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;

float4 ps_skinned(VS_OUTPUT input) : COLOR0
float4 color = tex2D(Sampler, input.Tex0);

// sorry, sadly I don't know a nicer way
// to do component-wise multiplications
color.r *= input.Diffuse.r;
color.g *= input.Diffuse.g;
color.b *= input.Diffuse.b;

return color;

Unfortunately I don't have MDX set up on this machine, so I can't check if the code above works or even compiles. It should give you a basic idea to use a pixel shader in this sample. Note that it still uses per-vertex lighting, so if you want to switch to per-pixel lighting, you'd have to pass the final vertex position to the pixel shader and perform the lighting calculations there (basically lines 82-83). Note: you may need to replace ps_1_1 on line 115 with ps_1_4, to compile against newer shader model to accomodate the hight number of instructions.

Reviewing this sample project, it may be a bit messy and not too well suited to the current hardware standards. Since it uses shader model 1.1 (vs_1_1), it's rather limited in the number of bones it can support and uses that ackward array of vertex shaders to optimized for a given number of bone influences per vertex. At a quick glance, I'd say these lines could be changed to support characters with more bones and influences per bone:

Line 12: static const int MAX_MATRICES = 50; // Limits # of bones that can be set
Line 114: VertexShader = compile vs_2_0 VShade(4); // Default to 4 bone influences, SM2 more registers to allow for higher MAX_MATRICES count
Line 115: PixelShader = compile ps_2_0 ps_skinned(); // IIRC, vs_2_0 vertex shaders can only work with ps_2_0 pixel shaders

This last part is of course optional. Just drop a line if you should even want to discuss this further [smile]

Share this post

Link to post
Share on other sites
Lol! Thanks remigius, it does give a better explanation of what is ACTUALLY going on but I just had to apply a bit of logic which I am rapidly running out of as the day goes on and I try to master the mysteries of DirectX!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!