using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARFoundation
{
///
/// Renders an as a Mesh with MeshTopology.Points.
///
///
/// Related information: AR Point Cloud Manager component
///
[RequireComponent(typeof(ARPointCloud))]
[HelpURL(typeof(ARPointCloudMeshVisualizer))]
public sealed class ARPointCloudMeshVisualizer : MonoBehaviour
{
///
/// Get the Mesh that this visualizer creates and manages.
///
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();
if (meshFilter != null)
meshFilter.sharedMesh = mesh;
}
void Awake()
{
mesh = new Mesh();
m_PointCloud = GetComponent();
}
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();
if (meshRenderer != null)
meshRenderer.enabled = visible;
}
ARPointCloud m_PointCloud;
static List s_Vertices = new ();
}
}