ARKit RealityKit WorldMap 持久化

ARKit RealityKit WorldMap Persistence

所以我有一个 RealityKit 应用程序,我在其中 添加 各种Entities。 我在 Apple's SceneKit example 寻找代码的持久性灵感,我实现它只是为了在 WorldMap Load

时发现缺少 Entities

我假设您能够从 ARView 的会话中保存和加载 worldMap,但问题是这只会保留旧样式器 ARAnchors,而不是来自新 RealityKit 功能的很酷的新 Entity 对象。

我所做的工作是使用采用 ARAnchor 的构造函数来初始化我的 AnchorEntities。因此,从我的 hitTest 或 RayCast 中,我采用世界变换并将其保存为 ARAnchor,然后使用它来初始化 AnchorEntity。我给了这个 ARAnchor 一个唯一的名称,稍后用于在加载持久化世界地图时重新映射到实体,因为该地图仍然只有 ARAnchors。

let arAnchor = ARAnchor(name: anchorId, transform: rayCast.worldTransform) // ARAnchor with unique name or ID
let anchorEntity = AnchorEntity(anchor: arAnchor)

这是第一次将锚点添加到场景之前的样子。保存世界地图、关闭并重新加载后,我会遍历已加载或持久化的 ARAnchors,并将每个锚点与其各自的实体相关联,这些实体映射到 ARAnchor.

中的名称
let anchorEntity = AnchorEntity(anchor: persistedArAnchor) // use the existing ARAnchor that persisted to construct an Entity
var someModelEntity: Entity = myEntityThatMatchesTheAnchorName // remake the entity that maps to the existing named ARAnchor
anchorEntity.addChild(someModelEntity)
arView.scene.addAnchor(anchorEntity)
 

这是间接的,但是利用 AnchorEntityARAnchor 之间的关联是我能找到的第一个解决方案,考虑到只知道如何持久化 ARAnchors 而不是实体中的限制世界地图.