【IOS】ARAnchor到底是什麼?
我正在嘗試瞭解和使用ARKit。但是有一件事我無法完全理解。
蘋果關於ARAnchor說:
A real-world position and orientation that can be used for placing objects in an AR scene.
但這還不夠。所以我的問題是:
ARAnchor是什麼? ARAnchor是特徵點的一部分嗎? 解決辦法
更新:2020年7月19日。
TL; DR
anchor ARAnchor是一個不可見的空物件,可以將3D模型保持在世界空間中 anchor 的位置。將ARAnchor視為模型的區域性軸transform node(可以對其進行平移,旋轉和縮放)。每個3D模型都有一個樞軸點,對不對?因此,此樞軸點必須滿足ARAnchor。
如果不在ARKit / RealityKit應用程式中使用 anchor ,則3D模型可能會從放置它們的位置移開,這會對使用者體驗產生影響。因此, anchor 是AR場景中的關鍵元素。
根據ARKit文件2017:
ARAnchoris a real-world position and orientation that can be used for placing objects in AR Scene. Adding an anchor to the session helps ARKit to optimize world-tracking accuracy in the area around that anchor, so that virtual objects appear to stay in place relative to the real world. If a virtual object moves, remove the corresponding anchor from the old position and add one at the new position.
ARAnchor是ARKit框架中現有的所有其他 anchor 型別的父類,因此所有這些子類均繼承自ARAnchor類,但不能直接在您的程式碼中使用。我還必須說ARAnchor和Feature Points沒有共同點。 Feature Points用於除錯。ARAnchor不會自動跟蹤真實目標。如果需要自動化,則必須使用rendered(...)或session(...)例項方法,如果您分別符合協議(protocol)ARSCNViewDelegate或ARSessionDelegate,則可以呼叫這些方法。這是帶有平面 anchor 的可視表示的影象。但請記住:預設情況下,您既看不到檢測到的平面,也看不到其對應的
ARPlaneAnchor。因此,如果您想在場景中看到任何 anchor ,則必須使用三個瘦SCNCylinder原語“視覺化”它。
在ARKit中,您可以使用不同的場景自動將
ARAnchors新增到場景中:planeDetection 例項屬性為ON,則ARKit可以將ARPlaneAnchors新增到當前 session 。有時啟用的planeDetection會大大增加場景理解階段所需的時間。 ARTrackable 協議(protocol))detectionImages 例項屬性。在ARKit 2.0中,您總共可以跟蹤多達25張影象,在ARKit 3.0和ARKit 4.0中,分別可以跟蹤多達100張影象。但是,在兩種情況下均為not more than just 4 images simultaneously。 ARTrackable協議(protocol))ARBodyTrackingConfiguration() 執行 session 來啟用 body 跟蹤。您將以CG Skeleton的 Root Joint 或在跟蹤角色的骨盆位置獲得ARBodyAnchor。 ARTrackable協議(protocol))ARReferenceObject屬性指定detectionObjects例項。 true框架中將isCollaborationEnabled值用於MultipeerConnectivity例項屬性。 ARTrackable協議(protocol))There are also other regular approaches to create anchors in AR session:
ARHitTestResult類及其對ARSCNView和ARSKView的命中測試方法,因此您必須習慣射線鑄造。 world anchor一樣生成ARKit的AnchorEntity(.world(transform: mtx))版本。 此程式碼段顯示瞭如何在委託(delegate)人的方法中使用ARPlaneAnchor:
renderer(_:didAdd:for:):func renderer(_ renderer: SCNSceneRenderer,
didAdd node: SCNNode,
for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor
else { return }
let grid = Grid(anchor: planeAnchor)
node.addChildNode(grid)
}
anchor 實體
根據RealityKit文件2019:
AnchorEntityis an anchor that tethers virtual content to a real-world object in an AR session.
WWDC'19中釋出了 RealityKit 框架和 Reality Composer 應用程式。他們有一個名為
AnchorEntity的新類。您可以將AnchorEntity用作任何實體層次結構的根點,並且必須將其新增到Scene anchors集合中。 AnchorEntity自動跟蹤現實世界的目標。在RealityKit和Reality Composer中,AnchorEntity在層次結構的頂部。該 anchor 能夠容納一百個模型,在這種情況下,與為每個模型使用100個個人 anchor 相比,它更穩定。讓我們看看它在程式碼中的外觀:
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let modelAnchor = try! Experience.loadModel()
arView.scene.anchors.append(modelAnchor)
return arView
}
AnchorEntity 具有三個組成部分:To find out the difference between
ARAnchorandAnchorEntitylook at THIS POST.
以下是RealityKit 2.0 for iOS中提供的九種AnchorEntity案例:
// Fixed position in the AR scene
AnchorEntity(.world(transform: mtx))
// For body tracking (a.k.a. Motion Capture)
AnchorEntity(.body)
// Pinned to the tracking camera
AnchorEntity(.camera)
// For face tracking (Selfie Camera config)
AnchorEntity(.face)
// For image tracking config
AnchorEntity(.image(group: "Group", name: "model"))
// For object tracking config
AnchorEntity(.object(group: "Group", name: "object"))
// For plane detection with surface classification
AnchorEntity(.plane([.any], classification: [.seat], minimumBounds: [1.0, 1.0]))
// When you use ray-casting
AnchorEntity(raycastResult: myRaycastResult) /* no dot notation */
// When you use ARAnchor with a given identifier
AnchorEntity(.anchor(identifier: uuid))
// Creates anchor entity on a basis of ARAnchor
AnchorEntity(anchor: arAnchor) /* no dot notation */
這是RealityKit 2.0中適用於macOS的僅兩種AnchorEntity案例:// Fixed world position in VR scene
AnchorEntity(.world(transform: mtx))
// Camera transform
AnchorEntity(.camera)
Also it’s not superfluous to say that you can use any subclass of
ARAnchorforAnchorEntityneeds:
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let faceAnchor = anchors.first as? ARFaceAnchor
else {
return
}
arView.session.add(anchor: faceAnchor)
let anchor = AnchorEntity(anchor: faceAnchor)
anchor.addChild(model)
arView.scene.anchors.append(anchor)
}