有人知道如何在AR中通过相机获取参考图像的名称?
我认为Anchor正在读取标识符并将其连接到返回名称和物理尺寸的参考图像。
我想要一个 AR资源文件夹,我可以放置不同的图像,然后,根据相机识别的内容,我想显示一个模型而不是另一个模型。
非常感谢!
答案 0 :(得分:3)
ARReferenceImage
具有您可以访问的以下属性:
var name: String?
图像的描述性名称。
var physicalSize: CGSize
图像的真实尺寸(以米为单位)。
因此,为了获取参考图像的名称和其他属性,您可以使用以下delegate
回调:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
//1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
guard let currentImageAnchor = anchor as? ARImageAnchor else { return }
//2. Get The Targets Name
let name = currentImageAnchor.referenceImage.name!
//3. Get The Targets Width & Height
let width = currentImageAnchor.referenceImage.physicalSize.width
let height = currentImageAnchor.referenceImage.physicalSize.height
//4. Log The Reference Images Information
print("""
Image Name = \(name)
Image Width = \(width)
Image Height = \(height)
""")
}
请注意,此代码假定您的图片具有在ARResources Asset Folder
中设置的名称,例如:
然后您可以在回调中放置逻辑或调用另一个函数,该函数在SCNNode
的转换处添加SCNScene
或ARImageAnchor
,例如:
//1. Create An SCNNode
let nodeHolder = SCNNode()
//2. Determine Which ImageTarget Has Been Detected
if name == "ImageOne"{
let nodeGeometry = SCNBox(width: 0.02, height: 0.02, length: 0.02, chamferRadius: 0)
nodeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
nodeHolder.geometry = nodeGeometry
}else if name == "ImageTwo"{
let nodeGeometry = SCNSphere(radius: 0.02)
nodeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
nodeHolder.geometry = nodeGeometry
}
//3. Add The SCNNode At The Position Of The Anchor
nodeHolder.position = SCNVector3(currentImageAnchor.transform.columns.3.x,
currentImageAnchor.transform.columns.3.y,
currentImageAnchor.transform.columns.3.z)
//4. Add It To The Scene
augmentedRealityView?.scene.rootNode.addChildNode(nodeHolder)