ARPointCloudVisualizer.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. namespace UnityARInterface
  3. {
  4. public class ARPointCloudVisualizer : ARBase
  5. {
  6. [SerializeField]
  7. private ParticleSystem m_PointCloudParticlePrefab;
  8. [SerializeField]
  9. private int m_MaxPointsToShow = 300;
  10. [SerializeField]
  11. private float m_ParticleSize = 1.0f;
  12. private ParticleSystem m_ParticleSystem;
  13. private ParticleSystem.Particle [] m_Particles;
  14. private ParticleSystem.Particle[] m_NoParticles;
  15. private ARInterface.PointCloud m_PointCloud;
  16. private void OnDisable()
  17. {
  18. m_ParticleSystem.SetParticles(m_NoParticles, 1);
  19. }
  20. // Use this for initialization
  21. void Start()
  22. {
  23. m_ParticleSystem = Instantiate(m_PointCloudParticlePrefab, GetRoot());
  24. m_NoParticles = new ParticleSystem.Particle[1];
  25. m_NoParticles[0].startSize = 0f;
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. if (ARInterface.GetInterface().TryGetPointCloud(ref m_PointCloud))
  31. {
  32. var scale = GetScale();
  33. var numParticles = Mathf.Min(m_PointCloud.points.Count, m_MaxPointsToShow);
  34. if (m_Particles == null || m_Particles.Length != numParticles)
  35. m_Particles = new ParticleSystem.Particle[numParticles];
  36. for (int i = 0; i < numParticles; ++i)
  37. {
  38. m_Particles[i].position = m_PointCloud.points[i] * scale;
  39. m_Particles[i].startColor = new Color(1.0f, 1.0f, 1.0f);
  40. m_Particles[i].startSize = m_ParticleSize * scale;
  41. }
  42. m_ParticleSystem.SetParticles(m_Particles, numParticles);
  43. }
  44. else
  45. {
  46. m_ParticleSystem.SetParticles(m_NoParticles, 1);
  47. }
  48. }
  49. }
  50. }