Appearance
基于 ARFoundation 在 Unity 中接入
基于ARFoundation算法实现,提供云定位算法能力
1. 获取插件
插件下载地址:点我下载
插件版本支持: Unity2020 及以上
或参考示例工程:点我下载
2. 更新说明
- V 1.0.0
基于 ARFoundation 算法实现,提供云定位算法能力
3. 接入流程
本文示例代码是基于 Unity2022 版本
- 选择合适的 Unity 版本,更新必要的 Package。如有部分依赖库,建议也添加。
- AR Foundation
- Google ARCore XR Plugin
- Apple ARKit XR Plugin
- OpenXR Plugin
- XR Core Utilities
- XR Interaction Toolkit
- XR Plugin Management
- 导入插件,并打开示例场景 SpatialComputing 。导入文件结构见下图。
3.1 场景说明
场景中关键配置建下图
2. 示例代码
示例代码脚本 SpatialComputingTest.cs
csharp
//启动AR
//1.拷贝算法文件至可读路径下)
//2.初始化VPS,成功后默认自动云定位
//for copy asset
public string assetsDirectory_ios = "";
public string assetsDirectory_android = "";
public List<string> filesToCopy_android = new List<string>() { "", "", ""};
private CopyStreamingAssets m_CopyStreamingAssets;
void Start()
{
if (m_CopyStreamingAssets == null)
{
m_CopyStreamingAssets = gameObject.AddComponent<CopyStreamingAssets>();
if (m_CopyStreamingAssets != null)
{
m_CopyStreamingAssets.assetsDirectory_ios = assetsDirectory_ios;
m_CopyStreamingAssets.assetsDirectory_android = assetsDirectory_android;
m_CopyStreamingAssets.filesToCopy_android = filesToCopy_android;
m_CopyStreamingAssets.OnCopyCompleted += OnCopyCompleted;
}
}
}
void OnCopyCompleted(string assetPath)
{
if (assetPath == null)
{
UnityEngine.Debug.LogError("configuration resources assetPath is error");
return;
}
UnityEngine.Debug.Log("OnCopyCompleted : " + assetPath);
if (m_SpatialComputingController != null)
{
m_SpatialComputingController.InitVPS(assetPath);
}
}
csharp
//监听云定位结果
public SpatialComputingController m_SpatialComputingController;
void Start()
{
if (m_modelGO != null) {
m_modelGO.SetActive(false); // 默认启动后隐藏
}
}
// Update is called once per frame
void Update()
{
if (m_SpatialComputingController != null)
{
var status = m_SpatialComputingController.currentVpsResultState.vps_result_status;
if (status == LOCSTATUS.SUCCESS)
{
if (m_modelGO != null && m_modelGO.activeSelf == false)
{
m_modelGO.SetActive(true);// 显示虚拟内容
}
}
}
}
3.2 必要参数说明
EZXRCoreExtensions.SpatialComputing.SpatialComputingController, 算法的参数及状态相关数据。
- m_url : string,云定位地址。
- m_vpsRequestAuto :bool,是否开启自动定位,true 开启,false 关闭。
- currentVpsResultState : SC_VPSResultState,云定位结果,相关结构体参数见下图。
csharp
//SC_VPSResultState
public struct SC_VPSResultState
{
// 如果时间戳小于0,说明是无效时间戳
public double t_s; //unix时间戳,以s为单位,精确到小数点后3或4位
public LOCSTATUS vps_result_status;
}
public enum LOCSTATUS
{
SUCCESS = 0x01, //定位成功,返回pose等信息
FAIL_UNKNOWN = 0x10, //定位失败,走完了定位算法流程,但是图像无法定位到给定地图中
FAIL_MATCH = 0x11, //定位失败,具体原因1 hy: not used
FAIL_INLIER = 0x12, //定位失败,具体原因2 hy: not used
INVALID_DEVICEINFO = 0x20, //数据不合法,传入的protobuf.deviceInfo不符合规范
INVALID_LOCALIZER = 0x21, //数据不合法,部署阶段的localizer未成功初始化
INVALID_IMAGE = 0x22, //数据不合法,传入的图像或protobuf.deviceInfo中出现不被接受的图像格式(仅接收通道数为1或3,且类型为CV_8U或CV_8UC3的图像)
INVALID_IMAGE_PROTO_MATCH = 0x23, //数据不合法,传入的图像文件长度,与protobuf.deviceInfo中记录的图像字节数不匹配
INVALID_MESSAGE = 0x24, //传入的message不合法
FAIL_SUMMARY_UNKNOWN = 0x30, //hy: not used
FAIL_SUMMARY_NOMAP = 0x31 //未加载完成可用的summary map hy: not used
}
4. 建议说明
- 开发时,虚拟内容请参考建图产物(Mesh 或点云文件)进行摆放;