AdSense

2017年2月25日土曜日

Google Tango対応ARアプリを作った

#AR_FukuokaでGoogle Tango対応ARアプリの作り方を学びました。

スマホ(Phab 2 Pro)が3Dで空間認識しているので、タッチした実空間の位置にキャラクタが出てきます。


UnityでGoogle Tango SDKを使い、タッチ座標を取得してそこにキャラクタを配置するプログラムを書いています。


MS HoloLensと同じことが5万程度のスマホでできるのが凄いです。







using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Tango;

public class NewBehaviourScript : MonoBehaviour {

    public TangoPointCloud pointcloud;

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.touchCount > 0) {
            Touch t = Input.GetTouch (0);
            TouchPhase p = t.phase;    //ふれているときの状態
            if (p == TouchPhase.Began) {     //触れ始めならば
                //transform.Translate (0.1f, 0, 0);  //X軸に0.1動かす
                StartCoroutine(FindPlane(t.position));
            }
        }
    }

    private IEnumerator FindPlane(Vector2 touchPosition)
    {
        Camera cam = Camera.main;
        Vector3 pos;    //3つのパラメータ
        Plane plane;

        if (!pointcloud.FindPlane (cam, touchPosition, out pos, out plane)) {
            yield break;
        }

        transform.position = pos;

        //こっち向かせる
        transform.forward = new Vector3 (cam.transform.position.x - transform.position.x, 0, cam.transform.position.z - transform.position.z).normalized;

            
    }

}