【问题标题】:How to count AR point cloud number? (Unity AR Foundation 3.0.1)如何计算AR点云数? (Unity AR 基金会 3.0.1)
【发布时间】:2020-02-12 12:28:44
【问题描述】:

我一直在尝试计算 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;

 }
}
}

【问题讨论】:

  • arSessionOrigin 分配了吗?您确定此对象附加了ARSessionOrigin 吗?进一步的trackablesParent 分配是否正确?最后你确定ARPointCloud 附加在一个活跃且启用的孩子身上吗?您可以尝试像 arSessionOrigin.trackablesParent.GetComponentInChildren&lt;ARPointCloud&gt;(true); 一样传递 true 以在搜索中包含非活动子对象。虽然你不应该在Update 这样做.. 非常昂贵
  • 其实我也不知道。我正在研究 ARF 基本示例。所以我的程序现在做的是简单的浊点可视化和图像跟踪。我没有改变 ARF 基本示例的结构。我是 Unity 和 ARF 的新手。
  • 我太笨了! arSessionOrigin 未分配!你说的对!谢谢@derHugo

标签: c# unity3d augmented-reality


【解决方案1】:

我正在使用 unity AR Foundation 1.5.0 preview.6,他们有自己的点云计数脚本,但在他们的脚本中,我们无法编辑任何内容。所以我们需要创建一个自定义的点云计数脚本。

请检查所有图片。

First, make sure you add you add a reference to point cloud manager.

Then remove Ar point Cloud Particle Visualizer.

Attach a script and Reference.

脚本:-

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>();
    }
}

这对我有用。

【讨论】:

    猜你喜欢
    • 2022-05-26
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2021-12-15
    • 2022-04-30
    • 1970-01-01
    相关资源
    最近更新 更多