VyncCelect.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using UnityEngine;
  10. using Debug = UnityEngine.Debug;
  11. public class VyncCelect
  12. {
  13. // Start is called before the first frame update
  14. private const string dllPath = "KinectDataTransfer";
  15. public string HOST = "192.168.1.11";
  16. private const int PORT = 66666;
  17. private int _deviceId=0;
  18. private byte[] __KinectSNArray = new byte[512];
  19. private byte[] _outDeviceSN=new byte[12];
  20. private byte[] _curDeviceSN=new byte[12];
  21. private byte _dataType=0;
  22. private uint _frameIndex=0;
  23. public uint _headDataNum=0;
  24. public float[] _headDataArray;
  25. private uint _bodyDataNum=0;
  26. private uint _TriNum=0;
  27. public ushort[] _TriArray;
  28. private bool _bDumpFrame=false;
  29. private bool _bCompressed=true;
  30. public float headPointSize=0.005f;
  31. public float bodyPointSize=0.01f;
  32. public float timeDelay=0f;
  33. private Stopwatch st = new Stopwatch();
  34. private const int BUFFER_STRIDE = 9;
  35. public string _curDeviceSN_NUM;
  36. public int _curDeviceSN_Array_NUM;
  37. public byte _curDeviceNUM=0;
  38. public string _AllDeviceSN_NUM;
  39. [DllImport(dllPath)]
  40. private static extern int kinect_init_render(string host, int port);
  41. [DllImport(dllPath)]
  42. private static extern int kinect_fetch_all_devices_allocated(ref byte DataNum,bool dumpFrame);
  43. [DllImport(dllPath)]
  44. private static extern int kinect_list_available_devices(ref byte DataNum,byte[] outAvailableDevicesSN);
  45. [DllImport(dllPath)]
  46. private static extern int kinect_bind_device(byte[] deviceSN);
  47. [DllImport(dllPath)]
  48. private static extern int kinect_unbind_device();
  49. [DllImport(dllPath)]
  50. private static extern int kinect_fetch_one_device_allocated( byte[] outDeviceSN, ref byte DataType, ref uint FrameIndex, ref uint headDataNum, float[] headDataArray, ref uint bodyDataNum, ref uint TriNum, ushort[] TriArray, int deviceId);
  51. //DeviceId 设备号 DataType发送类型 0点云 1mesh FrameIndex传输的帧索引 headDataNum单帧的头部点数量 bodyDataNum单帧的身体点数量 headDataArray,bodyDataArray数据集6位一组 x y z r g b TriNum mesh顶点数量 TriArray mesh顶点数组 bDumpFrame是否本地保存单帧数据 bCompressed是否压缩
  52. [DllImport(dllPath)]
  53. private static extern int kinect_disconnect();
  54. private Thread thread;
  55. private static VyncCelect instance;
  56. public static VyncCelect GetInstance()
  57. {
  58. if (instance == null)
  59. {
  60. instance = new VyncCelect();
  61. }
  62. return instance;
  63. }
  64. public int limitPoints = 30000; //单shader的绘制长度
  65. public int topologyNum=3;//拓展成三个
  66. public int headPointGroups; //head随需要的组件数量
  67. //public int totalPoint;
  68. //序列化数组
  69. public List<Vector3[]> headpostionList;
  70. public List<Color[]> headcolorList;
  71. //只取6W个点
  72. public int offsetZ=1;
  73. public string fileStr;
  74. public float[] _headDataArray1;
  75. public void StartThread()
  76. {
  77. headpostionList=new List<Vector3[]>();
  78. headcolorList=new List<Color[]>();
  79. invision_kinect_initParas();
  80. }
  81. private void invision_kinect_initParas()
  82. {
  83. //0 000472192412 216 1 17720 8308
  84. //_headDataNum = 17720;
  85. // _bodyDataNum = 8308;
  86. Debug.Log("invision_kinect_initParas");
  87. _headDataArray = new float[300000];
  88. _TriArray=new ushort[1];
  89. int res= kinect_init_render(HOST, PORT);
  90. if (0 == res)
  91. {
  92. kinect_list_available_devices(ref _curDeviceNUM,__KinectSNArray);
  93. _curDeviceSN_Array_NUM = (int)_curDeviceNUM;
  94. Debug.Log("xjytest获取sn数量:" + _curDeviceSN_Array_NUM);
  95. byte[] currSN = new byte[12];
  96. for (int i = 0; i < _curDeviceSN_Array_NUM; i++)
  97. {
  98. Array.Copy(__KinectSNArray, i * 12, currSN, 0, 12);
  99. string strSN = Encoding.ASCII.GetString(currSN);
  100. _AllDeviceSN_NUM += strSN + ",";
  101. Debug.Log("xjytest获取sn No." + i + " available sn=" + strSN);
  102. }
  103. if (_curDeviceSN_Array_NUM > 0)
  104. {
  105. byte[] firstSN = new byte[12];
  106. Array.Copy(__KinectSNArray, firstSN, 12);
  107. _curDeviceSN = firstSN;
  108. string strSN = Encoding.ASCII.GetString(firstSN);
  109. Debug.Log("xjytest绑定的 sn=" + strSN);
  110. int resSn = kinect_bind_device(firstSN);
  111. Debug.Log("xjytest绑定sn的结果 res=" + resSn);
  112. _curDeviceSN_NUM = strSN;
  113. }
  114. }
  115. ThreadStart childRef=new ThreadStart(readCloudPoint);
  116. thread=new Thread(childRef);
  117. thread.Start();
  118. }
  119. //判断是否更新
  120. public bool isUpdatae=false;
  121. public bool isLost=true;
  122. private int devices;
  123. private bool isLock=false;
  124. private void readCloudPoint()
  125. {
  126. while (true)
  127. {
  128. if (isUpdatae)
  129. {
  130. //Debug.Log("xjytest还没有绘制,等待");
  131. Thread.Sleep(5);
  132. continue;
  133. }
  134. //private static extern int invision_kinect_fetchOneFrameData(int Index,int BufferSize,Out float[] PosArray,Out Byte[] ColData,Out int TNum, Out short[] TDatas);
  135. st.Start(); //开始计时
  136. /*
  137. //测试
  138. UpdateCloudData(true);
  139. st.Stop();
  140. timeDelay = st.ElapsedMilliseconds;
  141. // Debug.Log("Running time " + st.ElapsedMilliseconds.ToString() + "ms");
  142. st.Reset();
  143. isUpdatae = true;
  144. isLost=false;
  145. Thread.Sleep(5);
  146. continue;*/
  147. //Debug.Log("通讯开始");
  148. isLost=true;
  149. isUpdatae = false;
  150. kinect_fetch_all_devices_allocated(ref _curDeviceNUM,false);
  151. devices = (int)_curDeviceNUM;
  152. for (int i = 0; i < devices; i++)
  153. {
  154. int mDataType = kinect_fetch_one_device_allocated( _outDeviceSN,ref _dataType, ref _frameIndex,
  155. ref _headDataNum, _headDataArray,ref _bodyDataNum,
  156. ref _TriNum, _TriArray, i);
  157. /*if (i == 1)
  158. {
  159. _headDataArray = _headDataArray1;
  160. _headDataNum = 17720;
  161. _bodyDataNum = 8308;
  162. }*/
  163. // Debug.Log("开始数据处理:"+"devices:"+devices+" _outDeviceSN"+Encoding.ASCII.GetString(_outDeviceSN)+" _dataType:"+ (int)(_dataType)+" _deviceId:"+ i+" __headDataNum:"+Convert.ToInt32(_headDataNum)+" _headDataArray:"+_headDataArray.Length+" ___bodyDataNum:"+Convert.ToInt32(_bodyDataNum));
  164. if (mDataType == 0)
  165. {
  166. // Debug.Log("xjytest当前绑定的sn设备:"+ Encoding.ASCII.GetString(_outDeviceSN));
  167. if (Convert.ToInt32(_dataType) == 1||Convert.ToInt32(_dataType) == 0)
  168. {
  169. if (devices-1==i)
  170. {
  171. UpdateCloudData(true,i+1);
  172. isUpdatae = true;
  173. isLost=false;
  174. st.Stop();
  175. timeDelay = st.ElapsedMilliseconds;
  176. // Debug.Log("Running time " + st.ElapsedMilliseconds.ToString() + "ms");
  177. st.Reset();
  178. Thread.Sleep(5);
  179. }
  180. else
  181. {
  182. UpdateCloudData(false,i+1);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. //临时数据
  190. private int TempHeadNum=0;
  191. private int pointIndex = 0;
  192. private int arylndex=0;
  193. private int indexTemp=0;
  194. private int startIndex ;
  195. private int lostIndex = 0;
  196. private void UpdateCloudData(bool isOver,int index)
  197. {
  198. TempHeadNum += (int) _headDataNum+(int) _bodyDataNum;
  199. headPointGroups = Mathf.CeilToInt(TempHeadNum * topologyNum *1.0f/ limitPoints*1.0f );
  200. //Debug.Log("xjytest:headPointGroups: "+headPointGroups+" headpostionList.Count"+headpostionList.Count+" TempHeadNum:"+TempHeadNum+" index:"+index);
  201. if (headpostionList.Count < headPointGroups)
  202. {
  203. int len=headpostionList.Count;
  204. for (int l = 0; l < headPointGroups-len; l++)
  205. {
  206. headpostionList.Add(new Vector3[limitPoints]);
  207. headcolorList.Add(new Color[limitPoints]);
  208. }
  209. }else if (isOver&&headpostionList.Count > headPointGroups)
  210. {
  211. //Debug.Log("xjytest:删除: " +headpostionList.Count+" 第" +(headPointGroups)+"开始删除"+(headpostionList.Count-headPointGroups));
  212. headpostionList.RemoveRange(headPointGroups,headpostionList.Count-headPointGroups);
  213. headcolorList.RemoveRange(headPointGroups,headcolorList.Count-headPointGroups);
  214. }
  215. float x, y, z, r, g, b;
  216. //startIndex = pointIndex/3;
  217. for (int i=0; i < (int) _headDataNum+(int) _bodyDataNum; i++)
  218. {
  219. x = -_headDataArray[i * 6]+((int)(index/2))*(index%2==0?1:-1);
  220. y = -_headDataArray[i * 6+1];
  221. z = _headDataArray[i * 6+2]+ 1;
  222. r = _headDataArray[i * 6+5];
  223. g = _headDataArray[i * 6+4];
  224. b = _headDataArray[i * 6+3];
  225. //fileStr += (-x).ToString() + "," + (-y).ToString() + "," + (z).ToString() + "," + b.ToString() + "," +g.ToString() + "," + r.ToString() + ",";
  226. arylndex = pointIndex / (limitPoints);
  227. indexTemp = pointIndex - arylndex * limitPoints;
  228. if (i < (int) _headDataNum)
  229. {
  230. headcolorList[arylndex][indexTemp] = new Color(r, g, b, 1.0f);
  231. headpostionList[arylndex][indexTemp] = new Vector3(x - headPointSize, y, z+offsetZ);
  232. headcolorList[arylndex][indexTemp+1] = new Color(r, g, b, 1.0f);
  233. headpostionList[arylndex][indexTemp+1] = new Vector3(x , y + headPointSize, z+offsetZ);
  234. headcolorList[arylndex][indexTemp+2] = new Color(r, g, b, 1.0f);
  235. headpostionList[arylndex][indexTemp+2] = new Vector3(x + headPointSize, y, z+offsetZ);
  236. }
  237. else
  238. {
  239. headcolorList[arylndex][indexTemp] = new Color(r, g, b, 1.0f);
  240. headpostionList[arylndex][indexTemp] = new Vector3(x - bodyPointSize, y, z+offsetZ);
  241. headcolorList[arylndex][indexTemp+1] = new Color(r, g, b, 1.0f);
  242. headpostionList[arylndex][indexTemp+1] = new Vector3(x , y + bodyPointSize, z+offsetZ);
  243. headcolorList[arylndex][indexTemp+2] = new Color(r, g, b, 1.0f);
  244. headpostionList[arylndex][indexTemp+2] = new Vector3(x + bodyPointSize, y, z+offsetZ);
  245. }
  246. //Debug.Log("xjytest:lndex:"+index+" aryindex"+arylndex+" indexTemp:"+indexTemp+" "+(-x).ToString() + "," + (-y).ToString() + "," + (z).ToString() + "," + b.ToString() + "," +g.ToString() + "," + r.ToString() );
  247. pointIndex+=topologyNum;
  248. if(r==0&g==0&b==0)
  249. lostIndex++;
  250. }
  251. //Debug.Log("xjytest:pointIndex:"+pointIndex+" lostIndex:"+lostIndex+" floatZ:"+((int)(index/2))*(index%2==0?1:-1));
  252. if (isOver)
  253. {
  254. if (pointIndex < headPointGroups * limitPoints)
  255. {
  256. arylndex = pointIndex/(limitPoints);
  257. Array.Clear( headcolorList[arylndex], pointIndex-limitPoints*arylndex, limitPoints*(arylndex+1)-pointIndex);
  258. Array.Clear( headpostionList[arylndex], pointIndex-limitPoints*arylndex, limitPoints*(arylndex+1)-pointIndex);
  259. // Debug.Log("xjytest:headPointGroups clear:"+ (pointIndex-limitPoints*arylndex)+" "+(limitPoints*(arylndex+1)-pointIndex));
  260. restValue();
  261. }
  262. }
  263. //Debug.Log("Cached headpoint: " + pointIndex + ", Cached bodypoint: " + bodyPointIndex);
  264. }
  265. public void restValue()
  266. {
  267. TempHeadNum = 0;
  268. pointIndex = 0;
  269. lostIndex = 0;
  270. fileStr = "";
  271. }
  272. public void destory()
  273. {
  274. Debug.Log("OnDestroy xjytest invision_kinect_destroy Data");
  275. int res = kinect_unbind_device();
  276. Debug.Log("xjytest after invision_kinect_unbind_device res=" + res);
  277. thread.Abort();
  278. kinect_disconnect();
  279. }
  280. }