Rendering sprites using shaders?


20-08-2009 09:40:53

Hi i found this shader on net:

// Input parameters.
float2 ViewportSize : register(c0);
float2 TextureSize : register(c1);
float4x4 MatrixTransform : register(c2);
sampler TextureSampler : register(s0);

// Vertex shader for rendering sprites on Windows.
void SpriteVertexShader(inout float4 position : POSITION0,
inout float4 color : COLOR0,
inout float2 texCoord : TEXCOORD0)
// Apply the matrix transform.
position = mul(position, transpose(MatrixTransform));

// Half pixel offset for correct texel centering.
position.xy -= 0.5;

// Viewport adjustment.
position.xy /= ViewportSize;
position.xy *= float2(2, -2);
position.xy -= float2(1, -1);

// Compute the texture coordinate.
texCoord /= TextureSize;

// Pixel shader for rendering sprites (shared between Windows and Xbox).
void SpritePixelShader(inout float4 color : COLOR0, float2 texCoord : TEXCOORD0)
color *= tex2D(TextureSampler, texCoord);

and i passed the TextureSize getting the width,height of sprite to draw,
added 4 position,color and texture to the vertex buffer( top-left: x,y [100/100] top-right: x+width,y [356,100],bottom-right:x+width,y+height[356,228] and bottom-left:x,y+height [100,228] ),i also set the viewport size getting
the autoconstant of ViewportSize and the MatrixTransform as Matrix4.Identity,but i'm not getting any result,anyone knows why?

Anyone knows sprite batching using shader?

Please help me :(