2017-07-24 110 views
1

我有一個Texture2D的數組,它由webcamtexture幾秒鐘填充。這部分工作,所有50左右的框架顯示在檢查員罰款。setPixels32()統一創建空白紋理

現在我們通過調用.GetPixels32()來修改這些圖像以獲得每幀的Color32[]。作爲一個例子,我們可以嘗試將每個通道設置爲當前幀與幀之間的每個像素的Math.Max(實質上是「淡化」混合),並用新修改的紋理替換陣列中的紋理。 (這個代碼從第二幀開始,問題是不能從數組出界)

Color32[] previousColors = previousFrame.GetPixels32(); 
    Color32[] currentColors = currentFrame.GetPixels32(); 
    Color32[] outColors = new Color32[colors.Length]; 
    int i = 0; 
    while (i < currentColors .Length) 
    { 
     outColors[i].b = Math.Max(previousColors[i].b, currentColors[i].b); 
     outColors[i].r = Math.Max(previousColors[i].r, currentColors[i].r); 
     outColors[i].g = Math.Max(previousColors[i].g, currentColors[i].g); 
    } 
    bufferedPics[frameIndex].SetPixels32(outColors); 
    bufferedPics[frameIndex].Apply(); 

這裏的問題出現時:在檢查器中,陣列中的幀中的所有現在顯示爲空白。但點擊它們右側的圓圈,它會顯示經過適當修改的減輕框架。試圖在其他地方使用這些框架,它們顯示出空白和透明。

+0

你能告訴你如何編輯像素嗎?也許是一個示例操作(例如將子區域設置爲藍色)。請注意,由於您構建了一個新陣列,因此所有像素都設置爲透明黑色。 –

+0

請給出您的問題措辭和格式化一些更多的努力,讓其他用戶更容易理解您的問題並提供答案。 – Xaser

+0

@Xaser抱歉格式化,這是我在這裏提出的第一篇文章。感謝您的修復。 – v8ntage

回答

1

Color32四個通道:紅色,綠色,藍色和阿爾法。默認情況下,alpha是0(意思是透明的)。您必須將Alpha設置爲以使圖像不透明。

Color32[] previousColors = previousFrame.GetPixels32(); 
Color32[] currentColors = currentFrame.GetPixels32(); 
Color32[] outColors = new Color32[colors.Length]; 
int i = 0; 
while (i < currentColors .Length) 
{ 
    outColors[i].b = Math.Max(previousColors[i].b, currentColors[i].b); 
    outColors[i].r = Math.Max(previousColors[i].r, currentColors[i].r); 
    outColors[i].g = Math.Max(previousColors[i].g, currentColors[i].g); 
    outColors[i].a = 255; // set the alpha channel to opaque 
} 
bufferedPics[frameIndex].SetPixels32(outColors); 
bufferedPics[frameIndex].Apply();
+0

這是正確的答案:)再次感謝 – v8ntage