ARKit框架API简介

ARAnchor

物体的3D锚点,表示物体在3D空间的位置和方向

/**
 标识符
 */
@property (nonatomic, readonly) NSUUID *identifier;

/**
 锚点的旋转变换矩阵,定义了锚点的旋转、位置、缩放。是一个4x4的矩阵(读者可以自行科普什么叫4x4矩阵)
 */
@property (nonatomic, readonly) matrix_float4x4 transform;

/**
 构造方法,一般我们无需构造。因为添加一个3D物体时ARKit会有代理告知我们物体的锚点
 */
- (instancetype)initWithTransform:(matrix_float4x4)transform;

ARFrame

主要用于追踪相机的当前状态,包括位置、图像帧以及时间等参数

/**
时间戳.
 */
@property (nonatomic, readonly) NSTimeInterval timestamp;

/**
 缓冲区图像帧
 */
@property (nonatomic, readonly) CVPixelBufferRef capturedImage;

/**
相机(表示这个ARFrame是哪一个相机的,iPhone7plus有两个摄像机)
 */
@property (nonatomic, copy, readonly) ARCamera *camera;

/**
 返回当前相机捕捉到的锚点数据(当一个3D虚拟模型加入到ARKit中时,锚点值得就是这个模型在AR中的位置)
 */
@property (nonatomic, copy, readonly) NSArray<ARAnchor *> *anchors;

/**
灯光(指的是灯光强度 一般是0-2000,系统默认1000)
 */
@property (nonatomic, copy, nullable, readonly) ARLightEstimate *lightEstimate;

/**
特征点(捕捉平地或者人脸的,比较苹果有自带的人脸识别功能)
 */
@property (nonatomic, nullable, readonly) ARPointCloud *rawFeaturePoints;

/**
根据2D坐标点搜索3D模型,这个方法通常用于,当我们在手机屏幕点击某一个点的时候,可以捕捉到这一个点所在的3D模型的位置,至于为什么是一个数组非常好理解。手机屏幕一个是长方形,这是一个二维空间。而相机捕捉到的是一个由这个二维空间射出去的长方体,我们点击屏幕一个点可以理解为在这个长方体的边缘射出一条线,这一条线上可能会有多个3D物体模型
point:2D坐标点(手机屏幕某一点)
ARHitTestResultType:捕捉类型  点还是面
(NSArray<ARHitTestResult *> *):追踪结果数组  详情见本章节ARHitTestResult类介绍

 */
- (NSArray<ARHitTestResult *> *)hitTest:(CGPoint)point types:(ARHitTestResultType)types;

/**
相机窗口的的坐标变换(可用于相机横竖屏的旋转适配)
 */
- (CGAffineTransform)displayTransformWithViewportSize:(CGSize)viewportSize orientation:(UIInterfaceOrientation)orientation;

ARHitTestResult

点击的回调结果,通常用于获取ARKit的捕捉结果,获取物体到相机的距离/位置、方向等

//捕捉类型枚举
typedef NS_OPTIONS(NSUInteger, ARHitTestResultType) {
    /** 点. */
    ARHitTestResultTypeFeaturePoint              = (1 << 0),

    /** 水平面 y为0. */
    ARHitTestResultTypeEstimatedHorizontalPlane  = (1 << 1),

    /** 已结存在的平面. */
    ARHitTestResultTypeExistingPlane             = (1 << 3),

    /** 已结存在的锚点和平面. */
    ARHitTestResultTypeExistingPlaneUsingExtent  = (1 << 4),
} NS_SWIFT_NAME(ARHitTestResult.ResultType);

/**
捕捉类型
 */
@property (nonatomic, readonly) ARHitTestResultType type;

/**
 3D虚拟物体与相机的距离(单位:米)
 */
@property (nonatomic, readonly) CGFloat distance;

/**
本地坐标矩阵(世界坐标指的是相机为场景原点的坐标,而每一个3D物体自身有一个场景,本地坐标就是相对于这个场景的坐标)类似于frame和bounds的区别
 */
