Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to get world position from screen position?

Discussion in 'Project Tiny' started by SINePrime, May 6, 2020.

  1. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    54
    I'm trying to get the world position of my mouse cursor using the methods in Unity.Tiny.Input.InputSystem (GetWorldInputPosition and TranslateScreenToWorld) but they all return float3.zero.

    Is there something special I need to do in my scene to properly use these methods?
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Those methods aren't implemented, you can check the source to verify that. You can create your own solution or try to use the undocumented "ScreenToWorld" in Unity.Tiny.Rendering.Native assembly.
     
  3. SINePrime

    SINePrime

    Joined:
    Jan 24, 2019
    Posts:
    54
    Weird that they're even included if they're not implemented, but okay; I'll try and follow the samples which use ScreenToWorld. Thanks for the help!
     
  4. jin1023

    jin1023

    Joined:
    Apr 1, 2020
    Posts:
    5
    public static float3 ViewportToWorldPoint(EntityManager entityManager, float3 position)
    {
    float3 worldPos = new float3();
    var cameraEntity = entityManager.CreateEntityQuery(typeof(Unity.Tiny.Rendering.Camera))
    .GetSingletonEntity();
    var cameraInfo = entityManager.GetComponentData<Unity.Tiny.Rendering.Camera>(cameraEntity);
    var cameraPosition = entityManager.GetComponentData<LocalToWorld>(cameraEntity);

    worldPos.x = cameraPosition.Position.x + position.z * cameraInfo.aspect * ((position.x - 0.5f) / 0.5f) * math.tan(math.radians(cameraInfo.fov / 2f));
    worldPos.y = cameraPosition.Position.y + position.z * ((position.y - 0.5f) / 0.5f) * math.tan(math.radians(cameraInfo.fov / 2f));
    worldPos.z = cameraPosition.Position.z + position.z;

    return math.rotate(cameraPosition.Rotation, worldPos);
    }

    This is my solution。

    InputPosition.z = 100f;
     
    Kmsxkuse likes this.