2
\$\begingroup\$

Assuming I define four vertices of a quad with texture coordinates that cover a whole texture or region of a texture, I can animate these coordinates by setting a transform using

SetTransform( D3DTS_TEXTURE0, &texTrans )

...scaling, translating etc.

If I render using a shader, and still want to animate the coordinates, presumably I can pass in the same transformation matrix and multiply the coordinates in the vertex shader?

Instead of in the vertex shader

Output.TextureUV = vTexCoord0; 

do

Output.TextureUV = mul( vTexCoord0, texTrans );

Is this a/the correct way to render an animated sprite with shader?

\$\endgroup\$
2
  • \$\begingroup\$ I think it is at least I do it the same way. Do you have a problem with this? \$\endgroup\$ Jun 19, 2014 at 17:13
  • \$\begingroup\$ @János Not really. Just wanted confirmation. I've just never seen an example of a HLSL shader that does this, not a lot of 2D stuff out there in general, really. I thought maybe that was because you could still use SetTransform in some way, but that seemed unlikely. Anyway, good to know this is correct. Thanks. \$\endgroup\$ Jun 20, 2014 at 10:41

1 Answer 1

4
\$\begingroup\$

I just stumbled back on this question, so I figure I should supply an answer. :)

The shader code I wrote in the question is almost right. I forgot that to convert a point with these transformations, the point needs to be in homogeneous coordinates.

See: https://stackoverflow.com/questions/5066532/how-to-multiple-a-2d-point-with-a-4d-matrix

So the correct vertex shader code would be:

Output.TextureUV = mul( float4( vTexCoord0, 0.0f, 1.0f ), texTrans );
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .