Unity How-to Guide: Placing Virtual Objects in Augmented Reality

Tango Augmented Reality (AR) apps help place virtual objects (anything from new living room furniture to a full-sized dinosaur) into your physical world. In this tutorial, you'll use Project Tango’s depth APIs to add virtual cats to your environment. When the user touches a surface viewed through their device's camera, it creates a virtual cat based on real-world geometry and places the cat on the surface. The app uses the depth camera to locate the tapped surface and register (place) the cat in the correct 3D position.

Preliminary steps

  1. If you haven't set up your computer to develop with Tango in Unity yet, see Getting Started With Unity.

  2. Go to the Setup for All Unity Tango Apps page and perform the basic setup operations on that page. When you have finished, return to this page and continue on with the steps below.

Add prefabs

  1. In the TangoPrefabs folder, locate the Tango Camera prefab, and then drag it to the Hierarchy panel.

  2. Do the same with the Tango Point Cloud prefab.

The Tango Point Cloud prefab has useful functions related to point clouds, including finding the floor, transforming the point cloud to Unity global space, and rendering debug points. It also contains a function called FindPlane() that finds a plane for you based on a touch event. FindPlane() plays an important role in this tutorial.

Configure the Tango Manager

In the Hierarchy panel, select Tango Manager, and then in the Inspector panel, make sure the following options are selected:

  • Auto-connect to Service.
  • Enable Motion Tracking with Auto Reset.
  • Enable Depth
  • Enable Video Overlay with Method = Texture (ITangoCameraTexture).

Create a UI Controller gameObject

  1. On the GameObject menu, click Create Empty.

  2. In the Inspector panel for this new gameObject, change the name to "UI Controller."

Add a script to the UI Controller gameObject

  1. In the Inspector panel, click Add Component.

  2. In the Component drop-down menu, click New Script. (You may have to scroll down to see it.)

  3. In the New Script dialog, change the name to "KittyUIController".

  4. Click Create and Add.

This script (KittyUIController.cs) will handle the touch event, call the FindPlane() function, and place your kitten into the scene.

Add the code for the KittyUIController script

  1. In the KittyUIController (Script) component, click the gear icon to view the context menu, and then select Edit Script.

  2. In the script editor, replace the existing script with the code below. To copy the code to the Clipboard, move the pointer over the code, and then in the upper right corner of the code section, click the Click to copy button.

    using UnityEngine;
    using System.Collections;
    
    public class KittyUIController : MonoBehaviour
    {
        public GameObject m_kitten;
        private TangoPointCloud m_pointCloud;
    
        void Start()
        {
            m_pointCloud = FindObjectOfType<TangoPointCloud>();
        }
    
        void Update ()
        {
            if (Input.touchCount == 1)
            {
                // Trigger place kitten function when single touch ended.
                Touch t = Input.GetTouch(0);
                if (t.phase == TouchPhase.Ended)
                {
                    PlaceKitten(t.position);
                }
            }
        }
    
        void PlaceKitten(Vector2 touchPosition)
        {
            // Find the plane.
            Camera cam = Camera.main;
            Vector3 planeCenter;
            Plane plane;
            if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
            {
                Debug.Log("cannot find plane.");
                return;
            }
    
            // Place kitten on the surface, and make it always face the camera.
            if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
            {
                Vector3 up = plane.normal;
                Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
                Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
                Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
            }
            else
            {
                Debug.Log("surface is too steep for kitten to stand on.");
            }
        }
    }
    

Code details

Declare a gameObject named m_kitten.

    public GameObject m_kitten;

In the Start() function, create a reference to the TangoPointCloud script on the TangoPointCloud gameObject. You'll use this to call the FindPlane() method.

        m_pointCloud = FindObjectOfType<TangoPointCloud>();

In the Update() function, check the touch count and its state. If a touch has ended, place the kitten.

if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch ended.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Ended)
            {
                PlaceKitten(t.position);
            }
        }

In the PlaceKitten() function, place the kitten in 3D space.

    void PlaceKitten(Vector2 touchPosition)
    {
        // Code to query the AR Camera's location, and then call FindPlane()
        // based on the AR Camera's position and the touch position.
        // FindPlane() returns an estimated plane from the touch point,
        // and then places the kitten on the plane if it's not too steep.

    }
  • When you are finished examining the script, save it, and then return to the Unity Editor.

Importing your kitten model

  1. Download kitty.unitypackage, which includes a kitten model with some simple animations.

  2. Double-click your downloaded kitty.unitypackage to import it into your project. In the Import Unity Package dialog, make sure all items are selected. The imported Kitty folder appears in the Project panel under Assets.

  3. Make sure UIController is the currently selected gameObject.

  4. In the Kitty folder, locate the Kitty prefab, and then drag it to the Kitten field in the Kitty UI Controller (Script) component.

Build and run your project

  1. On the File menu, click Build and Run.

  2. In the Build Settings dialog, click Build and Run.

  3. In the Build Android dialog, enter a name for your app in the Save As field, and then click Save.

Experiment with your app

  1. Point your device at a surface in your environment and then touch your device's screen within the boundaries of the surface. A kitten appears on the surface.

  2. To make more kittens appear, touch other surfaces. Note that the kittens adjust their size realistically (the farther into the distance the surface, the smaller the kitten).

Enviar comentarios sobre…