[ARKit] Face Tracking 앱 만들기 (4) - ARSCNFaceGeometry, SCNMaterial
[기본 개념]
SCNNode 에 SCNGeometry 를 attach하고
SCNGeometry 에 SCNMaterial 를 attach한다.
즉, material -> geometry -> node 이렇게 붙는 것이다.
간단하게 정의를 살펴보자면
- SCNNode: scene graph의 구성요소이다. 3D 공간에서의 postion, transfrom 을 나타낼수 있다. 여기에 geometry, lights, camera 등을 attach 할 수 있다.
- SCNGeometry: 3D shape. SCNPlane, SCNBox, SCNSphere, SCNPyramid, SCNCone, SCNText 등이 SCNGeometry의 서브클래스이다.
- SCNMaterial : geometry의 surface의 모양을 정의하는 shading attributes의 모음
[1] material을 설정해준 ARSCNFaceGeometry로 노드를 만들기
ARSCNFaceGeometry 도 SCNGeometry의 서브클래스이다.
1 - ARSCNFaceGeometry를 만들어준다
2 - geometry에 첫번째 attach된 material인 firstMaterial를 가져온다. (geometry를 만들면 firstMaterial은 꼭 존재하는 값이여서 애플은 강제 언래핑을 쓴 것 같다)
그리고 가져온 material의 contents를 바꿔준다.
컬러를 지정해줄 수 있고 주석처럼 이미지를 지정해줄 수도 있다.
scn파일의 이쪽 부분을 건드리는 것과 같다.
3 - material을 바꿔준 geometry로 node를 만든다.
이렇게 돌리면 노란색 material가 attached된 faceGeometry가 나오게 되고 face tracking도 잘되는 것을 볼 수 있다.
(너무 무섭게 나와서 일부러 눈감았음)
[2] material에 lightingModel 설정해주기
materail에는 lightingModel이라는 속성이 있다.
physicallyBased로 설정해준다.
physicallyBased는 현실적인 것에 기반을 둔 shading이라고 한다.
(Shading based on a realistic abstraction of physical lights and materials.)
material.lightingModel = .physicallyBased
이렇게 하고 돌려주면
입체적인 마스크가 나오게 된다.
(이것도 무섭게 나와서 일부러 눈감고 기록함)
[3] faceGeometry를 update해서 표정 tracking하기
ARSCNFaceGeometry에는 update라는 함수가 있다.
didUpdate에서 geometry를 업데이트 해주면..!
이렇게 내 표정까지 트래킹하게 된다.
[최종 코드]
[Reference]
developer.apple.com/documentation/arkit/tracking_and_visualizing_faces
Tracking and Visualizing Faces | Apple Developer Documentation
Sample Code Tracking and Visualizing Faces Detect faces in a front-camera AR experience, overlay virtual content, and animate facial expressions in real-time. Download OverviewThis sample app presents a simple interface allowing you to choose between five
developer.apple.com
참고로 애플 예제에서는 이렇게 생긴 이미지를 material로 해줬음.