UniMelb Comp30022 IT Project (Capstone) - 2.Vuforia in Unity

2 Vuforia in Unity

Tutorial: https://www.youtube.com/watch?v=X6djed8e4n0&t=213s

Preparation:

Download "Vuforia for Unity" from https://developer.vuforia.com/downloads/sdk?d=windows-30-16-4815&retU

import Vuforia into Unity by dragging "vuforia-unity-xxx.unitypackage" into Unity project window

AR Camera:

Assets > Vuforia > Prefabs > drag ARCamera into the scene & save as ufoScene

Add a license key in https://developer.vuforia.com/targetmanager/licenseManager/licenseListing

Select ARCamera object and Open Vuforia Configuration in the inspector window, and put in app license key

save and run, the camera is on~

Import Game Asset: http://wirebeings.com/markerless-gps-ar.html

extract it and drag it into unity

drag a flying disk into the scene; scale to 0.1; position to (4.3, 29.7, 200)

now the ufo is fixed on the center of the screen

For the purpose of fixing the position of the ufo at the geolocation in reality:

Change World Center Mode to "Device Tracking"

Open Vuforia Configuration and tick Enable device pose track

UI Text: to show the distance

add UI > Text called distance, and adjust the position and change the text color and content, change Horizontal Overflow to overflow

add a UI Image as the background for the UI Text, adjust the position and change the color and transparent

add a tag called distanceText and set the tag of Text distance to it

Script:

add a script called AugmentedScript to Flying Disk object

ref: http://wirebeings.com/markerless-gps-ar.html

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class AugmentedScript : MonoBehaviour
{
  private float originalLatitude;
  private float originalLongitude;
  private float currentLongitude;
  private float currentLatitude;

  private GameObject distanceTextObject;
  private double distance;

  private bool setOriginalValues = true;

  private Vector3 targetPosition;
  private Vector3 originalPosition;

  private float speed = .1f;

  IEnumerator GetCoordinates()
  {
    // update/get device‘s gps coordinates
  }

  public void Calc(float lat1, float lon1, float lat2, float lon2)
  {// calculates distance between two sets of coordinates, taking into account the curvature of the earth
  }

  void Start(){
    // initialization
  }

  void Update(){
    // update each frame
  }
}

Script Comments:

void start(): initialization

void Start(){
    // get the reference to UIText distance to get the user gps coordinates
    distanceTextObject = GameObject.FindGameObjectWithTag ("distanceText");
    // keep getting the gps coordinates from the phone
    StartCoroutine ("GetCoordinates");
    // initialize target and original position, transform refers to the flying disk
    targetPosition = transform.position;
    originalPosition = transform.position;
}

void update(): update each frame

void Update(){
    // linearly interpolate from current position to target position
    transform.position = Vector3.Lerp(transform.position, targetPosition, speed);
    // rotate by 1 degree about the y axis every frame
    transform.eulerAngles += new Vector3 (0, 1f, 0);
}

Vector3.Lerp(): https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

public static Vector3 Lerp(Vector3 a, Vector3 b, float t)

Linearly interpolates between the vectors a and b by the interpolant t (t = [0,1])

When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway between a and b.

IEnumerator GetCoordinates(): update/get device‘s gps coordinates

