.camera AnchorEntity 位于何处?
Where is the .camera AnchorEntity located?
将 child 添加到我的 AnchorEntity(.camera)
时,child 似乎在我的相机后面生成(这意味着我只能看到我的 child 当我回转)。我也尝试直接向我的 Anchor 添加网格,但不幸的是,ARKit / RealityKit 在您进入网格时不会渲染网格(因为它以相机为中心,理论上总是如此。但是,它也可能是如果它总是位于屏幕后面 [用户所在的位置] 而我永远看不到它)。
此外,尽管将平移变换设置为 (0,0,0),但奇怪的是 child 实体不会随摄像机 AnchorEntity 一起移动。
我的两个问题是:
.camera 锚点实际上位于物理 iPad / 相机所在的位置还是位于更靠后的位置(可能是用户通常持有 iPad )?
如何让 AnchorEntity(.camera) 的 child 实体随着 iPad / 相机实际移动 space 移动?
Question I. Is the .camera
anchor actually located right where the physical iPad / iPhone camera is located or is it located further back (perhaps where the user would normally hold the iPad / iPhone)?
第一个问题的答案
在 RealityKit 和 ARKit 框架中,ARCamera 像其他实体(节点)一样有一个 pivot point
,它位于镜头连接到相机机身的位置(在 刺刀 级别)。这个枢轴可以系绳 AnchorEntity(.camera)
。换句话说,虚拟相机和现实世界相机的支点大致在同一个地方。
因此,如果您将 RealityKit 的 AnchorEntity 附加到相机的枢轴,则将其放置在相机卡口所在的坐标上。并且这个 AnchorEntity(.camera) 将被自动跟踪而不需要实现 session(_:didUpdate:)
方法。
但是,如果将 ARKit 的 ARAnchor 附加到相机的枢轴,则必须实施 session(_:didUpdate:)
方法来为每个 ARFrame 不断更新该锚点的位置和方向。
Question II. How do you get a child entity of the AnchorEntity(.camera)
to move as the iPad / camera moves in real space?
第二个问题的答案
如果您想以 60 fps 的速度在 RealityKit
秒内不断更新模型的位置(当 ARCamera 移动和旋转时),您需要使用以下方法:
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let box = MeshResource.generateBox(size: 0.25)
let material = SimpleMaterial(color: .systemPink, isMetallic: true)
let boxEntity = ModelEntity(mesh: box, materials: [material])
let cameraAnchor = AnchorEntity(.camera) // ARCamera anchor
cameraAnchor.addChild(boxEntity)
arView.scene.addAnchor(cameraAnchor)
boxEntity.transform.translation = [0, 0,-1] // Box offset 1 m
}
}
...或者您可以在 session(_:didUpdate:) 方法中实现 ARKit
s currentFrame
属性:
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let transform = arView.session.currentFrame?.camera.transform
else { return }
let arkitAnchor = ARAnchor(transform: transform)
let anchor = AnchorEntity(anchor: arkitAnchor)
anchor.addChild(boxEntity)
arView.scene.addAnchor(anchor)
}
}
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var boxEntity = ModelEntity(...)
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self // Session's delegate
}
}
将 child 添加到我的 AnchorEntity(.camera)
时,child 似乎在我的相机后面生成(这意味着我只能看到我的 child 当我回转)。我也尝试直接向我的 Anchor 添加网格,但不幸的是,ARKit / RealityKit 在您进入网格时不会渲染网格(因为它以相机为中心,理论上总是如此。但是,它也可能是如果它总是位于屏幕后面 [用户所在的位置] 而我永远看不到它)。
此外,尽管将平移变换设置为 (0,0,0),但奇怪的是 child 实体不会随摄像机 AnchorEntity 一起移动。
我的两个问题是:
.camera 锚点实际上位于物理 iPad / 相机所在的位置还是位于更靠后的位置(可能是用户通常持有 iPad )?
如何让 AnchorEntity(.camera) 的 child 实体随着 iPad / 相机实际移动 space 移动?
Question I. Is the
.camera
anchor actually located right where the physical iPad / iPhone camera is located or is it located further back (perhaps where the user would normally hold the iPad / iPhone)?
第一个问题的答案
在 RealityKit 和 ARKit 框架中,ARCamera 像其他实体(节点)一样有一个 pivot point
,它位于镜头连接到相机机身的位置(在 刺刀 级别)。这个枢轴可以系绳 AnchorEntity(.camera)
。换句话说,虚拟相机和现实世界相机的支点大致在同一个地方。
因此,如果您将 RealityKit 的 AnchorEntity 附加到相机的枢轴,则将其放置在相机卡口所在的坐标上。并且这个 AnchorEntity(.camera) 将被自动跟踪而不需要实现 session(_:didUpdate:)
方法。
但是,如果将 ARKit 的 ARAnchor 附加到相机的枢轴,则必须实施 session(_:didUpdate:)
方法来为每个 ARFrame 不断更新该锚点的位置和方向。
Question II. How do you get a child entity of the
AnchorEntity(.camera)
to move as the iPad / camera moves in real space?
第二个问题的答案
如果您想以 60 fps 的速度在 RealityKit
秒内不断更新模型的位置(当 ARCamera 移动和旋转时),您需要使用以下方法:
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let box = MeshResource.generateBox(size: 0.25)
let material = SimpleMaterial(color: .systemPink, isMetallic: true)
let boxEntity = ModelEntity(mesh: box, materials: [material])
let cameraAnchor = AnchorEntity(.camera) // ARCamera anchor
cameraAnchor.addChild(boxEntity)
arView.scene.addAnchor(cameraAnchor)
boxEntity.transform.translation = [0, 0,-1] // Box offset 1 m
}
}
...或者您可以在 session(_:didUpdate:) 方法中实现 ARKit
s currentFrame
属性:
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let transform = arView.session.currentFrame?.camera.transform
else { return }
let arkitAnchor = ARAnchor(transform: transform)
let anchor = AnchorEntity(anchor: arkitAnchor)
anchor.addChild(boxEntity)
arView.scene.addAnchor(anchor)
}
}
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
var boxEntity = ModelEntity(...)
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self // Session's delegate
}
}