ARKit和Vision - 如何在找到的QR码上放置SCNPlane?

问题描述 投票:1回答:1

通过Vision框架,我能够检测到QR码。我想做的下一件事是使用ARKit将SCNPlane完全放在QRCode上。我编写了下面的代码,以找到QRCode在现实世界中的位置。但是SCNPlane一直在错误的地方。下面代码中的条形码变量属于VNBarcodeObservation类型。

也许你们其中一个人知道我做错了什么。

let rect = CGPoint(x: barcode.boundingBox.origin.x, y: barcode.boundingBox.origin.y)
let hitTestResults = sceneView.hitTest(rect, types: [.existingPlaneUsingExtent, .existingPlane])
if let hitResult = hitTestResults.first {

    //TODO: change to width and height
    let plane = SCNPlane(width: 0.1, height: 0.1)
    let material = SCNMaterial()
    material.diffuse.contents = UIColor.red
    plane.materials = [material]

    let node = SCNNode()        
    node.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)

    node.position = SCNVector3(hitResult.worldTransform.columns.3.x,
                                         hitResult.worldTransform.columns.3.y,
                                         hitResult.worldTransform.columns.3.z)

    node.geometry = plane  
    sceneView.scene.rootNode.addChildNode(node)
}

编辑:

添加了当前情况的图片。红色平面添加在左上角。但是,必须在QRCode上精确添加此平面。

enter image description here

ios scenekit qr-code arkit apple-vision
1个回答
2
投票

使用中心点而不是原点为我工作:

let center2 = CGPoint(x: result.boundingBox.midX, y: result.boundingBox.midY)

let hitTestResults2 = frame.hitTest(center2, types: [.featurePoint/*, .estimatedHorizontalPlane, .existingPlane, .existingPlaneUsingExtent*/] )
© www.soinside.com 2019 - 2024. All rights reserved.