Let's Make A Complete Holographic App With Unity

We have been exploring Hololens in terms of Direct X usage (trust me there is lot to cover when we go back to Direct X) now we will change focus to Unity. The structuring of an app is similar to adding game objects in Unity scene and then we take it forward using Holographic features.

The ground rules of development

Relax and follow how you have been developing apps for Unity on a long term basis and you don't have to do a fancy thing that is out of the blue. When you are developing for Hololens, you will have to keep a tap and the build should be targeted for UWP.

Things that are required

  • Unity Microsoft Hololens technical review.
  • UWP tools for Unity technical review.
  • Visual Studio 2015 Update 2.
Let's get started,

Shoot up Unity

project

Open the project Origami or a new project as I had been using this.

Open a new Scene,

new Scene

Things that we need to change.

Make sure in the main Camera Settings at the inspector we put in position values as
X=0,Y=0,Z=0,
Settings

On the camera options we choose Clear flags to solid color and Background as RGBA all set to 0.

RGBA

Now we create an empty object, name it  CSharpCorner as more of our projection will happen in this empty object, we need to adjust where this CSharpCorner object is lying.

We position at X = 0, Y=-0.5,Z=2

We create a new game object Plane and put it as child of CSharpCorner,

object

It looks like this,

look

We will use 3d Object creation and make 3 spheres,

3d object

Now we create a C# Script for rotation, The script looks like this it allows rotation around y axis.
  1. using UnityEngine;    
  2. using System.Collections;      
  3. public class Rotate : MonoBehaviour {      
  4. // Use this for initialization    
  5. void Start () {      
  6. }    
  7. // Update is called once per frame    
  8. void Update () {    
  9. // Rotate the object around its local Y axis at 1 degree per second    
  10. transform.Rotate(Vector3.right * Time.deltaTime);    
  11.     
  12. // ...also rotate around the World's Y axis    
  13. transform.Rotate(Vector3.up * Time.deltaTime, Space.World);    
  14.     
  15. }    
  16. }     
code

Now we drag the rotate script to all the sphere objects, We will use World Cursor it allows us to Gaze through the objects and now we add a C# script to it. 

We drag World Cursor Script to the Cursor object,

object
  1. using UnityEngine;    
  2. using System.Collections;    
  3.     
  4. public class WorldCursor : MonoBehaviour {    
  5. private MeshRenderer meshRenderer;    
  6.     
  7. // Use this for initialization    
  8. void Start () {    
  9. // Grab the mesh renderer that's on the same object as this script.    
  10. meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();    
  11.     
  12. }    
  13.     
  14. // Update is called once per frame    
  15. void Update () {    
  16. // Do a raycast into the world based on the user's    
  17. // head position and orientation.    
  18. var headPosition = Camera.main.transform.position;    
  19. var gazeDirection = Camera.main.transform.forward;    
  20.     
  21. RaycastHit hitInfo;    
  22.     
  23. if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))    
  24. {    
  25. // If the raycast hit a hologram...    
  26. // Display the cursor mesh.    
  27. meshRenderer.enabled = true;    
  28.     
  29. // Move thecursor to the point where the raycast hit.    
  30. this.transform.position = hitInfo.point;    
  31.     
  32. // Rotate the cursor to hug the surface of the hologram.    
  33. this.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);    
  34. }    
  35. else    
  36. {    
  37. // If the raycast did not hit a hologram, hide the cursor mesh.    
  38. meshRenderer.enabled = false;    
  39. }    
  40.     
  41. }    
  42. }  
The script creates the interaction technique with the cursor for our orientation,

orientation

After the changes we create the build for UWP platform,

platform

With successful build, we open the solution in Visual Studio,

Visual Studio

Compile it in Hololens Emulator

solution

Emulator

Let's see how it looks,

output

See the world cursor changes gaze as you move around the objects.

Conclusion

In this tutorial we have covered the Gaze gesture in Unity from scratch and also checked on how we can create a scene for Hololens using Unity.

Up Next
    Ebook Download
    View all
    Learn
    View all