问题
i am currently writing a game with MonoGame and I need to draw textured shapes. I want to draw polygons with 4 corners. All my renderstuff is done with the spritebatch. I tried to use TriangleStripes, but it does not work very well for me, because I don't have any experience with shaders and mixing this with the rest of the spritebatch-rendercode seems complicated. One solution i thought about was to draw a quad and strech the texture with texturecoordinats. Is this possible somehow? I can't find something about this in relation to the spritebatch. Does anyone have tutorials or knows how to archive my goal?
Thanks in advance.
回答1:
If you want to draw your texture in a non-rectangular way you can't use SpriteBatch
. But drawing polygons isn't hard either and you can use BasicEffect
so you don't have to worry about shaders.
Start by setting up your Vertex and Index arrays:
BasicEffect basicEffect = new BasicEffect(device);
basicEffect.Texture = myTexture;
basicEffect.TextureEnabled = true;
VertexPositionTexture[] vert = new VertexPositionTexture[4];
vert[0].Position = new Vector3(0, 0, 0);
vert[1].Position = new Vector3(100, 0, 0);
vert[2].Position = new Vector3(0, 100, 0);
vert[3].Position = new Vector3(100, 100, 0);
vert[0].TextureCoordinate = new Vector2(0, 0);
vert[1].TextureCoordinate = new Vector2(1, 0);
vert[2].TextureCoordinate = new Vector2(0, 1);
vert[3].TextureCoordinate = new Vector2(1, 1);
short[] ind = new short[6];
ind[0] = 0;
ind[1] = 2;
ind[2] = 1;
ind[3] = 1;
ind[4] = 2;
ind[5] = 3;
BasicEffect
is great a standard shader, you can use it to draw textures, colors and even apply lighting to it.
VertexPositionTexture
is how your vertex buffer is set up.
You can use VertexPositionColor
if you only want colors and VertexPositionColorTexture
if you want both (tinting the texture with a color).
The ind
array says in what order you draw the two triangles making up the quad.
Then in your render loop you do this:
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
{
effectPass.Apply();
device.DrawUserIndexedPrimitives<VertexPositionTexture>(
PrimitiveType.TriangleList, vert, 0, vert.Length, ind, 0, ind.Length / 3);
}
That's it! This is a very brief introduction, I suggest you play around with this and if you get stuck there's a ton of tutorials and information on this, google is your friend.
来源:https://stackoverflow.com/questions/33136388/monogame-xna-draw-polygon-in-spritebatch