浏览代码

Update face tracking with rear camera sample

/3.1
Tim Mowrer 5 年前
当前提交
4c278b8b
共有 6 个文件被更改,包括 272 次插入133 次删除
  1. 97
      Assets/Scenes/FaceTracking/DisplayFaceInfo.cs
  2. 211
      Assets/Scenes/FaceTracking/RearCameraWithFrontCameraFaceMesh.unity
  3. 2
      Assets/Scenes/FaceTracking/ToggleCameraFacingDirection.cs.meta
  4. 43
      Assets/Scenes/FaceTracking/ToggleCameraFacingDirection.cs
  5. 52
      Assets/Scenes/FaceTracking/ToggleManagers.cs
  6. 0
      /Assets/Scenes/FaceTracking/ToggleCameraFacingDirection.cs.meta

97
Assets/Scenes/FaceTracking/DisplayFaceInfo.cs


using System.Collections;
using System.Collections.Generic;
using System.Text;
using Unity.Collections;
using UnityEngine.XR.ARSubsystems;
[RequireComponent(typeof(ARSession))]
[RequireComponent(typeof(ARSessionOrigin))]
public class DisplayFaceInfo : MonoBehaviour
{
[SerializeField]

{
get { return m_FaceInfoText; }
set { m_FaceInfoText = value; }
get => m_FaceInfoText;
set => m_FaceInfoText = value;
}
[SerializeField]
Text m_InstructionsText;
public Text instructionsText
{
get => m_InstructionsText;
set => m_InstructionsText = value;
ARSession m_Session;
ARCameraManager m_CameraManager;
StringBuilder m_Info = new StringBuilder();
bool m_FaceTrackingSupported;
bool m_FaceTrackingWithWorldCameraSupported;
m_Session = GetComponent<ARSession>();
m_CameraManager = GetComponent<ARSessionOrigin>().camera?.GetComponent<ARCameraManager>();
}
void OnEnable()
{
// Detect face tracking with world-facing camera support
var subsystem = m_Session?.subsystem;
if (subsystem != null)
{
var configs = subsystem.GetConfigurationDescriptors(Allocator.Temp);
if (configs.IsCreated)
{
using (configs)
{
foreach (var config in configs)
{
if (config.capabilities.All(Feature.FaceDetection))
{
m_FaceTrackingSupported = true;
}
if (config.capabilities.All(Feature.WorldFacingCamera | Feature.FaceDetection))
{
m_FaceTrackingWithWorldCameraSupported = true;
}
}
}
}
}
if (m_FaceManager.subsystem != null && faceInfoText != null)
m_Info.Clear();
if (m_FaceManager.subsystem != null)
faceInfoText.text = $"Supported number of tracked faces: {m_FaceManager.supportedFaceCount}\n" +
$"Max number of faces to track: {m_FaceManager.currentMaximumFaceCount}\n" +
$"Number of tracked faces: {m_FaceManager.trackables.count}";
m_Info.Append($"Supported number of tracked faces: {m_FaceManager.supportedFaceCount}\n");
m_Info.Append($"Max number of faces to track: {m_FaceManager.currentMaximumFaceCount}\n");
m_Info.Append($"Number of tracked faces: {m_FaceManager.trackables.count}\n");
}
if (m_CameraManager)
{
m_Info.Append($"Requested camera facing direction: {m_CameraManager.requestedFacingDirection}\n");
m_Info.Append($"Current camera facing direction: {m_CameraManager.currentFacingDirection}\n");
}
if (!m_FaceTrackingSupported)
{
if (m_InstructionsText)
{
m_InstructionsText.text = "Face tracking is not supported.";
}
else
{
m_Info.Append("Face tracking is not supported.");
}
}
else if (m_CameraManager.requestedFacingDirection == CameraFacingDirection.World && !m_FaceTrackingWithWorldCameraSupported)
{
m_Info.Append("Face tracking in world facing camera mode is not supported.");
}
if (m_FaceInfoText)
{
m_FaceInfoText.text = m_Info.ToString();
}
}
}

211
Assets/Scenes/FaceTracking/RearCameraWithFrontCameraFaceMesh.unity


m_Children:
- {fileID: 249888566}
- {fileID: 444253638}
- {fileID: 425493004}
m_RootOrder: 3
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &208234939
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 208234941}
- component: {fileID: 208234940}
- component: {fileID: 208234943}
m_Layer: 0
m_Name: AR Session
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &208234940
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 208234939}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3859a92a05d4f5d418cb6ca605290e74, type: 3}
m_Name:
m_EditorClassIdentifier:
m_AttemptUpdate: 1
m_MatchFrameRate: 1
--- !u!4 &208234941
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 208234939}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_RootOrder: 3
--- !u!114 &208234943
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 208234939}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fa850fbd5b8aded44846f96e35f1a9f5, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &249888565
GameObject:
m_ObjectHideFlags: 0

m_Script: {fileID: 11500000, guid: 4966719baa26e4b0e8231a24d9bd491a, type: 3}
m_Name:
m_EditorClassIdentifier:
m_FocusMode: 1
m_LightEstimationMode: 0
m_FocusMode: -1
m_LightEstimationMode: -1
m_AutoFocus: 1
m_LightEstimation: 0
m_FacingDirection: 1
--- !u!1 &406902928
GameObject:
m_ObjectHideFlags: 0

