使用 ARKit anchor 放置 RealityKit Scene
Using ARKit anchor to place RealityKit Scene
我目前正在努力将 RealityKit 场景添加到 ARKit 锚点。正如我之前的问题,直接将 RealityKit 场景添加到 ARKit 锚节点是不可能的。
但是我如何使用 ARKit Anchor 来放置我的场景?
例如,我想在房间里找一个物体,根据找到的物体,我想使用不同的场景,并根据识别到的物体位置将它们锚定在房间里。
感谢您的帮助。
当前尝试:
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()
// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)
let configuration = ARWorldTrackingConfiguration()
guard let referenceObjects = ARReferenceObject.referenceObjects(
inGroupNamed: "AR Objects", bundle: nil) else {
fatalError("Missing expected asset catalog resources.")
}
configuration.detectionObjects = referenceObjects
arView.session.run(configuration) // not possible at arview, but i need arview for the .rcproject :-/
return arView
}
编辑:添加代码
ARKit 没有节点。 SceneKit 可以。
不过,如果你需要使用ARKit的anchor将RealityKit的模型放到AR场景中,就这么简单:
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let boxScene = try! Experience.loadBox()
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
let entity = boxScene.steelBox
var transform = simd_float4x4(diagonal: [1,1,1,1])
transform.columns.3.z = -3.0 // three meters away
let arkitAnchor = ARAnchor(name: "ARKit anchor",
transform: transform)
let anchorEntity = AnchorEntity(anchor: arkitAnchor)
anchorEntity.name = "RealityKit anchor"
arView.session.add(anchor: arkitAnchor)
anchor.addChild(entity)
arView.scene.anchors.append(anchorEntity)
}
}
这要归功于 AnchorEntity 便捷初始化程序:
convenience init(anchor: ARAnchor)
您还可以使用 session(_:didAdd:)
和 session(_:didUpdate:)
实例方法:
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let objectAnchor = anchors.first as? ARObjectAnchor
else { return }
let anchor = AnchorEntity(anchor: objectAnchor)
let entity = boxScene.steelBox
anchor.addChild(entity)
arView.session.add(anchor: objectAnchor)
arView.scene.anchors.append(anchor)
}
}
我目前正在努力将 RealityKit 场景添加到 ARKit 锚点。正如我之前的问题,直接将 RealityKit 场景添加到 ARKit 锚节点是不可能的。
但是我如何使用 ARKit Anchor 来放置我的场景? 例如,我想在房间里找一个物体,根据找到的物体,我想使用不同的场景,并根据识别到的物体位置将它们锚定在房间里。
感谢您的帮助。
当前尝试:
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()
// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)
let configuration = ARWorldTrackingConfiguration()
guard let referenceObjects = ARReferenceObject.referenceObjects(
inGroupNamed: "AR Objects", bundle: nil) else {
fatalError("Missing expected asset catalog resources.")
}
configuration.detectionObjects = referenceObjects
arView.session.run(configuration) // not possible at arview, but i need arview for the .rcproject :-/
return arView
}
编辑:添加代码
ARKit 没有节点。 SceneKit 可以。
不过,如果你需要使用ARKit的anchor将RealityKit的模型放到AR场景中,就这么简单:
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let boxScene = try! Experience.loadBox()
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
let entity = boxScene.steelBox
var transform = simd_float4x4(diagonal: [1,1,1,1])
transform.columns.3.z = -3.0 // three meters away
let arkitAnchor = ARAnchor(name: "ARKit anchor",
transform: transform)
let anchorEntity = AnchorEntity(anchor: arkitAnchor)
anchorEntity.name = "RealityKit anchor"
arView.session.add(anchor: arkitAnchor)
anchor.addChild(entity)
arView.scene.anchors.append(anchorEntity)
}
}
这要归功于 AnchorEntity 便捷初始化程序:
convenience init(anchor: ARAnchor)
您还可以使用 session(_:didAdd:)
和 session(_:didUpdate:)
实例方法:
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
guard let objectAnchor = anchors.first as? ARObjectAnchor
else { return }
let anchor = AnchorEntity(anchor: objectAnchor)
let entity = boxScene.steelBox
anchor.addChild(entity)
arView.session.add(anchor: objectAnchor)
arView.scene.anchors.append(anchor)
}
}