【发布时间】:2021-12-27 16:30:59
【问题描述】:
当它被识别时,我想将来自本地设备的 3D 模型放置在参考图像的顶部。为此,我尝试了以下方法:
- 将参考图像添加到会话配置中:
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
// Check if the device supports the AR experience
if (!ARConfiguration.isSupported) {
TLogger.shared.error_objc("Device does not support Augmented Reality")
return
}
guard let qrCodeReferenceImage = UIImage(named: "QRCode") else { return }
let detectionImages: Set<ARReferenceImage> = convertToReferenceImages([qrCodeReferenceImage])
let configuration = ARWorldTrackingConfiguration()
configuration.detectionImages = detectionImages
arView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}
- 使用
ARSessionDelegate在检测到参考图像时得到通知,并将 3D 模型放置在与他的ARImageAnchor相同的位置:
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
for anchor in anchors {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
let position = imageAnchor.transform
addEntity(self.localModelPath!, position)
}
}
func addEntity(_ modelPath: URL, _ position: float4x4) {
// Load 3D Object as Entity
let entity = try! Entity.load(contentsOf: modelPath)
// Create the Anchor which gets added to the AR Scene
let anchor = AnchorEntity(world: position)
anchor.addChild(entity)
anchor.transform.matrix = position
arView.scene.addAnchor(anchor)
}
但是,每当我尝试将包含我的 3D 模型(实体)的锚点放置在特定位置时,它都不会出现在 arView 中。看起来模型正在加载,因为在执行 addEntity 函数时丢失了几帧。当我没有专门设置锚点位置时,模型会出现在镜头前。
任何人都可以在这里引导我走向正确的方向吗?
【问题讨论】:
标签: ios swift arkit realitykit