DrawBounds.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using EZXR.Glass.Core;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace EZXR.Glass.Inputs
  6. {
  7. public class DrawBounds : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// 得到目标模型的Mesh
  11. /// </summary>
  12. public MeshFilter meshFilter;
  13. /// <summary>
  14. /// 用于实例化线的Prefab
  15. /// </summary>
  16. GameObject prefab_Line;
  17. /// <summary>
  18. /// (测试用)顶点Prefab
  19. /// </summary>
  20. GameObject prefab_Vertice;
  21. /// <summary>
  22. /// Bound的缩放系数
  23. /// </summary>
  24. public float scaleFactor = 1;
  25. /// <summary>
  26. /// Mesh.bounds的各个顶点在物体坐标系下的坐标,点序如下:
  27. /// 1 0
  28. /// 5 4
  29. /// 2 3
  30. /// 6 7
  31. /// </summary>
  32. Vector3[] localVertices = new Vector3[8];
  33. /// <summary>
  34. /// Mesh.bounds的各个顶点在世界坐标系下的坐标
  35. /// </summary>
  36. [HideInInspector]
  37. public Vector3[] vertices = new Vector3[8];
  38. /// <summary>
  39. /// 每个bound的长度
  40. /// </summary>
  41. float[] linesLength = new float[12];
  42. /// <summary>
  43. /// 每个bound的中点在物体坐标系下的坐标
  44. /// </summary>
  45. Vector3[] localMidPoints = new Vector3[12];
  46. /// <summary>
  47. /// 每个bound的中点在世界坐标系下的坐标
  48. /// </summary>
  49. [HideInInspector]
  50. public Vector3[] midPoints = new Vector3[12];
  51. /// <summary>
  52. /// 所有的线
  53. /// </summary>
  54. [HideInInspector]
  55. public Transform[] lines = new Transform[16];
  56. // Start is called before the first frame update
  57. void Awake()
  58. {
  59. if (meshFilter == null)
  60. {
  61. meshFilter = GetComponent<MeshFilter>();
  62. }
  63. if (prefab_Line == null)
  64. {
  65. prefab_Line = ResourcesManager.Load<GameObject>("DrawBounds/BoundLine");
  66. }
  67. if (prefab_Vertice == null)
  68. {
  69. prefab_Vertice = ResourcesManager.Load<GameObject>("DrawBounds/BoundVertice");
  70. }
  71. localVertices[0] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(meshFilter.mesh.bounds.extents.x, meshFilter.mesh.bounds.extents.y, meshFilter.mesh.bounds.extents.z));
  72. localVertices[1] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(-meshFilter.mesh.bounds.extents.x, meshFilter.mesh.bounds.extents.y, meshFilter.mesh.bounds.extents.z));
  73. localVertices[2] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(-meshFilter.mesh.bounds.extents.x, -meshFilter.mesh.bounds.extents.y, meshFilter.mesh.bounds.extents.z));
  74. localVertices[3] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(meshFilter.mesh.bounds.extents.x, -meshFilter.mesh.bounds.extents.y, meshFilter.mesh.bounds.extents.z));
  75. localVertices[4] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(meshFilter.mesh.bounds.extents.x, meshFilter.mesh.bounds.extents.y, -meshFilter.mesh.bounds.extents.z));
  76. localVertices[5] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(-meshFilter.mesh.bounds.extents.x, meshFilter.mesh.bounds.extents.y, -meshFilter.mesh.bounds.extents.z));
  77. localVertices[6] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(-meshFilter.mesh.bounds.extents.x, -meshFilter.mesh.bounds.extents.y, -meshFilter.mesh.bounds.extents.z));
  78. localVertices[7] = scaleFactor * (meshFilter.mesh.bounds.center + new Vector3(meshFilter.mesh.bounds.extents.x, -meshFilter.mesh.bounds.extents.y, -meshFilter.mesh.bounds.extents.z));
  79. vertices[0] = meshFilter.transform.TransformPoint(localVertices[0]);
  80. vertices[1] = meshFilter.transform.TransformPoint(localVertices[1]);
  81. vertices[2] = meshFilter.transform.TransformPoint(localVertices[2]);
  82. vertices[3] = meshFilter.transform.TransformPoint(localVertices[3]);
  83. vertices[4] = meshFilter.transform.TransformPoint(localVertices[4]);
  84. vertices[5] = meshFilter.transform.TransformPoint(localVertices[5]);
  85. vertices[6] = meshFilter.transform.TransformPoint(localVertices[6]);
  86. vertices[7] = meshFilter.transform.TransformPoint(localVertices[7]);
  87. //Instantiate(prefab_Vertice, vertices[0], Quaternion.identity);
  88. //Instantiate(prefab_Vertice, vertices[1], Quaternion.identity);
  89. //Instantiate(prefab_Vertice, vertices[2], Quaternion.identity);
  90. //Instantiate(prefab_Vertice, vertices[3], Quaternion.identity);
  91. //Instantiate(prefab_Vertice, vertices[4], Quaternion.identity);
  92. //Instantiate(prefab_Vertice, vertices[5], Quaternion.identity);
  93. //Instantiate(prefab_Vertice, vertices[6], Quaternion.identity);
  94. //Instantiate(prefab_Vertice, vertices[7], Quaternion.identity);
  95. localMidPoints[0] = (localVertices[0] + localVertices[1]) / 2.0f;
  96. localMidPoints[1] = (localVertices[1] + localVertices[2]) / 2.0f;
  97. localMidPoints[2] = (localVertices[2] + localVertices[3]) / 2.0f;
  98. localMidPoints[3] = (localVertices[3] + localVertices[0]) / 2.0f;
  99. localMidPoints[4] = (localVertices[4] + localVertices[5]) / 2.0f;
  100. localMidPoints[5] = (localVertices[5] + localVertices[6]) / 2.0f;
  101. localMidPoints[6] = (localVertices[6] + localVertices[7]) / 2.0f;
  102. localMidPoints[7] = (localVertices[7] + localVertices[4]) / 2.0f;
  103. localMidPoints[8] = (localVertices[0] + localVertices[4]) / 2.0f;
  104. localMidPoints[9] = (localVertices[1] + localVertices[5]) / 2.0f;
  105. localMidPoints[10] = (localVertices[2] + localVertices[6]) / 2.0f;
  106. localMidPoints[11] = (localVertices[3] + localVertices[7]) / 2.0f;
  107. midPoints[0] = meshFilter.transform.TransformPoint(localMidPoints[0]);
  108. midPoints[1] = meshFilter.transform.TransformPoint(localMidPoints[1]);
  109. midPoints[2] = meshFilter.transform.TransformPoint(localMidPoints[2]);
  110. midPoints[3] = meshFilter.transform.TransformPoint(localMidPoints[3]);
  111. midPoints[4] = meshFilter.transform.TransformPoint(localMidPoints[4]);
  112. midPoints[5] = meshFilter.transform.TransformPoint(localMidPoints[5]);
  113. midPoints[6] = meshFilter.transform.TransformPoint(localMidPoints[6]);
  114. midPoints[7] = meshFilter.transform.TransformPoint(localMidPoints[7]);
  115. midPoints[8] = meshFilter.transform.TransformPoint(localMidPoints[8]);
  116. midPoints[9] = meshFilter.transform.TransformPoint(localMidPoints[9]);
  117. midPoints[10] = meshFilter.transform.TransformPoint(localMidPoints[10]);
  118. midPoints[11] = meshFilter.transform.TransformPoint(localMidPoints[11]);
  119. linesLength[0] = prefab_Line.transform.localScale.x + (vertices[0] - vertices[1]).magnitude;
  120. linesLength[1] = prefab_Line.transform.localScale.x + (vertices[1] - vertices[2]).magnitude;
  121. linesLength[2] = prefab_Line.transform.localScale.x + (vertices[2] - vertices[3]).magnitude;
  122. linesLength[3] = prefab_Line.transform.localScale.x + (vertices[3] - vertices[0]).magnitude;
  123. linesLength[4] = prefab_Line.transform.localScale.x + (vertices[4] - vertices[5]).magnitude;
  124. linesLength[5] = prefab_Line.transform.localScale.x + (vertices[5] - vertices[6]).magnitude;
  125. linesLength[6] = prefab_Line.transform.localScale.x + (vertices[6] - vertices[7]).magnitude;
  126. linesLength[7] = prefab_Line.transform.localScale.x + (vertices[7] - vertices[4]).magnitude;
  127. linesLength[8] = prefab_Line.transform.localScale.x + (vertices[0] - vertices[4]).magnitude;
  128. linesLength[9] = prefab_Line.transform.localScale.x + (vertices[1] - vertices[5]).magnitude;
  129. linesLength[10] = prefab_Line.transform.localScale.x + (vertices[2] - vertices[6]).magnitude;
  130. linesLength[11] = prefab_Line.transform.localScale.x + (vertices[3] - vertices[7]).magnitude;
  131. for (int i = 0; i < linesLength.Length; i++)
  132. {
  133. lines[i] = Instantiate(prefab_Line, midPoints[i], Quaternion.identity).transform;
  134. lines[i].localScale = new Vector3(prefab_Line.transform.localScale.x, prefab_Line.transform.localScale.y, linesLength[i]);
  135. lines[i].parent = transform;
  136. }
  137. lines[0].LookAt(vertices[0], transform.up);
  138. lines[1].LookAt(vertices[1], transform.forward);
  139. lines[2].LookAt(vertices[2], transform.up);
  140. lines[3].LookAt(vertices[3], transform.forward);
  141. lines[4].LookAt(vertices[4], transform.up);
  142. lines[5].LookAt(vertices[5], transform.forward);
  143. lines[6].LookAt(vertices[6], transform.up);
  144. lines[7].LookAt(vertices[7], transform.forward);
  145. lines[8].LookAt(vertices[0], transform.up);
  146. lines[9].LookAt(vertices[1], transform.up);
  147. lines[10].LookAt(vertices[2], transform.up);
  148. lines[11].LookAt(vertices[3], transform.up);
  149. }
  150. }
  151. }