代码之家  ›  专栏  ›  技术社区  ›  KNV

ARKit和Vision如何在找到的二维码上放置SCNPlane?

  •  1
  • KNV  · 技术社区  · 7 年前

    通过 Vision

    也许你们中有人知道我做错了什么。

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

    编辑:

    添加了当前情况的图片。红色平面添加在左上角。然而,该平面必须精确地添加到te QRCode上。

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   meow    7 年前

    使用中心点而不是原点对我有效:

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