PlaneDetector.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. using System.Collections.Generic;
  10. using UnityEngine;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> A guide to show how to use plane detect. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/discover/introduction-nrsdk")]
  15. public class PlaneDetector : MonoBehaviour
  16. {
  17. /// <summary> Detected plane prefab. </summary>
  18. public GameObject DetectedPlanePrefab;
  19. /// <summary>
  20. /// A list to hold new planes NRSDK began tracking in the current frame. This object is used
  21. /// across the application to avoid per-frame allocations. </summary>
  22. private List<NRTrackablePlane> m_NewPlanes = new List<NRTrackablePlane>();
  23. /// <summary> Updates this object. </summary>
  24. public void Update()
  25. {
  26. NRFrame.GetTrackables<NRTrackablePlane>(m_NewPlanes, NRTrackableQueryFilter.New);
  27. for (int i = 0; i < m_NewPlanes.Count; i++)
  28. {
  29. // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
  30. // the origin with an identity rotation since the mesh for our prefab is updated in Unity World coordinates.
  31. GameObject planeObject = Instantiate(DetectedPlanePrefab, Vector3.zero, Quaternion.identity, transform);
  32. planeObject.GetComponent<NRTrackableBehaviour>().Initialize(m_NewPlanes[i]);
  33. }
  34. }
  35. }
  36. }