Unity 结合ARKit

主要类 UnityARCameraManager
调用顺序
//创建ARSerrion
m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
//创建Configuration
ARKitWorldTrackingSessionConfiguration //6自由度

Paste_Image.png

ARKitSessionConfiguration //3自由度

Paste_Image.png

config.planeDetection //设置的话 会根据场景来寻找平面 ARKit会自动添加一个ARPlaneAnchor对象到该session
config.alignment //AR坐标系与真实坐标系转换
UnityARAlignmentGravity

Paste_Image.png

UnityARAlignmentGravityAndHeading

Paste_Image.png

UnityARAlignmentCamera
只根据当前设备的位置进行显示

config.enableLightEstimation //开启光照捕捉
config.getPointCloudData //获得帧数据点云信息

m_session.RunWithConfig(config); //运行
m_session.RunWithConfigAndOptions(config, option) //重置设备的角度位置信息

//设置AR场景中的虚拟摄像机位置
UnityARCamera scamera = new UnityARCamera ();
scamera.worldTransform = new UnityARMatrix4x4 (new Vector4 (1, 0, 0, 0), new Vector4 (0, 1, 0, 0), new Vector4 (0, 0, 1, 0), new Vector4 (0, 0, 0, 1));
Matrix4x4 projMat = Matrix4x4.Perspective (60.0f, 1.33f, 0.1f, 30.0f);
scamera.projectionMatrix = new UnityARMatrix4x4 (projMat.GetColumn(0),projMat.GetColumn(1),projMat.GetColumn(2),projMat.GetColumn(3));

    UnityARSessionNativeInterface.SetStaticCamera (scamera);

获得ar场景中设备的位置
在Update中调用m_session.GetCameraPose() 获得虚拟摄像机的位置 经过计算 转换成实际的 摄像机位置 设置到MainCamera上

在MainCamera中添加UnityARVideo脚本 并设置Clearmaterial为YUVMaterial 改脚本会将ARKit反悔的图片数据根据材质进行合成

调用UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest(ARPoint, ARHitTestResultType)//由摄像机发射射线 将碰撞的anchor由近至远返回
ARHitTestResultType有五种碰撞类型
/** Result type from intersecting the nearest feature point. */
ARHitTestResultTypeFeaturePoint = (1 << 0), //基本的anchor点

    /** Result type from detecting and intersecting a new horizontal plane. */
    ARHitTestResultTypeHorizontalPlane  = (1 << 1), //无限的预估水平平面

    /** Result type from detecting and intersecting a new vertical plane. */
    ARHitTestResultTypeVerticalPlane    = (1 << 2), //无限的预估垂直平面

    /** Result type from intersecting with an existing plane anchor. */
    ARHitTestResultTypeExistingPlane    = (1 << 3), //无限范围的真实平面

    /** Result type from intersecting with an existing plane anchor, taking into account the plane's extent. */
    ARHitTestResultTypeExistingPlaneUsingExtent  = ( 1 << 4) //实际范围的真实平面
Paste_Image.png

如果之前config中设置平面捕捉(planeDetection ) 可以通过回调UnityARSessionNativeInterface.ARAnchorAddedEvent, UnityARSessionNativeInterface.ARAnchorUpdatedEvent, and UnityARSessionNativeInterface.ARAnchorRemovedEvent. 获得设置ARPlaneAnchor 信息
demo中是在UnityARAnchorManager中进行了实现

如果想要获得帧数据 添加UnityARSessionNativeInterface.ARFrameUpdatedEvent 回调 demo中是在PointCloudParticleExample中进行实现

如果设置了获取光照信息 可以通过arcamera.lightEstimation.ambientIntensity; 来进行获取 demo中在UnityARAmbient中 进行了设置

UnityARUserAnchorComponent。cs中 介绍了如何添加自定义Anchor 这样我们就可以在自定义的anchor平面上 使用session进行操作
UnityARSessionNativeInterface.ARSessionInterruptedEvent 添加session中断回调
UnityARSessionNativeInterface.ARSessionTrackingChanged 追踪中断回调即ARCamera tracking state

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS ARKit 教程:不触摸屏幕,用空气中的手势作画 本文翻译自 iOS ARKit Tutorial: Dr...
    零度_不结冰阅读 5,813评论 0 6
  • ARkit Introducing ARKit iOS 11引入ARKit,这是 个全新的框架,允许开发者轻松地为...
    坤哥爱卿阅读 5,228评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,502评论 19 139
  • 从没想过自己会迷上大秦帝国,会对战国有兴趣,秦国起初是多么的弱小穷困,赳赳老秦,共赴国难,与当时的霸主魏国的正面交...
    萝卜蹲蹲bibi阅读 1,144评论 0 0
  • 奥地利诗人里尔克写了一首很牛逼的诗,叫做“秋日”,读起来温婉爽快,齿感绵延不绝——当然,这也归功于北岛翻译的好: ...
    佛祖保佑我主阅读 11,432评论 69 126