Getting world coordinates for UI elements relative to a camera displaying to a render texture

I’m trying to use a GridLayoutGroup to help segment/manage an inventory system where I use a camera with a render texture attached to a UI panel to render the actual items as their icons.

I have faded circle shaped buttons setup in an evenly spaced layout group in a UI panel.
The brighter circle are the positions I’m getting when trying to instantiate a sphere in the direct center of these buttons, and I can’t figure out why they are offset like this.


The amount they are offset changes when I change the screen size. (using maximize on play)

Here is the code I’m using to instantiate these spheres.

        foreach (RectTransform btn in inventoryGrid.GetComponentInChildren<RectTransform>()) 
        {
            GameObject sphere = Instantiate(testSphere, renderCamera.transform);

            Vector3 point = rayCastCam.ScreenToWorldPoint(btn.position);
            point.z = backgroundPlane.transform.position.z;

            btn.GetComponentInChildren<Text>().text = point.ToString();

            sphere.transform.position = point;
        }

What adds to my confusion is the world points I’m getting seem to be correct. Buttons have their world coords on them, and they read the same as the transform position of the offset spheres.

Anyways I haven’t been able to crack this, any help would be greatly appreciated!

If anyone is interested I figured this out. From what I can tell using screen coords just doesn’t really work with an ortho rendertexture. So I just mapped the pixel values in the rendertexture UI image to my ortho camera world positions.

(in this case inventoryGrid is the object containing all my buttons, but is more importantly the same
size and position as my rendertexture UI image)

//map from orthocam coordinate range to inv bg image ui 
public Vector2 RTtoOrthoWorldCoord(Vector2 coord)
{
    Vector3 camPos = renderCamera.transform.position;
    float camSize = renderCamera.orthographicSize;
    Rect rect = inventoryGrid.GetComponent<RectTransform>().rect;

    float x = map(coord.x, 0f, rect.width, camPos.x - camSize, camPos.x + camSize);
    float y = map(coord.y, 0f, rect.height, camPos.y - camSize, camPos.y + camSize);

    return new Vector2(x, y);
}
float map(float s, float a1, float a2, float b1, float b2)
{
    return b1 + (s - a1) * (b2 - b1) / (a2 - a1);
}