首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ARKit图像检测,添加图像

ARKit图像检测,添加图像
EN

Stack Overflow用户
提问于 2018-02-20 09:07:58
回答 1查看 1.9K关注 0票数 0

我正在使用ARKit 1.5 (测试版)进行图像检测。一旦我检测到我的图像,我想使用检测到的平面放置一个AR场景图像。如何做到这一点?

到目前为止,我的代码检测图像(在我的assets文件夹中):

代码语言:javascript
复制
/// - Tag: ARImageAnchor-Visualizing
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let imageAnchor = anchor as? ARImageAnchor else { return }
    let referenceImage = imageAnchor.referenceImage
    updateQueue.async {

        // Create a plane to visualize the initial position of the detected image.
        let plane = SCNPlane(width: referenceImage.physicalSize.width,
                             height: referenceImage.physicalSize.height)
        let planeNode = SCNNode(geometry: plane)
        planeNode.opacity = 0.25

        /*
         `SCNPlane` is vertically oriented in its local coordinate space, but
         `ARImageAnchor` assumes the image is horizontal in its local space, so
         rotate the plane to match.
         */
        planeNode.eulerAngles.x = -.pi / 2

        /*
         Image anchors are not tracked after initial detection, so create an
         animation that limits the duration for which the plane visualization appears.
         */
        planeNode.runAction(self.imageHighlightAction)

        // Add the plane visualization to the scene.
        node.addChildNode(planeNode)
    }

    DispatchQueue.main.async {
        let imageName = referenceImage.name ?? ""
        self.statusViewController.cancelAllScheduledMessages()
        self.statusViewController.showMessage("Detected image “\(imageName)”")
    }
}

var imageHighlightAction: SCNAction {
    return .sequence([
        .wait(duration: 0.25),
        .fadeOpacity(to: 0.85, duration: 1.50),
        .fadeOpacity(to: 0.15, duration: 1.50),
        .fadeOpacity(to: 0.85, duration: 1.50),
        .fadeOut(duration: 0.75),
        .removeFromParentNode()
    ])
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-20 10:08:05

假设referenceImage.name的值是实际的图像文件名。

代码语言:javascript
复制
if let imageName = referenceImage.name {
    plane.materials = [SCNMaterial()]
    plane.materials[0].diffuse.contents = UIImage(named: imageName)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48876486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档