Spritebatch中的MonoGame/XNA绘制多边形 [英] MonoGame / XNA Draw Polygon in Spritebatch

查看:118
本文介绍了Spritebatch中的MonoGame/XNA绘制多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用MonoGame编写游戏,我需要绘制纹理形状.我想绘制具有4个角的多边形. 我所有的渲染都是用spritebatch完成的. 我尝试使用TriangleStripes,但对我而言效果不佳,因为我没有使用着色器的经验,因此将其与其他spritebatch-rendercode混合起来似乎很复杂. 我想到的一种解决方案是绘制一个四边形并使用texturecoordinats拉伸纹理.这有可能吗?我找不到与spritebatch有关的东西. 有人有教程或不知道如何归档我的目标吗?

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?

谢谢.

推荐答案

如果要以非矩形方式绘制纹理,则不能使用SpriteBatch.但是绘制多边形也不难,您可以使用BasicEffect,因此您不必担心着色器. 首先设置您的顶点和索引数组:

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是很棒的标准着色器,您可以使用它来绘制纹理,颜色,甚至对其应用照明.

BasicEffect is great a standard shader, you can use it to draw textures, colors and even apply lighting to it.

VertexPositionTexture是设置顶点缓冲区的方式.

VertexPositionTexture is how your vertex buffer is set up.

如果只需要颜色,则可以使用VertexPositionColor;如果要两者都使用,则可以使用VertexPositionColorTexture(用颜色为纹理着色).

You can use VertexPositionColor if you only want colors and VertexPositionColorTexture if you want both (tinting the texture with a color).

ind数组表示以什么顺序绘制构成四边形的两个三角形.

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);
}

就是这样! 这是一个非常简短的介绍,我建议您尝试一下,如果遇到麻烦,这里有大量的教程和信息,google是您的朋友.

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.

这篇关于Spritebatch中的MonoGame/XNA绘制多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