swiftUI,realitykit识别到图像加载整个AR的Scene。 // Load the “ArrowScene” scene from the “Experience” Reality File 说明: 1.ArrowScene为Experience中场景的名称。 2.Experience为Experience.rcproject文件。 3.ReferenceImagesLibrary是在Assets.xcassets中建立的group。识别的时候会遍历里面的图片。
func makeUIView(context: Context) -> ARView {
arView = ARView(frame: .zero)
let config = ARImageTrackingConfiguration()
guard let trackedImagesLib = ARReferenceImage.referenceImages(inGroupNamed: "ReferenceImagesLibrary", bundle: Bundle.main) else {
fatalError("无法加载参考图像库")
}
config.trackingImages = trackedImagesLib
config.maximumNumberOfTrackedImages = 1
arView.session.run(config, options:[ ])
arView.session.delegate = arView
return arView
}
public func session(_ session: ARSession, didAdd anchors: [ARAnchor]){
guard let imageAnchor = anchors[0] as? ARImageAnchor else {
return
}
DispatchQueue.main.async {
do {
let myModelEntity = try Experience.loadArrowScene()
let imageAnchorEntity = AnchorEntity(anchor:imageAnchor)
imageAnchorEntity.addChild(myModelEntity)
self.scene.addAnchor(imageAnchorEntity)
} catch {
print("无法加载模型")
}
}
}
|