NetMeshData.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using OpenCVForUnity.CoreModule;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace CScript.Entity
  6. {
  7. [System.Serializable]
  8. public class NetMeshData
  9. {
  10. public List<int> netColorList;
  11. public List<int> netFrontVertexList;
  12. public List<int> netSideVertexList;
  13. public List<int> netUvList;
  14. public List<byte> netTexture;
  15. public List<int> netFrontTriangles;
  16. public List<int> netSideTriangles;
  17. private Vector3[] _frontVertexList;
  18. private Vector3[] _sideVertexList;
  19. private Vector2[] _uvList;
  20. private Color[] _colorList;
  21. private int[] _triangles1;
  22. private int[] _triangles2;
  23. public int width ;
  24. public int height ;
  25. /// <summary>
  26. /// 顶点
  27. /// </summary>
  28. public List<short> netAtlasVertexList;
  29. /// <summary>
  30. /// uv
  31. /// </summary>
  32. public List<ushort> netAtlasUVList;
  33. /// <summary>
  34. /// 三角形
  35. /// </summary>
  36. public List<int> netTrianglesList;
  37. /// <summary>
  38. /// nose顶点
  39. /// </summary>
  40. public List<short> netNoseVertexList;
  41. /// <summary>
  42. /// nose 颜色
  43. /// </summary>
  44. public List<short> netNoseColorList;
  45. /// <summary>
  46. /// nose三角顺序
  47. /// </summary>
  48. public List<int> netNoseTriangles;
  49. public NetMeshData()
  50. {
  51. netColorList = new List<int>();
  52. netFrontVertexList = new List<int>();
  53. netSideVertexList = new List<int>();
  54. netUvList = new List<int>();
  55. netTexture = new List<byte>();
  56. netFrontTriangles = new List<int>();
  57. netSideTriangles = new List<int>();
  58. netAtlasVertexList = new List<short>();
  59. netAtlasUVList = new List<ushort>();
  60. netTrianglesList = new List<int>();
  61. netNoseVertexList = new List<short>();
  62. netNoseColorList = new List<short>();
  63. netNoseTriangles = new List<int>();
  64. }
  65. public void Clear()
  66. {
  67. netColorList.Clear();
  68. netColorList.TrimExcess();
  69. netFrontVertexList.Clear();
  70. netFrontVertexList.TrimExcess();
  71. netSideVertexList.Clear();
  72. netSideVertexList.TrimExcess();
  73. netUvList.Clear();
  74. netUvList.TrimExcess();
  75. netTexture.Clear();
  76. netTexture.TrimExcess();
  77. netFrontTriangles.Clear();
  78. netFrontTriangles.TrimExcess();
  79. netSideTriangles.Clear();
  80. netSideTriangles.TrimExcess();
  81. _frontVertexList = null;
  82. _sideVertexList = null;
  83. _uvList = null;
  84. _colorList = null;
  85. _triangles1 = null;
  86. _triangles2 = null;
  87. netAtlasVertexList.Clear();
  88. netAtlasVertexList.TrimExcess();
  89. netAtlasUVList.Clear();
  90. netAtlasUVList.TrimExcess();
  91. netTrianglesList.Clear();
  92. netTrianglesList.TrimExcess();
  93. netNoseVertexList.Clear();
  94. netNoseVertexList.TrimExcess();
  95. netNoseColorList.Clear();
  96. netNoseColorList.TrimExcess();
  97. netNoseTriangles.Clear();
  98. netNoseTriangles.TrimExcess();
  99. }
  100. int[] tempIntArray;
  101. public Vector3[] frontVertexList
  102. {
  103. get
  104. {
  105. if (_frontVertexList == null)
  106. {
  107. if (App.AppConfig.isNewMeshData)
  108. {
  109. (tempIntArray, _triangles1) = SetVertexAndTriangle(netAtlasVertexList, netTrianglesList);
  110. _frontVertexList = GetVector3(tempIntArray);
  111. }
  112. else
  113. {
  114. _frontVertexList = GetVector3(netFrontVertexList);
  115. }
  116. }
  117. return _frontVertexList;
  118. }
  119. }
  120. public Vector3[] sideVertexList
  121. {
  122. get
  123. {
  124. if (_sideVertexList == null)
  125. {
  126. if (App.AppConfig.isNewMeshData)
  127. {
  128. _sideVertexList = GetVector3(netNoseVertexList);
  129. }
  130. else
  131. {
  132. _sideVertexList = GetVector3(netSideVertexList);
  133. }
  134. }
  135. return _sideVertexList;
  136. }
  137. }
  138. public Color[] colorList
  139. {
  140. get
  141. {
  142. if (_colorList == null)
  143. {
  144. if (App.AppConfig.isNewMeshData)
  145. {
  146. _colorList = GetColor(netNoseColorList);
  147. }
  148. else
  149. {
  150. _colorList = GetColor(netColorList);
  151. }
  152. }
  153. return _colorList;
  154. }
  155. }
  156. Vector2[] v2Array;
  157. public Vector2[] uvList
  158. {
  159. get
  160. {
  161. if (_uvList == null)
  162. {
  163. if (App.AppConfig.isNewMeshData)
  164. {
  165. v2Array = GetVector2(netAtlasUVList);
  166. //_uvList = GetVector2(netAtlasUVList);
  167. _uvList = new Vector2[v2Array.Length];
  168. for (int i = 0, count = v2Array.Length; i < count; i++)
  169. {
  170. if (i % 3 == 0)
  171. {
  172. _uvList[i] = v2Array[i + 2];
  173. }
  174. else if (i % 3 == 1)
  175. {
  176. _uvList[i] = v2Array[i - 1];
  177. }
  178. else if (i % 3 == 2)
  179. {
  180. _uvList[i] = v2Array[i - 1];
  181. }
  182. }
  183. }
  184. else
  185. {
  186. _uvList = GetVector2(netUvList);
  187. }
  188. }
  189. return _uvList;
  190. }
  191. }
  192. public int[] Triangles1
  193. {
  194. get
  195. {
  196. if (_triangles1 == null || _triangles1.Length==0)
  197. {
  198. if (App.AppConfig.isNewMeshData)
  199. {
  200. (tempIntArray, _triangles1) = SetVertexAndTriangle(netAtlasVertexList, netTrianglesList);
  201. }
  202. else
  203. {
  204. _triangles1 = netFrontTriangles.ToArray();
  205. }
  206. }
  207. return _triangles1;
  208. }
  209. }
  210. public int[] Triangles2
  211. {
  212. get
  213. {
  214. if (_triangles2 == null || _triangles2.Length ==0)
  215. {
  216. if (App.AppConfig.isNewMeshData)
  217. {
  218. _triangles2 = netNoseTriangles.ToArray();
  219. }
  220. else
  221. {
  222. _triangles2 = netSideTriangles.ToArray();
  223. }
  224. }
  225. return _triangles2;
  226. }
  227. }
  228. private Vector3[] GetVector3(IList<int> vales)
  229. {
  230. Vector3[] vector3s = new Vector3[vales.Count / 3];
  231. for (int index = 0; index < vales.Count / 3; index++)
  232. {
  233. Vector3 vector3 = new Vector3();
  234. vector3.x = vales[index * 3 + 0] / 10000.0f;
  235. vector3.y = vales[index * 3 + 1] / 10000.0f;
  236. vector3.z = vales[index * 3 + 2] / 10000.0f;
  237. vector3s[index] = vector3;
  238. }
  239. return vector3s;
  240. }
  241. private Vector3[] GetVector3(int[] vales)
  242. {
  243. Vector3[] vector3s = new Vector3[vales.Length / 3];
  244. for (int index = 0; index < vales.Length / 3; index++)
  245. {
  246. Vector3 vector3 = new Vector3();
  247. vector3.x = vales[index * 3 + 0] / 10000.0f;
  248. vector3.y = vales[index * 3 + 1] / 10000.0f;
  249. vector3.z = vales[index * 3 + 2] / 10000.0f;
  250. vector3s[index] = vector3;
  251. }
  252. return vector3s;
  253. }
  254. private Vector3[] GetVector3(List<short> vales)
  255. {
  256. Vector3[] vector3s = new Vector3[vales.Count / 3];
  257. for (int index = 0; index < vales.Count / 3; index++)
  258. {
  259. Vector3 vector3 = new Vector3();
  260. vector3.x = vales[index * 3 + 0] / 10000.0f;
  261. vector3.y = vales[index * 3 + 1] / 10000.0f;
  262. vector3.z = vales[index * 3 + 2] / 10000.0f;
  263. vector3s[index] = vector3;
  264. }
  265. return vector3s;
  266. }
  267. private Vector3[] GetVector3(List<ushort> vales)
  268. {
  269. Vector3[] vector3s = new Vector3[vales.Count / 3];
  270. for (int index = 0; index < vales.Count / 3; index++)
  271. {
  272. Vector3 vector3 = new Vector3();
  273. vector3.x = vales[index * 3 + 0] / 10000.0f;
  274. vector3.y = vales[index * 3 + 1] / 10000.0f;
  275. vector3.z = vales[index * 3 + 2] / 10000.0f;
  276. vector3s[index] = vector3;
  277. }
  278. return vector3s;
  279. }
  280. private Vector2[] GetVector2(List<int> vales)
  281. {
  282. Vector2[] vector2s = new Vector2[vales.Count / 2];
  283. for (int index = 0; index < vales.Count / 2; index++)
  284. {
  285. Vector2 vector2 = new Vector2();
  286. vector2.x = vales[index * 2 + 0] / 10000.0f;
  287. vector2.y = -vales[index * 2 + 1] / 10000.0f;
  288. vector2s[index] = vector2;
  289. }
  290. return vector2s;
  291. }
  292. private Vector2[] GetVector2(List<short> vales)
  293. {
  294. Vector2[] vector2s = new Vector2[vales.Count / 2];
  295. for (int index = 0; index < vales.Count / 2; index++)
  296. {
  297. Vector2 vector2 = new Vector2();
  298. vector2.x = vales[index * 2 + 0] / 10000.0f;
  299. vector2.y = vales[index * 2 + 1] / 10000.0f;
  300. vector2s[index] = vector2;
  301. }
  302. return vector2s;
  303. }
  304. private Vector2[] GetVector2(List<ushort> vales)
  305. {
  306. Vector2[] vector2s = new Vector2[vales.Count / 2];
  307. for (int index = 0; index < vales.Count / 2; index++)
  308. {
  309. Vector2 vector2 = new Vector2();
  310. vector2.x = vales[index * 2 + 0] / 10000.0f;
  311. vector2.y = 1 - (vales[index * 2 + 1] / 10000.0f);
  312. vector2s[index] = vector2;
  313. }
  314. return vector2s;
  315. }
  316. private Color[] GetColor(List<int> vales)
  317. {
  318. Color[] colors = new Color[vales.Count / 3];
  319. for (int index = 0; index < vales.Count / 3; index++)
  320. {
  321. Color color = new Color();
  322. //color.r = vales[index * 3 + 0] / 10000.0f;
  323. //color.g = vales[index * 3 + 1] / 10000.0f;
  324. //color.b = vales[index * 3 + 2] / 10000.0f;
  325. color.b = vales[index * 3 + 0] / 10000.0f;
  326. color.g = vales[index * 3 + 1] / 10000.0f;
  327. color.r = vales[index * 3 + 2] / 10000.0f;
  328. color.a = 1;
  329. colors[index]=color;
  330. }
  331. return colors;
  332. }
  333. private Color[] GetColor(List<ushort> vales)
  334. {
  335. Color[] colors = new Color[vales.Count / 3];
  336. for (int index = 0; index < vales.Count / 3; index++)
  337. {
  338. Color color = new Color();
  339. color.r = vales[index * 3 + 0] / 10000.0f;
  340. color.g = vales[index * 3 + 1] / 10000.0f;
  341. color.b = vales[index * 3 + 2] / 10000.0f;
  342. color.a = 1;
  343. colors[index] = color;
  344. }
  345. return colors;
  346. }
  347. private Color[] GetColor(List<short> vales)
  348. {
  349. Color[] colors = new Color[vales.Count / 3];
  350. for (int index = 0; index < vales.Count / 3; index++)
  351. {
  352. Color color = new Color();
  353. color.b = vales[index * 3 + 0] / 10000.0f;
  354. color.g = vales[index * 3 + 1] / 10000.0f;
  355. color.r = vales[index * 3 + 2] / 10000.0f;
  356. color.a = 1;
  357. colors[index] = color;
  358. }
  359. return colors;
  360. }
  361. List<int> atlasXyzRe = new List<int>();
  362. List<int> atlasIndRe = new List<int>();
  363. private (int[], int[]) SetVertexAndTriangle(List<short> atlasXyz,List<int> atlasInd)
  364. {
  365. atlasXyzRe.Clear();
  366. atlasXyzRe.Capacity = atlasInd.Count * 3;
  367. atlasIndRe.Clear();
  368. atlasIndRe.Capacity = atlasInd.Count;
  369. for (int i = 0,count= atlasInd.Count; i < count; i++)
  370. {
  371. atlasXyzRe.Add(atlasXyz[atlasInd[i] * 3 + 0]);
  372. atlasXyzRe.Add(atlasXyz[atlasInd[i] * 3 + 1]);
  373. atlasXyzRe.Add(atlasXyz[atlasInd[i] * 3 + 2]);
  374. atlasIndRe.Add(i);
  375. }
  376. return (atlasXyzRe.ToArray(),atlasIndRe.ToArray());
  377. }
  378. }
  379. }