此篇文章皆出自蘋果官方文檔, 我只不過信手拿來記述,再加入點自己的理解。揉合揉合,以加深理解。不愿意看下面的,可以直接點擊鏈接查看官方文檔,并且包括例子都可以下載。
概述
本篇文章主要是技術蘋果官網教程來使用SceneKit來進行平面檢測,并且生成一個虛擬的平面,自己可以延伸在平面上記載3D圖像。
為了演示平面檢測,應用程序只需放置一個SCNPlane對象來可視化每個檢測到的對象。
SCNPlane 根據其平面width和height屬性,在其局部坐標空間的x軸和y軸尺寸上定義一個平面。要使平面定向不同,請調整transfrom包含平面幾何的節點屬性。你也可以使用屬性創建圓角矩形平面cornerRadius
配置并運行AR會話
ARSCNView類中包含一個SceneKit視圖ARSession管理創建的增強現實(AR)的經驗所需要的運動跟蹤和圖像處理對象。如果要運行會話,必須提供會話配置:
要啟動AR會話,請使用所需選項(如平面檢測)創建會話配置對象,然后調用如下方法:
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal //水平檢測
sceneView.session.run(configuration)
檢測到平面放置3D內容
設置AR會話后,可以使用SceneKit在視圖中放置虛擬內容。
當啟用平面檢測時,ARKit會為每個檢測到的平面添加和更新錨點。
默認情況下,ARSCNView該類SCNNode為每個錨點SceneKit場景添加一個對象。您的視圖的代理可以實現向場景添加內容的方法
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
// Place content only for anchors found by plane detection.
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
// Create a SceneKit plane to visualize the plane anchor using its position and extent.
let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))
let planeNode = SCNNode(geometry: plane)
planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
/*
`SCNPlane` is vertically oriented in its local coordinate space, so
rotate the plane to match the horizontal orientation of `ARPlaneAnchor`.
*/
planeNode.eulerAngles.x = -.pi / 2
// Make the plane visualization semitransparent to clearly show real-world placement.
planeNode.opacity = 0.25
/*
Add the plane visualization to the ARKit-managed node so that it tracks
changes in the plane anchor as plane estimation continues.
*/
node.addChildNode(planeNode)
}
如果將內容添加為對應于錨點的節點的子節點,則ARSCNView類將自動移動該內容,因為ARKit會優化其對飛機位置和范圍的估計。為了顯示估計平面的完整范圍,此示例應用程序還實現了該方法,更新對象的大小以反映ARKit提供的esitmate.
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
// Update content only for plane anchors and nodes matching the setup created in `renderer(_:didAdd:for:)`.
guard let planeAnchor = anchor as? ARPlaneAnchor,
let planeNode = node.childNodes.first,
let plane = planeNode.geometry as? SCNPlane
else { return }
// Plane estimation may shift the center of a plane relative to its anchor's transform.
planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
/*
Plane estimation may extend the size of the plane, or combine previously detected
planes into a larger one. In the latter case, `ARSCNView` automatically deletes the
corresponding node for one plane, then calls this method to update the size of
the remaining plane.
*/
plane.width = CGFloat(planeAnchor.extent.x)
plane.height = CGFloat(planeAnchor.extent.z)
}
官網的代碼在github放了一份,可以運行查看一下
知行辦公,專業移動辦公平臺https://zx.naton.cn/
【總監】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【運營】運維艄公,897221533@qq.com;****
【產品設計】流浪貓,364994559@qq.com;
【體驗設計】兜兜,2435632247@qq.com;
【iOS】淘碼小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的頓悟,1217022114@qq.com;
【java】首席工程師MR_W,feixue300@qq.com;
【測試】土鏡問道,847071279@qq.com;
【數據】fox009521,42151960@qq.com;