Hololens object rotation with gesture

   This post is about how to rotate (self) holographic object by hand gesture in Hololens (Windows Mixed Reality toolkit). Initial setups and tools preparation are described in this link. To grab a holographic object, we use GestureRecognizer class, IManipulationHandler interface helps us to manipulate the object, and RaycastHit finds out the selected object.


1. Download HoloToolkit and add it into your project assets.

2. Apply Mixed Reality plugin functions.

3. Add below items from prefabs of HoloToolkit assets.
  a). HololensCamera
  b). InputManager
  c). Cursor

4. Add empty game object and name it as an ObjectManager
  a) Add Cube (or any other) game object into your ObjectManager.

5. Create Rotate.cs script and paste below source code.

6. Add Rotate.cs script into ObjectManager.

7. Select ObjectManager, click add components, and  Gaze Stabilizer script (search by typing "gaze" in Add Component)

8. Repeat #4, #6, and #7 to add some more game object into your scene. 

9. Build and run your app in Hololens.

Rotate.cs looks as below.

using UnityEngine;
using HoloToolkit.Unity.InputModule;
using UnityEngine.XR.WSA.Input;

public class Rotate : MonoBehaviour, IManipulationHandler {
        
    GameObject selectedObject = null;
    GestureRecognizer tapRecognizer;   
           
    public float RotateSpeed = 0.4f;
    public float RotationFactor = 50f;

    // Use this for initialization
    void Start ()
    {
        tapRecognizer = new GestureRecognizer();  
        tapRecognizer.StartCapturingGestures();
    }

    // Update is called once per frame
    void Update()
    {
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            selectedObject = hitInfo.collider.gameObject;
        }
        else
        {
            selectedObject = null;
        }
    }

    public void OnManipulationStarted(ManipulationEventData eventData)
    {
        InputManager.Instance.PushModalInputHandler(selectedObject);
    }

    public void OnManipulationUpdated(ManipulationEventData eventData)
    {
        var rotation = new Vector3(eventData.CumulativeDelta.y * RotationFactor,
                   eventData.CumulativeDelta.x * RotationFactor,
                   eventData.CumulativeDelta.z * RotationFactor);

        transform.Rotate(rotation.x * RotateSpeed,
                         rotation.y * RotateSpeed,
                         rotation.z * RotateSpeed, Space.Self);
    }

    public void OnManipulationCanceled(ManipulationEventData eventData)
    {

    }

    public void OnManipulationCompleted(ManipulationEventData eventData)
    {

    }
}
 
 
 
 

No comments:

Post a Comment