IEnumerator GetCoordinates()
{
    // while true so this function keeps running once started.
    while (true) {
        // code from LocationService.Start documentation sample
        // https://docs.unity3d.com/ScriptReference/LocationService.Start.html

        // check if user has location service enabled
        if (!Input.location.isEnabledByUser)
            yield break;
        // Start service before querying location
        Input.location.Start (1f, 0.1f);
        // Wait until service initializes
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
            yield return new WaitForSeconds (1);
            maxWait--;
        }
        // Service didn‘t initialize in 20 seconds
        if (maxWait < 1) {
            print ("Timed out");
            yield break;
        }
        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed) {
            print ("Unable to determine device location");
            yield break;
        } else {
            // start doing the device‘s coordinates processing

            // Access granted and location value could be retrieved
            print ("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);

            // if original value has not yet been set, then save player‘s coordinates when app starts
            if (setOriginalValues) {
                // "private bool setOriginalValues = true;" at the start
                originalLatitude = Input.location.lastData.latitude;
                originalLongitude = Input.location.lastData.longitude;
                setOriginalValues = false;
            }
            //overwrite current lat and lon everytime
            currentLatitude = Input.location.lastData.latitude;
            currentLongitude = Input.location.lastData.longitude;
            //calculate the distance between where the player was when the app started and where they are now.
            Calc (originalLatitude, originalLongitude, currentLatitude, currentLongitude);
        }
        Input.location.Stop();
    }
}

Input.location.Start(): https://docs.unity3d.com/ScriptReference/LocationService.Start.html

public void Calc(float lat1, float lon1, float lat2, float lon2): calculates distance between two sets of coordinates, taking into account the curvature of the earth

public void Calc(float lat1, float lon1, float lat2, float lon2)
{
    var R = 6378.137; // Radius of earth in KM
    var dLat = lat2 * Mathf.PI / 180 - lat1 * Mathf.PI / 180;
    var dLon = lon2 * Mathf.PI / 180 - lon1 * Mathf.PI / 180;
    float a = Mathf.Sin(dLat / 2) * Mathf.Sin(dLat / 2) +
            Mathf.Cos(lat1 * Mathf.PI / 180) * Mathf.Cos(lat2 * Mathf.PI / 180) * Mathf.Sin(dLon / 2) * Mathf.Sin(dLon / 2);
    var c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1 - a));
    distance = R * c;
    distance = distance * 1000f; // meters
    //set the distance text on the canvas
    distanceTextObject.GetComponent<Text> ().text = "Distance: " + distance;
    //convert distance from double to float
    float distanceFloat = (float)distance;
    //set the target position of the ufo, this is where we lerp to in the update function
    targetPosition = originalPosition - new Vector3 (0, 0, distanceFloat * 12);
    //distance was multiplied by 12 so I didn‘t have to walk that far to get the UFO to show up closer
}    

Build:

switch platform in build settings to Android

in player settings >

Identification > packageName: com.Company.ProductName

Build

Install:

go to .../Android/sdk/platform-tools

confirm your mobile device by ./adb devices

install the signed .apk by ./adb install apk_dir

and it works!

Oh...not really. The geo-location can not be access since there is no permission for the app to access gps service

if (!Input.location.isEnabledByUser) yield break;

http://answers.unity3d.com/questions/38222/android-plugin-and-permissions.html

--> set permissions in manifest.xml

Assets > Plugins > Android > AndroidManifest.xml

add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

https://docs.unity3d.com/Manual/android-manifest.html;

https://developer.android.com/reference/android/Manifest.permission.html

And then I figure out the solution showed above is not a solution.

what actually happened in my device is the "Time Out", rather than "isEnabledByUser"

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #f4f4f4; background-color: #000000 }
span.s1 { }

时间: 2024-08-04 11:10:35

UniMelb Comp30022 IT Project (Capstone) - 2.Vuforia in Unity的相关文章

HoloLens开发手记 - 开始使用Vuforia Getting started with Vuforia

Vuforia在6.1版本的Unity SDK里实现了对HoloLens的支持. 查看 Developing for Windows 10 in Unity 这篇文章来了解如何配置Unity和Visual Studio的Windows 10开发环境. 了解Unity中Vuforia HoloLens项目结构的最好方式就是通过学习官方提供的示例项目,点此下载Unity HoloLens sample. 示例项目是一个完整的HoloLens Unity项目,里面包含了Vuforia SDK和预先配置

UMA - Unity Multipurpose Avatar

