我想使用來自網路的影像加載作為錨點來將我的物件附加到現實世界中。
但是,我發現reality kit只提供了使用asset檔案夾中的影像作為錨點,似乎無法通過代碼添加。
AnchorEntity(.image(group: "", name: ""))
那么 ARKit 有沒有一種可能的方式來做這些事情呢?
uj5u.com熱心網友回復:
我找到了一種在代碼中加載 ARreferenceImage 的方法:
//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)
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/526550.html
