TrackablePlaneMono.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class TrackablePlaneMono : MonoBehaviour, IPointerClickHandler
  8. {
  9. private PlaneTrackable planeTrackable;
  10. private List<Vector3> meshVertices = new List<Vector3>();
  11. private List<Vector3> previousMeshVertices = new List<Vector3>();
  12. private List<int> meshIndices = new List<int>();
  13. private List<Color> meshColors = new List<Color>();
  14. private Mesh mesh;
  15. private MeshRenderer meshRenderer;
  16. public TextMeshPro verticesCountText;
  17. public GameObject clickPrefab;
  18. private MeshCollider collider;
  19. private Vector3 planeGravity = Vector3.zero;
  20. public GameObject vertexTextPrefab;
  21. private List<GameObject> vertexTextList = new List<GameObject>();
  22. public void Init(PlaneTrackable trackable)
  23. {
  24. this.planeTrackable = trackable;
  25. }
  26. /// <summary>
  27. /// The Unity Awake() method.
  28. /// </summary>
  29. public void Awake()
  30. {
  31. mesh = GetComponent<MeshFilter>().mesh;
  32. //TODO
  33. meshRenderer = GetComponent<MeshRenderer>();
  34. }
  35. private void Update()
  36. {
  37. if (planeTrackable == null)
  38. {
  39. return;
  40. }
  41. UpdateMesh();
  42. }
  43. public void CreateOrHideVerticesText(List<Vector3> vertices)
  44. {
  45. while (vertexTextList.Count < vertices.Count)
  46. {
  47. GameObject vertexText = GameObject.Instantiate(vertexTextPrefab, this.transform);
  48. vertexTextList.Add(vertexText);
  49. }
  50. for (int i = 0; i < vertexTextList.Count; i++)
  51. {
  52. vertexTextList[i].SetActive(true);
  53. }
  54. if (vertexTextList.Count > vertices.Count)
  55. {
  56. for (int i = vertices.Count; i < vertexTextList.Count; i++)
  57. {
  58. vertexTextList[i].SetActive(false);
  59. }
  60. }
  61. for (int i = 0; i < vertices.Count; i++)
  62. {
  63. vertexTextList[i].transform.position = vertices[i];
  64. //vertexTextList[i].GetComponent<TextMeshPro>().text = string.Format("({0},{1},{2})", vertices[i].x, vertices[i].y, vertices[i].z);
  65. }
  66. }
  67. private void UpdateMesh()
  68. {
  69. Vector3[] vertices = planeTrackable.GetPolygonVertices();
  70. verticesCountText.text = vertices.Length.ToString();
  71. meshVertices.Clear();
  72. meshVertices.AddRange(vertices);
  73. if (SCPolygonUtils.AreVerticesEqual(meshVertices, previousMeshVertices))
  74. {
  75. return;
  76. }
  77. previousMeshVertices.Clear();
  78. previousMeshVertices.AddRange(meshVertices);
  79. CreateOrHideVerticesText(meshVertices);
  80. planeGravity = SCPolygonUtils.GetPolygonGravityPoint(vertices);
  81. verticesCountText.transform.localPosition = planeGravity + new Vector3(0f, 0.5f, 0f);
  82. int planePolygonCount = meshVertices.Count;
  83. meshColors.Clear();
  84. for (int i = 0; i < planePolygonCount; ++i)
  85. {
  86. meshColors.Add(Color.clear);
  87. }
  88. // Feather distance 0.2 meters.
  89. const float featherLength = 0.2f;
  90. // Feather scale over the distance between plane center and vertices.
  91. const float featherScale = 0.2f;
  92. // Add vertex 4 to 7.
  93. for (int i = 0; i < planePolygonCount; ++i)
  94. {
  95. Vector3 v = meshVertices[i];
  96. // Vector from plane center to current point
  97. Vector3 d = v - planeGravity;
  98. float scale = 1.0f - Mathf.Min(featherLength / d.magnitude, featherScale);
  99. meshVertices.Add((scale * d) + planeGravity);
  100. meshColors.Add(Color.white);
  101. }
  102. meshIndices.Clear();
  103. int firstOuterVertex = 0;
  104. int firstInnerVertex = planePolygonCount;
  105. // Generate triangle (4, 5, 6) and (4, 6, 7).
  106. for (int i = 0; i < planePolygonCount - 2; ++i)
  107. {
  108. meshIndices.Add(firstInnerVertex);
  109. meshIndices.Add(firstInnerVertex + i + 1);
  110. meshIndices.Add(firstInnerVertex + i + 2);
  111. }
  112. // Generate triangle (0, 1, 4), (4, 1, 5), (5, 1, 2), (5, 2, 6), (6, 2, 3), (6, 3, 7)
  113. // (7, 3, 0), (7, 0, 4)
  114. for (int i = 0; i < planePolygonCount; ++i)
  115. {
  116. int outerVertex1 = firstOuterVertex + i;
  117. int outerVertex2 = firstOuterVertex + ((i + 1) % planePolygonCount);
  118. int innerVertex1 = firstInnerVertex + i;
  119. int innerVertex2 = firstInnerVertex + ((i + 1) % planePolygonCount);
  120. meshIndices.Add(outerVertex1);
  121. meshIndices.Add(outerVertex2);
  122. meshIndices.Add(innerVertex1);
  123. meshIndices.Add(innerVertex1);
  124. meshIndices.Add(outerVertex2);
  125. meshIndices.Add(innerVertex2);
  126. }
  127. mesh.Clear();
  128. mesh.SetVertices(meshVertices);
  129. mesh.SetTriangles(meshIndices, 0);
  130. mesh.SetColors(meshColors);
  131. UpdateMeshCollider();
  132. }
  133. private void UpdateMeshCollider()
  134. {
  135. if (collider == null)
  136. {
  137. collider = this.gameObject.AddComponent<MeshCollider>();
  138. }
  139. collider.sharedMesh = mesh;
  140. }
  141. public void OnPointerClick(PointerEventData eventData)
  142. {
  143. Vector3 instancePosition = eventData.pointerCurrentRaycast.worldPosition + Vector3.up * 0.3f;
  144. GameObject instanceObj = Instantiate(clickPrefab);
  145. instanceObj.transform.position = instancePosition;
  146. instanceObj.AddComponent<Rigidbody>();
  147. }
  148. public int GetPlaneId()
  149. {
  150. return planeTrackable.trackableId;
  151. }
  152. }