1 (edited by Thibault 2018-12-12 20:40:10)

Topic: Adapt map control for VR

Hi

I have the chance to work with an oculus rift this week, and i'm trying some experiments.

I'm trying to virtualize my application. I mean that i did set up the main camera to render to a texture instead of rendering on the screen, an put this texture to a "fake screen". Now i want to be able to interact with the map.

https://gph.is/2C7N8SR

As you can see in this Gif, i'm trying to control the map using the oculus  touch.
Right now i'm performing a raycast, and retrieving the texture coordinate of the hit point.

So i think that i should trigger a mouse click, using the texture coordinate to "emulate" a screen position. This would give me the ability to use the map control, add marker at certain coordinates, etc ...

1) Is that a good approach ? How would you proceed to call a mouse clic at a certain position ?

2) Or are there any functions to control the map that i could call to do this job ?

3) Would this also give me the abilitie to press my UI buttons ? (my ui is on a canvas which is set to overlay the "map" camera.) I know i could set those ui elements to world space, position them onto the fake screen, add a box collider to every button and trigger them with my controller but if i could just simulate a screen click that should work without having to change anything in my UI right?

Re: Adapt map control for VR

Hello.

First of all, look at this example:
http://infinity-code.com/atlas/online-m … ample.html
This allows you to emulate the cursor position and mouse click.

Unfortunately, I do not know whether it will work for UI buttons or not.

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: Adapt map control for VR

Thanks for the example, I did some tests based on this code.
I'm facing several issues though.

https://gph.is/2SMO0lo

In this GIF i'm trying to place a cube prefab as a marker on click. As you cans see they all appears at the same position, bottom left of the fake screen.

This is the console output :

https://i.imgur.com/5uK0QJh.jpg


based on this code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Test_VR_controller_UI_interaction : MonoBehaviour {


    public Text outputText;
    public Camera texCam;
    public GameObject prefab;

    LineRenderer LR;

    private Vector2 cursorPos = new Vector2();
    private bool isPointingTabletScreen = false;

    // Use this for initialization
    void Start () {
        
        LR = gameObject.AddComponent<LineRenderer>();
        LR.startWidth = 0.01f;
        LR.endWidth = 0.01f;
        LR.positionCount = 2;
        LR.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
        LR.startColor = Color.yellow;
        LR.endColor = Color.yellow;

        // Intercepts getting the cursor coordinates.
        OnlineMapsControlBase.instance.OnGetInputPosition += OnGetInputPosition;

        // Intercepts getting the number of touches.
        OnlineMapsControlBase.instance.OnGetTouchCount += OnGetTouchCount;

        OnlineMapsControlBase.instance.OnMapClick += OnMapClick;// Subscribe to the click event.
    }
    
    // Update is called once per frame
    void Update () {
        bool bDown = OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.RTouch); // for right hand

        LR.SetPosition(0, transform.position);

        RaycastHit hit;
        // Does the ray intersect any objects
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
        {
            LR.SetPosition(1, hit.point);
            LR.endColor = Color.red;

            //outputText.text = hit.transform.gameObject.name;
            outputText.text = hit.transform.gameObject.name + "  " + hit.textureCoord.ToString();
            //outputText.text = OnlineMapsTileSetControl.instance.GetCoords(hit.textureCoord).ToString("f8");

            if (bDown)
            {
                if (hit.transform.gameObject.name == "tablet_screen")
                {
                    cursorPos = hit.textureCoord;
                    isPointingTabletScreen = true;
                    return;
                }
            }
        }
        else
        {
            LR.SetPosition(1, transform.position + transform.TransformDirection(Vector3.forward) * 1000);
        }

        isPointingTabletScreen = false;
    }

    private Vector2 OnGetInputPosition()
    {
        return cursorPos;
    }

    private int OnGetTouchCount()
    {
        if (isPointingTabletScreen)
        {
            Debug.Log("Fake mouse clic at texture coord : "+cursorPos.ToString("f8")+" which is "+ OnlineMapsTileSetControl.instance.GetCoords(cursorPos).ToString("f8"));
            return 1;
        }
        else
            return 0;
    }
    
    private void OnMapClick()
    {
        double lng, lat;
        OnlineMapsControlBase.instance.GetCoords(out lng, out lat);
        Debug.Log("OnMapClick at " + lng + "   " + lat);

        OnlineMapsMarker3D marker = OnlineMapsControlBase3D.instance.AddMarker3D(lng, lat, prefab);
    }
}

1) I don't understand  why the longitude/latitude is different when i debug.log it in "OnGetTouchCount()" and in "OnMapClick()".
Shouldn't it be the same value ?

Since the value given by "Debug.Log("OnMapClick at " + lng + "   " + lat)" is always different, why are my markers at the same position ?

2)And also, why is OnGetTouchCount() always called twice when i press my button ?

3)Is there a way to emulate a cursor drag in a similar way that OnGetTouchCount is emulating a click? (To move the map)

4 (edited by Thibault 2018-12-13 19:11:40)

Re: Adapt map control for VR

Ok so i solved the the first part of 1) by replacing :

Debug.Log("Fake mouse clic at texture coord : "+cursorPos.ToString("f8")+" which is "+ OnlineMapsTileSetControl.instance.GetCoords(cursorPos).ToString("f8"));

With :

double lng, lat;
OnlineMapsTileSetControl.instance.GetCoords(out lng, out lat, cursorPos);
Debug.Log("Fake mouse clic at texture coord : "+cursorPos.ToString("f8")+" which is "+ lng + "   " + lat);

So the remaining questions are :

1)Since the value given by "Debug.Log("OnMapClick at " + lng + "   " + lat)" is always different, why are my markers at the same position ?

EDIT : -> In fact, even if the lat/lng seems different, google maps is pointing at the same place where my markers are, so i guess that the lat / lng are not different enough to spawn my cubes at different place.

So i need to find out why "OnlineMapsTileSetControl.instance.GetCoords(out lng, out lat, cursorPos);" is giving similar position even when cursorPos (which is my texture coordinates) is a lot different.




2)And also, why is OnGetTouchCount() always called twice when i press my button ?

3)Is there a way to emulate a cursor drag in a similar way that OnGetTouchCount is emulating a click? (To move the map)

Re: Adapt map control for VR

Ok, part 1) is definitly solved !

The correct code is :

private Vector2 OnGetInputPosition()
    {
        return texCam.ViewportToScreenPoint(cursorPos);
    }

Stupid mistake really, as i was giving a position between (0,0) and (1,1) instead of (0,0) to (screen.width,screen.height). And so, it was always spawning bottom left of the screen.

2)And also, why is OnGetTouchCount() always called twice when i press my button ?

3)Is there a way to emulate a cursor drag in a similar way that OnGetTouchCount is emulating a click? (To move the map)

Re: Adapt map control for VR

Each of these methods can be called several times per frame.

I looked at your code in post number 3, and I think you always have a click.
Check your Update method. I think there is a bug.
Fix this and all events (including click and drag) will work correctly.

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: Adapt map control for VR

Actually, it's "OnGetTouchCount()" which is called every frame, no matter if i press my button or not. Is that the intended behavior ?

Re: Adapt map control for VR

This method is called to find out if the button is pressed.
So of course, this is the planned behavior.

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!