2
\$\begingroup\$

I want a way to find out if, for example, the pixel at Vector2(2, 5) on the game window is color Color.Red, or some other color or set of coordinates. How can I do this?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Render your scene to a texture render target (RenderTarget2D) by creating one, setting it active via SetRenderTarget and then drawing your scene. Since a RenderTarget2D is also a Texture2D, you can use the GetData method to copy the texture data to an array of the appropriate type, which you can index to acquire the appropriate color data using linearized 2D indexing (that is, compute the index as index = (y * width) + x, where width is the width of the texture).

Note that the "appropriate type" of the array will depend on the format of the render target you have created.

Generally, this is an inefficient collection of operations relative to other possible ways to achieve the overall end-result you are looking to achieve. However, since your question doesn't provide further details about why you want to read the color data at a specific pixel, it's difficult to provide further guidance. Consider asking another question with more details about your end goal.

\$\endgroup\$
1
  • \$\begingroup\$ This was what I found to work best for me. In the end, I didn't end up using it, but thanks for leaving an answer! \$\endgroup\$ Commented May 6, 2014 at 0:38
2
\$\begingroup\$

I would imagine render all your going to draw to a texture, and then ask the texture for it's color data. I've never tried it myself, but that's where I'd start. I would rather have left this as a comment.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .