是否可以使用ARKit检测放置在其上的物体的地板?

时间:2018-02-14 12:16:01

标签: ios swift arkit

我想使用ARkit检测地板,当桌子等物品存在并希望为检测到的表面着色时,这是否可能使用ARkit,我是ARKit世界的新手并且不确定这是否可能在哪里开始。

1 个答案:

答案 0 :(得分:5)

您的会话配置功能应如下所示,您可以在viewWillAppear上调用它

func setUpSceneView() {
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal

        sceneView.session.run(configuration)

        sceneView.delegate = self
        sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
    }

你也可以使用.vertical作墙或其他什么..

在视图上加载时将tapGestureRecognizer添加到场景中,以便您可以将SCNNode添加到您触摸检测到的曲面的位置

viewDidLoad中的addTapGestureToSceneView()

func addTapGestureToSceneView() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(placeOnSurface.addNodeToSceneView(withGestureRecognizer:)))
        sceneView.addGestureRecognizer(tapGestureRecognizer)
    }

方法addNodeToSceneView会在检测到平面时将SNNode置于曲面上。

@objc func addShipToSceneView(withGestureRecognizer recognizer: UIGestureRecognizer) {
        let tapLocation = recognizer.location(in: sceneView)
        let hitTestResults = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)

        guard let hitTestResult = hitTestResults.first else { return }
        let translation = hitTestResult.worldTransform.translation
        let x = translation.x
        let y = translation.y
        let z = translation.z

        var name:String = "YourModel.obj" //you can add node whatever


        let group = DispatchGroup()
        group.enter()
        DispatchQueue.global(qos: .background).async {
            print("This is run on the background queue")

            let tempScene = SCNScene(named: name)!
            var geom:SCNGeometry = tempScene.rootNode.childNodes[0].geometry!
            var boxNode:SCNNode = SCNNode(geometry: geom)
            boxNode.position = SCNVector3(x,y,z)
            self.sceneView.scene.rootNode.addChildNode(boxNode)
            group.leave()

        }
        group.notify(queue: .main){
            //Here you know that the node is has been put
        }
    }

and you need some functions for render



    extension float4x4 {
    var translation: float3 {
        let translation = self.columns.3
        return float3(translation.x, translation.y, translation.z)
    }
}



    extension placeOnSurface: ARSCNViewDelegate {
        func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
            // 1
            guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

            // 2
            let width = CGFloat(planeAnchor.extent.x)
            let height = CGFloat(planeAnchor.extent.z)
            let plane = SCNPlane(width: width, height: height)

            // 3
            plane.materials.first?.diffuse.contents = UIColor.transparentLightBlue

            // 4
            let planeNode = SCNNode(geometry: plane)

            // 5
            let x = CGFloat(planeAnchor.center.x)
            let y = CGFloat(planeAnchor.center.y)
            let z = CGFloat(planeAnchor.center.z)
            planeNode.position = SCNVector3(x,y,z)
            planeNode.eulerAngles.x = -.pi / 2

            // 6
            node.addChildNode(planeNode)
        }

        func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
            // 1
            guard let planeAnchor = anchor as?  ARPlaneAnchor,
                let planeNode = node.childNodes.first,
                let plane = planeNode.geometry as? SCNPlane
                else { return }

            // 2
            let width = CGFloat(planeAnchor.extent.x)
            let height = CGFloat(planeAnchor.extent.z)
            plane.width = width
            plane.height = height

            // 3
            let x = CGFloat(planeAnchor.center.x)
            let y = CGFloat(planeAnchor.center.y)
            let z = CGFloat(planeAnchor.center.z)
            planeNode.position = SCNVector3(x, y, z)
        }
    }

我还建议你在viewDidLoad中调用configureLighting()函数以获得更好的用户体验

func configureLighting() {
        sceneView.autoenablesDefaultLighting = true
        sceneView.automaticallyUpdatesLighting = true
    }