MonoGame / XNA在Spritebatch中绘制多边形

问题描述 投票:1回答:1

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

提前致谢。

c# xna monogame
1个回答
3
投票

如果要以非矩形方式绘制纹理,则无法使用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);
}

而已!这是一个非常简短的介绍,我建议你玩这个,如果你被困在那里有大量的教程和信息,谷歌是你的朋友。

© www.soinside.com 2019 - 2024. All rights reserved.