NxrSlamPlane.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Assets.NXR.Scripts.Slam;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class NxrSlamPlane : MonoBehaviour
  6. {
  7. public bool Test;
  8. public bool Reload;
  9. NxrSlam.NXRPlaneData testdata;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. Debug.Log("NxrSlamPlane.Start." + GetHashCode());
  14. // test
  15. testdata = new NxrSlam.NXRPlaneData(null);
  16. testdata.size = 14;
  17. testdata.points = new NxrSlam.NXRPlaneData.Point3D[]
  18. {
  19. new NxrSlam.NXRPlaneData.Point3D(0.597963809967041,-0.483312696218491,0.43642520904541),
  20. new NxrSlam.NXRPlaneData.Point3D(0.448058247566223,-0.483312696218491,0.369752883911133),
  21. new NxrSlam.NXRPlaneData.Point3D(0.448058247566223,-0.483312696218491,0.369752883911133),
  22. new NxrSlam.NXRPlaneData.Point3D(0.32343664765358,-0.483312696218491,0.323436379432678),
  23. new NxrSlam.NXRPlaneData.Point3D(0.3141910135746,-0.483312696218491,0.323615431785584),
  24. new NxrSlam.NXRPlaneData.Point3D(0.0990831553936005,-0.483312696218491,0.33366933465004),
  25. new NxrSlam.NXRPlaneData.Point3D(0.0974671021103859,-0.483312696218491,0.333752363920212),
  26. new NxrSlam.NXRPlaneData.Point3D(0.0561475902795792,-0.483312696218491,0.337889224290848),
  27. new NxrSlam.NXRPlaneData.Point3D(-0.465384006500244,-0.483312696218491,0.51269006729126),
  28. new NxrSlam.NXRPlaneData.Point3D(-0.466641992330551,-0.483312696218491,0.517888009548187),
  29. new NxrSlam.NXRPlaneData.Point3D(-0.232375964522362,-0.483312696218491,0.975189507007599),
  30. new NxrSlam.NXRPlaneData.Point3D(0.267775535583496,-0.483312696218491,0.737699329853058),
  31. new NxrSlam.NXRPlaneData.Point3D(0.360924899578094,-0.483312696218491,0.65373432636261),
  32. new NxrSlam.NXRPlaneData.Point3D(0.597963809967041,-0.483312696218491,0.43642520904541)
  33. };
  34. // test
  35. }
  36. // Update is called once per frame
  37. void Update()
  38. {
  39. if (Test)
  40. {
  41. Test = false;
  42. NxrSlam.Instance.GeneratePlaneObject(testdata);
  43. } else if(Reload)
  44. {
  45. MakePlane(testdata);
  46. }
  47. }
  48. public GameObject PointPrefab;
  49. Stack<GameObject> PointsRecycleStack = new Stack<GameObject>();
  50. List<GameObject> PointsList = new List<GameObject>();
  51. public int PlanePointsCount { set; get; }
  52. public int PlaneId { set; get; }
  53. public NxrSlam.NXRPlaneData PlaneData;
  54. public void MakePlane(NxrSlam.NXRPlaneData data)
  55. {
  56. PlaneData = data;
  57. // Debug.Log(data.id + ".NxrSlamPlane.MakePlane=" +(GetComponent<MeshFilter>()==null) +GetHashCode());
  58. PlanePointsCount = data.size;
  59. PlaneId = data.id;
  60. double xSum = 0, ySum = 0, zSum = 0;
  61. Vector2[] meshUvs = new Vector2[data.size];
  62. Vector3[] meshVertices = new Vector3[data.size];
  63. Vector2[] vertices2D = new Vector2[data.size];
  64. float minX = 0, maxX = 0;
  65. float minZ = 0, maxZ = 0;
  66. for (int i = 0; i < data.size; i++)
  67. {
  68. NxrSlam.NXRPlaneData.Point3D point3d = data.points[i];
  69. xSum += point3d.x;
  70. ySum += point3d.y;
  71. zSum += point3d.z;
  72. meshVertices[i].x = (float)point3d.x;
  73. meshVertices[i].y = (float)point3d.y;
  74. meshVertices[i].z = (float)point3d.z;
  75. //GameObject cubeObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  76. //cubeObj.name = "C" + i;
  77. //cubeObj.transform.position =new Vector3((float)point3d.x, (float)point3d.y, (float)point3d.z);
  78. if (point3d.x < minX)
  79. {
  80. minX = (float) point3d.x;
  81. }
  82. if (point3d.x > maxX)
  83. {
  84. maxX = (float)point3d.x;
  85. }
  86. if (point3d.z < minZ)
  87. {
  88. minZ = (float)point3d.z;
  89. }
  90. if (point3d.z > maxZ)
  91. {
  92. maxZ = (float)point3d.z;
  93. }
  94. vertices2D[i] =new Vector2((float) point3d.x, (float)point3d.z);
  95. }
  96. float xLength = maxX - minX;
  97. float zLength = maxZ - minZ;
  98. for (int i = 0; i < data.size; i++)
  99. {
  100. float u = meshVertices[i].x / xLength;
  101. float v = meshVertices[i].z / zLength;
  102. meshUvs[i] = new Vector2(u, v);
  103. }
  104. NxrTriangulator nxrTriangulator = new NxrTriangulator(vertices2D);
  105. int[] triangles = nxrTriangulator.Triangulate();
  106. // System.Array.Reverse(triangles);
  107. string trianglesStr = "";
  108. foreach (int i in triangles)
  109. {
  110. trianglesStr += i;
  111. trianglesStr += ",";
  112. }
  113. Debug.Log("triangles=" + trianglesStr);
  114. Vector3 center;
  115. center.x = (float)(xSum / data.size);
  116. center.y = (float)(ySum / data.size);
  117. center.z = (float)(zSum / data.size);
  118. // this.transform.position = center;
  119. // Debug.Log("PlaneCenter=" + center.x + "," + center.y + "," + center.z);
  120. Mesh mesh = new Mesh
  121. {
  122. vertices = meshVertices,
  123. uv = meshUvs,
  124. triangles = triangles,
  125. name = "planeMesh" + data.id
  126. };
  127. GetComponent< MeshFilter>().mesh = mesh;
  128. mesh.RecalculateNormals();
  129. mesh.RecalculateBounds();
  130. GeneratePoints(data);
  131. }
  132. private void GeneratePoints(NxrSlam.NXRPlaneData data)
  133. {
  134. if (PointPrefab == null)
  135. {
  136. PointPrefab = Resources.Load<GameObject>("Prefabs/PlanePoint");
  137. }
  138. // 先回收
  139. foreach (GameObject p in PointsList)
  140. {
  141. PointsRecycleStack.Push(p);
  142. }
  143. PointsList.Clear();
  144. // 隐藏
  145. foreach (GameObject p in PointsRecycleStack)
  146. {
  147. p.SetActive(false);
  148. }
  149. // Debug.Log("GeneratePoints : Data=" + data.size + ",PointsList=" + PointsList.Count + ",PointsRecycleStack=" + PointsRecycleStack.Count);
  150. for (int i = 0, size = data.size; i < size; i++)
  151. {
  152. NxrSlam.NXRPlaneData.Point3D point3d = data.points[i];
  153. Vector3 pointPos = new Vector3((float)point3d.x, (float)point3d.y, (float)point3d.z);
  154. GameObject go = null;
  155. if (PointsRecycleStack.Count > 0)
  156. {
  157. go = PointsRecycleStack.Pop();
  158. }
  159. else
  160. {
  161. go = Instantiate(PointPrefab, Vector3.zero, Quaternion.identity, transform) as GameObject;
  162. go.name = "Point_" + data.id + "_" + i;
  163. }
  164. go.SetActive(true);
  165. pointPos.y += 0.03f;
  166. go.transform.position = pointPos;
  167. TextMesh textMesh = go.GetComponent<TextMesh>();
  168. if (textMesh != null)
  169. {
  170. textMesh.text = "" + i;
  171. }
  172. PointsList.Add(go);
  173. }
  174. }
  175. }