MeshData.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using CScript.Net;
  6. using OpenCVForUnity.UnityUtils;
  7. using OpenCVForUnity.CoreModule;
  8. using OpenCVForUnity.ImgprocModule;
  9. namespace CScript.Entity
  10. {
  11. [System.Serializable]
  12. public class MeshData
  13. {
  14. public List<VertexColorData> frontDataList;
  15. public List<VertexColorData> sideDataList;
  16. public Texture2D texture;
  17. public int[] frontTriangles;
  18. public int[] sideTriangles;
  19. private Vector3[] _frontVertexList;
  20. private Vector3[] _sideVertexList;
  21. private Vector2[] _uvList;
  22. private Color[] _colorList;
  23. public MeshData(NetMeshData netMeshData)
  24. {
  25. //Debug.Log("new meshData ");
  26. if (netMeshData == null)
  27. return;
  28. texture = Utilities.AppUtil.ByteToTex2d(netMeshData.netTexture.ToArray());
  29. // OpenCVForUnity.CoreModule.Mat ymat = new OpenCVForUnity.CoreModule.Mat(1280, 720, CvType.CV_8UC3);
  30. //ymat.put(0,0, netMeshData.texture.ToArray());
  31. //Imgproc.cvtColor(ymat, uvMat, Imgproc.COLOR_YUV2BGR_I420);
  32. //Utils.matToTexture2D(uvMat, texture);
  33. //texture = netMeshData.texture1;
  34. //texture = netMeshData.texture;
  35. frontTriangles = netMeshData.netFrontTriangles.ToArray();
  36. sideTriangles = netMeshData.netSideTriangles.ToArray();
  37. _frontVertexList = GetVector3(netMeshData.netFrontVertexList);
  38. _sideVertexList = GetVector3(netMeshData.netSideVertexList);
  39. _uvList = GetVector2(netMeshData.netUvList);
  40. _colorList = GetColor(netMeshData.netColorList);
  41. }
  42. public MeshData(NetMessage nm)
  43. {
  44. //CaoTintRequest caoTintRequest = nm.Request.Caoting;
  45. //texture = Utilities.AppUtil.ByteToTex2d(caoTintRequest.Texture);
  46. ////texture = netMeshData.texture;
  47. //frontTriangles = caoTintRequest.frontTriangles;
  48. //sideTriangles = caoTintRequest.sideTriangles;
  49. //_frontVertexList = GetVector3(caoTintRequest.frontVertexLists);
  50. //_sideVertexList = GetVector3(caoTintRequest.sideVertexLists);
  51. //_uvList = GetVector2(caoTintRequest.uvLists);
  52. ////_colorList = GetColor(caoTintRequest.colorList);
  53. }
  54. private Vector3[] GetVector3(List<int> vales)
  55. {
  56. Vector3[] vector3s = new Vector3[vales.Count/3];
  57. for (int index = 0; index < vales.Count / 3; index++)
  58. {
  59. Vector3 vector3 = new Vector3();
  60. vector3.x = vales[index * 3 + 0] / 10000.0f;
  61. vector3.y = vales[index * 3 + 1] / 10000.0f;
  62. vector3.z = vales[index * 3 + 2] / 10000.0f;
  63. vector3s[index] = vector3;
  64. }
  65. return vector3s;
  66. }
  67. private Vector2[] GetVector2(List<int> vales)
  68. {
  69. Vector2[] vector2s = new Vector2[vales.Count/2];
  70. for (int index = 0; index < vales.Count / 2; index++)
  71. {
  72. Vector2 vector2 = new Vector2();
  73. vector2.x = vales[index * 2 + 0] / 10000.0f;
  74. vector2.y = vales[index * 2 + 1] / 10000.0f;
  75. vector2s[index] = vector2;
  76. }
  77. return vector2s;
  78. }
  79. private Color[] GetColor(List<int> vales)
  80. {
  81. Color[] colors = new Color[vales.Count / 3];
  82. for (int index = 0; index < vales.Count / 3; index++)
  83. {
  84. Color color = new Color();
  85. color.r= vales[index * 3 + 0] / 10000.0f;
  86. color.g = vales[index * 3 + 1] / 10000.0f;
  87. color.b = vales[index * 3 + 2] / 10000.0f;
  88. color.a = 1;
  89. colors[index] = color;
  90. }
  91. return colors;
  92. }
  93. public Vector3[] frontVertexList
  94. {
  95. get
  96. {
  97. if (_frontVertexList == null)
  98. {
  99. _frontVertexList = new Vector3[frontDataList.Count];
  100. for (int i = 0, count = frontDataList.Count; i < count; i++)
  101. {
  102. _frontVertexList[i] = frontDataList[i].vertex;
  103. }
  104. }
  105. return _frontVertexList;
  106. }
  107. }
  108. public Vector3[] sideVertexList
  109. {
  110. get
  111. {
  112. if (_sideVertexList == null)
  113. {
  114. _sideVertexList = new Vector3[sideDataList.Count];
  115. for (int i = 0, count = sideDataList.Count; i < count; i++)
  116. {
  117. _sideVertexList[i] = sideDataList[i].vertex;
  118. }
  119. }
  120. return _sideVertexList;
  121. }
  122. }
  123. public Color[] colorList
  124. {
  125. get
  126. {
  127. if (_colorList == null)
  128. {
  129. _colorList = new Color[sideDataList.Count];
  130. for (int i = 0, count = sideDataList.Count; i < count; i++)
  131. {
  132. _colorList[i] = sideDataList[i].vertexColor;
  133. }
  134. }
  135. return _colorList;
  136. }
  137. }
  138. public Vector2[] uvList
  139. {
  140. get
  141. {
  142. if (_uvList == null)
  143. {
  144. _uvList = new Vector2[frontDataList.Count];
  145. for (int i = 0, count = frontDataList.Count; i < count; i++)
  146. {
  147. _uvList[i] = frontDataList[i].uv;
  148. }
  149. }
  150. return _uvList;
  151. }
  152. }
  153. public MeshData()
  154. {
  155. frontDataList = new List<VertexColorData>();
  156. sideDataList = new List<VertexColorData>();
  157. }
  158. }
  159. }