PolygonPlaneVisualizer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 NRKernal;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using UnityEngine;
  15. /// <summary> A polygon plane visualizer. </summary>
  16. public class PolygonPlaneVisualizer : NRTrackableBehaviour
  17. {
  18. /// <summary> The material. </summary>
  19. public Material Material;
  20. /// <summary> The renderer. </summary>
  21. private MeshRenderer m_Renderer;
  22. /// <summary> Specifies the filter. </summary>
  23. private MeshFilter m_Filter;
  24. /// <summary> The collider. </summary>
  25. private MeshCollider m_Collider;
  26. /// <summary> List of positions. </summary>
  27. List<Vector3> m_PosList = new List<Vector3>();
  28. /// <summary> The plane mesh. </summary>
  29. Mesh m_PlaneMesh = null;
  30. #if UNITY_EDITOR
  31. /// <summary> The vectors data editor. </summary>
  32. [SerializeField]
  33. private Vector3[] VectorsDataEditor = new Vector3[4];
  34. /// <summary> Gets boundary polygon. </summary>
  35. /// <param name="tran"> The tran.</param>
  36. /// <param name="polygonList"> List of polygons.</param>
  37. public void GetBoundaryPolygon(Transform tran, List<Vector3> polygonList)
  38. {
  39. polygonList.Clear();
  40. var unityWorldTPlane = Matrix4x4.TRS(tran.position, tran.rotation, Vector3.one);
  41. for (int i = 0; i < VectorsDataEditor.Length; i++)
  42. {
  43. polygonList.Add(unityWorldTPlane.MultiplyPoint3x4(VectorsDataEditor[i]));
  44. }
  45. }
  46. #endif
  47. void Start()
  48. {
  49. StartCoroutine(UpdateMesh());
  50. }
  51. /// <summary> Updates this object. </summary>
  52. private IEnumerator UpdateMesh()
  53. {
  54. WaitForSeconds wait = new WaitForSeconds(1f / 5);
  55. while (true)
  56. {
  57. yield return wait;
  58. #if UNITY_EDITOR
  59. var center = new Pose(transform.position, transform.rotation);
  60. GetBoundaryPolygon(transform, m_PosList);
  61. #else
  62. var center = Trackable.GetCenterPose();
  63. ((NRTrackablePlane)Trackable).GetBoundaryPolygon(m_PosList);
  64. #endif
  65. this.DrawFromCenter(center, m_PosList);
  66. #if !UNITY_EDITOR
  67. if (Trackable.GetTrackingState() == TrackingState.Stopped)
  68. {
  69. Destroy(gameObject);
  70. }
  71. #endif
  72. }
  73. }
  74. /// <summary> Draw from center. </summary>
  75. /// <param name="centerPose"> The center pose.</param>
  76. /// <param name="vectors"> The vectors.</param>
  77. private void DrawFromCenter(Pose centerPose, List<Vector3> vectors)
  78. {
  79. if (vectors == null || vectors.Count < 3)
  80. {
  81. return;
  82. }
  83. var center = centerPose.position;
  84. Vector3[] vertices3D = new Vector3[vectors.Count + 1];
  85. vertices3D[0] = transform.InverseTransformPoint(center);
  86. for (int i = 1; i < vectors.Count + 1; i++)
  87. {
  88. vertices3D[i] = transform.InverseTransformPoint(vectors[i - 1]);
  89. }
  90. int[] triangles = GenerateTriangles(vectors);
  91. if (m_PlaneMesh == null)
  92. {
  93. m_PlaneMesh = new Mesh();
  94. }
  95. m_PlaneMesh.vertices = vertices3D;
  96. m_PlaneMesh.triangles = triangles;
  97. if (m_Renderer == null)
  98. {
  99. m_Renderer = gameObject.GetComponent<MeshRenderer>();
  100. m_Renderer.material = Material;
  101. var planeInVector_1 = vertices3D[0] - vertices3D[1];
  102. var planeInVector_2 = vertices3D[1] - vertices3D[2];
  103. m_Renderer.material.SetVector("_PlaneIn", planeInVector_1);
  104. m_Renderer.material.SetVector("_PlaneNormal", Vector3.Cross(planeInVector_1, planeInVector_2));
  105. }
  106. if (m_Filter == null)
  107. {
  108. m_Filter = gameObject.GetComponent<MeshFilter>();
  109. }
  110. m_Filter.mesh = m_PlaneMesh;
  111. if (m_Collider == null)
  112. {
  113. m_Collider = gameObject.GetComponent<MeshCollider>();
  114. }
  115. m_Collider.sharedMesh = m_PlaneMesh;
  116. }
  117. public static int[] GenerateTriangles(List<Vector3> posList)
  118. {
  119. List<int> indices = new List<int>();
  120. for (int i = 0; i < posList.Count; i++)
  121. {
  122. if (i != posList.Count - 1)
  123. {
  124. indices.Add(0);
  125. indices.Add(i + 1);
  126. indices.Add(i + 2);
  127. }
  128. else
  129. {
  130. indices.Add(0);
  131. indices.Add(i + 1);
  132. indices.Add(1);
  133. }
  134. }
  135. return indices.ToArray();
  136. }
  137. }
  138. }