@property (nonatomic, readonly) matrix_float4x4 localTransform;

/**
世界坐标矩阵
 */
@property (nonatomic, readonly) matrix_float4x4 worldTransform;

/**
 锚点(3D虚拟物体,在虚拟世界有一个位置,这个位置参数是SceneKit中的SCNVector3:三维矢量),而锚点anchor是这个物体在AR现实场景中的位置,是一个4x4的矩阵
 */
@property (nonatomic, strong, nullable, readonly) ARAnchor *anchor;

ARLightEstimate

灯光效果

/**
灯光强度  范围0-2000 默认1000
 */
@property (nonatomic, readonly) CGFloat ambientIntensity;

ARPlaneAnchor

平面锚点,是ARAnchor的子类,ARKit可以自动识别平面并且添加一个锚点到场景中

所有被检测到的表面都会关联到一个ARPlaneAnchor对象,并可进一步使用提供物体到镜头的距离、位置和方向的ARHitTestResult进行描述。ARPlaneAnchorARAnchor的一个子类实例,可添加到AR场景中,实现在场景中放置虚拟物体。

/**
平面类型,目前只有一个,就是水平面
 */
@property (nonatomic, readonly) ARPlaneAnchorAlignment alignment;

/**
3轴矢量结构体,表示平地的中心点  x/y/z
 */
@property (nonatomic, readonly) vector_float3 center;

/**
3轴矢量结构体,表示平地的大小(宽度和高度)  x/y/z
 */
@property (nonatomic, readonly) vector_float3 extent;

ARPointCloud

点状渲染云 主要用于渲染场景

/**
 点的数量
 */
@property (nonatomic, readonly) NSUInteger count;

/**
每一个点的位置的集合(结构体带*表示的是结构体数组)
 */
@property (nonatomic, readonly) const vector_float3 *points;

ARConfiguration

会话追踪配置,主要就是追踪相机的位置

子类ARWorldTrackingConfiguration

/**
当前设备是否支持,一般A9芯片以下设备不支持
 */
@property(class, nonatomic, readonly) BOOL isSupported;

/**
会话的对齐方式,这里的对其指的是3D世界的坐标。枚举值见下方
 */
@property (nonatomic, readwrite) ARWorldAlignment worldAlignment;

typedef NS_ENUM(NSInteger, ARWorldAlignment) {
    ARWorldAlignmentGravity,//相机位置
    ARWorldAlignmentGravityAndHeading,//相机位置及方向
    ARWorldAlignmentCamera//相机方向
} 

/**
是否需要自适应灯光效果,默认是YES
 */
@property (nonatomic, readwrite, getter=isLightEstimationEnabled) BOOL lightEstimationEnabled;

@end


//世界会话追踪配置,苹果建议我们使用这个类,这个子类只有一个属性,也就是可以帮助我们追踪相机捕捉到的平地
@interface ARWorldTrackingSessionConfiguration : ARSessionConfiguration

/**
侦查类型。枚举值见下方(默认侦查平地)
 */
@property (nonatomic, readwrite) ARPlaneDetection planeDetection;

ARError

描述ARKit中的错误信息

//作用域,一般会表示是哪一个类出现问题
NSString *const ARErrorDomain;
typedef NS_ERROR_ENUM(ARErrorDomain, ARErrorCode) {
    /** Unsupported session configuration. */
    ARErrorCodeUnsupportedConfiguration   = 100,//不支持会话追踪配置,由于A9芯片以下的机型会报错

    /** A sensor required to run the session is not available. */
    ARErrorCodeSensorUnavailable          = 101,//失活状态

    /** A sensor failed to provide the required input. */
    ARErrorCodeSensorFailed               = 102,//传感器故障

    /** World tracking has encountered a fatal error. */
    ARErrorCodeWorldTrackingFailed        = 200,//追踪失败
};