swift - ARKit图像检测,添加图像

标签 swift xcode image scenekit arkit

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

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

/// - 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的值是实际的图像文件名。

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

关于swift - ARKit图像检测,添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48876486/

相关文章:

ios - 像 UIKit UIView 这样的 SwiftUI View ,如何更改图像的大小

image - 在 Flutter 中使用永久背景图片

php - 从 MySQL 数据生成动态图

ios - 如何在swift中创建具有多个参数的选择器

ios - 如何使用 NSURLSession 快速发送 HTTPS POST 请求

json - Swift 4 Decodable - 动态类型、键和属性

ios - 无法将WebKit框架构建/合并到iOS项目中

ios - QuickBlox-对话框必须在Objective-C中的内存缓存中

ios - 删除导航栏下方的阴影线而不删除栏颜色?

html - div中的动态图像尺寸