我一直在努力计算ar会话中收集到的所有AR点云的数量。
我尝试了以下代码,但是arPointCloud一直在抛出错误消息:
Object reference not set to an instance of an object如果有人能帮我我会很高兴的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
public class PointNumberCount : MonoBehaviour
{
ARSessionOrigin arSessionOrigin;
ARPointCloud arPointCloud;
int totalNumber;
List<Vector3> featurePoints = new List<Vector3>();
void Start()
{
arSessionOrigin = GetComponent<ARSessionOrigin>();
arPointCloud = arSessionOrigin.trackablesParent.GetComponentInChildren<ARPointCloud>();
}
void Update()
{
arPointCloud = arSessionOrigin.trackablesParent.GetComponentInChildren<ARPointCloud>();
featurePoints = new List<Vector3>(arPointCloud.positions);
totalNumber = featurePoints.Count;
}
}
}发布于 2020-02-18 06:33:50
我使用团结AR基础1.5.0预览6,他们有自己的脚本计数点云,但在他们的脚本,我们不能编辑任何东西。因此,我们需要创建一个计算点云的自定义脚本。
请检查所有的图像。
剧本:-
using System;
using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.UI;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// Renders an <see cref="ARPointCloud"/> as a <c>ParticleSystem</c>.
/// </summary>
[RequireComponent(typeof(ARPointCloud))]
[RequireComponent(typeof(ParticleSystem))]
public class ARPointCloudParticleVisualizer : MonoBehaviour
{
public Text t;
void OnPointCloudChanged(ARPointCloudUpdatedEventArgs eventArgs)
{
var points = s_Vertices;
points.Clear();
foreach (var point in m_PointCloud.positions)
s_Vertices.Add(point);
int numParticles = points.Count;
if (m_Particles == null || m_Particles.Length < numParticles)
m_Particles = new ParticleSystem.Particle[numParticles];
var color = m_ParticleSystem.main.startColor.color;
var size = m_ParticleSystem.main.startSize.constant;
for (int i = 0; i < numParticles; ++i)
{
m_Particles[i].startColor = color;
m_Particles[i].startSize = size;
m_Particles[i].position = points[i];
m_Particles[i].remainingLifetime = 1f;
}
// Remove any existing particles by setting remainingLifetime
// to a negative value.
for (int i = numParticles; i < m_NumParticles; ++i)
{
m_Particles[i].remainingLifetime = -1f;
}
m_ParticleSystem.SetParticles(m_Particles, Math.Max(numParticles, m_NumParticles));
m_NumParticles = numParticles;
}
void Awake()
{
m_PointCloud = GetComponent<ARPointCloud>();
m_ParticleSystem = GetComponent<ParticleSystem>();
}
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)
{
if (m_ParticleSystem == null)
return;
var renderer = m_ParticleSystem.GetComponent<Renderer>();
t.text = m_NumParticles.ToString();
if (renderer != null)
renderer.enabled = visible;
}
ARPointCloud m_PointCloud;
ParticleSystem m_ParticleSystem;
ParticleSystem.Particle[] m_Particles;
int m_NumParticles;
static List<Vector3> s_Vertices = new List<Vector3>();
}
}这是对我有用的。
https://stackoverflow.com/questions/60188351
复制相似问题