一、创建AR项目
- Content Technology:SceneKit渲染框架
- 代码解析:
#import "ViewController.h"
@interface ViewController () <ARSCNViewDelegate>
//ARKit框架中用于3D显示的预览视图
@property (nonatomic, strong) IBOutlet ARSCNView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set the view's delegate
//设置视图代理
self.sceneView.delegate = self;
// Show statistics such as fps and timing information
//设置显示FPS和其他参数
self.sceneView.showsStatistics = YES;
// Create a new scene
//使用模型创建节点(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建,这里系统有一个默认的3D飞机)
//使用SceneKit资源文件创建场景对象
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
// Set the scene to the view
//设置ARKit的场景为SceneKit的当前场景(SCNScene是Scenekit中的场景,类似于UIView)
//把从文件中加载的场景设置到视图中
self.sceneView.scene = scene;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Create a session configuration
//创建一个追踪设备配置(ARWorldTrackingSessionConfiguration主要负责传感器追踪手机的移动和旋转)
//创建会话配置对象
ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];
// Run the view's session
// 开始启动ARSession会话(启动AR)
//运行当前视图自带的会话
[self.sceneView.session runWithConfiguration:configuration];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 暂停ARSession会话
//停止运行当前视图自带的会话
[self.sceneView.session pause];
}
二、流程
- 首先建立会话、然后配置会话、接着直接运行
- 在viewDidLoad方法中,首先设置好视图代理、初始化视图场景,此时视图会自带一个ARSession会话
- 然后在viewWillAppear:方法中开始初始化会话配置类、直接运行当前视图自带的会话
- 紧接着会话开始所有的处理操作
- 最后由ARSCNView渲染视图