{"id":1196,"date":"2025-06-01T11:21:18","date_gmt":"2025-06-05T03:39:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=1196"},"modified":"2025-06-05T11:21:18","modified_gmt":"2025-06-05T03:39:18","slug":"swift-arkit","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-arkit\/","title":{"rendered":"Swift ARKit \u81c9\u90e8\u7279\u6548\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>Swift ARKit \u81c9\u90e8\u7279\u6548\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b<\/h1>\n<h2>\u7c21\u4ecb<\/h2>\n<p>ARKit \u662f\u860b\u679c\u516c\u53f8\u63a8\u51fa\u7684\u4e00\u5957\u589e\u5f37\u73fe\u5be6\u6846\u67b6\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728 iOS \u61c9\u7528\u4e2d\u5275\u5efa\u9a5a\u4eba\u4e14\u4e92\u52d5\u6027\u5f37\u7684 AR \u9ad4\u9a57\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u548c ARKit \u5275\u5efa\u81c9\u90e8\u7279\u6548\u3002\u6b64\u6559\u5b78\u5c07\u6db5\u84cb\u57fa\u672c\u8a2d\u7f6e\u3001\u5be6\u4f5c\u7bc4\u4f8b\u4ee5\u53ca\u5e38\u898b\u932f\u8aa4\u6392\u9664\uff0c\u5e6b\u52a9\u4f60\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2>\u57fa\u672c\u8a2d\u7f6e<\/h2>\n<h3>\u65b0\u589e ARSCNView<\/h3>\n<pre><code class=\"language-swift line-numbers\">override func viewDidLoad() {\n    super.viewDidLoad()\n    sceneView = ARSCNView()\n    sceneView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)\n    sceneView.delegate = self\n    sceneView.showsStatistics = true\n    self.view.addSubview(sceneView)\n\n    guard ARFaceTrackingConfiguration.isSupported else {\n        fatalError(\"Face tracking is not supported on this device\")\n    }\n}\n<\/code><\/pre>\n<h3>\u555f\u52d5\u8207\u66ab\u505c AR Session<\/h3>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u9032\u5165\u6642\u958b\u59cb\u57f7\u884c\noverride func viewWillAppear(_ animated: Bool) {\n    super.viewWillAppear(animated)\n    let configuration = ARFaceTrackingConfiguration()\n    sceneView.session.run(configuration)\n}\n\n\/\/ \u9000\u51fa\u6642\u66ab\u505c\noverride func viewWillDisappear(_ animated: Bool) {\n    super.viewWillDisappear(animated)\n    sceneView.session.pause()\n}\n<\/code><\/pre>\n<h3>ARSCNViewDelegate \u5be6\u4f5c<\/h3>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u8a2d\u7f6e node\nfunc renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {\n    let faceMesh = ARSCNFaceGeometry(device: sceneView.device!)\n    let node = SCNNode(geometry: faceMesh)\n    return node\n}\n<\/code><\/pre>\n<h3>\u8868\u60c5\u5075\u6e2c\u8207 BlendShapes<\/h3>\n<p>\u900f\u904e BlendShapes\uff0c\u6211\u5011\u53ef\u4ee5\u5075\u6e2c\u4f7f\u7528\u8005\u7684\u81c9\u90e8\u8868\u60c5\u4e26\u505a\u51fa\u76f8\u61c9\u7684\u53cd\u61c9\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5075\u6e2c\u5f35\u5634\nfunc expression(anchor: ARFaceAnchor) {\n    let jawOpen = anchor.blendShapes[.jawOpen]\n    if jawOpen?.decimalValue ?? 0.0 > 0.1 {\n        DispatchQueue.main.async {\n            self.view.showToast(text: \"\u5f35\u5634\u5df4\")\n        }\n    }\n}\n<\/code><\/pre>\n<h3>\u81c9\u90e8\u77db\u9ede\u5b9a\u4f4d\u8207\u66f4\u65b0<\/h3>\n<pre><code class=\"language-swift line-numbers\">func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {\n    if let faceAnchor = anchor as? ARFaceAnchor, let faceGeometry = node.geometry as? ARSCNFaceGeometry {\n        faceGeometry.update(from: faceAnchor.geometry)\n        expression(anchor: faceAnchor)\n    }\n}\n<\/code><\/pre>\n<h2>\u81c9\u90e8\u7279\u6548<\/h2>\n<p>\u5728\u9019\u4e00\u90e8\u5206\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u70ba\u81c9\u90e8\u6dfb\u52a0\u4e0d\u540c\u7684\u7279\u6548\uff0c\u4f8b\u5982\u9f3b\u5b50\u3001\u773c\u775b\u548c\u5e3d\u5b50\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let noseOptions = [\"nose_1\", \"nose_2\", \"nose_3\", \"nose_4\"]\nlet eyeOptions = [\"eye_1\", \"eye_2\", \"eye_3\"]\nlet mouthOptions = [\"mouth_1\", \"mouth_2\", \"mouth_3\", \"mouth_4\"]\nlet hatOptions = [\"hat_1\", \"hat_2\", \"hat_3\", \"hat_4\"]\nlet features = [\"nose\", \"leftEye\", \"rightEye\", \"mouth\", \"hat\"]\n<\/code><\/pre>\n<h3>\u66f4\u65b0\u7279\u6548\u4f4d\u7f6e<\/h3>\n<pre><code class=\"language-swift line-numbers\">func updateFeatures(for node: SCNNode, using anchor: ARFaceAnchor) {\n    for (feature, indices) in zip(features, featureIndices) {\n        let child = node.childNode(withName: feature, recursively: false) as? ImageNode\n        let vertices = indices.map { anchor.geometry.vertices[$0] }\n        child?.updatePosition(for: vertices)\n    }\n}\n<\/code><\/pre>\n<h3>\u6416\u982d\/\u9ede\u982d\u5075\u6e2c<\/h3>\n<pre><code class=\"language-swift line-numbers\">var isLeft = false\nvar isRight = false\nvar isUp = false\nvar isDown = false\n\n\/\/ \u6aa2\u67e5\u6416\u982d\nfunc checkShakingHead() {\n    if isLeft && isRight {\n        DispatchQueue.main.async {\n            self.view.showToast(text: \"\u6416\u982d\u6210\u7acb\")\n        }\n        isLeft = false\n        isRight = false\n    }\n}\n\n\/\/ \u6aa2\u67e5\u9ede\u982d\nfunc checkNod() {\n    if isUp && isDown {\n        DispatchQueue.main.async {\n            self.view.showToast(text: \"\u9ede\u982d\u6210\u7acb\")\n        }\n        isUp = false\n        isDown = false\n    }\n}\n<\/code><\/pre>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u9019\u7bc7\u6559\u5b78\uff0c\u4f60\u61c9\u8a72\u80fd\u5920\u4f7f\u7528 Swift \u548c ARKit \u5275\u5efa\u57fa\u672c\u7684\u81c9\u90e8\u7279\u6548\u3002\u96a8\u8457\u4f60\u5c0d ARKit \u7684\u6df1\u5165\u4e86\u89e3\uff0c\u4f60\u53ef\u4ee5\u5617\u8a66\u66f4\u591a\u7684\u5275\u65b0\u548c\u5be6\u9a57\uff0c\u70ba\u4f7f\u7528\u8005\u63d0\u4f9b\u66f4\u8c50\u5bcc\u7684\u4e92\u52d5\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. ARKit \u662f\u5426\u652f\u6301\u6240\u6709 iOS \u8a2d\u5099\uff1f<\/h3>\n<p>ARKit \u76ee\u524d\u50c5\u652f\u6301\u5177\u5099 A9 \u8655\u7406\u5668\u53ca\u4ee5\u4e0a\u7684\u8a2d\u5099\uff0c\u5efa\u8b70\u6aa2\u67e5\u4f60\u7684\u8a2d\u5099\u898f\u683c\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406 ARKit \u4e2d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u914d\u7f6e\u4e0d\u652f\u6301\u3001\u76f8\u6a5f\u6b0a\u9650\u554f\u984c\u7b49\u3002\u8acb\u6aa2\u67e5\u4f60\u7684 Info.plist \u6587\u4ef6\uff0c\u78ba\u4fdd\u5df2\u6388\u4e88\u76f8\u6a5f\u4f7f\u7528\u6b0a\u9650\u3002<\/p>\n<h3>3. \u6211\u53ef\u4ee5\u5728 ARKit \u4e2d\u4f7f\u7528\u81ea\u5b9a\u7fa9\u6a21\u578b\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0cARKit \u652f\u6301\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684 3D \u6a21\u578b\uff0c\u4f60\u53ef\u4ee5\u5c07\u5176\u5c0e\u5165\u4e26\u5728 AR \u74b0\u5883\u4e2d\u986f\u793a\u3002<\/p>\n<p>\u5c08\u6848\u4e0b\u8f09: <a href=\"https:\/\/github.com\/Bgihe\/FaceARTest\" title=\"Github\" target=\"_blank\" rel=\"noopener\">Github<\/a><\/p>\n<p>\u53c3\u8003\u6587\u4ef6: <a href=\"https:\/\/www.raywenderlich.com\/5491-ar-face-tracking-tutorial-for-ios-getting-started\" title=\"AR Face Tracking Tutorial for iOS: Getting Started\" target=\"_blank\" rel=\"noopener\">AR Face Tracking Tutorial for iOS: Getting Started<\/a><\/p>\n<p>\u66f4\u591a\u95dc\u65bc Swift \u7684\u6587\u7ae0\u8acb\u67e5\u770b\u4ee5\u4e0b\u9023\u7d50\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-340\/\" title=\"Swift \u5f48\u51fa\u8996\u7a97 AlertController \u7684\u4f7f\u7528\u65b9\u6cd5\">Swift \u5f48\u51fa\u8996\u7a97 AlertController \u7684\u4f7f\u7528\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-8\/\" title=\"Swift \u5224\u65b7\u87a2\u5e55\u65b9\u5411\">Swift \u5224\u65b7\u87a2\u5e55\u65b9\u5411<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-6\/\" title=\"Swift Core Data \u5be6\u73fe\">Swift Core Data \u5be6\u73fe<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-4\/\" title=\"Swift UISegmentedControl\">Swift UISegmentedControl<\/a><\/li>\n<li><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-5\/\" title=\"Swift \u5be6\u73fe\u62bd\u5c5c\u6548\u679c\">Swift \u5be6\u73fe\u62bd\u5c5c\u6548\u679c<\/a><\/li>\n<\/ul>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`html Swift ARKit &#8230;<\/p>\n","protected":false},"author":1,"featured_media":1200,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-1196","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/04\/ARkit_Anchor_1-1.jpg","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-ji","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=1196"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1196\/revisions"}],"predecessor-version":[{"id":7576,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1196\/revisions\/7576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/1200"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}