我目前正在用 MonoGame 编写游戏,我需要绘制纹理形状。我想绘制有 4 个角的多边形。
我所有的渲染都是用 spritebatch 完成的。
我尝试使用 TriangleStripes,但它对我来说效果不佳,因为我对着色器没有任何经验,并且将其与 spritebatch-rendercode 的其余部分混合似乎很复杂。
我想到的一个解决方案是绘制一个四边形并使用纹理坐标拉伸(stretch)纹理。这有可能吗?我找不到与 spritebatch 相关的内容。
有没有人有教程或知道如何存档我的目标?

提前致谢。

最佳答案

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

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 是一个很棒的标准着色器,您可以使用它来绘制纹理、颜色,甚至对其应用照明。
VertexPositionTexture 是顶点缓冲区的设置方式。

如果您只想要颜色,则可以使用 VertexPositionColor,如果您想要两者都需要,则可以使用 VertexPositionColorTexture(用颜色为纹理着色)。
ind 数组说明您绘制组成四边形的两个三角形的顺序。

然后在渲染循环中执行以下操作:
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
{
    effectPass.Apply();
    device.DrawUserIndexedPrimitives<VertexPositionTexture>(
        PrimitiveType.TriangleList, vert, 0, vert.Length, ind, 0, ind.Length / 3);
}

就是这样!
这是一个非常简短的介绍,我建议您尝试一下,如果您遇到困难,这里有大量的教程和信息,谷歌是您的 friend 。

关于c# - MonoGame/XNA 在 Spritebatch 中绘制多边形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33136388/

10-17 02:36