[SOLVED] Sampling a single pixel from a RenderTarget2D

Hi fellow devs,

I try to implement a simple 3D position picking. My idea was to use x and y screen coordinates, add the depth value and unproject the whole thing to end up with a Vector3 in my world.

The first part works quite good, the depth value more of a problem.

I need to get the depth value at my mouse position. So first question: How do I get it out of my RenderTarget2D? Is that possible without rendering a depth image?

What I am currently doing is:

I render a depth image into a separate RenderTarget2D and do:

var depths = new Vector4[ 1 ];
_DepthRenderTarget.GetData(
    0,
    0, 
    new Rectangle( screenX, screenY, 1, 1 ), 
    depths,
    0,
    1 );
return depths[ 0 ].X;

Unfortunately this returns a value thats falling the whole time until it reaches 0 and then start from 1 again. And I have no idea why this is happening.

Any ideas? Thanks for reading.

Best regards,
Georg

Edit: Nevermind. I was drawing the depth buffer and positioning an object at the calculated position. The depths of that object of course where taken into account in every new loop - causing the object to move towards the camera and decreasing the depth with every loop. Sorry :smiley: Solved.