1.本部分教程使用的是原生iOS开发,目前不涉及Unity,主要使用SceneKit。
node
2.使用的软件版本:Xcode 9。swift
3.使用的开发语言版本:Swift 4bash
4.本系列专栏但愿经过简洁的语言与图片帮助你们更好的理解ARKit开发流程,帮助你们开发出炫酷的AR应用。微信
打开Xcode,点击File > New > Project…,选择Single View App,点击Next建立项目,命名为ARKitDemo。以下图操做所示:session
设置SceneKit Viewapp
打开Main.storyboard,将ARKit SceneKit视图拖放到视图控制器上。
ide
而后将ARKit SceneKit视图约束填充整个视图控制器。测试
链接IBOutletui
在ViewController.swift文件的顶部添加一个import语句来导入ARKit:spa
import ARKit复制代码
而后按住control键并从ARKit SceneKit视图拖拽到ViewController.swift文件。当提示时,命名IBOutlet sceneView。能够删除didReceiveMemoryWarning() 方法,目前咱们并不须要它。
配置ARSCNView Session
咱们的AR应用程序是经过摄像头观察世界和周围的环境。因此接下来咱们须要设置Camera:
配置ARKit SceneKit View。在ViewController类中插入如下代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
sceneView.session.run(configuration)
}复制代码
在viewWillAppear(_:)方法中,咱们初始化了一个名为ARWorldTrackingConfiguration的AR配置。主要用于实现World Tracking功能。
The World Tracking配置跟踪设备的方向和位置,它还能经过设备的Camera探测真实世界的表面。
设置sceneView’s AR session来运行咱们刚刚初始化的配置。AR session管理视图内容的运动跟踪和camera图像处理。
如今在ViewController中添加另外一个方法:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}复制代码
在viewWillDisappear(_:)方法中主要处理:中止tracking和视图内容的图像。
Camera 受权
在咱们运行应用程序以前,咱们须要通知用户咱们将使用他们设备的摄像头实现加强现实功能。这是iOS 10发布以来的一个要求。
打开Info.plist。右键单击空白区域并选择Add row。设置私隐相机使用说明键。值能够设置为Augmented Reality。以下图所示:
接下来咱们提早测试下Camera是否能够顺利的调用出来。将测试手机链接到Mac上。在Xcode上构建并运行该项目。该应用程序应该会提示你容许摄像头进入。以下图所示:
插入如下代码到你的ViewController类:
func addBox() {
let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
let boxNode = SCNNode()
boxNode.geometry = box
boxNode.position = SCNVector3(0, 0, -0.2)
let scene = SCNScene()
scene.rootNode.addChildNode(boxNode)
sceneView.scene = scene
}复制代码
建立一个Box,1 Float = 1 meter。
建立一个node。node表示物体在三维空间中的位置和坐标。node自己没有可见的内容。
给node设置一个形状(Box)。
设置box的位置。这个位置相对于camera的,右边是X正,左边是X负。上面表示Y正,向下表示Y负。向后表示Z正,向前表示Z负。
建立一个scene(SceneKit scene),将box添加到场景中去。
将sceneView的scene设置为显示刚刚建立的场景。
override func viewDidLoad() {
super.viewDidLoad()
addBox()
}复制代码
接下来Build,打开camera,出现白色的box,效果以下图:
addBox()的方法也能够这样写:
func addBox() {
let box = SCNBox(width: 0.05, height: 0.05, length: 0.05, chamferRadius: 0)
let boxNode = SCNNode()
boxNode.geometry = box
boxNode.position = SCNVector3(0, 0, -0.2)
sceneView.scene.rootNode.addChildNode(boxNode)
}复制代码
在ViewController.swift文件中插入如下方法:
@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapLocation)
guard let node = hitTestResults.first?.node else { return }
node.removeFromParentNode()
}复制代码
建立了一个didTap(带有gesturerecognizer:)方法。检索用户相对于sceneView的点击位置,而后t查看是否点击了任何node。而后从hitTestResults中找到第一个node。若是结果确实包含至少一个node,那么咱们将从其父节点删除第一个节点。
在测试删除对象以前,更新viewDidLoad()方法,添加对addTapGestureToSceneView()方法的调用:
override func viewDidLoad() {
super.viewDidLoad()
addBox()
addTapGestureToSceneView()
}复制代码
在ViewController类的末尾建立一个extension:
extension float4x4 {
var translation: float3 {
let translation = self.columns.3
return float3(translation.x, translation.y, translation.z)
}
}复制代码
extension将矩阵转换为float3。修改addBox()方法:
func addBox(x: Float = 0, y: Float = 0, z: Float = -0.2) {
let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
let boxNode = SCNNode()
boxNode.geometry = box
boxNode.position = SCNVector3(x, y, z)
sceneView.scene.rootNode.addChildNode(boxNode)
}复制代码
修改didTap(使用gesturerecognizer:)方法,在guard let语句内部和return语句以前。添加如下代码:
let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)
if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {
let translation = hitTestResultWithFeaturePoints.worldTransform.translation
addBox(x: translation.x, y: translation.y, z: translation.z)
}复制代码
接下来实现使用x、y和z在检测到的点击时添加一个新的box。didTap(withGestureRecognizer:)方法的代码以下:
@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
let tapLocation = recognizer.location(in: sceneView)
let hitTestResults = sceneView.hitTest(tapLocation)
guard let node = hitTestResults.first?.node else {
let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)
if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {
let translation = hitTestResultWithFeaturePoints.worldTransform.translation
addBox(x: translation.x, y: translation.y, z: translation.z)
}
return
}
node.removeFromParentNode()
}复制代码
运行效果以下图:
参考连接:https://www.appcoda.com/arkit-introduction-scenekit/
------AR Portal(AR开发者社区)整理
关注微信公众号(AR开发者交流社区,提供AR开发干货,推进AR内容发展):AR开发者社区