2015-10-14 140 views
1

我目前正在使用MonoGame編寫遊戲,我需要繪製紋理形狀。我想繪製四角的多邊形。 我所有的渲染圖都是用spritebatch完成的。 我嘗試過使用TriangleStripes,但它對我來說效果並不好,因爲我沒有任何着色器經驗,並且將其與spritebatch-rendercode的其餘部分進行混合似乎很複雜。 我想到的一個解決方案是繪製四邊形並用texturecoordinats繪製紋理。這可能以某種方式嗎?我無法找到與spritebatch有關的事情。 有沒有人有教程或知道如何存檔我的目標?MonoGame/XNA在Spritebatch中繪製多邊形

在此先感謝。

+0

如果您設置的目標矩形大於源矩形,則使用spritebatch將拉伸紋理。如果您只想渲染其中的一部分,您還可以設置不覆蓋整個紋理的源矩形。你是否需要使用textrued?或者你想達到什麼目的? –

+0

是的,紋理也應該傾斜,這是我的問題。 – mick

回答

2

如果你想畫你的紋理中,你不能使用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); 

       } 

這就是它! 這是一個非常簡短的介紹,我建議你玩這個,如果你陷入困境,這裏有大量的教程和信息,谷歌是你的朋友。

+0

感謝您的評論。我不知道基本效果。我見過的所有教程都使用了自定義着色器,而monogame無法編譯它們 – mick