VyncGetData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using UnityEngine;
  9. using Debug = UnityEngine.Debug;
  10. public class VyncGetData
  11. {
  12. // Start is called before the first frame update
  13. private const string dllPath = "InVisionKinectDll";
  14. //private const string dllPath = "InVisionKinectReceiver";
  15. //public string HOST = "172.16.15.121";
  16. public string HOST = "192.168.50.85";
  17. //private const string HOST = "192.168.1.100";
  18. private const int PORT = 6666;
  19. private const int IMAGE_WIDTH = 640;
  20. private const int IMAGE_HEIGHT = 576;
  21. private char _deviceId=(char)0;
  22. private char _dataType=(char)0;
  23. private uint _frameIndex=0;
  24. private uint _headDataNum=0;
  25. private float[] _headDataArray;
  26. private uint _bodyDataNum=0;
  27. private float[] _bodyDataArray;
  28. private uint _TriNum=0;
  29. public short[] _TriArray=new short[]{};
  30. private bool _bDumpFrame=false;
  31. private bool _bCompressed=true;
  32. public float headPointSize=0.005f;
  33. public float bodyPointSize=0.01f;
  34. public float timeDelay=0f;
  35. private Stopwatch st = new Stopwatch();
  36. private const int BUFFER_STRIDE = 9;
  37. [DllImport(dllPath)]
  38. private static extern int invision_kinect_init(string host, int port, int width, int height);
  39. [DllImport(dllPath)]
  40. //INVISION_KINECT_CLIENT_API int invision_kinect_fetchOneFrameData_unity(uint8_t& outDeviceId, uint8_t& outDataType, uint32_t& outFrameIndex,
  41. // uint32_t& outVertexNum, float* outVertexArray, uint32_t &outSecondaryVertexNum, float *outSecondaryVertexArray,
  42. // uint32_t& outMeshTrianglesNum, uint16_t* outVertexIndexArray, bool bDumpFrame, bool bCompressed);
  43. //DeviceId 设备号 DataType发送类型 0点云 1mesh FrameIndex传输的帧索引 headDataNum单帧的头部点数量 bodyDataNum单帧的身体点数量 headDataArray,bodyDataArray数据集6位一组 x y z r g b TriNum mesh顶点数量 TriArray mesh顶点数组 bDumpFrame是否本地保存单帧数据 bCompressed是否压缩
  44. private static extern int invision_kinect_fetchOneFrameData_unity( ref char DeviceId, ref char DataType, ref uint FrameIndex, ref uint headDataNum, float[] headDataArray, ref uint bodyDataNum, float[] bodyDataArray, ref uint TriNum,ref short[] TriArray, bool bDumpFrame, bool bCompressed);
  45. [DllImport(dllPath)]
  46. private static extern int invision_kinect_destroy();
  47. private Thread thread;
  48. private static VyncGetData instance;
  49. public static VyncGetData GetInstance()
  50. {
  51. if (instance == null)
  52. {
  53. instance = new VyncGetData();
  54. }
  55. return instance;
  56. }
  57. public int limitPoints = 30000; //单shader的绘制长度
  58. public int topologyNum=3;//拓展成三个
  59. public int headPointGroups; //head随需要的组件数量
  60. public int bodyPointGroups; //body随需要的组件数量
  61. //public int totalPoint;
  62. //序列化数组
  63. public List<Vector3[]> headpostionList;
  64. public List<Color[]> headcolorList;
  65. public List<Vector3[]> bodypostionList;
  66. public List<Color[]> bodycolorList;
  67. //只取6W个点
  68. public Vector3[] curListImg;
  69. public Color[] curListCol;
  70. public int offsetZ=1;
  71. public void StartThread()
  72. {
  73. // totalPoint = limitPoints*topologyNum;
  74. // numPointGroups = Mathf.CeilToInt(totalPoint * 1.0f / limitPoints * 1.0f);
  75. headpostionList=new List<Vector3[]>();
  76. headcolorList=new List<Color[]>();
  77. bodypostionList=new List<Vector3[]>();
  78. bodycolorList=new List<Color[]>();
  79. //curListImg=new Vector3[totalPoint];
  80. //curListCol=new Color[totalPoint];
  81. invision_kinect_initParas();
  82. }
  83. private void invision_kinect_initParas()
  84. {
  85. Debug.Log("invision_kinect_initParas");
  86. invision_kinect_init(HOST, PORT, IMAGE_WIDTH, IMAGE_HEIGHT);
  87. _headDataArray = new float[IMAGE_WIDTH * IMAGE_HEIGHT];
  88. _bodyDataArray = new float[IMAGE_WIDTH * IMAGE_HEIGHT];
  89. //imageColor = new Int32[IMAGE_WIDTH * IMAGE_HEIGHT];
  90. //imageBufferSize = IMAGE_WIDTH * IMAGE_HEIGHT * BUFFER_STRIDE;
  91. // imageBuffer = new Byte[imageBufferSize]; //0~5 bytes:xyz, 6~8 bytes:rgb
  92. //receiveBufferSize = 0;
  93. ThreadStart childRef=new ThreadStart(readCloudPoint);
  94. thread=new Thread(childRef);
  95. thread.Start();
  96. }
  97. //判断是否更新
  98. public bool isUpdatae=false;
  99. public bool isLost=true;
  100. private void readCloudPoint()
  101. {
  102. while (true)
  103. {
  104. if (isUpdatae)
  105. {
  106. Thread.Sleep(5);
  107. continue;
  108. }
  109. //private static extern int invision_kinect_fetchOneFrameData(int Index,int BufferSize,Out float[] PosArray,Out Byte[] ColData,Out int TNum, Out short[] TDatas);
  110. st.Start(); //开始计时
  111. // Debug.Log("通讯开始 ");
  112. int mDataType = invision_kinect_fetchOneFrameData_unity( ref _deviceId,ref _dataType, ref _frameIndex, ref _headDataNum, _headDataArray,ref _bodyDataNum, _bodyDataArray, ref _TriNum,ref _TriArray, _bDumpFrame, _bCompressed);
  113. // Debug.Log("开始数据处理:_dataType"+(int)(_dataType)+" "+Convert.ToInt32(_dataType)+" _deviceId:"+ (int)(_deviceId)+" "+Convert.ToInt32(_deviceId)+" __headDataNum:"+Convert.ToInt32(_headDataNum)+" ___bodyDataNum:"+Convert.ToInt32(_bodyDataNum));
  114. if (mDataType == 0)
  115. {
  116. if (Convert.ToInt32(_dataType) == 1)
  117. {
  118. //点云数据
  119. if(_headDataNum<=0)
  120. {
  121. // Debug.Log("丢失更新1");
  122. isLost=true;
  123. isUpdatae = false;
  124. st.Stop();
  125. timeDelay = st.ElapsedMilliseconds;
  126. // Debug.Log("Running time " + st.ElapsedMilliseconds.ToString() + "ms");
  127. st.Reset();
  128. Thread.Sleep(5);
  129. }else
  130. {
  131. //Debug.Log("更新");
  132. UpdateCloudData();
  133. st.Stop();
  134. timeDelay = st.ElapsedMilliseconds;
  135. // Debug.Log("Running time " + st.ElapsedMilliseconds.ToString() + "ms");
  136. st.Reset();
  137. isUpdatae = true;
  138. isLost=false;
  139. Thread.Sleep(5);
  140. }
  141. }
  142. else
  143. {
  144. //mesh数据
  145. /* Debug.Log("更新更新");
  146. isLost=true;
  147. st.Stop();
  148. timeDelay = st.ElapsedMilliseconds;
  149. // Debug.Log("Running time " + st.ElapsedMilliseconds.ToString() + "ms");
  150. st.Reset();
  151. isUpdatae = false;
  152. Thread.Sleep(5);*/
  153. }
  154. }
  155. else
  156. {
  157. //mesh数据
  158. //Debug.Log("丢失更新2");
  159. //CloudDataclear();
  160. isLost=true;
  161. isUpdatae = false;
  162. Thread.Sleep(5);
  163. }
  164. }
  165. }
  166. private void UpdateCloudData()
  167. {
  168. headPointGroups = Mathf.CeilToInt((int)_headDataNum * topologyNum *1.0f/ limitPoints*1.0f );
  169. bodyPointGroups = Mathf.CeilToInt((int)_bodyDataNum * topologyNum *1.0f/ limitPoints*1.0f);
  170. if (headpostionList.Count < headPointGroups)
  171. {
  172. int len=headpostionList.Count;
  173. for (int i = 0; i < headPointGroups-len; i++)
  174. {
  175. headpostionList.Add(new Vector3[limitPoints]);
  176. headcolorList.Add(new Color[limitPoints]);
  177. }
  178. }
  179. if (bodypostionList.Count < bodyPointGroups)
  180. {
  181. int len=bodypostionList.Count;
  182. for (int i = 0; i < bodyPointGroups-len; i++)
  183. {
  184. bodypostionList.Add(new Vector3[limitPoints]);
  185. bodycolorList.Add(new Color[limitPoints]);
  186. }
  187. }
  188. int pointIndex = 0;
  189. long lostCount = 0;
  190. int arylndex=0;
  191. int indexTemp=0;
  192. float x, y, z, r, g, b;
  193. for (int i = 0; i < (int)_headDataNum; i++)
  194. {
  195. x = -_headDataArray[i * 6];
  196. y = -_headDataArray[i * 6+1]-0.4f;
  197. z = _headDataArray[i * 6+2];
  198. r = _headDataArray[i * 6+5];
  199. g = _headDataArray[i * 6+4];
  200. b = _headDataArray[i * 6+3];
  201. arylndex = pointIndex / (limitPoints);
  202. indexTemp = pointIndex - arylndex * limitPoints;
  203. headcolorList[arylndex][indexTemp] = new Color(r, g, b, 1.0f);
  204. headpostionList[arylndex][indexTemp] = new Vector3(x - headPointSize, y, z+offsetZ);
  205. headcolorList[arylndex][indexTemp+1] = new Color(r, g, b, 1.0f);
  206. headpostionList[arylndex][indexTemp+1] = new Vector3(x , y + headPointSize, z+offsetZ);
  207. headcolorList[arylndex][indexTemp+2] = new Color(r, g, b, 1.0f);
  208. headpostionList[arylndex][indexTemp+2] = new Vector3(x + headPointSize, y, z+offsetZ);
  209. //curListImg[i]=new Vector3(-x , -y, z);
  210. // curListCol[i]=new Color(r, g, b, 1.0f);
  211. pointIndex+=3;
  212. }
  213. //Debug.Log(headpostionList[0][0].z+"~~~~~~~~~~~~~");
  214. if (pointIndex < headPointGroups * limitPoints)
  215. {
  216. arylndex = pointIndex/(limitPoints);
  217. Array.Clear( headcolorList[arylndex], pointIndex-limitPoints*arylndex, limitPoints*(arylndex+1)-pointIndex);
  218. Array.Clear( headpostionList[arylndex], pointIndex-limitPoints*arylndex, limitPoints*(arylndex+1)-pointIndex);
  219. //Array.Clear(curListCol, pointIndex, totalPoint-pointIndex );
  220. // Array.Clear(curListImg, pointIndex, totalPoint -pointIndex);
  221. }
  222. int bodyPointIndex = 0;
  223. for (int i = 0; i < (int)_bodyDataNum; i++)
  224. {
  225. x =-_bodyDataArray[i * 6];
  226. y = -_bodyDataArray[i * 6+1]-0.4f;
  227. z = _bodyDataArray[i * 6+2];
  228. r = _bodyDataArray[i * 6+5];
  229. g = _bodyDataArray[i * 6+4];
  230. b = _bodyDataArray[i * 6+3];
  231. arylndex = bodyPointIndex / (limitPoints);
  232. indexTemp = bodyPointIndex - arylndex * limitPoints;
  233. bodycolorList[arylndex][indexTemp] = new Color(r, g, b, 1.0f);
  234. bodypostionList[arylndex][indexTemp] = new Vector3(x - bodyPointSize, y, z+offsetZ);
  235. bodycolorList[arylndex][indexTemp+1] = new Color(r, g, b, 1.0f);
  236. bodypostionList[arylndex][indexTemp+1] = new Vector3(x , y + bodyPointSize, z+offsetZ);
  237. bodycolorList[arylndex][indexTemp+2] = new Color(r, g, b, 1.0f);
  238. bodypostionList[arylndex][indexTemp+2] = new Vector3(x + bodyPointSize, y, z+offsetZ);
  239. //curListImg[i]=new Vector3(-x , -y, z);
  240. // curListCol[i]=new Color(r, g, b, 1.0f);
  241. bodyPointIndex+=3;
  242. }
  243. if (bodyPointIndex < bodyPointGroups * limitPoints)
  244. {
  245. arylndex = bodyPointIndex/(limitPoints);
  246. Array.Clear( bodycolorList[arylndex], bodyPointIndex-limitPoints*arylndex, limitPoints*(arylndex+1)-bodyPointIndex);
  247. Array.Clear( bodypostionList[arylndex], bodyPointIndex-limitPoints*arylndex, limitPoints*(arylndex+1)-bodyPointIndex);
  248. //Array.Clear(curListCol, pointIndex, totalPoint-pointIndex );
  249. // Array.Clear(curListImg, pointIndex, totalPoint -pointIndex);
  250. }
  251. //Debug.Log("Cached headpoint: " + pointIndex + ", Cached bodypoint: " + bodyPointIndex);
  252. }
  253. public void destory()
  254. {
  255. Debug.Log("OnDestroy invision_kinect_destroy Data");
  256. thread.Abort();
  257. invision_kinect_destroy();
  258. }
  259. }