所以我試圖將渲染目標的結果存儲到一個顏色數組中,所以我可以用迭代器單獨操縱它們。出於某種原因,下面的代碼將數組中的每個值設置爲R = 0,G = 0,B = 0,A = 0。彷彿它從未初始化。 GetData方法使用不正確?還有別的嗎?它應該是一個非常豐富多彩的形象,絕對不透明。從RenderTarget2D存儲渲染到顏色數組錯誤
問題不是紋理繪製不正確,我執行代碼時沒有設置渲染目標(使用默認的後臺緩衝區),遊戲運行得非常好。
//Creating a new render target and setting it
RenderTarget2D unprocessed = new RenderTarget2D(graphics.GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height,false,SurfaceFormat.Color,DepthFormat.Depth24,4,RenderTargetUsage.PreserveContents);
graphics.GraphicsDevice.SetRenderTarget(unprocessed);
//Drawing background and main player
spriteBatch.Draw(bgTex,new Rectangle(0,0,Window.ClientBounds.Width,Window.ClientBounds.Height),Color.White);
mainPlayer.Draw(spriteBatch);
//resetting render target
graphics.GraphicsDevice.SetRenderTarget(null);
//creating array of Color to copy render to
Color[] baseImage = new Color[Window.ClientBounds.Width * Window.ClientBounds.Height];
//I pause the program here and baseImage is an array of black transparent colors
unprocessed.GetData<Color>(baseImage);
spriteBatch.End();
哈哈謝謝反正! 我注意到代碼缺少一個spritebatch開始,但它只是因爲我忘了將它包含在我複製的代碼中。 – Kartik