Как посчитать номер облака точек AR? (Unity AR Foundation 3.0.1) - PullRequest
0 голосов
/ 12 февраля 2020

Я пытался подсчитать количество всех облаков точек 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;

 }
}
}

1 Ответ

1 голос
/ 18 февраля 2020

Я использую unity AR foundation 1.5.0 preview.6, и у них есть свой собственный скрипт для подсчета облака точек, но в их скрипте мы ничего не можем редактировать. Поэтому нам нужно создать собственный скрипт для подсчета облака точек.

Пожалуйста, проверьте все изображения.

Сначала убедитесь, что вы добавили ссылку на менеджер облака точек.

Затем удалите визуализатор частиц облака облака Ar.

Прикрепите скрипт и справку.

Скрипт: -

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

Это работает для меня.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...