有人知道如何在 AR 中获得相机红色参考图像的名称吗?
我认为 Anchor 正在读取标识符并将其连接到返回name和physical size的参考图像。
我想要一个AR Resources文件夹,我可以在其中放置不同的图像,然后根据相机识别的内容,我想显示一个模型而不是另一个模型。
非常感谢!
AnARReferenceImage
具有您可以访问的以下属性:
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)