How do I get cursor position in 3D(Z buffer depth) fast in C#?

Hi, I’m trying to create a plugin that makes use of the cursor position in 3D world. This APi is what I found.

So there is a ZBufferCapture.WorldPointAt method, but while this method takes ~10ms on small scenes, it takes like 100ms~200ms on larger scenes with filesizes up to 200MB and that performance cannot be accepted. Repeating the calculation of getting cursor position in 3D should be fast and responsive, while not making the viewport freeze.

There is a command ‘ShowZBuffer’ in Rhino, which shows the depth of the scene.

Displaying this Z-depth is realtime and is lightweight, but why does ZBufferCapture.WorldPointAt takes as long as 100~200ms? Is there any workaround? It would be nice to achieve ~10ms per calculation.

Here’s the code to reproduce

Point2d cursorInScreen = MouseCursor.Location;
int cursorInScreenX = (int)cursorInScreen.X;
int cursorInScreenY = (int)cursorInScreen.Y;
System.Drawing.Point cursorInSystemScreen = new System.Drawing.Point(cursorInScreenX, cursorInScreenY);
System.Drawing.Point cursorInView = vp.ScreenToClient(cursorInSystemScreen);
int cursorX = cursorInView.X;
int cursorY = cursorInView.Y;
int viewWidth = vp.Size.Width;
int viewHeight = vp.Size.Height;

using (ZBufferCapture depthBuffer = new ZBufferCapture(vp))
    float currentCursorWorldPosition = depthBuffer.ZValueAt(cursorX, cursorY);