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)
// 二维图片检测//config指向
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:[ ])
// 1.语句将ARSession代理设置为ARView
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 {
// Load the "ArrowScene" scene from the "Experience" Reality File
let myModelEntity = try Experience.loadArrowScene()
let imageAnchorEntity = AnchorEntity(anchor:imageAnchor)
imageAnchorEntity.addChild(myModelEntity)
self.scene.addAnchor(imageAnchorEntity)
//drummerEntity.playAnimation(drummerEntity.availableAnimations[0].repeat())
} catch {
print("无法加载模型")
}
}
}