Mouse position on the internal camera

Viewed 225

What do I have:

  • Camera2D that looks down on the Plane, horizontally in a global system
  • The Image from Camera2D is saved to the Texture2D
  • MainCamera which displays Canvas from the UI
  • There is a RawImage in Canvas to which Texture2D is assigned

In short, in Main UI I have a screen in which I display the content of another camera.

My problem:

  • I'd like to draw something on Plane.
  • To do this I need to find a mapping of the Mouse Position in the global coordinate system

I try to do it in different ways, e.g.

var mp = Input.mousePosition;
var vec = mainCamera.ScreenToWorldPoint(mp);
//var vec = image2DCamera.ScreenToWorldPoint(mp);

This solution works when there is only one camera, but in this case it does not fulfill its purpose.

How to do it correctly in such a situation?

enter image description here

EDIT:

Plane plane = new Plane(new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1));
float distance;

var mp = Input.mousePosition;
var mp2 = new Vector3(mp.x, mp.y, 100);
Ray ray = image2DCamera.ScreenPointToRay(mp2);
if (plane.Raycast(ray, out distance))
{
    worldPosition = ray.GetPoint(distance);
}

Debug.Log("mp : " + mp2);
Debug.Log("worldPosition : " + worldPosition);
1 Answers

I managed to solve it in a simple way. Without using Ray etc.

Below is the code with the added cube that shows how the mouse moves on the UI screen (RawImage) and on the Plane in another place.

It also works fine with Camera Zoom.

public class MyController : MonoBehaviour
{
    public Camera portalCamera; // Camera for 2D View
    public RawImage texture2DView; // Rect on UI with 2D View

    GameObject mouseCube;

    DomaManager domaManager;

    void Start()
    {
        domaManager = DomaManager.Instance;

        mouseCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        mouseCube.name = "MouseCube";
        mouseCube.GetComponent<Renderer>().material.color = Color.blue;
        mouseCube.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    }

    void Update()
    {
        var mp = Input.mousePosition;

        int marginLeft = Mathf.RoundToInt(((Screen.width - texture2DView.rectTransform.rect.width) / 2) + texture2DView.transform.localPosition.x);
        int marginBottom = Mathf.RoundToInt(((Screen.height - texture2DView.rectTransform.rect.height) / 2) + texture2DView.transform.localPosition.y);

        Rect rect2DUI = new Rect(marginLeft, marginBottom, texture2DView.rectTransform.rect.width, texture2DView.rectTransform.rect.height);

        if (rect2DUI.Contains(mp))
        {
            // Mouse Position in areaTexture2D
            var rectUIMousePosition = new Vector3(mp.x - rect2DUI.xMin, mp.y - rect2DUI.yMin, mp.z);

            // Position corners camera rect
            //var topRight = secondCamera.ViewportToWorldPoint(new Vector3(1, 1, secondCamera.nearClipPlane));
            //var topLeft = secondCamera.ViewportToWorldPoint(new Vector3(0, 1, secondCamera.nearClipPlane));
            //var bottomleft = secondCamera.ViewportToWorldPoint(new Vector3(0, 0, secondCamera.nearClipPlane));
            //var bottomRight = secondCamera.ViewportToWorldPoint(new Vector3(1, 0, secondCamera.nearClipPlane));

            // Mouse Position in camera area
            var positionInCameraRect = portalCamera.ViewportToWorldPoint(new Vector3(rectUIMousePosition.x / portalCamera.pixelWidth, rectUIMousePosition.y / portalCamera.pixelHeight, portalCamera.nearClipPlane));

            var realePositionInCameraRect = new Vector3(positionInCameraRect.x, 0, positionInCameraRect.z);

            // Cube follow Mouse Position
            mouseCube.transform.position = realePositionInCameraRect;
        }
    }
}

enter image description here

Related