How to draw 2D polygon texture by using Vertex.

Hi. I want to draw a polygon by using VertexPositionTexture and BasicEffect to fill a texture. However, I can’t get right result. I need the texture to be static for all polygon. Here is an example:


How can I get such result?
And yet, I would like to know how can I manipulate the texture of polygon? How to change the origin of texture and angle of imposition on polygon?

Assuming that the initial UV maps to a texture within the [0,1] range, you could multiply the UVs with a transform matrix (translate,scale & rotate). If you want to update this on every frame you could later write a shader to do this in the GPU.

Make sure you are using TextureAddressMode.Wrap for rendering.

Yes, probably i need to make a shader. Too bad I’ve just begun to learn HLSL. Maybe there is a ready example somewhere? I couldn’t find what I need in internet. Either an example wasn’t compatible with MonoGame or vertex position is set in a weird algorithm (a triangle with positions of -1 to 1 on all the screen). I know that a texture in vertex draws by triangles, but not by quadrilaterals. Because of that a texture on polygon within more than four vertexes acts very weird. But I need a texture only to fill a polygon.

Don’t try to solve two problems at once. Start with DrawUserPrimitives/DrawIndexedUserPrimitives and a VertexPositionTexture.
Create a transform matrix , for example Matrix.CreateScale() and transform all your vertices with Vector2.Transform().
If you are going to update the texture transform regulary for a lot of polygons and this becomes a perf botleneck move it to a shader.
If it’s a one time them move the vertices to a VertexBuffer.

Start with a simple quad/rectagle and UV (0,0)(0,1)(1,1)(1,0) to test that your transforms work as you want them.
You can map a flat polygon by copying the position.XY to the UV and then divide by the max UV values to bring it back in the [0,1] range.

1 Like