ios - 在 ARKit ARWorldMap 中保持对象方向
问题描述
我正在尝试使用 ARWorldMap 在 ARKit 中保留一个模型。我可以保存和加载模型,但我在保存之前应用到对象的方向不会与对象保持一致。
我目前在做什么
对象被保存和加载:
/// - Tag: GetWorldMap
@objc func saveExperience(_ button: UIButton) {
sceneView.session.getCurrentWorldMap { worldMap, error in
guard let map = worldMap
else { self.showAlert(title: "Can't get current world map", message: error!.localizedDescription); return }
// Add a snapshot image indicating where the map was captured.
guard let snapshotAnchor = SnapshotAnchor(capturing: self.sceneView) else {
fatalError("Can't take snapshot")
}
map.anchors.append(snapshotAnchor)
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: map, requiringSecureCoding: true)
try data.write(to: self.mapSaveURL, options: [.atomic])
DispatchQueue.main.async {
self.loadExperienceButton.isHidden = false
self.loadExperienceButton.isEnabled = true
}
} catch {
fatalError("Can't save map: \(error.localizedDescription)")
}
}
}
/// - Tag: RunWithWorldMap
@objc func loadExperience(_ button: UIButton) {
/// - Tag: ReadWorldMap
let worldMap: ARWorldMap = {
guard let data = mapDataFromFile
else { fatalError("Map data should already be verified to exist before Load button is enabled.") }
do {
guard let worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: data)
else { fatalError("No ARWorldMap in archive.") }
return worldMap
} catch {
fatalError("Can't unarchive ARWorldMap from file data: \(error)")
}
}()
// Display the snapshot image stored in the world map to aid user in relocalizing.
if let snapshotData = worldMap.snapshotAnchor?.imageData,
let snapshot = UIImage(data: snapshotData) {
self.snapshotThumbnail.image = snapshot
} else {
print("No snapshot image in world map")
}
// Remove the snapshot anchor from the world map since we do not need it in the scene.
worldMap.anchors.removeAll(where: { $0 is SnapshotAnchor })
let configuration = self.defaultConfiguration // this app's standard world tracking settings
configuration.initialWorldMap = worldMap
sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
isRelocalizingMap = true
virtualObjectAnchor = nil
}
回转:
@objc func didRotate(_ gesture: UIRotationGestureRecognizer) {
sceneView.scene.rootNode.eulerAngles.y = objectRotation
gesture.rotation = 0
}
然后它被渲染:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard anchor.name == virtualObjectAnchorName else {
return
}
// save the reference to the virtual object anchor when the anchor is added from relocalizing
if virtualObjectAnchor == nil {
virtualObjectAnchor = anchor
}
node.addChildNode(virtualObject)
}
我怎样才能做到这一点?
我该怎么做呢?我尝试了多种解决方案,但从未保持方向。它将对象加载到正确的位置,但从不保留旋转和缩放,即使我将其应用于根节点。我能看到的唯一选择是将转换存储为单独的数据对象,然后加载并应用它。但似乎应该可以将这些数据与对象一起存储。
解决方案
Apple Documentation forARWorldMap
显示一个ARWorldMap
类的属性是:
当您存档世界地图时,这些是唯一保存的信息。在会话期间添加到锚点的任何有关节点的信息(例如更改节点比例和方向)在归档期间不会与世界地图一起保存。
我记得看过一个 WWDC 会议,他们演示了一款名为 SwiftShot 的多人 AR 游戏,玩家用球击打不同的物体。他们提供了源代码,我注意到他们使用了一个ARAnchor
名为的自定义子类BoardAnchor
,用于在锚类中存储附加信息,例如游戏板的大小。请参阅:SwiftShot:为增强现实创建游戏。
您可以使用相同的方法来存储,例如,节点的比例和方向,这样当您取消归档世界地图并重新定位时,您可以使用ARSCNViewDelegate
'srenderer(_:didAdd:for:)
根据存储在你的习惯ARAnchor
。
推荐阅读
- android - 如何在 Android 11 上启动ActivityForResult?
- php - 在 CodeIgniter 4 中重定向后会话丢失
- android - 开关关闭时如何控制 FirebaseMessagingService?
- r - 关于将 sapply 索引传递给函数的问题
- css - 检测点击并在 CSS 上启动“功能”
- oracle - 我正在寻找一种在 Oracle SQL Developer 中搜索某个单词的方法
- python - 在 NLP 中提取包括连字符在内的复合名词的麻烦
- python - Appium双击
- python - 我在 Django shell 中的查询工作正常但无法工作查看文件
- python - python打包pdfkit:将HTML转换为PDF,如何取消自动换行