使用 ARKit 和 RealityKit 的 AR 持久性
AR持久性
ARPersistence是一个 Swift 包,它提供了一个实用程序,用于在 ARKit 和 RealityKit 项目中保存和加载 AR 锚点和模型实体转换。它使您能够保留 AR 对象的状态并在以后恢复它们。
上面的示例演示了 ARPersistence 在 AR 应用程序中的用法。它展示了在 AR 场景中放置多个 3D 模型、保存场景的最终状态并稍后加载以恢复模型的准确放置和配置的能力。
特征
- 保存和加载 AR 锚点和模型实体转换。
- 将 AR 世界地图和模型实体详细信息存储在文件中。
- 检索并应用保存的数据以恢复 AR 体验。
要求
- iOS 15.0+
安装
您可以使用 Swift 包管理器将ARPersistence添加到您的项目中。
- 打开您的 Xcode 项目。
- 转到“文件”->“Swift 包”->“添加包依赖项”。
- 输入存储库 URL
https://github.com/Shiru99/ARPersistence
:。 - 选择您要使用的版本或分支。
- 单击“Next”,Xcode 将解析包并将其添加到您的项目中。
用法
-
在 Swift 文件中导入 ARPersistence 模块:
import ARPersistence
-
使用
ARPersistence.shared
实例保存和加载AR场景状态:// Save the current state of the AR scene ARPersistence.shared.save(arView) // Load the previously saved state of the AR scene ARPersistence.shared.load(arView)
-
确保
ARAnchor
名称与modelName
. (必须)let arAnchor = ARAnchor(name: modelName, transform: matrix_identity_float4x4)
-
创建 时
AnchorEntity
,使用ARAnchor
对其进行初始化并将name
的属性设置AnchorEntity
为arAnchor.identifier.uuidString
。(必须)let anchorEntity = AnchorEntity(anchor: arAnchor) anchorEntity.name = arAnchor.identifier.uuidString
请务必按照以下步骤正确保存和加载 AR 场景状态,确保 ARAnchor 名称与 modelName 匹配,并且 AnchorEntity 已正确初始化和命名。
接触
如有任何疑问或建议,请随时联系Shiru99