[英]ARKit & Reality composer - how to Anchor scene using image coordinates
我已經編寫了代碼來初始化 3 個 Reality Composer 場景中的一個,當按下按鈕時,具體取決於一個月中的哪一天。
這一切都很好。
Reality Composer 場景使用圖像檢測將對象放置在環境中,但目前只要圖像離開相機視圖,對象就會消失。
我想將場景錨定在第一次檢測到圖像的根節點,以便用戶可以環顧場景,即使圖像觸發器不在相機視圖中,也可以維護對象。
我嘗試在下面傳遞一個 func 渲染器代碼,但我收到錯誤說視圖 controller class 沒有 the.planeNode
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
let referenceImage = imageAnchor.referenceImage
// Create a plane to visualize the initial position of the detected image.
let plane = SCNPlane(width: referenceImage.physicalSize.width,
height: referenceImage.physicalSize.height)
plane.materials.first?.diffuse.contents = UIColor.blue.withAlphaComponent(0.20)
self.planeNode = SCNNode(geometry: plane)
self.planeNode?.opacity = 1
/*
`SCNPlane` is vertically oriented in its local coordinate space, but
`ARImageAnchor` assumes the image is horizontal in its local space, so
rotate the plane to match.
*/
self.planeNode?.eulerAngles.x = -.pi / 2
/*
Image anchors are not tracked after initial detection, so create an
animation that limits the duration for which the plane visualization appears.
*/
// Add the plane visualization to the scene.
if let planeNode = self.planeNode {
node.addChildNode(planeNode)
}
if let imageName = referenceImage.name {
plane.materials = [SCNMaterial()]
plane.materials[0].diffuse.contents = UIImage(named: imageName)
}
這是我的代碼
import UIKit
import RealityKit
import ARKit
import SceneKit
class ViewController: UIViewController {
@IBOutlet var move: ARView!
@IBOutlet var arView: ARView!
var ARBorealAnchor3: ARboreal.ArBoreal3!
var ARBorealAnchor2: ARboreal.ArBoreal2!
var ARBorealAnchor: ARboreal.ArBoreal!
var Date1 = 1
override func viewDidLoad() {
super.viewDidLoad()
func getSingle() {
let date = Date()
let calendar = Calendar.current
let day = calendar.component(.day, from: date)
Date1 = day
}
getSingle()
ARBorealAnchor = try! ARboreal.loadArBoreal()
ARBorealAnchor2 = try!
ARboreal.loadArBoreal2()
ARBorealAnchor3 = try!
ARboreal.loadArBoreal3()
if Date1 == 24 {
arView.scene.anchors.append(ARBorealAnchor)
}
if Date1 == 25 {
arView.scene.anchors.append(ARBorealAnchor2)
}
if Date1 == 26 {
arView.scene.anchors.append(ARBorealAnchor3)
}
}
}
任何幫助將不勝感激。
干杯,丹尼爾薩維奇
發生的情況是,當圖像錨點離開視野時,AnchorEntity 變得未錨定,然后 RealityKit 將停止渲染它及其所有后代。
解決此問題的一種方法可能是將圖像錨點和要渲染的內容分開,在代碼中手動添加圖像錨點,然后當第一次檢測到圖像錨點時,將內容添加到不同世界錨點下的場景中。 當圖像錨變換更新時,更新您的世界錨以匹配。
這樣,您可以在圖像錨點可見時使用它來獲取最新的變換,但是當它消失時,內容的渲染不會與之綁定。 如下所示(您必須創建一個名為 ARTest 的 AR 資源組,並向其中添加一個名為“test”的圖像,以使錨點正常工作):
import ARKit
import SwiftUI
import RealityKit
import Combine
struct ContentView : View {
var body: some View {
return ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
let arDelegate = SessionDelegate()
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
arDelegate.set(arView: arView)
arView.session.delegate = arDelegate
// Create an image anchor, add it to the scene. We won't add any
// rendering content to the anchor, it will be used only for detection
let imageAnchor = AnchorEntity(.image(group: "ARTest", name: "test"))
arView.scene.anchors.append(imageAnchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
final class SessionDelegate: NSObject, ARSessionDelegate {
var arView: ARView!
var rootAnchor: AnchorEntity?
func set(arView: ARView) {
self.arView = arView
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
// If we already added the content to render, ignore
if rootAnchor != nil {
return
}
// Make sure we are adding to an image anchor. Assuming only
// one image anchor in the scene for brevity.
guard anchors[0] is ARImageAnchor else {
return
}
// Create the entity to render, could load from your experience file here
// this will render at the center of the matched image
rootAnchor = AnchorEntity(world: [0,0,0])
let ball = ModelEntity(
mesh: MeshResource.generateBox(size: 0.01),
materials: [SimpleMaterial(color: .red, isMetallic: false)]
)
rootAnchor!.addChild(ball)
// Just add another model to show how it remains in the scene even
// when the tracking image is out of view.
let ball2 = ModelEntity(
mesh: MeshResource.generateBox(size: 0.10),
materials: [SimpleMaterial(color: .orange, isMetallic: false)]
)
ball.addChild(ball2)
ball2.position = [0, 0, 1]
arView.scene.addAnchor(rootAnchor!)
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let rootAnchor = rootAnchor else {
return
}
// Code is assuming you only have one image anchor for brevity
guard let imageAnchor = anchors[0] as? ARImageAnchor else {
return
}
if !imageAnchor.isTracked {
return
}
// Update our fixed anchor to image transform
rootAnchor.transform = Transform(matrix: imageAnchor.transform)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
注意:當您四處移動時,ARImageAnchor 的變換似乎會頻繁更新,因為 ARKit 正在嘗試計算准確的圖像平面(例如,內容可能看起來在正確的位置,但 z 值不准確),請確保您的圖像尺寸在 AR 資源組中是准確的,以便圖像獲得更好的跟蹤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.