ARPointCloudMeshVisualizer.cs
2.3 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
using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// Renders an <see cref="ARPointCloud"/> as a <c>Mesh</c> with <c>MeshTopology.Points</c>.
/// </summary>
[RequireComponent(typeof(ARPointCloud))]
[HelpURL(HelpUrls.ApiWithNamespace + nameof(ARPointCloudMeshVisualizer) + ".html")]
public sealed class ARPointCloudMeshVisualizer : MonoBehaviour
{
/// <summary>
/// Get the <c>Mesh</c> that this visualizer creates and manages.
/// </summary>
public Mesh mesh { get; private set; }
void OnPointCloudChanged(ARPointCloudUpdatedEventArgs eventArgs)
{
s_Vertices.Clear();
if (m_PointCloud.positions.HasValue)
{
foreach (var point in m_PointCloud.positions)
s_Vertices.Add(point);
}
mesh.Clear();
mesh.SetVertices(s_Vertices);
var indices = new int[s_Vertices.Count];
for (int i = 0; i < s_Vertices.Count; ++i)
{
indices[i] = i;
}
mesh.SetIndices(indices, MeshTopology.Points, 0);
var meshFilter = GetComponent<MeshFilter>();
if (meshFilter != null)
meshFilter.sharedMesh = mesh;
}
void Awake()
{
mesh = new Mesh();
m_PointCloud = GetComponent<ARPointCloud>();
}
void OnEnable()
{
m_PointCloud.updated += OnPointCloudChanged;
UpdateVisibility();
}
void OnDisable()
{
m_PointCloud.updated -= OnPointCloudChanged;
UpdateVisibility();
}
void Update()
{
UpdateVisibility();
}
void UpdateVisibility()
{
var visible =
enabled &&
(m_PointCloud.trackingState != TrackingState.None);
SetVisible(visible);
}
void SetVisible(bool visible)
{
var meshRenderer = GetComponent<MeshRenderer>();
if (meshRenderer != null)
meshRenderer.enabled = visible;
}
ARPointCloud m_PointCloud;
static List<Vector3> s_Vertices = new List<Vector3>();
}
}