2012-06-28 1 views
0

그래서 렌더 타겟에서 결과를 색상 배열로 저장하려고 시도하므로 반복기를 사용하여 개별적으로 조작 할 수 있습니다. 몇 가지 이유로 아래 코드는 배열의 모든 값을 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(); 

답변

0

저는 몇 가지 문제가 있다고 생각합니다. 첫 번째는 렌더링 타겟을 처리하기 전에 spriteBatch.End()를 호출해야한다는 것입니다. 나는 또한 spriteBatch.Begin()에 대한 호출이 없다는 것을 알아 차렸다.

RenderTarget2D unprocessed = new RenderTarget2D(/*...*/); 
graphics.GraphicsDevice.SetRenderTarget(unprocessed); 
graphics.GraphicsDevice.Clear(Color.Black); 

//Drawing 
spriteBatch.Begin(); 
spriteBatch.Draw(/*...*/); 
spriteBatch.End(); 

//Getting 
graphics.GraphicsDevice.SetRenderTarget(null); 

방금 ​​자신의 질문에 대답했습니다. .... 신경 쓰지 마세요.

+0

감사합니다. 코드에서 spritebatch begin이 누락되었다는 것을 알았지 만, 복사 한 코드에이를 포함시키는 것을 잊어 버린 것이 었습니다. – Kartik

0

문제가 해결되었습니다. 기본적으로 이것이 검은 색 인 이유는 렌더링 타겟이 데이터를 가져 오기 전에 먼저 스프라이트 배치를 끝내야하기 때문입니다. 그래서 스프라이트 배치 과정에서 색상 값은 여전히 ​​검은 색입니다. 내가 수정 한 것은 드로잉이 발생하기 전후에 스프라이트 배치를 시작하고 끝내고 바로 문제를 해결했기 때문입니다.