Search Unity

How do I refer to a specific space in a spatial mapped room?

Discussion in 'VR' started by Stryk3r, Aug 12, 2016.

  1. Stryk3r

    Stryk3r

    Joined:
    Aug 12, 2016
    Posts:
    2
    Hi I am a HoloLens newbie and went through the HoloLens tutorials. I would like to fire some UI elements when the user is gazing over a certain section of a spatially mapped room.

    So for example n the default room of the HoloLens emulator, when the user gazes at the sofa, I would like to fire an event and have some holograms appear at the sofa but only at the sofa.

    So my question is how do I access the data that stores that sofa's transform.position vector?
     
  2. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Hello,

    It sounds like you need to find a specific location and then store that location for later use. This might work for what you want

    The best way I think to do this is send a ray cast from the camera or the hand, then collect the hit information. You would want the Vector3 position of the hit, from there you could create a game object on that position. It would be a good idea to world lock the object too. Then you could store that game object in the world anchor store for later use or to use it as a anchor for that session. All this information can be found in the documentation that installs with the HTP build.

    Code Snippet for Ray Cast:


    Code (CSharp):
    1. RaycastHit hit;
    2.  
    3. // Creates a Surface Marker is it hits something, used to test physics on Surface Obersver
    4. if (Physics.Raycast(ray, out hit, 10f))
    5. {
    6.   Debug.Log("Hit Something: " + hit.collider);
    7.   SurfaceColliderVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    8.   SurfaceColliderVisual.name = "SurfaceSLAM";
    9.   SurfaceColliderVisual.transform.localPosition = hit.point;
    10.   SurfaceColliderVisual.transform.localScale = new Vector3(.1f, .1f, .1f);
    11.   SurfaceColliderVisual.GetComponent<Renderer>().material.color = Color.cyan;
    12.   SurfaceColliderVisual.layer = 1 << LayerMask.NameToLayer("Default");
    13. }
    14. else
    15. {
    16.   Debug.Log("No hits detected");
    17. }

    Let us know if you have questions.

    Thank you,
    Wesley
     
  3. Stryk3r

    Stryk3r

    Joined:
    Aug 12, 2016
    Posts:
    2

    Hi, thank you for replying to my original question. A few further questions, I have.

    The hologram will probably already be in the scene only deactivated and when I gaze at a specific real world object like a coffee table then the hologram will move to that position and reactivate. I am reading about the spatialanchor class and this seems to be what I need to using for this.

    My question is how do I store or create a spatialanchor point on an object in the real world, like the coffee table? If a raycast is sent out to the coffee table's spatial mesh, how would I know if its the coffee table that the ray is hitting?
     
  4. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Hello,

    I would recommend reading the documentation on the WorldAnchorStore, the documentation should be installed by default with this build (unless it was unchecked during install).

    You want to create a instance of the WorldAnchorStore and then add anchors to the store, this way you can access the store and load objects.

    If you use the same room everything should line up, I think it is a question of object life management. Maybe it would be best to run a setup phase every time on start up.

    I would recommended checking out the Microsoft game fragments, it might give you some ideas.

    Thank you,
    Wesley