TrackablePlaneMono.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 void Init(PlaneTrackable trackable)
  21. {
  22. this.planeTrackable = trackable;
  23. }
  24. /// <summary>
  25. /// The Unity Awake() method.
  26. /// </summary>
  27. public void Awake()
  28. {
  29. mesh = GetComponent<MeshFilter>().mesh;
  30. //TODO
  31. meshRenderer = GetComponent<MeshRenderer>();
  32. }
  33. private void Update()
  34. {
  35. if (planeTrackable == null)
  36. {
  37. return;
  38. }
  39. UpdateMesh();
  40. }
  41. private void UpdateMesh()
  42. {
  43. Vector3[] vertices = planeTrackable.GetPolygonVertices();
  44. verticesCountText.text = vertices.Length.ToString();
  45. meshVertices.Clear();
  46. meshVertices.AddRange(vertices);
  47. if (SCPolygonUtils.AreVerticesEqual(meshVertices, previousMeshVertices))
  48. {
  49. return;
  50. }
  51. previousMeshVertices.Clear();
  52. previousMeshVertices.AddRange(meshVertices);
  53. planeGravity = SCPolygonUtils.GetPolygonGravityPoint(vertices);
  54. verticesCountText.transform.localPosition = planeGravity + new Vector3(0f, 0.5f, 0f);
  55. int planePolygonCount = meshVertices.Count;
  56. meshColors.Clear();
  57. for (int i = 0; i < planePolygonCount; ++i)
  58. {
  59. meshColors.Add(Color.clear);
  60. }
  61. // Feather distance 0.2 meters.
  62. const float featherLength = 0.2f;
  63. // Feather scale over the distance between plane center and vertices.
  64. const float featherScale = 0.2f;
  65. // Add vertex 4 to 7.
  66. for (int i = 0; i < planePolygonCount; ++i)
  67. {
  68. Vector3 v = meshVertices[i];
  69. // Vector from plane center to current point
  70. Vector3 d = v - planeGravity;
  71. float scale = 1.0f - Mathf.Min(featherLength / d.magnitude, featherScale);
  72. meshVertices.Add((scale * d) + planeGravity);
  73. meshColors.Add(Color.white);
  74. }
  75. meshIndices.Clear();
  76. int firstOuterVertex = 0;
  77. int firstInnerVertex = planePolygonCount;
  78. // Generate triangle (4, 5, 6) and (4, 6, 7).
  79. for (int i = 0; i < planePolygonCount - 2; ++i)
  80. {
  81. meshIndices.Add(firstInnerVertex);
  82. meshIndices.Add(firstInnerVertex + i + 1);
  83. meshIndices.Add(firstInnerVertex + i + 2);
  84. }
  85. // Generate triangle (0, 1, 4), (4, 1, 5), (5, 1, 2), (5, 2, 6), (6, 2, 3), (6, 3, 7)
  86. // (7, 3, 0), (7, 0, 4)
  87. for (int i = 0; i < planePolygonCount; ++i)
  88. {
  89. int outerVertex1 = firstOuterVertex + i;
  90. int outerVertex2 = firstOuterVertex + ((i + 1) % planePolygonCount);
  91. int innerVertex1 = firstInnerVertex + i;
  92. int innerVertex2 = firstInnerVertex + ((i + 1) % planePolygonCount);
  93. meshIndices.Add(outerVertex1);
  94. meshIndices.Add(outerVertex2);
  95. meshIndices.Add(innerVertex1);
  96. meshIndices.Add(innerVertex1);
  97. meshIndices.Add(outerVertex2);
  98. meshIndices.Add(innerVertex2);
  99. }
  100. mesh.Clear();
  101. mesh.SetVertices(meshVertices);
  102. mesh.SetTriangles(meshIndices, 0);
  103. mesh.SetColors(meshColors);
  104. UpdateMeshCollider();
  105. }
  106. private void UpdateMeshCollider()
  107. {
  108. if (collider == null)
  109. {
  110. collider = this.gameObject.AddComponent<MeshCollider>();
  111. }
  112. collider.sharedMesh = mesh;
  113. }
  114. public void OnPointerClick(PointerEventData eventData)
  115. {
  116. Vector3 instancePosition = eventData.pointerCurrentRaycast.worldPosition + Vector3.up * 0.3f;
  117. GameObject instanceObj = Instantiate(clickPrefab);
  118. instanceObj.transform.position = instancePosition;
  119. instanceObj.AddComponent<Rigidbody>();
  120. }
  121. }