纯代码集成AR进项目示例代码

注意:支持AR的苹果设备有: iPhone SE、iPhone 6s、iPhone 6s Plus、iPhone 7、iPhone 7 Plus、iPad Pro (所有型号)、9.7英寸iPad(2017款)。

项目代码:https://github.com/biyu6/ARTest01.git

先在info.plist中添加相机的访问权限,然后需要导入一个目录文件:art.scnassets/ship.scn


主要代码如下:

#import "MyARVC.h"
#import <ARKit/ARKit.h>//ARKit框架

@interface MyARVC ()
//AR视图:展示3D界面
@property(nonatomic,strong)ARSCNView *sceneView;
//AR会话:负责管理相机追踪配置及3D相机坐标
@property(nonatomic,strong)ARSession *arSession;
//会话追踪配置:负责追踪相机的运动
@property(nonatomic,strong)ARConfiguration *arConfiguration;

@end
@implementation MyARVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addARSceneView];
    [self addGoBackBtn];
}
- (void)addARSceneView{//添加AR视图
    //将AR视图添加到当前视图
    [self.view addSubview:self.sceneView];
    //开启AR会话(此时相机开始工作)
    [self.arSession runWithConfiguration:self.arConfiguration];
}
- (void)addGoBackBtn{//添加一个返回按钮
    UIButton *goBackBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 25, 35, 35)];
    [goBackBtn setImage:[UIImage imageNamed:@"AR_Back"] forState:UIControlStateNormal];
    [goBackBtn addTarget:self action:@selector(goBackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:goBackBtn];
}
- (void)goBackBtnClick{//返回
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //暂停ARSession会话
    [self.sceneView.session pause];
    NSLog(@"AR场景已暂停");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"%@======内存警告",[NSString stringWithUTF8String:object_getClassName(self)]);
}
- (void)dealloc{
    NSLog(@"%@======已释放",[NSString stringWithUTF8String:object_getClassName(self)]);
}

#pragma mark- 点击屏幕添加飞机
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //使用场景加载scn文件(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建)
    SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];//加载该目录下的3D飞机
    //获取飞机节点(一个场景有且只有一个根节点,但会有多个子节点)
    SCNNode *shipNode = scene.rootNode.childNodes[0];
    shipNode.position = SCNVector3Make(0, -1, -1);//调整x、y、z坐标相对于世界原点,也就是相机位置
    //将飞机节点添加到当前屏幕中
    [self.sceneView.scene.rootNode addChildNode:shipNode];
}

#pragma mark- 懒加载搭建AR界面
- (ARSCNView *)sceneView{//创建AR视图
    if (_sceneView == nil) {
        //创建AR视图
        _sceneView = [[ARSCNView alloc] initWithFrame:self.view.bounds];
        //设置视图会话
        _sceneView.session = self.arSession;
        //自动刷新灯光,默认就是YES
        _sceneView.automaticallyUpdatesLighting = YES;
    }
    return _sceneView;
}
- (ARSession *)arSession{//创建AR会话
    if(_arSession == nil){
        _arSession = [[ARSession alloc] init];
    }
    return _arSession;
}
- (ARConfiguration *)arConfiguration{//AR追踪配置
    if (_arConfiguration == nil) {
        //创建追踪会话配置(A9芯片以上)
        ARWorldTrackingConfiguration *configuration = [[ARWorldTrackingConfiguration alloc] init];
        //设置追踪方向(水平、垂直)
        configuration.planeDetection = ARPlaneDetectionHorizontal;
        _arConfiguration = configuration;
        //自适应灯光(相机从暗到强光快速过渡效果会平缓一些)
        _arConfiguration.lightEstimationEnabled = YES;
    }
    return _arConfiguration;
}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 有时候购买的商品,属性很多,想做成表格进行筛选。自然用技术宅的爬虫方式做了 ```python # -*- ...
    超哥__阅读 4,507评论 0赞 8
  • Chapter 1 构建自适应用户界面 @(Intermediate iOS 10 Programming wit...
    刘铭iOS阅读 2,043评论 0赞 1
  • 有时走过街前,会看一看路的远方,是否那看不见的尽头,也有人在这样张望。
    furx阅读 484评论 2赞 1
  • 另一种碎片(组诗) 神龟子 (一) 影子 垃圾车里躺着 一条死去的狗 我不忍多看一眼 穿过一条街 我回...
    AA神龟子阅读 331评论 0赞 1
  • 1.〖奸臣〗 这是一部不折不扣的“小黄片”。 但看完后觉得,眼前的黄不是黄,是一道刺穿黑夜的光,真正污秽的肮脏的,...
    苏莞儿阅读 536评论 0赞 8

友情链接更多精彩内容