2012-11-04 21 views
1

我有我似乎無法找到那個是更有效地我目前的想法有點當下不同的解決方案的問題。由隨機產生的線分開的紋理

我正在用XNA這將對移動沿着一個隨機生成的路徑的小遊戲,但我不知道該怎麼紋理分離的上方和下方線條,因爲線是隨機生成的。我希望本圖片說明問題:

enter image description here

所以線(S)隨機加入。我自己的想法是創建許多不同高度的矩形,並將它們放在一起,但我認爲這樣做效率有點低,並且也會迫使我的線條變得足夠厚,以致矩形可能會更寬一些比只有1-2像素,否則你可能會看到線附近的一些「emtpy」斑點。

如果有不清楚的地方,請隨時提問,我將它添加到這個問題,但我想我的問題應該是很清楚。

+0

你是如何表示線條? – neeKo

+0

這些線條將是小圖像(比如10x30像素),它們將以一個隨機角度旋轉並相鄰放置。 – phil13131

+0

不要加:旋轉的角度很可能是-0.2至0.2弧度和兩個adjecent線旋轉之間的角度差將不大於0.1弧度 – phil13131

回答

2

你可以取代你不需要透明像素的像素:

//extract pixel data from texture 
Texture2D topTexture = ... 
Color[] topTextureData = new Color[topTexture.Width * topTexture.Height]; 
topTexture.GetData<Color>(topTextureData); 

for(int x = 0; x < topTexture.Width; x++) 
{ 
    //depending on how you represent lines, set transparent all the pixels at and below line 
    //basically, for each x dimension, you find where the line is - you have to 
    //write the method for getting this y, as I don't know how you represent lines 
    int lineY = GetLineYAtThisX(x); 

    //all the pixels at (and below) the line are set transparent 
    for(int y = lineY; y < topTexture.Height; y++) 
    { 
     topTextureData[x + y * topTexture.Width] = Color.Transparent; 
    } 
} 

//save this data into another texture, so you don't ruin the original one. 
Texture2D maskedTopTexture = new Texture2D(GraphicsDevice, topTexture.Width, topTexture.Height); 
maskedTopTexture.SetData<Color>(topTextureData); 

你甚至不必爲下一個做到這一點,只畫上一個上面。

+0

非常好的想法,我希望電腦可以做得足夠快,保持30 fps – phil13131

1

一個解決這個問題是創建一個掩蔽紋理,將模糊選擇區域的紋理的外部。

mad skills incoming!

黑色區域將無法繪製。對其他紋理做相反的處理,你會得到你想要的。

我不知道如何與上spritebatch工作,但您的問題,一般的解決方案可能使用口罩。

+0

的問題是,我需要隨機創建這個面膜,究竟能我使用XNA來做到這一點? – phil13131

+0

有一種方法可以將渲染的紋理(或整個遊戲屏幕)保存到Texture2D。看看[這個問題的答案](http://stackoverflow.com/a/11943483/1306322) – user1306322

+0

但是,然後,我將需要創建適合線下的黑色紋理。這是整個問題,還是你有一些想法做到這一點?目前它聽起來有點複雜 – phil13131