当前位置:网站首页>ARFoundation从零开始8-Geospatial API(地理空间)开发
ARFoundation从零开始8-Geospatial API(地理空间)开发
2022-07-29 05:07:00 【suelee_hm】
ARCore Geospatial API为开发者带来了Live View的底层技术,包括大规模云锚点功能,以及全球视觉定位系统(VPS)的访问权限,可用于开发多人共享的AR应用。
该技术基于谷歌地球3D模型数据和街景图像数据,原理是将数百亿张街景图像转换为支持VPS定位的3D点云,共提取了数万亿个3D数据。只需不到一秒时间,便可根据3D点云数据定位设备的位置和方向。
项目示例源代码:
https://github.com/sueleeyu/ar-geospatial
一、创建项目工程和安装ARCore Extensions软件包
详见:
https://blog.csdn.net/weixin_40239288/article/details/125328974
二、设置google cloud项目
使用视觉定位系统(VPS),需要关联一个 Google Cloud 项目, 并在该Google Cloud 项目中启用ARCore API,新建Google Cloud项目:
1.访问:https://console.cloud.google.com/projectcreate,新建项目。
2.点击创建后新建成功:

3.侧边栏菜单选择API和服务-库:

4.输入ARCore API并搜索

5.点击选择ARCore API

6.点击启用

7.进入如下页面

三、设置授权
应用需要授权后,才能调用Geospatial API,授权有无密钥授权和API密钥授权,推荐使用无密钥授权。
实现API密钥授权:
1.转到 Cloud Console 中的 API 和服务 → 凭据面板,选择创建凭据,然后从下拉菜单中选择 API 密钥:

创建成功后复制该密钥:

复制该密钥
2.在 Unity 中,转到 Edit > Project Settings > XR > ARCore Extensions。 Android Authentiation Strategy 下拉菜单中,选择 API Key

3.在Android API Key 字段中,粘贴从 Google Cloud Platform 获得的 API 密钥,并勾选Optional Features 下的两个要使用的API的复选框:

授权完成。下一步启用Geospatial API。
四、启用Geospatial API
1.Assets下选择 ARCoreExtensionsConfig 脚本对象,然后在 Inspector 窗口中,下拉 Geospatial Mode 选择 Enabled。

五、编写脚本
1. 挂载AREarth Manager,选择Hierarchy-AR Session Origin-Add Component-AR Earth Manager:

2.Hierarchy-‘+’-Create Empty,命名为GeospatialController;Assets-Scripts右键新建GeospatialController.cs,拖动到GeospatialController对象:


获取位置:
主要功能在update()中实现:
1.判断检查设备兼容性,调用:
EarthManager.IsGeospatialModeSupported(GeospatialMode.Enabled);
需返回 FeatureSupported.Supported才可。
2.检查跟踪状态
var earthTrackingState = EarthManager.EarthTrackingState;
仅当earthTrackingState==rackingState.Tracking时,地理空间值才有效。如下所示:
var earthTrackingState = EarthManager.EarthTrackingState;
if (earthTrackingState == TrackingState.Tracking)
{
// Values obtained by the Geospatial API are valid as long as
// earthTrackingState is TrackingState.Tracking.
// Use Geospatial APIs in this block.
}
3.获取相机地理空间姿态。REarthManager管理相机地理空间姿态,当TrackingState为Tracking时,请求相对于地球的相机位置方向:
var earthTrackingState = EarthManager.EarthTrackingState;
if (earthTrackingState == TrackingState.Tracking)
{
// camera_geospatial_pose contains geodetic location, rotation, and
// confidences values.
var cameraGeospatialPose = EarthManager.CameraGeospatialPose;
}
cameraGeospatialPose里包含了地理空间姿态的信息:
a.以纬度和经度表示的位置。此外,还会提供位置信息精确度的估算值。
b.海拔,以及海拔高度的估值。
c.方向、设备所朝的方向的近似值,以及对航向的准确性的估算值。
放置地理空间锚点:
使用ARAnchorManagerExtensions中ARAnchorManager.AddAnchor创建ARGeospatialAnchor,以将内容锚定到指定的地理坐标。
在定位时相对于地球的位置和方向放置锚点时,纬度和经度由 WGS84 规范定义,海拔高度值由 WGS84 椭球体的高度(以米为单位)定义。身份旋转以锚点朝向进行,使 X+ 指向东方,Y+ 指向上方,远离地球中心,Z+ 指向南方。
private bool PlaceGeospatialAnchor(GeospatialAnchorHistory history)
{
Quaternion quaternion =
Quaternion.AngleAxis(180f - (float)history.Heading, Vector3.up);
var anchor = AnchorManager.AddAnchor(
history.Latitude, history.Longitude, history.Altitude, quaternion);
if (anchor != null)
{
GameObject anchorGO = Instantiate(GeospatialPrefab, anchor.transform);
_anchorObjects.Add(anchorGO);
return true;
}
return false;
}
注意:不支持在北极或南极(90 度或 -90 度)内创建锚点。如果尝试使用此类锚点,AnchorManager.AddAnchor 会返回 null。
计算海拔[Geospatial 文档]:
获取放置锚点的海拔高度有点棘手。您必须将设备位置的海拔与 Geospatial API 的海拔高度进行比较。确定海拔高度的方法有两种:
如果锚点的位置靠近用户,请考虑使用与设备海拔高度相似的海拔高度。
否则,请考虑从 Google 地图的高度获取海拔值。
使用相机 GeospatialPose 的海拔高度
您可能只需要使用从 AREarthManager.CameraGeospatialPose 获取的海拔高度,而无需交叉检查来自其他来源(如 Maps API)的数据,可能足够了。请参阅上文获取相机地理空间位置方向。
如果您能通过自行观察局部位置提前获取该位置姿势,则可以使用该数据交叉检查应用为用户获取的 GeospatialPose。
从 Google 地图获取海拔
GeospatialPose 用于报告 WGS84 椭球体上方的海拔高度(以米为单位)。您的应用需要将 GeospatialPose.Altitude 的值与设备位置的海拔进行比较,以确定锚点的准确海拔高度。
您可以使用 Google 地图获取纬度和经度。请务必先关闭“地球视图”,方法是导航至图层 > 更多,然后取消选中屏幕左下角的地球视图复选框。这将会强制执行 2D 视角,并消除角度 3D 视图可能产生的错误。
确定纬度和经度后,请使用 Elevation API 根据 EGM96 规范获取高度。您必须将 Maps API EGM96 海拔转换为 WGS84,才能与 GeospatialPose 海拔进行比较。请参阅同时包含命令行和 HTML 接口的 GeoidEval 转换工具。Maps API 按照开箱即用的 WGS84 规范报告纬度和经度。
六、UI和界面
1.创建ARView Panel,点击‘+’-UI-Panel,命名为ARView,remove component Image和Canvas Renderer


