Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

How to highlight one face of a cube when gazing (using the HoloToolKit cursor)?

MusaMusa
edited June 2017 in Questions And Answers

Hi guys,
I am doing a Hololens app using Unity 3D engine now and I want to implement one specific function, which is when the user is looking a 3D game object (for example, like a cube), it could highlight (or simply change color) the face that the cursor is on.
I did some research and find for me it is not an easy question. One solution I can think of is to first know which mesh triangle the user is gazing by getting it's index. And because every face has different triangle index so I could use some simple if else or case statement to determine which face the user is gazing. Then I would switch the color for all triangles in that face. When user changes the face he is gazing at, just set back to the original color.
Am I doing this right? Or there is better solution? It would be great if someone could give a code example with some comments.
Any advise or suggestion are welcome!
Thanks!

The code I have some far is like this, but it does not work:

  public class FindTriangleIndex : MonoBehaviour {
    GameObject cursor;

    // Use this for initialization
    void Start () {
        cursor = GameObject.FindGameObjectWithTag("cursor");

        if (cursor == null)
        {
            Debug.Log("cursor not found");
        }
    }

    // Update is called once per frame
    void Update () {
        //This generates our ray. We input the positions x, and y, as screen height and width divided by two to send a ray towards whatever the
        //screen center is viewing.
        //Camera.main is a static var, capable of being accessed from any code without references.
        Ray ray = Camera.main.ScreenPointToRay(cursor.transform.position);

        //This defines a RaycastHit object. Its information is filled out if Physics.Raycasthit actually hits.
        RaycastHit hit = new RaycastHit();

        //This uses the ray's parameters to call physics.raycast. Mathf.Infinity is the distance the raycast will go before giving up. [Don't worry, distances of this size cause no lag].
        //We specify 'out' as that is just how c# rolls. [It goes into variable references, ETC. All of this is automagic in JS]
        Debug.Log(Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity));

        if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity))
        {
            //This is the index of the tri the camera is looking at:
            int ind = hit.triangleIndex;
            Debug.Log("Hit tri index is " + ind * 3);
        }
    }
}
Tagged:

Best Answer

Answers

  • Options
    MusaMusa
    edited June 2017

    Thanks, timg. I end up with generating a new game object for every face so they could be easily gazed and changed to different color.

Sign In or Register to comment.