GitHub 中文社区iOSiOS 艾欧艾斯

ARKit从入门到精通(1)-ARKit初体验

2017-06-07  本文已影响6375人  坤小

ARKit从入门到精通(1)-ARKit初体验

转载请标注出处:http://www.jianshu.com/p/c97b230fa391

0901.gif 0902.gif 1001.gif 1101.gif

1.1-AR技术简介

0101.png

1.2-ARKit概述及特点介绍

1.3-ARKit初体验之3D效果

0102.png 0103.png 0104.png

#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
    //ARKit统计信息
    self.sceneView.showsStatistics = YES;
    
    // Create a new scene
    //使用模型创建节点(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建,这里系统有一个默认的3D飞机)
    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主要负责传感器追踪手机的移动和旋转)
    ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new];
    
    // Run the view's session
    // 开始启动ARSession会话(启动AR)
    [self.sceneView.session runWithConfiguration:configuration];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // Pause the view's session
    // 暂停ARSession会话
    [self.sceneView.session pause];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

0105.png 0106.gif

1.2-ARKit初体验之2D效果

0107.png 0108.png


#import "ViewController.h"
#import "Scene.h"

@interface ViewController () <ARSKViewDelegate>

//ARSKView是ARKit框架中负责展示2D AR的预览视图
@property (nonatomic, strong) IBOutlet ARSKView *sceneView;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Set the view's delegate
    //设置场景视图代理
    self.sceneView.delegate = self;
    
    // Show statistics such as fps and node count
    
    //显示帧率
    self.sceneView.showsFPS = YES;
    //显示界面节点(游戏开发中,一个角色对应一个节点)
    self.sceneView.showsNodeCount = YES;
    
    // Load the SKScene from 'Scene.sks'
    //加载2D场景(2D是平面的)
    Scene *scene = (Scene *)[SKScene nodeWithFileNamed:@"Scene"];
    
    // Present the scene
    //AR预览视图展现场景(这一点与3D视图加载有区别)
    [self.sceneView presentScene:scene];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // Create a session configuration
    //创建设备追踪设置
    ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new];
    
    // Run the view's session
    
    //开始启动AR
    [self.sceneView.session runWithConfiguration:configuration];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // Pause the view's session
    [self.sceneView.session pause];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - ARSKViewDelegate


//点击界面会调用,类似于touch begin方法  anchor是2D坐标的瞄点
- (SKNode *)view:(ARSKView *)view nodeForAnchor:(ARAnchor *)anchor {
    // Create and configure a node for the anchor added to the view's session.
    
    //创建节点(节点可以理解为AR将要展示的2D图像)
    SKLabelNode *labelNode = [SKLabelNode labelNodeWithText:@"�👽"];
    //显示在屏幕中心
    labelNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
    labelNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    return labelNode;
}

- (void)session:(ARSession *)session didFailWithError:(NSError *)error {
    // Present an error message to the user
    
}

- (void)sessionWasInterrupted:(ARSession *)session {
    // Inform the user that the session has been interrupted, for example, by presenting an overlay
    
}

- (void)sessionInterruptionEnded:(ARSession *)session {
    // Reset tracking and/or remove existing anchors if consistent tracking is required
    
}

@end

0109.gif
上一篇下一篇

猜你喜欢

热点阅读