DetectedPlaneGenerator.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.NRExamples
  10. {
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. /// <summary> Manages the visualization of detected planes in the scene. </summary>
  14. public class DetectedPlaneGenerator : MonoBehaviour
  15. {
  16. /// <summary> A prefab for tracking and visualizing detected planes. </summary>
  17. public GameObject DetectedPlanePrefab;
  18. /// <summary>
  19. /// A list to hold new planes NRSDK began tracking in the current frame. This object is used
  20. /// across the application to avoid per-frame allocations. </summary>
  21. private List<NRTrackablePlane> m_NewPlanes = new List<NRTrackablePlane>();
  22. /// <summary> The Unity Update method. </summary>
  23. public void Update()
  24. {
  25. // Check that motion tracking is tracking.
  26. if (NRFrame.SessionStatus != SessionState.Running)
  27. {
  28. return;
  29. }
  30. // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
  31. NRFrame.GetTrackables<NRTrackablePlane>(m_NewPlanes, NRTrackableQueryFilter.New);
  32. for (int i = 0; i < m_NewPlanes.Count; i++)
  33. {
  34. // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
  35. // the origin with an identity rotation since the mesh for our prefab is updated in Unity World
  36. // coordinates.
  37. GameObject planeObject = Instantiate(DetectedPlanePrefab, Vector3.zero, Quaternion.identity, transform);
  38. planeObject.GetComponent<DetectedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
  39. }
  40. }
  41. }
  42. }