UMA version 1.0.1.0R Unity 4.3 What is UMA? UMA - Unity Multipurpose Avatar, is an open avatar creation framework, it provides both base code and example content to create avatars. Using the UMA pack, it ?s possible to customize the code and content

Team Foundation Server 2013 with Update 3 Install LOG

[Info   @10:14:58.155] ====================================================================[Info   @10:14:58.163] Team Foundation Server Administration Log[Info   @10:14:58.175] Version  : 12.0.30723.0[Info   @10:14:58.175] DateTime : 10/03/2014 18:1

Unity3D技术之Visual Studio C# 集成说明

欢迎来到unity学习.unity培训.unity企业培训教育专区,这里有很多U3D资源.U3D培训视频.U3D教程.U3D常见问题.U3D项目源码,我们致力于打造业内unity3d培训.学习第一品牌. Visual Studio C# 集成 我可以使用哪些功能? 更加复杂的 C# 开发环境.其中包括智能自动完成.计算机辅助更改源文件.智能语法高亮提示,还有其他更多功能. Express 和 Pro 有何不同? VisualStudio C# 2010 是 Microsoft 产品.它包括快速版

Unity 游戏移植到 Windows10 之路 -- 环境搭建

孙广东   2015.8.31 原文:http://blogs.msdn.com/b/windows__windows_game_dev_faq_/archive/2015/08/19/unity-windows10.aspx Windows10 是微软公司最新一代的跨平台及设备应用的操作系统.它统一了包括个人电脑.平板电脑.智慧型手机.嵌入式系统.Xbox One以及新产品Surface Hub和HoloLens等等的整个Windows产品系列的作业系统,共享一个通用的应用程式架构(UWP)和

Pok&#233;mon GO国内玩不了?腾讯AR专家教你自己做!

作者:Zero,腾讯测试开发工程师 本文由腾讯WeTest授权发布,如需转载请联系腾讯WeTest获得授权. WeTest导读 Pokémon Go一出,新鲜的玩法.经典的IP效应让这款使用了Unity以及AR技术的手游火遍了"大洋"南北.可惜的是这款新鲜的游戏还没有惠及中国市场的玩家们.腾讯内部的AR专家秉持着"一言不合就自己开发"的原则,自发对AR游戏进行了预研,本文将通过在Unity中对OpenCV及Vuforia库的使用,简单介绍制作AR游戏的一系列流程.

使用 Mono.Cecil 辅助 Unity3D 手游进行性能测试

Unity3D 引擎在  UnityEngine 名字空间下,提供了  Profiler 类(Unity 5.6 开始似乎改变了这个名字空间),用于辅助对项目性能进行测试.以 Android 平台为例,在构建之前,需要在 Unity 的 File/Build Settings 菜单项弹出的窗口中,勾选 Development Build 一项.后用  adb forward  的方式,将 Android 设备的 TCP 输出转发到电脑,实现和 Unity Profiler 的连接(网上很容易找到

Unity 游戏移植到 Windows10

Windows10 是微软公司最新一代的跨平台及设备应用的操作系统.它统一了包括个人电脑.平板电脑.智慧型手机.嵌入式系统.Xbox One以及新产品Surface Hub和HoloLens等等的整个Windows产品系列的作业系统,共享一个通用的应用程式架构(UWP)和Windows Store的生态系统.随着今年的7-29 号Windows10发布大会的召开,目前微软已经开始向全球用户推送Windows10 系统,在未来的一年内所有用户都将能免费升级. 虽然Unity引擎的官方正式版本5.1

Jenkins 搭建U3D自动发布 Android

工具 [u3d相关的PostProcessBuildPlayer,PerformBuild.cs] 1.Jenkins 开源包  Java -jar jenkins.war,参考链接 http://www.cnblogs.com/itech/archive/2011/11/02/2233343.html. 2.JDK 3.ANT 4.Eclipse 5.Jenkins 插件管理 Ant Plugin This plugin adds Apache Ant support to Jenkins.