2022年2月28日 星期一

ARKit(SceneKit) 更新模型臉部表情



  1. 使用包含BlendShapes的模型。如sample的fbx模型
  2. 在Blender中匯入fbx模型,確認Object Data Properties有外形鍵

  3. 匯出成dae檔,確認有勾選外形鍵

  4. 使用ColladaMorphAdjuster將剛剛的dae檔,轉成SceneKit支援的dae檔
  5. 在XCode中,可以看到模型的Attributes裡,Geometry Morphers中會有類似臉部細心的key

  6. Editor -> convert to SceneKit file format
  7. 載入模型

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

        if anchor is ARFaceAnchor {

            let scene = SCNScene(named: "art.scnassets/POLYWINK_BOD_157_SAMPLE.scn")!

            face = scene.rootNode.childNode(withName: "POLYWINK_Louise", recursively: true)

            face?.geometry?.firstMaterial?.lightingModel = .physicallyBased

            return scene.rootNode

        } else {

            return nil

        }

    }

  8. 將BlendShapesLocation對照到模型的外形鍵

    let blendShapeMap: [ARFaceAnchor.BlendShapeLocation: String] = [

        .eyeBlinkLeft: "Eye_Closed_Left",

        .eyeBlinkRight: "Eye_Closed_Right"

    ]

  9. 將faceAnchor的blendShapes對照到node的morpher

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {

        if let faceAnchor = anchor as? ARFaceAnchor {

            let blendShapes = faceAnchor.blendShapes

            blendShapeMap.forEach{ key, name in

                if let weight = blendShapes[key] as? CGFloat {

                    face?.morpher?.setWeight(weight, forTargetNamed: name)

                }

            }

        }

    }

沒有留言: