ARKit 图像检测,添加图像 [英] ARKit image detection, add image

查看:19
本文介绍了ARKit 图像检测,添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ARKit 1.5(测试版)进行图像检测.检测到图像后,我想使用检测到的平面放置 AR 场景图像.如何才能做到这一点?

I'm using ARKit 1.5 (beta) for image detection. Once I detect my image I would like to then place a AR scene image using the plane detected. How can this be done?

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

My code so far which detects the image (which is in my assets folder):

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

推荐答案

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

Assuming that referenceImage.name's value is the actual image filename.

if let imageName = referenceImage.name {
    plane.materials = [SCNMaterial()]
    plane.materials[0].diffuse.contents = UIImage(named: imageName)
}

这篇关于ARKit 图像检测,添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