在ARImageTrackingConfiguration

时间:2018-06-27 14:21:07

标签: ios swift arkit

在ARKit 2中,添加了新配置:ARImageTrackingConfiguration,根据SDK,它可以具有更好的性能和一些新的用例。

在Xcode 10b2上进行了实验(请参见https://forums.developer.apple.com/thread/103894如何解决资产加载问题),我的代码现在正确地调用了已跟踪图像的委托,并且此后添加了节点,但是我找不到任何文档,坐标系统已定位,因此没有人知道如何将节点放入场景中以使其覆盖检测到的图像:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    DispatchQueue.main.async {
        if let imageAnchor = anchor as? ARImageAnchor {
            let imageNode = SCNNode.createImage(size: imageAnchor.referenceImage.physicalSize)
            imageNode.transform = // ... ???
            node.addChildNode(imageNode)
        }
    }
}

ps:与ARWorldTrackingConfiguration相比,原点似乎一直在移动(很可能是将相机置于0,0,0)。

pps:SCNNode.createImage是一个辅助函数,无需任何坐标计算。

1 个答案:

答案 0 :(得分:1)

假设我已正确阅读您的问题,则可以执行以下操作:

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

    let nodeToReturn = SCNNode()

    //1. Check We Have Detected Our Image
    if let validImageAnchor = anchor as? ARImageAnchor {

        //2. Log The Information About The Anchor & Our Reference Image
        print("""
            ARImageAnchor Transform = \(validImageAnchor.transform)
            Name Of Detected Image = \(validImageAnchor.referenceImage.name)
            Width Of Detected Image = \(validImageAnchor.referenceImage.physicalSize.width)
            Height Of Detected Image = \(validImageAnchor.referenceImage.physicalSize.height)
         """)

        //3. Create An SCNPlane To Cover The Detected Image
        let planeNode = SCNNode()

        let planeGeometry = SCNPlane(width: validImageAnchor.referenceImage.physicalSize.width,
                                    height: validImageAnchor.referenceImage.physicalSize.height)

        planeGeometry.firstMaterial?.diffuse.contents = UIColor.white

        planeNode.geometry = planeGeometry

        //a. Set The Opacity To Less Than 1 So We Can See The RealWorld Image
        planeNode.opacity = 0.5

        //b. Rotate The PlaneNode So It Matches The Rotation Of The Anchor
        planeNode.eulerAngles.x = -.pi / 2

        //4. Add It To The Node
        nodeToReturn.addChildNode(planeNode)

        //5. Add Something Such As An SCNScene To The Plane
        if let modelScene = SCNScene(named: "art.scnassets/model.scn"), let modelNode = modelScene.rootNode.childNodes.first{

            //a. Set The Model At The Center Of The Plane & Move It Forward A Tad
            modelNode.position = SCNVector3Zero
            modeNode.position.z = 0.15

            //b. Add It To The PlaneNode
            planeNode.addChildNode(modelNode)
        }
    }

    return nodeToReturn

}

希望这会为您指明正确的方向...