1、新建项目FaceTracking
2、安装ARFoundation及FaceTracking的相关插件
在当前场景中新建 AR Session和 AR Session Orgin,将 AR Session Orgin 设置为 Main Camera后删除当前场景中的原有 Main Camera。
可以在Assert Store里搜索并下载喜欢的mask模型
添加ARFaceManager脚本,并将下载的模型添加到脚本中
注:经测试,如果直接添加模型,将模型y轴旋转180度,在显示时,面具方向会反向,所以需要包裹在GameObject对象中。
以上图片均进行处理,只为显示面部增强和表情追踪效果。
需要单独加入眼睛追踪的脚本
!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
#if UNITY_IOS && !UNITY_EDITOR
using UnityEngine.XR.ARKit;
#endif
/// <summary>
/// Visualizes the eye poses for an <see cref="ARFace"/>.
/// </summary>
/// <remarks>
/// Face space is the space where the origin is the transform of an <see cref="ARFace"/>.
/// </remarks>
[RequireComponent(typeof(ARFace))]
public class EyePoseVisualizer : MonoBehaviour
{
[SerializeField]
GameObject m_EyePrefab;
public GameObject eyePrefab
{
get => m_EyePrefab;
set => m_EyePrefab = value;
}
GameObject m_LeftEyeGameObject;
//GameObject m_RightEyeGameObject;
ARFace m_Face;
XRFaceSubsystem m_FaceSubsystem;
void Awake()
{
m_Face = GetComponent<ARFace>();
}
void CreateEyeGameObjectsIfNecessary()
{
if (m_Face.leftEye != null && m_LeftEyeGameObject == null)
{
m_LeftEyeGameObject = Instantiate(m_EyePrefab, m_Face.leftEye);
m_LeftEyeGameObject.SetActive(false);
}
//if (m_Face.rightEye != null && m_RightEyeGameObject == null)
//{
// m_RightEyeGameObject = Instantiate(m_EyePrefab, m_Face.rightEye);
// m_RightEyeGameObject.SetActive(false);
//}
}
void SetVisible(bool visible)
{
if (m_LeftEyeGameObject != null) //&& m_RightEyeGameObject != null
{
m_LeftEyeGameObject.SetActive(visible);
//m_RightEyeGameObject.SetActive(visible);
}
}
void OnEnable()
{
var faceManager = FindObjectOfType<ARFaceManager>();
if (faceManager != null && faceManager.subsystem != null && faceManager.subsystem.SubsystemDescriptor.supportsEyeTracking)
{
m_FaceSubsystem = (XRFaceSubsystem)faceManager.subsystem;
SetVisible((m_Face.trackingState == TrackingState.Tracking) && (ARSession.state > ARSessionState.Ready));
m_Face.updated += OnUpdated;
}
else
{
enabled = false;
}
}
void OnDisable()
{
m_Face.updated -= OnUpdated;
SetVisible(false);
}
void OnUpdated(ARFaceUpdatedEventArgs eventArgs)
{
CreateEyeGameObjectsIfNecessary();
SetVisible((m_Face.trackingState == TrackingState.Tracking) && (ARSession.state > ARSessionState.Ready));
}
}
更新4.0.2后不能直接启动相机,需要配置以下服务:
注:本ARKit系列博客是根据 【子羽老师】发布在腾讯课堂的ARKit视觉风暴课程学习整理总结:https://ke.qq.com/course/575145
本文由 代码君 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://www.loseboy.cn/archives/09arkit15之面部增强实战
最后更新:2020-06-22 15:03:59
Update your browser to view this website correctly. Update my browser now