Calculate correct position in Screen Space - Camera canvas

I’m in need of calculate the correct position of a 3D object which I’m displaying over a RawImage in my UI. If I put my UI image in the center of the screen I can calculate it perfectly, if I move my image around my UI canvas I’m getting results which are off by an offset, depending on the side I’m moving. I’ve taken a couple of screenshot of what I mean, the orange side is my 3D quad, the white square is just an image debugging where my point should be calculated.

Correct:

Wrong:

The setup I have is this: - a world camera pointing on a 3D quad - a ui canvas with a dedicated perspective camera (Screen space - Camera) - a panel in my ui canvas displaying the 3D quad

The code:

var worldPoint = t.Value.MeshRenderer.bounds.min; //t.Value is the 3D quad
var screenPoint = worldCamera.WorldToScreenPoint(worldPoint);
screenPoint.z = (baseCanvas.transform.position - uiCamera.transform.position).magnitude; //baseCanvas is the UI canvas
var pos = uiCamera.ScreenToWorldPoint(screenPoint);
debugger.transform.position = pos; //debugger is just the square image used to see where my calculated point is landing

Another test:

var screenPoint = worldCamera.WorldToScreenPoint(t.Value.MeshRenderer.bounds.min);
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, uiCamera, out localPoint); //rectTransform is my UI panel
debugger.transform.localPosition = localPoint

But I always get the same result, how can I make the correct calculation considering the offset?

can you upload sample project?

Not right now, I’ll see if I can do something about it but can’t promise anything

1 Like