我有一个RealityKit应用程序正在做一些基本的AR图像跟踪。它检测到一个矩形的图像,我期待着在图像的每个角落放置一些球形点。我知道我可以使用ModelEntity自己创建球体,但我还没有弄清楚如何从参考图像中指定这些球相对于已建立的ARImageAnchor的位置。
我想我只需要与SceneKit的addChildNode(SCNNode)函数相对应,它使用SCNVector3Make()来指定一个职位。我只是无法找到一种方法来建立一个相对位置,并将一个子节点分配给这些ARImageAnchor函数之外的SceneKit。在RealityKit中是否有内置的东西可以实现这一点,或者是否有一种方法可以使用SceneKit来放置角点,同时仍然使用我目前在RealityKit中的设置来进行AR参考图像跟踪?
发布于 2022-03-26 11:43:31
尝试以下方法:
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var anchorEntity = AnchorEntity()
let model_01 = ModelEntity(mesh: .generateSphere(radius: 0.03))
let model_02 = ModelEntity(mesh: .generateSphere(radius: 0.03))
let model_03 = ModelEntity(mesh: .generateSphere(radius: 0.03))
let model_04 = ModelEntity(mesh: .generateSphere(radius: 0.03))
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
arView.session.delegate = self
guard let reference = ARReferenceImage.referenceImages(
inGroupNamed: "AR Resources",
bundle: nil)
else { return }
let config = ARImageTrackingConfiguration()
config.trackingImages = reference
arView.session.run(config)
self.anchorEntity.addChild(model_01)
self.anchorEntity.addChild(model_02)
self.anchorEntity.addChild(model_03)
self.anchorEntity.addChild(model_04)
arView.scene.anchors.append(self.anchorEntity)
}
}然后实现session(_:didUpdate:)方法:
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let imageAnchor = anchors.first as? ARImageAnchor
else { return }
let width = Float(imageAnchor.referenceImage.physicalSize.width)
let height = Float(imageAnchor.referenceImage.physicalSize.height)
let x = imageAnchor.transform.columns.3.x
let y = imageAnchor.transform.columns.3.y
let z = imageAnchor.transform.columns.3.z
let lowerLeft = SIMD3<Float>(x - width/2, y - height/2, z)
let lowerRight = SIMD3<Float>(x + width/2, y - height/2, z)
let upperRight = SIMD3<Float>(x + width/2, y + height/2, z)
let upperLeft = SIMD3<Float>(x - width/2, y + height/2, z)
self.model_01.position = lowerLeft
self.model_02.position = lowerRight
self.model_03.position = upperRight
self.model_04.position = upperLeft
self.anchorEntity = AnchorEntity(anchor: imageAnchor)
}
}

https://stackoverflow.com/questions/71241779
复制相似问题