使用Reality Composer创建任何场景时,必须首先选择锚的类型“地板,墙壁,面,对象”,这意味着在加载场景时它将自动将其自身放置到指定的锚中。
我的问题是,有什么办法可以从代码中手动设置它,以便例如进行一次命中测试然后手动将其锚定到特定点吗?
谢谢
答案 0 :(得分:1)
官方文档中没有提及能够在运行时更改默认锚点的信息,但是从您的描述看来,您可以按照以下说明尝试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
答案 1 :(得分:1)
使用以下代码(Reality Composer中的默认锚为
'UserDataPersistent','On'
),您可以轻松地应用其他类型的锚(在实现命中测试或光线投射时):
.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