//create a CIImage
let image = UIImage(named: "refimage.jpg",
in: Bundle(for: type(of:self)),
compatibleWith: nil)
let ciimage = CIImage(image: image!)
let cgimage = convertCIImageToCGImage(inputImage: ciimage!)!
let arReference = ARReferenceImage(cgimage, orientation: .up, physicalWidth: 0.05)
//add this image into ARReferenceImage<Set>
refImage.insert(arReference)
//add this set to the ARView Tracking Config
let config = ARImageTrackingConfiguration()
config.trackingImages = refImage
//run the config
arView.session.run(config)
然后,当系统在ARSessionDelegate中检测到图像时,您会收到通知
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
for i in anchors{
if let a = i as? ARImageAnchor{
DispatchQueue.main.async {
print("find anchor")
let imageAnchor = AnchorEntity(anchor: a)
self.boxEntity = ModelEntity(mesh: MeshResource.generateBox(size: 0.005),materials: [SimpleMaterial(color: .green, isMetallic: true)])
imageAnchor.addChild(self.boxEntity)
self.arView.scene.addAnchor(imageAnchor)
}
}
}
}
1条答案
按热度按时间798qvoo81#
我找到了一种在代码中加载ARreferenceImage的方法:
然后,当系统在ARSessionDelegate中检测到图像时,您会收到通知