09_ARKit1.5之面部增强实战

新建项目并配置环境

1、新建项目FaceTracking
2、安装ARFoundation及FaceTracking的相关插件

配置当前场景

在当前场景中新建 AR Session和 AR Session Orgin,将 AR Session Orgin 设置为 Main Camera后删除当前场景中的原有 Main Camera。
配置当前场景

准备Mask模型

可以在Assert Store里搜索并下载喜欢的mask模型
搜索并下载模型

导入模型

加入模型

添加ARFaceManager脚本,并将下载的模型添加到脚本中
加入模型

注:经测试,如果直接添加模型,将模型y轴旋转180度,在显示时,面具方向会反向,所以需要包裹在GameObject对象中。

效果展示

未包裹在GameObject对象中:

未包裹在GameObject对象

包裹在GameObject对象中:

包裹在GameObject对象中

以上图片均进行处理,只为显示面部增强和表情追踪效果。

眼睛追踪

需要单独加入眼睛追踪的脚本
image.png!

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));
    }
}

更新 ARFoundation 4.0.2

更新4.0.2后不能直接启动相机,需要配置以下服务:

更新 ARFoundation 4.0.2
更新 ARFoundation 4.0.2
更新 ARFoundation 4.0.2 文档

注:本ARKit系列博客是根据 【子羽老师】发布在腾讯课堂的ARKit视觉风暴课程学习整理总结:https://ke.qq.com/course/575145

# AR  ARKit 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×