rotating uv's

Started by
10 comments, last by Halsafar 18 years, 9 months ago
I want to rotate a texture on a plane . . . so i'm trying to rotate the uv coordinates. To achieve that, i want to conver the cartesian uv coordinates into polar coordinates, changing the angle and then converting them back into cartesian coordinates . . . this is my code (at the moment i'm not changing the angle - so the formula shouldn't change anygthing - but unfortunately it does): float angle = (atan((Input.uvcoordinate.y-0.5)/(Input.uvcoordinate.x-0.5))+PI*(step(Input.uvcoordinate.x-0.5,0))*sign(Input.uvcoordinate.y-0.5))/(2*PI); float radius = sqrt(pow(Input.uvcoordinate.x-0.5,2)+pow(Input.uvcoordinate.y-0.5,2)); Output.uvcoordinate = float2(radius*cos(angle)+0.5, radius*sin(angle)+0.5); Can someone help me with that?
Advertisement
Is there any reason that you're not using the texture matrix for this? In case you weren't aware, you can set the texture matrix for the texture stage to a rotation matrix, which will result in the texture coordinates being rotated
uhm - could you explain me that?

im still beginner . . . what is a texture matrix? The texturescoordinates are just a 2D vector aren't they?
I would just keep track of the current angle of the coordinates, update those angles every frame, then rotate the cartesian vectors by that angle.
Since someone asked about texture matrices yesterday, here's the link.

You can apply a transformation (translation, rotation, etc.) to your UVs inside the D3D pipeline, rather than modifying vertices. The advantage is that on modern cards, you just put you original vertices in static on-card buffers, this saves tons of AGP bandwidth, and will render faster.

The hardware T&L if you're using the fixed pipeline, or your vertex shader if you're using shaders, can transform the UVs by a matrix as it's processing your vertices for other things, such as light, etc. Just tell D3D what the matrix is, tell it you want to transform texture coordinates, and then draw.
And how can I implement this transformation in my HLSL code??

unfortunately i'm "just" the artist . . . modeling 3D stuff and writing some HLSL shaders for them - but i've absolutely no indepth clue of a 3D API . . .
now i'm trying that:

float2x2 rotation = {cos(time), -sin(time),
sin(time), cos(time)};

Output.uvcoordinate = dot(rotation, Input.uvcoordinate.xy);




but it also doesn't work . . . just getting the error

error X3017: 'dot': cannot implicitly convert from 'float2x2' to 'const float1'
You'll want the matrix to be a float3x2 if you're going to include translation at some point. If it's just rotation then float2x2 is fine.

You want to use mul, not dot. MUL is used to multiply a matrix by a matrix, or a vector by a matrix. DOT is used for dot products of 2 vectors, and always produces a single float output.

Output.UV = mul(IN.coord0, rotation);

if you opt for float3x2 and do translation too, you'll need this tag a 1.0 on the end of the UV coordinate for it to work correctly.
Output.UV = mul(float3(IN.Coord0.x, IN.coord0.y, 1), rotation);

(I haven't tested the above snippets...)
yeah! it worked! thank you so much!


if someone could explain me that d3d rotation stuff anyways, i'd really appreciate it!
Excuse me, but I'm poised with the same delima.
It seems if I want to use a HLSL shader then I cannot use a TextureMatrix to rotate my textures.


So where do I modify the tu,tv coords? before the pixel shader I would assume?

This topic is closed to new replies.

Advertisement