# [C#] [HLSL] How can I align textures to an axis?

This topic is 3033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a textured quadrangle with a normal of (0,0,-1) (facing "left").

How can I align the texture on that quadrangle to the "up" axis (0,1,0)?

This way when looking at the quadrangle (with the camera "up" = (0,1,0)) the texture would have the same orientation as when viewing it in software such as Paint. It shouldn't be rotated.

##### Share on other sites
Just a question of getting the texture coordinates correct. Assuming 0, 0 (u, v) is the top left of the texture, and you are looking directly at the quad.

0,0       1,0-------------|           ||           ||           ||           ||           |-------------0,1       1,1

You did mention HLSL... are you trying to generate the texcoords in the shader from the vertex position? If so, you can take the dot product of the local space positions of each vertex with the y axis (to find v) and the z axis (to find u).

##### Share on other sites
Quote:
 You did mention HLSL... are you trying to generate the texcoords in the shader from the vertex position? If so, you can take the dot product of the local space positions of each vertex with the y axis (to find v) and the z axis (to find u).

That is correct. I am using this formula:

    float3 v0 = input.Position.xyz - centre;    float widthInverse = 1.0f / width;    float heightInverse = 1.0f / height;        Output.TextureCoordinates.x = dot(v0, normalize(float3(right))) * widthInverse + 0.5f;    Output.TextureCoordinates.y = dot(v0, normalize(float3(up))) * heightInverse + 0.5f;

but it doesn't work and produces non axis aligned results.

I've also tried:

    float3 v0 = input.Position.xyz - centre;    float widthInverse = 1.0f / width;    float heightInverse = 1.0f / height;        Output.TextureCoordinates.x = dot(v0, float3(1,0,0)) * widthInverse + 0.5f;    Output.TextureCoordinates.y = dot(v0, float3((0,1,0)) * heightInverse + 0.5f;

but that doesn't work either. Any ideas as to why?

##### Share on other sites
Second one looks good since you need to take the dot product with the world axis.

However, if you are still testing using the left facing quad, you need to take the dot product with the z axis for the u texture coordinate. If the quad is front facing, and your coordinate system is y up, x left to right, what you have there should work fine.

[Edited by - 1863 on August 26, 2010 11:40:43 AM]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633732
• Total Posts
3013583
×