Reputation: 22916
I'm creating flat vertex based geometry with the aim of creating 2D terrain for a 2D side scroller type game.
So far I can create the vertex buffer fine and draw whatever triangle to screen I want. However I have an issue with texturing. How can I texture the triangles using tiles (if it's at all possible even). I'm using the BasicEffect class.
What would a good algorithm be to set up UV data across all the vertices such that the texture will tile across all of them seamlessly n number of times?
My current idea is to get the left most and right most vertices (as well as top most, bottom most) deriving the dimensions of the entire geometry based on that, then going through each triangle and setting the UV of its vertices (based on the triangles width/height) to a value that it will use a bit of the texture such that it will blend into the next one (i.e. one vertex UV would be 0.1, 0.3 and the next would be 0.2, 0.4).
This seems a bit complicated and has a HUGE draw back, the size of the tile must be larger than the size of the largest triangle face but it's the only way I can think of doing it. I've tried googling for a solution or at least someone else who did the same thing the way I have but came up with nothing.
Upvotes: 0
Views: 1063
Reputation: 47751
Yes, you will have to find an algorithm to correctly set up your tex coords ... that's unfortunately a function of what your textures and your geometry looks like so I'm not sure how well we'd be able to help.
As far as the technical bit, You should be able to set the TextureAddressMode
to Wrap
and the GPU will take care of sampling the texture accordingly to tile it based on the UV.
graphics.GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
graphics.GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
Upvotes: 3