ARKIT

04-ARKit初体验之2D效果

2017-06-20  本文已影响111人  60916fc63567

示例效果是点击屏幕,在中心点生成一个2D AR图像

2D效果的AR与3D效果有一点的区别####

1.使用步骤与3D基本类似,在创建Xcode的时候选择SpriteKit引擎#####
CF3E216F-08A4-4248-84F8-0F7FD49353C4.png
2.此时Xcode会为我们生成简洁的2D地图加载场景#####
E286772B-0F41-4DA7-AE60-35B212DD7375.png

<pre>
/** ViewController.swift
*AR初体验2D效果
*Created by Apple1 on 2017/6/20.
*Copyright © 2017年 Noitom. All rights reserved.
*/

import UIKit
import SpriteKit
import ARKit

class ViewController: UIViewController, ARSKViewDelegate {

@IBOutlet var sceneView: ARSKView!

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

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Create a session configuration
    //创建设备追踪设置
    let configuration = ARWorldTrackingSessionConfiguration()
    // Run the view's session
    //开始启动AR
    sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    
    // Pause the view's session
    sceneView.session.pause()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

// MARK: - ARSKViewDelegate
//点击界面会调用,类似touch begin 方法 anchor是2D坐标的瞄点
func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
    // Create and configure a node for the anchor added to the view's session.
    //创建节点(节点可以理解为AR将要展开的2D图像)
    let labelNode = SKLabelNode(text: "👾")
    labelNode.horizontalAlignmentMode = .center
    labelNode.verticalAlignmentMode = .center
    return labelNode;
}

func session(_ session: ARSession, didFailWithError error: Error) {
    // Present an error message to the user
    
}

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

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

}
<pre>

上一篇下一篇

猜你喜欢

热点阅读