@IBOutlet var sceneView: ARSCNView!
// viewdidload
let configuration = ARBodyTrackingConfiguration()
sceneView.session.run(configuration)
// ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let bodyAnchor = anchor as? ARBodyAnchor else { return }
// 刷新參數
node.transform = SCNMatrix4(bodyAnchor.transform)
// 遍歷人體骨架
for jointName in bodyAnchor.skeleton.definition.jointNames {
let jointIndex = bodyAnchor.skeleton.definition.index(for: jointName)
let jointTransform = bodyAnchor.skeleton.modelTransform(for: jointIndex)
let jointNode = node.childNode(withName: jointName, recursively: true) ?? createJointNode(jointName: jointName)
jointNode.transform = SCNMatrix4(jointTransform)
node.addChildNode(jointNode)
}
}
// 創建節點
private func createJointNode(jointName: ARSkeleton.JointName) -> SCNNode {
let sphere = SCNSphere(radius: 0.02)
sphere.firstMaterial?.diffuse.contents = UIColor.red
let jointNode = SCNNode(geometry: sphere)
jointNode.name = jointName.rawValue
return jointNode
}
}
記得加入相機的權限歐