首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算AR点云数?(团结AR基金会3.0.1)

如何计算AR点云数?(团结AR基金会3.0.1)
EN

Stack Overflow用户
提问于 2020-02-12 12:28:44
回答 1查看 933关注 0票数 0

我一直在努力计算ar会话中收集到的所有AR点云的数量。

我尝试了以下代码,但是arPointCloud一直在抛出错误消息:

代码语言:javascript
复制
Object reference not set to an instance of an object

如果有人能帮我我会很高兴的。

代码语言:javascript
复制
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;

 }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-18 06:33:50

我使用团结AR基础1.5.0预览6,他们有自己的脚本计数点云,但在他们的脚本,我们不能编辑任何东西。因此,我们需要创建一个计算点云的自定义脚本。

请检查所有的图像。

首先,确保添加了对点云管理器的引用。

然后删除Ar点云粒子视觉器。

附加一个脚本和引用。

剧本:-

代码语言:javascript
复制
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>();
    }
}

这是对我有用的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60188351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档