2.ARView下右键新建InfoPanel

设置属性如下:

3.InfoPanel下新建Text,命名InfoText,设置:

4.ARView下新建Panel,命名SnackBar,设置:

5.SnackBar下新建Text,命名DebugText,设置:


6.SnackBar下新建Text,命名SnackBarText,设置:


7. SnackBar下新建Button,命名SetAnchorButton,设置:

SetAnchorButton-Text:

8. SnackBar下新建Button,命名ClearAllButton,设置:

ClearAllButton-Text:

9.Canvas下右键-UI-Panel,命名PrivacyPrompt,PrivacyPrompt下右键新建Panel,命名FullscreenBackground,设置:

10. PrivacyPrompt下右键新建Panel,命名PrivacyContent,设置:

PrivacyContent下新建Panel,命名PrivacyPromptBackground,设置:

PrivacyContent下新建Panel,命名Information Panel,设置:

11. Information Panel下新建Text,命名Title Text,设置:

Information Panel下新建Panel,命名Text Area,设置:

Text Area下新建Text,命名Information Text,设置:

Information Text下新建Button,命名URL Button,删除URL Button下Text,设置:

Start Button设置click事件,调用GeospatialController. OnLearnMoreClicked ():

Information Panel下新建Button,命名Start Button,设置:

Start Button设置click事件,调用GeospatialController. OnGetStartedClicked():

七、打包运行


七、常见问题
1. 使用ARCore Extension API例子程序:Window-Package Manager-ARCore Extensions-Samples,点击Import按钮导入要使用的示例代码。

2.错误CS0234:命名空间“UnityEditor”中不存在类型或命名空间名称“iOS”:
解决:安装UnitySetup-iOS-Support-for-Editor-2020.2.6f1。
八、参考文献
1. Unity api:
Unity - Manual: Unity User Manual 2021.3 (LTS)
2. Geospatial 文档:
使用 ARCore Geospatial API 打造全球规模的沉浸式 AR 体验 | Google Developers
3.Google AR:
https://github.com/google-ar
https://github.com/google-ar/arcore-unity-extensions
4.本项目示例源代码:
边栏推荐
- How to add a map to the legendary server
- 开区网站打开自动播放音乐的添加跟修改教程
- Wechat picture identification
- The method and detailed code of automatically pop-up and QQ group when players visit the website
- Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
- [2022 freshmen learning] key points of the third week
- 力扣------对奇偶下标分别排序
- TCP three handshakes and four waves
- Mysql多对多关系,分组拼接把多个数据查询到一条数据上
- Exception - ...MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is ...
猜你喜欢

Network Security Learning - Intranet Security 1

WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!

MySQL sorts the queried result set according to the specified sequence

How does word view document modification traces? How word views document modification traces

scikit-learn——机器学习应用开发的步骤和理解

Use annotation test in idea

MySQL定时调用预置函数完成数据更新

How to set row height and column width in excel? The method of setting row height and column width in Excel

后置通知的流程分析与功能实现有哪些内容你还记得吗?

Solution | get the relevant information about the current employees' highest salary in each department |
随机推荐
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
Big silent event Google browser has no title
深度学习刷SOTA的一堆trick
[file download] easyexcel quick start
[config] configure array parameters
Google GTEST event mechanism
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
sql日志
一文带你搞懂环绕通知@Around与最终通知@After的实现
Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!
Pytorch learning notes
Legend how to configure multiple versions of wechat updates on one server
How to open IE browser by running win command
Vivo market API event reporting and docking
Solution | get the relevant information about the current employees' highest salary in each department |
ThreadPoolExecutor simple to use
What if excel is stuck and not saved? The solution of Excel not saved but stuck
ODOO开发教程之透视表