当您使用 Reality Composer 创建任何场景时,您必须首先选择哪种类型的锚点“地板、墙壁、面、对象”,这意味着当您加载场景时,它会自动将其放置到指定的锚点。
我的问题是,有什么方法可以从代码中手动设置它,以便我例如进行命中测试,然后手动将其锚定到特定点?
谢谢。
当您使用 Reality Composer 创建任何场景时,您必须首先选择哪种类型的锚点“地板、墙壁、面、对象”,这意味着当您加载场景时,它会自动将其放置到指定的锚点。
我的问题是,有什么方法可以从代码中手动设置它,以便我例如进行命中测试,然后手动将其锚定到特定点?
谢谢。
官方文档没有提及能够在运行时更改默认锚点,但从您的描述看来,您可以Select Object Anchoring to Place a Scene Near Detected Objects
按照此处所述尝试:
https ://developer.apple.com/documentation/realitykit/creating_3d_content_with_reality_composer/selecting_an_anchor_for_a_reality_composer_scene
您可以使用以下代码轻松应用其他类型的锚(在实现命中测试或光线投射时)(Reality Composer 中的默认锚是
.horizontal
):
import ARKit
import RealityKit
@IBAction func onTap(_ sender: UITapGestureRecognizer) {
let estimatedPlane: ARRaycastQuery.Target = .estimatedPlane
let alignment: ARRaycastQuery.TargetAlignment = .vertical
let tapLocation: CGPoint = sender.location(in: arView)
let result: [ARRaycastResult] = arView.raycast(from: tapLocation,
allowing: estimatedPlane,
alignment: alignment)
guard let rayCast: ARRaycastResult = result.first
else { return }
let anchor = AnchorEntity(world: rayCast.worldTransform)
anchor.addChild(myScene)
arView.scene.anchors.append(anchor)
}
或者您可以自动放置锚点(例如,ARFaceAnchor
对于检测到的人脸):
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession,didUpdate anchors: [ARAnchor]) {
guard let faceAnchor = anchors.first as? ARFaceAnchor
else { return }
let anchor = AnchorEntity(anchor: faceAnchor)
// RealityKit's Facial analog
// AnchorEntity(.face).self
anchor.addChild(glassModel)
arView.scene.anchors.append(anchor)
}
}
...或者您可以ARImageAnchor
以相同的方式放置:
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let imageAnchor = anchors.first as? ARImageAnchor,
let _ = imageAnchor.referenceImage.name
else { return }
let anchor = AnchorEntity(anchor: imageAnchor)
// RealityKit's image anchor analog
// AnchorEntity(.image(group: "Group", name: "model")).self
anchor.addChild(imageModel)
arView.scene.anchors.append(anchor)
}
}