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 coordinatesdistanceTextObject = GameObject.FindGameObjectWithTag ("distanceText");// keep getting the gps coordinates from the phoneStartCoroutine ("GetCoordinates");// initialize target and original position, transform refers to the flying disktargetPosition = transform.position;originalPosition = transform.position;
}

void update(): update each frame

void Update(){// linearly interpolate from current position to target positiontransform.position = Vector3.Lerp(transform.position, targetPosition, speed);// rotate by 1 degree about the y axis every frametransform.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 enabledif (!Input.location.isEnabledByUser)yield break;// Start service before querying location
        Input.location.Start (1f, 0.1f);// Wait until service initializesint maxWait = 20;while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {yield return new WaitForSeconds (1);maxWait--;}// Service didn't initialize in 20 secondsif (maxWait < 1) {print ("Timed out");yield break;}// Connection has failedif (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 retrievedprint ("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 startsif (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 everytimecurrentLatitude = 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 KMvar 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 canvasdistanceTextObject.GetComponent<Text> ().text = "Distance: " + distance;//convert distance from double to floatfloat distanceFloat = (float)distance;//set the target position of the ufo, this is where we lerp to in the update functiontargetPosition = 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"

转载于:https://www.cnblogs.com/FudgeBear/p/7493053.html

UniMelb Comp30022 IT Project (Capstone) - 2.Vuforia in Unity相关推荐

  1. 【Vuforia AR Unity 2018.3.12f1】MikuAR安卓程序开发实践(三)代码终结篇_2019.4.24

    Unity平台 + Vuforia SDK实现的AR程序开发 模型的三大操作(平移 旋转 缩放)代码 一.平移 二.旋转缩放 三.操作代码解析 模型的选定(射线法) 食用方法 自发光组件 食用方法 V ...

  2. vuforia for unity 入门教程

    (一)配置vuforia环境 与 运行简单AR程序 1.   安装unity2017 1)从官网上下载unity2017下载器 2)运行下载器勾选上vuforiasupport选项,其余不变,点击开始 ...

  3. vuforia ar unity 案例 从零开发 二 扫描图片展示模型

    首先打开vuforia官网 找到之前上传的扫描图 如没有点击add database上传  点击download 选择unityeditor   然后导入到unity里边 然后点击licenseman ...

  4. 【Vuforia AR Unity 2018.3.12f1】MikuAR安卓程序开发实践(二)MMD篇_2019.4.23

    Unity平台 + Vuforia SDK实现的AR程序开发 !版权声明 项目源码 关于Github项目下载过慢的解决方法 关于Gitee下载报错的问题 一.解析 1 源码目录 2 Unity项目目录 ...

  5. 【无需代码】利用Vuforia for Unity实现简单的AR功能

    准备工作: Unity2018+以上,目前高通Vuforia官网已经不支持下载,因为最新的Vuforia引擎已经集合到Unity2018以及更高版本里 Unity2018以下请自行下载官方包,百度搜索 ...

  6. vuforia ar unity 案例从零制作 三 番外 假如想在一个程序扫描多个图片,出现不同模型

    如果我们一张图做一个程序 app显然不合理,至少我们得做一个类别的,一个系列的,多个图包含的 那么经过测试,实现其实很简单 我们选中iamgetarget 按下 ctrl+D 复制一份,吧他的data ...

  7. 利用Vuforia、Unity 实现AR识别图触发音频播放

    这篇文章 主要归功于AR学院的大神们!! 原文链接:原文链接 我在这里做个补充. 实现功能:扫描到识别图显示模型的同时播放对应的声音,移走识别图暂停播放,再识别时从上次暂停的地方开始播放. 基本流程: ...

  8. Vuforia开发完全指南

    作者:Mac玩家 原文:Vuforia开发完全指南(二)--- 不懂编程也能做AR程序 链接:https://www.jianshu.com/p/5871935aba02 來源:简书 简书著作权归作者 ...

  9. 使用Vuforia创建神奇宝贝GO风格增强现实游戏

    您将要创造的 1.简介 在本系列的第一篇文章中 ,我们讨论了Vuforia在创建增强现实体验方面的出色表现,现在我们准备在实际的应用程序中实践这些概念. 在本教程中,我们将开始在Unity 3D上使用 ...

  10. [Vuforia] 详解·高通Vuforia识别追踪3D物体/模型,Unity开发

    前期硬件准备工作: 1.安卓机一台 2.电脑.软件就不说了 3.安卓机扫描3D物体的Scanner工具包下载:https://developer.vuforia.com/downloads/tool ...

最新文章

  1. C 语言编程 — 数据类型转换
  2. String,StringBuffer,StringBuilder的区别
  3. 2020计算机基础知识考试题及答案,2020年计算机二级公共基础知识考试模拟习题及答案...
  4. error:use of undeclared identifier
  5. IE6/IE7/Firefox浏览器不兼容原因及解决办法
  6. bugku 杂项 就五层你能解开吗_长春老旧小区加装电梯,你家符合条件吗?_媒体_澎湃新闻...
  7. Huawei LiteOS简介
  8. 使用 Hyper-v 虚拟化域控制器
  9. VC++ 6.0的一些使用技巧---IDE的使用
  10. 海量数据挖掘MMDS week6: 支持向量机Support-Vector Machines,SVM
  11. 逆向project第003篇:跨越CM4验证机制的鸿沟(上)
  12. www读取本地图片做微缩图
  13. 如何开通企业付款到零钱||小程序红包功能
  14. 分销APP联盟商家入驻商城系统开发
  15. Houdini学习笔记——【案例二】消散文字制作
  16. HTML/HTML5
  17. 学习常用SHEEL脚本1
  18. 讲义六 之 docker 搭建测试环境以及部署项目包 created by 爱软测_bill
  19. 2021年了,还依赖PC端报表?推荐几款好用的移动报表软件给你!
  20. 小白玩PVE proxmox RX560D显卡直通

热门文章

  1. 前段之路心得——(1)
  2. Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题
  3. 拖拽上传技术-----html5[转载]
  4. 使用linux内核,打造自己的linux
  5. spring boot 2.0 与FASTDFS进行整合
  6. 【EasyNetQ】- 发布
  7. SQL SERVER数据库优化相关资料
  8. 调试Python的方式
  9. win32汇编-窗口 对话框 (三)
  10. 新建一个包,并生成可以直接在命令行执行的指令