C# 如何计算AR点云数?(统一AR基金3.0.1)
我一直在尝试计算ar会话中收集的所有ar点云的数量 我尝试了以下代码,但arPointCloud不断抛出错误消息:C# 如何计算AR点云数?(统一AR基金3.0.1),c#,unity3d,augmented-reality,C#,Unity3d,Augmented Reality,我一直在尝试计算ar会话中收集的所有ar点云的数量 我尝试了以下代码,但arPointCloud不断抛出错误消息: Object reference not set to an instance of an object 如果有人能帮助我,我会很高兴的 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityEngine.UI; 使用UnityEngine.XR.ARFoundation; 公共类PointNumberCount
Object reference not set to an instance of an object
如果有人能帮助我,我会很高兴的
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.XR.ARFoundation;
公共类PointNumberCount:MonoBehavior
{
ARSessionOrigin ARSessionOrigin;
ARPointCloud-ARPointCloud;
整数总数;
列表特征点=新列表();
void Start()
{
arSessionOrigin=GetComponent();
arPointCloud=arSessionOrigin.trackablesParent.getComponentChildren();
}
无效更新()
{
arPointCloud=arSessionOrigin.trackablesParent.getComponentChildren();
featurePoints=新列表(arPointCloud.positions);
totalNumber=特征点。计数;
}
}
}
<代码> > p>我使用统一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>();
}
}
使用系统;
使用System.Collections.Generic;
使用UnityEngine.XR.ARSubsystems;
使用UnityEngine.UI;
名称空间UnityEngine.XR.ARFoundation
{
///
///将对象渲染为ParticleSystem。
///
[请求组件(类型(ARPointCloud))]
[要求成分(类型(粒子系统))]
公共类ARPOINTCLOUDLOCALVIZER:单行为
{
公共文本t;
void OnPointCloudChanged(ARPointCloudUpdatedEventArgs eventArgs)
{
var points=s_顶点;
点。清除();
foreach(m_PointCloud.positions中的var点)
s_顶点。添加(点);
int numParticles=points.Count;
if(m|u Particles==null | m|u Particles.Length
这对我有用。分配了arSessionOrigin
吗?是否确定此对象已附加ARSessionOrigin
?进一步的trackablesParent
分配是否正确?最后,您确定ARPointCloud
已附加到活动且已启用的子级上吗?您可以尝试传递true
如arSessionOrigin.trackablesParent.getComponentChildren(true)
以在搜索中包括非活动子对象。但是,您不应该在更新中执行此操作。。非常昂贵实际上我不知道。我正在研究ARF基本示例。所以我的程序现在做的是简单的云点可视化和图像跟踪。我没有改变ARF基本示例的结构。我对团结和ARF还不熟悉。我太笨了<代码>arSessionOrigin
未分配!你是对的!谢谢你,德胡戈
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>();
}
}