serializedVersion: 6
m_Component:
- component: {fileID: 406902931}
- component: {fileID: 406902934}
- component: {fileID: 406902934}
m_Layer: 0
m_Name: AR Session Origin
m_TagString: Untagged

m_Children:
- {fileID: 282840814}
m_Father: {fileID: 0}
m_RootOrder: 2
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &406902932
MonoBehaviour:

m_Name:
m_EditorClassIdentifier:
m_FaceInfoText: {fileID: 249888567}
m_InstructionsText: {fileID: 425493005}
--- !u!114 &406902933
MonoBehaviour:
m_ObjectHideFlags: 0

m_GameObject: {fileID: 406902928}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e1760703bbd54c04488a8d10600262ab, type: 3}
m_Script: {fileID: 11500000, guid: fa850fbd5b8aded44846f96e35f1a9f5, type: 3}
m_PlanePrefab: {fileID: 1568414836858200, guid: f0e0be81472ff1243aa2afa27e5d0e61,
type: 3}
m_DetectionMode: -1
--- !u!114 &406902934
MonoBehaviour:
m_ObjectHideFlags: 0

m_GameObject: {fileID: 406902928}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 79c03e9e9918e48e08e80457bbf530ac, type: 3}
m_Script: {fileID: 11500000, guid: 3859a92a05d4f5d418cb6ca605290e74, type: 3}
m_Name:
m_EditorClassIdentifier:
m_AttemptUpdate: 1
m_MatchFrameRate: 1
m_TrackingMode: 2
--- !u!1 &425493003
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 425493004}
- component: {fileID: 425493006}
- component: {fileID: 425493005}
m_Layer: 5
m_Name: Instructions
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &425493004
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 425493003}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 10187378}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0}
m_AnchorMax: {x: 0.5, y: 0}
m_AnchoredPosition: {x: 0, y: 128}
m_SizeDelta: {x: 1024, y: 128}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &425493005
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 425493003}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_PlaneManager: 1
m_FaceManager: 0
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 50
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 5
m_MaxSize: 50
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Tap screen to switch cameras
--- !u!222 &425493006
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 425493003}
m_CullTransparentMesh: 0
--- !u!1 &444253637
GameObject:
m_ObjectHideFlags: 0

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1526980365
GameObject:

m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1794229919
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1794229921}
- component: {fileID: 1794229920}
m_Layer: 0
m_Name: CameraSwitcher
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1794229920
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1794229919}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 85645ea6d9a984404b0688b24fb2a0a7, type: 3}
m_Name:
m_EditorClassIdentifier:
m_CameraManager: {fileID: 282840816}
m_Session: {fileID: 0}
--- !u!4 &1794229921
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1794229919}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

2
Assets/Scenes/FaceTracking/ToggleCameraFacingDirection.cs.meta


fileFormatVersion: 2
guid: 79c03e9e9918e48e08e80457bbf530ac
guid: 85645ea6d9a984404b0688b24fb2a0a7
MonoImporter:
externalObjects: {}
serializedVersion: 2

43
Assets/Scenes/FaceTracking/ToggleCameraFacingDirection.cs


using UnityEngine;
using UnityEngine.XR.ARFoundation;
public class ToggleCameraFacingDirection : MonoBehaviour
{
[SerializeField]
ARCameraManager m_CameraManager;
public ARCameraManager cameraManager
{
get => m_CameraManager;
set => m_CameraManager = value;
}
[SerializeField]
ARSession m_Session;
public ARSession session
{
get => m_Session;
set => m_Session = value;
}
void Update()
{
if (m_CameraManager == null || m_Session == null)
return;
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
if (m_CameraManager.currentFacingDirection == CameraFacingDirection.User)
{
m_CameraManager.requestedFacingDirection = CameraFacingDirection.World;
m_Session.requestedTrackingMode = TrackingMode.PositionAndRotation;
}
else
{
m_CameraManager.requestedFacingDirection = CameraFacingDirection.User;
m_Session.requestedTrackingMode = TrackingMode.RotationOnly;
}
}
}
}

52
Assets/Scenes/FaceTracking/ToggleManagers.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
public class ToggleManagers : MonoBehaviour
{
[SerializeField]
public bool m_PlaneManager;
public bool planeManager
{
get { return m_PlaneManager; }
set { m_PlaneManager = value; }
}
[SerializeField]
public bool m_FaceManager;
public bool faceManager
{
get { return m_FaceManager; }
set { m_FaceManager = value; }
}
void ToggleComponent<T>() where T : MonoBehaviour
{
var behaviour = GetComponent<T>();
if (behaviour == null)
return;
behaviour.enabled = !behaviour.enabled;
var enabledText = behaviour.enabled ? "enabled" : "disabled";
Logger.Log($"{typeof(T).Name} {enabledText}");
}
void Update()
{
if (Input.touchCount < 1)
return;
var touch = Input.touches[0];
if (touch.phase == TouchPhase.Began)
{
if (planeManager)
ToggleComponent<ARPlaneManager>();
if (faceManager)
ToggleComponent<ARFaceManager>();
}
}
}

/Assets/Scenes/FaceTracking/ToggleManagers.cs.meta → /Assets/Scenes/FaceTracking/ToggleCameraFacingDirection.cs.meta

正在加载...
取消
保存