Search code examples
iosscenekitqr-codearkitapple-vision

ARKit and Vision – How to place a SCNPlane on a found QR-Code?


Through Vision Framework I'm able to detect a QR-code. The next thing I would like to do is to place a SCNPlane exactly on the QRCode using ARKit. I wrote the code below to find the position of the QRCode in the real world. But the SCNPlane keeps added in the wrong place. The barcode variable in the code below is of the VNBarcodeObservation type.

Maybe one of you guys knows what I'm doing wrong.

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

Edit:

Added a picture of the current situation. The red plane is added in the upper left corner. However, this plane has to be added exactly on te QRCode.

enter image description here


Solution

  • Works for me using the center point instead of the origins:

    let center2 = CGPoint(x: result.boundingBox.midX, y: result.boundingBox.midY)
    
    let hitTestResults2 = frame.hitTest(center2, types: [.featurePoint/*, .estimatedHorizontalPlane, .existingPlane, .existingPlaneUsingExtent*/] )