获取参考图像的名称 swift ARKit 1.5

Get name of reference image swift ARKit 1.5

有人知道如何在 AR 中通过相机获取红色的 参考图像 的名称吗?

我认为 Anchor 正在读取标识符并将其连接到返回 namephysical size 的参考图像。

我想要一个 AR Resources 文件夹,我可以在其中放置不同的图像,然后,根据相机识别的内容,我想显示一个模型而不是另一个模型.

非常感谢!

ARReferenceImage 具有以下您可以访问的属性:

var name: String?

A descriptive name for the image.

var physicalSize: CGSize

The real-world dimensions, in meters, of the image.

因此,为了获取参考图像的名称和其他属性,您可以使用以下 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 中设置的名称,例如:

然后您可以将逻辑放在回调中或调用另一个函数,该函数在 ARImageAnchor 的转换处添加 SCNNodeSCNScene,例如:

//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)