ARFaceMeshVisualizer.cs
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// Generates a mesh for an <see cref="ARFace"/>.
/// </summary>
/// <remarks>
/// If this <c>GameObject</c> has a <c>MeshFilter</c> and/or <c>MeshCollider</c>,
/// this component will generate a mesh from the underlying <c>XRFace</c>.
/// </remarks>
[RequireComponent(typeof(ARFace))]
[HelpURL(HelpUrls.ApiWithNamespace + nameof(ARFaceMeshVisualizer) + ".html")]
public sealed class ARFaceMeshVisualizer : MonoBehaviour
{
/// <summary>
/// Get the <c>Mesh</c> that this visualizer creates and manages.
/// </summary>
public Mesh mesh { get; private set; }
void SetVisible(bool visible)
{
m_MeshRenderer = GetComponent<MeshRenderer>();
if (m_MeshRenderer == null)
{
return;
}
//if it is getting visible after being invisible for a while, set its topology
if (visible && !m_MeshRenderer.enabled)
{
SetMeshTopology();
}
m_MeshRenderer.enabled = visible;
}
void SetMeshTopology()
{
if (mesh == null)
{
return;
}
using (new ScopedProfiler("SetMeshTopology"))
{
using (new ScopedProfiler("ClearMesh"))
mesh.Clear();
if (m_Face.vertices.Length > 0 && m_Face.indices.Length > 0)
{
using (new ScopedProfiler("SetVertices"))
mesh.SetVertices(m_Face.vertices);
using (new ScopedProfiler("SetIndices"))
mesh.SetIndices(m_Face.indices, MeshTopology.Triangles, 0, false);
using (new ScopedProfiler("RecalculateBounds"))
mesh.RecalculateBounds();
if (m_Face.normals.Length == m_Face.vertices.Length)
{
using (new ScopedProfiler("SetNormals"))
mesh.SetNormals(m_Face.normals);
}
else
{
using (new ScopedProfiler("RecalculateNormals"))
mesh.RecalculateNormals();
}
}
if (m_Face.uvs.Length > 0)
{
using (new ScopedProfiler("SetUVs"))
mesh.SetUVs(0, m_Face.uvs);
}
var meshFilter = GetComponent<MeshFilter>();
if (meshFilter != null)
{
meshFilter.sharedMesh = mesh;
}
var meshCollider = GetComponent<MeshCollider>();
if (meshCollider != null)
{
meshCollider.sharedMesh = mesh;
}
m_TopologyUpdatedThisFrame = true;
}
}
void UpdateVisibility()
{
var visible = enabled &&
(m_Face.trackingState != TrackingState.None) &&
(ARSession.state > ARSessionState.Ready);
SetVisible(visible);
}
void OnUpdated(ARFaceUpdatedEventArgs eventArgs)
{
UpdateVisibility();
if (!m_TopologyUpdatedThisFrame)
{
SetMeshTopology();
}
m_TopologyUpdatedThisFrame = false;
}
void OnSessionStateChanged(ARSessionStateChangedEventArgs eventArgs)
{
UpdateVisibility();
}
void Awake()
{
mesh = new Mesh();
m_MeshRenderer = GetComponent<MeshRenderer>();
m_Face = GetComponent<ARFace>();
}
void OnEnable()
{
m_Face.updated += OnUpdated;
ARSession.stateChanged += OnSessionStateChanged;
UpdateVisibility();
}
void OnDisable()
{
m_Face.updated -= OnUpdated;
ARSession.stateChanged -= OnSessionStateChanged;
}
ARFace m_Face;
MeshRenderer m_MeshRenderer;
bool m_TopologyUpdatedThisFrame;
}
}