ARKit介紹
是什么辜伟?
-
ARKit是2017年6月6日,蘋果發(fā)布iOS11系統(tǒng)所新增框架,它能夠幫助我們以最簡單快捷的方式實現(xiàn)AR技術(shù)功能脊另。
-
ARKit框架提供了兩種AR技術(shù)导狡,一種是基于3D場景(SceneKit)實現(xiàn)的增強現(xiàn)實,一種是基于2D場景(SpriktKit)實現(xiàn)的增強現(xiàn)實
對ARKit不是很了解的伙伴可以移步這里偎痛。傳送門
ARKit官方文檔翻譯 http://www.reibang.com/p/e373f7f96b5c
做什么旱捧?
-
通過攝像頭和虛擬世界進行聯(lián)通,改變?nèi)藱C的交互方式踩麦。
怎么做枚赡?
開發(fā)環(huán)境介紹
Xcode9(最新版的是Xcode9 beta6 如果是最新Xcode 要macOS High Sierra 10.12.6版本)
A9硬件iOS手機設(shè)備從6s開始使用
ios 11 下載地址 https://developer.apple.com/download/ 升級iOS11時可用手機Safari打開下載安裝較為方便
廢話不多說,先來幾張效果圖
-
系統(tǒng)提供的飛機模型
IMG_1937.PNG
-
立方體
IMG_1942.PNG
-
球體
IMG_1944.PNG
猝不及防 - 實戰(zhàn)開始
-
Xcode9 新建AR項目 如圖兩步就OK了
屏幕快照 2017-09-03 15.42.51.png
屏幕快照 2017-09-03 15.43.16.png
此刻只需要運行你的Xcode就ok了 ,沒意外你就能看到圖一的場景了 ,這么簡單就沒了谓谦,當(dāng)然不是贫橙,我們要做的是在虛擬現(xiàn)實中多創(chuàng)建幾個節(jié)點,細(xì)心的同學(xué)就會看到第一張圖的左下角有個不明物體
首先來熟悉一下即將出現(xiàn)的陌生類
-
ARSCNView 用來顯示3D模型的視圖視圖容器
-
SCNScene 3D的場景
-
ARSessionConfiguration 增強現(xiàn)實的配置會話
-
ARWorldTrackingSessionConfiguration全球配置追蹤 比較重要 ARKit常用類
-
SCNPlane 平面
-
SCNNode 節(jié)點 在虛擬世界里面 萬物皆節(jié)點
屏幕快照 2017-09-03 16.18.20.png
屏幕快照 2017-09-03 14.50.08.png
3D世界的坐標(biāo)系
屏幕快照 2017-09-03 14.49.12.png
代碼如下
實現(xiàn) : 對飛機模型的AR場景 進行截圖 在增強現(xiàn)實的場景中創(chuàng)建多個節(jié)點(模型)
Tip - 在虛擬世界中萬物皆模型
步驟
- 1.判斷能不能獲取到當(dāng)前的Frame
- 2.創(chuàng)建一張截圖
- 3.對創(chuàng)建的圖片進行截圖
- 4.通過截圖創(chuàng)建一個節(jié)點并加到AR場景的根節(jié)點上
- 5.追蹤相機的位置
//
// ViewController.swift
// ARKit初體驗
//
// Created by cwb on 2017/9/1.
// Copyright ? 2017年 cwb. All rights reserved.
//
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
//用來展示3D模型的視圖
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
//設(shè)置3D場景視圖的的代理
sceneView.delegate = self
// Show statistics such as fps and timing information
//顯示統(tǒng)計數(shù)據(jù) 如fps
sceneView.showsStatistics = true
// Create a new scene
//創(chuàng)建一個場景 named: "art.scnassets/ship.scn" 讀取一個模型
let scene = SCNScene(named: "art.scnassets/ship.scn")!
// Set the scene to the view
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
//設(shè)置全局追蹤
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
//啟動追蹤
sceneView.session.run(configuration)
//創(chuàng)建一個手勢
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.creatImageNode(tapGesture:)))
view.addGestureRecognizer(tapGesture)
}
@objc func creatImageNodeWithTap(tapGesture:UITapGestureRecognizer) -> () {
/*
實現(xiàn) : 對飛機模型的AR場景 進行截圖 在增強現(xiàn)實的場景中創(chuàng)建多個節(jié)點(模型) 在虛擬世界中萬物皆模型
1.判斷能不能獲取到當(dāng)前的Frame
2.創(chuàng)建一張截圖
3.對創(chuàng)建的圖片進行截圖
4.通過截圖創(chuàng)建一個節(jié)點并加到AR場景的根節(jié)點上
4.追蹤相機的位置
*/
//守護 如果滿足條件就往下執(zhí)行 否則執(zhí)行 return語句
guard let currentFrame = sceneView.session.currentFrame else {
return
}
// 創(chuàng)建一張圖片
//SCNPlane
// A rectangular, one-sided plane geometry of specified width and height. API
//翻譯 SCNPlane創(chuàng)建的對象是一個有指定寬高的平面矩形
let imagePlane = SCNPlane(width: sceneView.bounds.width / 8000, height: sceneView.bounds.height / 8000)
// 渲染圖片
/*
SCNMaterial 渲染器
API A set of shading attributes that define the appearance of a geometry's surface when rendered.
翻譯 用來定義 幾何表面被渲染時候的陰影屬性
firstMaterial 獲取幾何上的第一個渲染物
diffuse
Specifies the receiver's diffuse property
diffuse 接收特定的屬性
*/
/*
lightingModel 環(huán)境的光感變量 (以下來自百度翻譯和自己的理解 不足及錯誤之處請指正)
blinn:
Shading that incorporates ambient, diffuse, and specular properties, where specular highlights are calculated using the Blinn-Phong formula.
陰影包含三個要素 : 環(huán)境 漫射 和 鏡面 blinn屬性是用Blinn-Phong公式計算的高光效果
constant:
Uniform shading that incorporates ambient lighting only.
均勻的環(huán)境 只包含了光線
lambert:
Shading that incorporates ambient and diffuse properties only.
僅包含環(huán)境屬性和漫射屬性
phong:
Shading that incorporates ambient, diffuse, and specular properties, where specular highlights are calculated using the Phong formula.
明暗結(jié)合環(huán)境反粥,擴散卢肃,和鏡面反射特性,在高光使用Phong公式計算
physicallyBased:
Shading based on a realistic abstraction of physical lights and materials.
基于物理光線和材質(zhì)的真實抽象的陰影才顿。
*/
// 在創(chuàng)建的圖片平面上截屏
imagePlane.firstMaterial?.diffuse.contents = sceneView.snapshot()
imagePlane.firstMaterial?.lightingModel = .constant
// 在圖片的幾何平面上創(chuàng)建一個節(jié)點
let planNode = SCNNode(geometry: imagePlane)
//把該節(jié)點添加到AR場景的根節(jié)點上
sceneView.scene.rootNode.addChildNode(planNode)
// 追蹤相機的位置 (參考z軸)
/*
4X4的矩陣
matrix_identity_float4x4
columns.3.z 3代表3軸 xyz
*/
var translate = matrix_identity_float4x4
//在z軸的-0.1米的方向 在面前能顯示 正數(shù)的話顯示在后腦勺
translate.columns.3.z = -0.1
// 追蹤相機的位置
//把截圖顯示在相機的前方10公分處
planNode.simdTransform = matrix_multiply(currentFrame.camera.transform, translate)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
//暫停追蹤
sceneView.session.pause()
}
實現(xiàn)效果
IMG_1935.PNG
總結(jié)
-
簡單粗暴的實現(xiàn)了對系統(tǒng)的飛機模型的增強現(xiàn)實世界的截屏莫湘,并把得到的平面幾何作為一個新的節(jié)點現(xiàn)實在場景中。
-
效果圖中的正方體和球體由于篇幅問題代碼未貼出,用到了SCNBox(正方體)郑气,和SCNSphere(球體)
-
代碼下載地址 https://github.com/ichenwanbing/ARKit-