2022年2月26日 星期六

ARKit(SceneKit) 臉部表情追蹤

  1.  建立臉部追蹤
  2. // Create a session configuration

     let configuration = ARFaceTrackingConfiguration()

    // Run the view's session

    sceneView.session.run(configuration)

  3.  建立臉部模型
  4. func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

        if anchor is ARFaceAnchor {

            let faceMesh = ARSCNFaceGeometry(device: sceneView.device!)

            faceMesh?.firstMaterial?.lightingModel = .physicallyBased

            let node = SCNNode(geometry: faceMesh)

            return node

        } else {

            return nil

        }

    }

  5.  更新臉部表情
  6. func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

        if let faceAnchor = anchor as? ARFaceAnchor, let faceGeometry = node.geometry as? ARSCNFaceGeometry {

            faceGeometry.update(from: faceAnchor.geometry)

        }

    }

沒有留言: