SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.1 KiB

4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.XR.ARSubsystems;
  4. namespace UnityEngine.XR.ARFoundation
  5. {
  6. /// <summary>
  7. /// Renders an <see cref="ARPointCloud"/> as a <c>ParticleSystem</c>.
  8. /// </summary>
  9. [RequireComponent(typeof(ARPointCloud))]
  10. [RequireComponent(typeof(ParticleSystem))]
  11. [HelpURL("https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@3.0/api/UnityEngine.XR.ARFoundation.ARPointCloudParticleVisualizer.html")]
  12. public sealed class ARPointCloudParticleVisualizer : MonoBehaviour
  13. {
  14. void OnPointCloudChanged(ARPointCloudUpdatedEventArgs eventArgs)
  15. {
  16. var points = s_Vertices;
  17. points.Clear();
  18. if (m_PointCloud.positions.HasValue)
  19. {
  20. foreach (var point in m_PointCloud.positions.Value)
  21. s_Vertices.Add(point);
  22. }
  23. int numParticles = points.Count;
  24. if (m_Particles == null || m_Particles.Length < numParticles)
  25. m_Particles = new ParticleSystem.Particle[numParticles];
  26. var color = m_ParticleSystem.main.startColor.color;
  27. var size = m_ParticleSystem.main.startSize.constant;
  28. for (int i = 0; i < numParticles; ++i)
  29. {
  30. m_Particles[i].startColor = color;
  31. m_Particles[i].startSize = size;
  32. m_Particles[i].position = points[i];
  33. m_Particles[i].remainingLifetime = 1f;
  34. }
  35. // Remove any existing particles by setting remainingLifetime
  36. // to a negative value.
  37. for (int i = numParticles; i < m_NumParticles; ++i)
  38. {
  39. m_Particles[i].remainingLifetime = -1f;
  40. }
  41. m_ParticleSystem.SetParticles(m_Particles, Math.Max(numParticles, m_NumParticles));
  42. m_NumParticles = numParticles;
  43. }
  44. void Awake()
  45. {
  46. m_PointCloud = GetComponent<ARPointCloud>();
  47. m_ParticleSystem = GetComponent<ParticleSystem>();
  48. }
  49. void OnEnable()
  50. {
  51. m_PointCloud.updated += OnPointCloudChanged;
  52. UpdateVisibility();
  53. }
  54. void OnDisable()
  55. {
  56. m_PointCloud.updated -= OnPointCloudChanged;
  57. UpdateVisibility();
  58. }
  59. void Update()
  60. {
  61. UpdateVisibility();
  62. }
  63. void UpdateVisibility()
  64. {
  65. var visible =
  66. enabled &&
  67. (m_PointCloud.trackingState != TrackingState.None);
  68. SetVisible(visible);
  69. }
  70. void SetVisible(bool visible)
  71. {
  72. if (m_ParticleSystem == null)
  73. return;
  74. var renderer = m_ParticleSystem.GetComponent<Renderer>();
  75. if (renderer != null)
  76. renderer.enabled = visible;
  77. }
  78. ARPointCloud m_PointCloud;
  79. ParticleSystem m_ParticleSystem;
  80. ParticleSystem.Particle[] m_Particles;
  81. int m_NumParticles;
  82. static List<Vector3> s_Vertices = new List<Vector3>();
  83. }
  84. }