Search Unity

VR UI world to canvas without using a camera

Discussion in 'AR/VR (XR) Discussion' started by dwatt-hollowworldgames, Aug 19, 2019.

  1. dwatt-hollowworldgames

    dwatt-hollowworldgames

    Joined:
    Apr 26, 2019
    Posts:
    104
    I need to convert raycasts on a canvas to canvas space. I have the world coordinates where the click occurred how do i convert the world space to canvas space. Then pass that through the event system to the element that was clicked so it can know where it was clicked not just that it was clicked.
     
  2. StayTalm_Unity

    StayTalm_Unity

    Unity Technologies

    Joined:
    May 3, 2017
    Posts:
    182
    Hi!
    I've got some sources you can look at that do something very similar.
    For converting form 3D raycast to UI canvas space, have a look at: https://github.com/Unity-Technologi...utSystem/Plugins/UI/TrackedDeviceRaycaster.cs

    More specifically this function: RayIntersectsRectTransform
    There, we get the 4 corners of the Canvas' RectTransform, and find where your ray intersects with that plane. Once we've got the intersection point, we just need to determine if that intersection point is within the 4 corners of the actual RectTransform.

    Now, the second part of your question involves taking that intersected plane and passing it through the underlying EventSystem. This was a little bit tricky, but have a look at the TrackedPointerEventData in this file https://github.com/Unity-Technologi.../InputSystem/Plugins/UI/TrackedDeviceModel.cs
    I created a new type of EventData, because I needed to pass the 3D data through to the Raycaster, because PointerEventData always assumed a screen-space 2D coordinate. This new type let's you pass it through the EventSystems' raycasting APIs. Important to note you need to set an invalid screenspace, so the other raycasters don't try to use the event. Then, once you've done your 3D raycasting, set the EventData's position property to the hit's screen space position using Camera.WorldToScreenSpace (see the TrackedDeviceRaycaster.cs file's PerformRaycast function).

    This should cover some simple examples, but if you'd like a full solution, have a look at this folder: https://github.com/Unity-Technologi.../com.unity.inputsystem/InputSystem/Plugins/UI
    It's got some other stuff in there, but provides a working 3D tracked device to UI EventSystem integration.

    Hope this helps, ping back here for more info!
     
  3. dwatt-hollowworldgames

    dwatt-hollowworldgames

    Joined:
    Apr 26, 2019
    Posts:
    104
    I have that working already. The controllers are able to click a ui element and it knows it was clicked. I want to have a 2d map on a canvas clicked on and know the point on the map that was clicked. your example uses a dot product to determine that its between it doesn't compute the point between.
     
    Last edited: Aug 20, 2019
  4. StayTalm_Unity

    StayTalm_Unity

    Unity Technologies

    Joined:
    May 3, 2017
    Posts:
    182
    Aha, I think I understand now.
    So you have, something like an Image UI element on a canvas that was clicked, and you want to know something like "The click at 20% X(left to right), and 70% Y(Top to bottom)" or something similar?
     
  5. dwatt-hollowworldgames

    dwatt-hollowworldgames

    Joined:
    Apr 26, 2019
    Posts:
    104