簡體   English   中英

Spritebatch中的MonoGame / XNA繪制多邊形

[英]MonoGame / XNA Draw Polygon in Spritebatch

我目前正在用MonoGame編寫游戲,我需要繪制紋理形狀。 我想繪制具有4個角的多邊形。 我所有的渲染都是用spritebatch完成的。 我嘗試使用TriangleStripes,但它對我而言效果不佳,因為我沒有使用着色器的經驗,因此將其與其他spritebatch-rendercode混合使用似乎很復雜。 我想到的一個解決方案是繪制一個四邊形並使用texturecoordinats拉伸紋理。 這有可能嗎? 我找不到與spritebatch有關的東西。 是否有人有教程或知道如何歸檔我的目標?

提前致謝。

如果要以非矩形方式繪制紋理,則不能使用SpriteBatch 但是繪制多邊形也不難,您可以使用BasicEffect因此您不必擔心着色器。 首先設置您的頂點和索引數組:

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

而已! 這是一個非常簡短的介紹,我建議您嘗試一下,如果遇到麻煩,這里有大量的教程和信息,google是您的朋友。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM