PackageHandler.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. // RayMix Libs - RayMix's .Net Libs
  2. // Copyright 2018 Ray@raymix.net. All rights reserved.
  3. // https://www.raymix.net
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of RayMix.net. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. using CScript.App;
  31. using CScript.Entity;
  32. using CScript.Utilities;
  33. using OpenCVForUnity.CoreModule;
  34. using OpenCVForUnity.ImgcodecsModule;
  35. using OpenCVForUnity.ImgprocModule;
  36. using OpenCVForUnity.UnityUtils;
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Diagnostics;
  40. using System.IO;
  41. using System.Linq;
  42. namespace CScript.Net
  43. {
  44. public struct PackageCheck
  45. {
  46. public string frameSymbols;
  47. public int frameLenght;
  48. public byte frameLastByteValue;
  49. }
  50. /// <summary>
  51. /// PackageHandler
  52. /// 数据包处理器
  53. /// </summary>
  54. public class PackageHandler : PackageHandler<object>
  55. {
  56. public PackageHandler(object sender) : base(sender)
  57. {
  58. }
  59. }
  60. /// <summary>
  61. /// PackageHandler
  62. /// 数据包处理器
  63. /// </summary>
  64. /// <typeparam name="T">消息发送者类型</typeparam>
  65. public class PackageHandler<T>
  66. {
  67. //private MemoryStream stream = new MemoryStream(1024*1024*8);
  68. private MemoryStream stream = new MemoryStream(4096000);
  69. private int readOffset = 0;
  70. private T sender;
  71. NetMeshData netMeshData;
  72. private List<short> _shortBufferList;
  73. private List<byte> _byteBufferList;
  74. //private byte[] _byteBufferArray = new byte[232643];
  75. int totalByteCount;
  76. int lostCount;
  77. private MemoryStream _streamDataBuffer = new MemoryStream(409600 * 10);
  78. private bool _start = false;
  79. string packageTypeStr = string.Empty;
  80. Mat rgbMat;
  81. private float _decodeTime;
  82. //private Dictionary<int, PackageCheck> _packageCheckInfoDic;
  83. public PackageHandler(T sender)
  84. {
  85. this.sender = sender;
  86. netMeshData = new NetMeshData();
  87. _shortBufferList = new List<short>();
  88. _byteBufferList = new List<byte>();
  89. totalByteCount = 0;
  90. lostCount = 0;
  91. //_isNewData = App.AppManager.Instance.isNewMeshData;
  92. //_packageCheckInfoDic = new Dictionary<int, PackageCheck>();
  93. }
  94. /// <summary>
  95. /// 接收数据到PackageHandler
  96. /// </summary>
  97. /// <param name="data"></param>
  98. /// <param name="offset"></param>
  99. /// <param name="count"></param>
  100. public void ReceiveData(byte[] data, int offset, int count)
  101. {
  102. //UnityEngine.Debug.LogError(data.Length + " $$$" + count + " " + stream.Position);
  103. // UnityEngine.Debug.LogWarning(data.Length + " $$$" + count + " " + stream.Position + " offset: " + offset + " stream.Capacity:" + stream.Capacity);
  104. if (stream.Position + count > stream.Capacity)
  105. {
  106. throw new Exception("PackageHandler write buffer overflow");
  107. }
  108. stream.Write(data, offset, count);
  109. ParsePackageTCP();
  110. //ParsePackage();
  111. }
  112. public void ReceiveQuicData(byte[] data, int offset, int count)
  113. {
  114. if (_streamDataBuffer.Position + count > _streamDataBuffer.Capacity)
  115. {
  116. throw new Exception("PackageHandler write buffer overflow");
  117. }
  118. _streamDataBuffer.Write(data, offset, count);
  119. totalByteCount += count;
  120. netMeshData.Clear();
  121. //UnPack1();
  122. UnityEngine.Debug.LogWarning("_streamDataBuffer.len:"+_streamDataBuffer.Length);
  123. bool isSuccess = UnPack2();
  124. UnityEngine.Debug.LogWarning("lostCount: " + lostCount + "isSuccess:" + isSuccess);
  125. UnityEngine.Debug.LogWarning("netMeshData:"+netMeshData);
  126. if (isSuccess)
  127. {
  128. NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime, lostCount);
  129. }
  130. netMeshData.Clear();
  131. _streamDataBuffer.Position = 0;
  132. _streamDataBuffer.SetLength(0);
  133. totalByteCount = 0;
  134. }
  135. public void Reset()
  136. {
  137. readOffset = 0;
  138. i = 0;
  139. stream.Position = 0;
  140. stream.SetLength(0);
  141. _streamDataBuffer.Position = 0;
  142. _streamDataBuffer.SetLength(0);
  143. _start = false;
  144. }
  145. /// <summary>
  146. /// 打包消息
  147. /// </summary>
  148. /// <param name="message"></param>
  149. /// <returns></returns>
  150. public static byte[] PackMessage()
  151. {
  152. byte[] package = null;
  153. using (MemoryStream ms = new MemoryStream())
  154. {
  155. //ProtoBuf.Serializer.Serialize(ms, message);
  156. package = new byte[ms.Length + 4];
  157. Buffer.BlockCopy(BitConverter.GetBytes(ms.Length), 0, package, 0, 4);
  158. Buffer.BlockCopy(ms.GetBuffer(), 0, package, 4, (int)ms.Length);
  159. }
  160. return package;
  161. }
  162. ///// <summary>
  163. ///// 提取消息
  164. ///// </summary>
  165. ///// <param name="packet"></param>
  166. ///// <param name="offset"></param>
  167. ///// <param name="length"></param>
  168. ///// <returns></returns>
  169. //public static SkillBridge.Message.NetMessage UnpackMessage(byte[] packet,int offset,int length)
  170. //{
  171. // SkillBridge.Message.NetMessage message = null;
  172. // using (MemoryStream ms = new MemoryStream(packet, offset, length))
  173. // {
  174. // message = ProtoBuf.Serializer.Deserialize<SkillBridge.Message.NetMessage>(ms);
  175. // }
  176. // return message;
  177. //}
  178. int i;
  179. private int _frameIndex;
  180. bool ParsePackageTCP()
  181. {
  182. if (!_start)
  183. {
  184. while (readOffset + 20 < stream.Position)
  185. {
  186. packageTypeStr = System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 0, 1);
  187. packageTypeStr += stream.GetBuffer()[readOffset + 1].ToString();
  188. packageTypeStr += System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 2, 1);
  189. packageTypeStr += System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 3, 1);
  190. // UnityEngine.Debug.Log(packageTypeStr);
  191. if (packageTypeStr == "$1@@")
  192. {
  193. _start = true;
  194. break;
  195. }
  196. else
  197. {
  198. readOffset+=1;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. if (readOffset + 20 < stream.Position)
  205. {
  206. int packageSize = BitConverter.ToInt32(stream.GetBuffer(), readOffset + 16);
  207. if (packageSize + readOffset + 20 <= stream.Position)
  208. {
  209. _start = false;
  210. _streamDataBuffer.Position = 0;
  211. _streamDataBuffer.SetLength(0);
  212. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 20, packageSize);
  213. netMeshData.Clear();
  214. UnPack2();
  215. NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime, 0);
  216. netMeshData.Clear();
  217. totalByteCount = 0;
  218. _frameIndex = 0;
  219. this.readOffset += (packageSize + 20);
  220. return ParsePackageTCP();
  221. }
  222. }
  223. }
  224. //未接收完/要结束了
  225. if (this.readOffset > 0)
  226. {
  227. long size = stream.Position - this.readOffset;
  228. if (this.readOffset < stream.Position)
  229. {
  230. Array.Copy(stream.GetBuffer(), this.readOffset, stream.GetBuffer(), 0, stream.Position - this.readOffset);
  231. }
  232. //Reset Stream
  233. this.readOffset = 0;
  234. stream.Position = size;
  235. stream.SetLength(size);
  236. //UnityEngine.Debug.LogWarning("stream.Position: "+stream.Position+" "+ stream.Length);
  237. netMeshData.Clear();
  238. }
  239. return true;
  240. }
  241. /// <summary>
  242. /// 数据包解析
  243. /// </summary>
  244. /// <returns></returns>
  245. bool ParsePackage()
  246. {
  247. if (!_start)
  248. {
  249. while (readOffset + 13 < stream.Position)
  250. {
  251. packageTypeStr = System.Text.Encoding.UTF8.GetString(stream.GetBuffer(), readOffset + 0, 1);
  252. if (packageTypeStr == "#")
  253. {
  254. _start = true;
  255. break;
  256. }
  257. else
  258. {
  259. readOffset++;
  260. }
  261. }
  262. }
  263. if (_start)
  264. {
  265. //UnityEngine.Debug.LogWarning("_start");
  266. if (readOffset + 13 < stream.Position)
  267. {
  268. packageTypeStr = System.Text.Encoding.UTF8.GetString(stream.GetBuffer(), readOffset + 0, 1);
  269. int packageSize = BitConverter.ToInt32(stream.GetBuffer(), readOffset + 9);
  270. //UnityEngine.Debug.LogWarning(readOffset + " " + stream.Position + " " + packageSize+" "+ packageTypeStr);
  271. if (packageSize + readOffset + 13 <= stream.Position)
  272. {
  273. if (packageTypeStr == "#")
  274. {
  275. // _packageCheckInfoDic.Clear();
  276. _frameIndex = 0;
  277. // UnityEngine.Debug.LogWarning("#");
  278. _streamDataBuffer.Position = 0;
  279. _streamDataBuffer.SetLength(0);
  280. //UnityEngine.Debug.LogWarning("#:"+packageSize);
  281. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
  282. }
  283. else if (packageTypeStr == "@")
  284. {
  285. // UnityEngine.Debug.LogWarning("@");
  286. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
  287. }
  288. else if (packageTypeStr == "$")
  289. {
  290. _start = false;
  291. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
  292. //byte[] unzipData = lz4.Decompress(_streamDataBuffer.GetBuffer());
  293. i++;
  294. netMeshData.Clear();
  295. if (AppConfig.isNewMeshData)
  296. {
  297. UnPack2();
  298. }
  299. else
  300. {
  301. UnPack1();
  302. }
  303. NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime,0);
  304. netMeshData.Clear();
  305. totalByteCount = 0;
  306. _frameIndex = 0;
  307. }
  308. this.readOffset += (packageSize + 13);
  309. return ParsePackage();
  310. }
  311. }
  312. }
  313. //未接收完/要结束了
  314. if (this.readOffset > 0)
  315. {
  316. long size = stream.Position - this.readOffset;
  317. if (this.readOffset < stream.Position)
  318. {
  319. Array.Copy(stream.GetBuffer(), this.readOffset, stream.GetBuffer(), 0, stream.Position - this.readOffset);
  320. }
  321. //Reset Stream
  322. this.readOffset = 0;
  323. stream.Position = size;
  324. stream.SetLength(size);
  325. //UnityEngine.Debug.LogWarning("stream.Position: "+stream.Position+" "+ stream.Length);
  326. netMeshData.Clear();
  327. }
  328. return true;
  329. }
  330. private void UnPack1()
  331. {
  332. int bufferOffset = 0;
  333. //包有效
  334. //总长度
  335. ulong totalLenght = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  336. bufferOffset += sizeof(UInt64);
  337. // rgb size
  338. uint rgbSize = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset) * 6;
  339. bufferOffset += sizeof(UInt32);
  340. _shortBufferList.Clear();
  341. _shortBufferList.TrimExcess();
  342. _shortBufferList.Capacity = (int)rgbSize;
  343. // rgb 内容
  344. for (int i = 0; i < rgbSize; i++)
  345. {
  346. _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  347. bufferOffset += sizeof(Int16);
  348. }
  349. uint vertexCount = rgbSize / 6;
  350. netMeshData.netSideVertexList.Capacity = (int)vertexCount * 3;
  351. netMeshData.netColorList.Capacity = (int)vertexCount * 3;
  352. // 顶点数据和颜色
  353. for (int i = 0; i < vertexCount; i++)
  354. {
  355. netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 0]);
  356. netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 1]);
  357. netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 2]);
  358. netMeshData.netColorList.Add(_shortBufferList[i * 6 + 3]);
  359. netMeshData.netColorList.Add(_shortBufferList[i * 6 + 4]);
  360. netMeshData.netColorList.Add(_shortBufferList[i * 6 + 5]);
  361. }
  362. // 三角形
  363. UInt64 indices_len_rgb = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  364. bufferOffset += sizeof(UInt64);
  365. if (indices_len_rgb > Int32.MaxValue)
  366. {
  367. //UnityEngine.Debug.Log("totalLenght:"+ totalLenght+" indices_len_rgb:" + indices_len_rgb);
  368. UnityEngine.Debug.Log($"totalLenght:{totalLenght} rgbSize:{rgbSize} indices_len_rgb:{indices_len_rgb}");
  369. }
  370. netMeshData.netSideTriangles.Capacity = (int)indices_len_rgb;
  371. for (ulong i = 0; i < indices_len_rgb; i++)
  372. {
  373. netMeshData.netSideTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  374. bufferOffset += sizeof(Int16);
  375. }
  376. int ys_width_len = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  377. bufferOffset += sizeof(Int32);
  378. int ys_height_len = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  379. bufferOffset += sizeof(Int32);
  380. netMeshData.width = ys_width_len;
  381. netMeshData.height = ys_height_len;
  382. // 前面顶点长度
  383. UInt32 vertexes_len = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  384. if (vertexes_len > Int32.MaxValue)
  385. {
  386. UnityEngine.Debug.Log(" vertexes_len*5:" + vertexes_len * 5);
  387. }
  388. UInt32 xyzuvs_size = vertexes_len * 5;
  389. bufferOffset += sizeof(UInt32);
  390. _shortBufferList.Clear();
  391. _shortBufferList.TrimExcess();
  392. _shortBufferList.Capacity = (int)xyzuvs_size;
  393. for (int i = 0; i < xyzuvs_size; i++)
  394. {
  395. _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  396. bufferOffset += sizeof(Int16);
  397. }
  398. netMeshData.netUvList.Capacity = (int)vertexes_len * 2;
  399. netMeshData.netFrontVertexList.Capacity = (int)vertexes_len * 3;
  400. vertexCount = xyzuvs_size / 5;
  401. // 前面顶点 和 uv
  402. for (int i = 0; i < vertexCount; i++)
  403. {
  404. netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 0]);
  405. netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 1]);
  406. netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 2]);
  407. netMeshData.netUvList.Add(_shortBufferList[i * 5 + 3]);
  408. netMeshData.netUvList.Add(_shortBufferList[i * 5 + 4]);
  409. }
  410. _shortBufferList.Clear();
  411. _shortBufferList.TrimExcess();
  412. UInt64 indices_len = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  413. bufferOffset += sizeof(UInt64);
  414. // 前面三角
  415. netMeshData.netFrontTriangles.Capacity = (int)indices_len;
  416. for (ulong i = 0; i < indices_len; i++)
  417. {
  418. netMeshData.netFrontTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  419. bufferOffset += sizeof(ushort);
  420. }
  421. // 贴图数据
  422. int imageYEncoderSize = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  423. bufferOffset += sizeof(int);// y byte size
  424. //netMeshData.netTexture.Capacity = imageYEncoderSize;
  425. _byteBufferList.Clear();
  426. _byteBufferList.Capacity = imageYEncoderSize;
  427. //byte temp;
  428. for (int i = 0; i < imageYEncoderSize; i++)
  429. {
  430. _byteBufferList.Add(_streamDataBuffer.GetBuffer()[bufferOffset++]);
  431. }
  432. DecodeImageMat(_byteBufferList.ToArray(),out rgbMat);
  433. }
  434. private bool UnPack2()
  435. {
  436. int bufferOffset = 0;
  437. //总长度
  438. ulong totalLenght = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  439. bufferOffset += sizeof(UInt64);
  440. int realCount = 0;
  441. // atlsaXyz size
  442. uint atlasXyzBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  443. if (atlasXyzBufferLenght > 1000000)
  444. {
  445. lostCount++;
  446. return false;
  447. }
  448. bufferOffset += sizeof(UInt32);
  449. realCount = (int)(atlasXyzBufferLenght / sizeof(Int16));
  450. //UnityEngine.Debug.LogWarning("0 realCount:" + realCount);
  451. netMeshData.netAtlasVertexList.Capacity = (int)realCount;
  452. for (int i = 0; i < realCount; i++)
  453. {
  454. netMeshData.netAtlasVertexList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  455. bufferOffset += sizeof(Int16);
  456. }
  457. //atlasUv size
  458. uint atlasUvBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  459. if (atlasUvBufferLenght > 1000000)
  460. {
  461. lostCount++;
  462. return false;
  463. }
  464. bufferOffset += sizeof(UInt32);
  465. realCount = (int)(atlasUvBufferLenght / sizeof(UInt16));
  466. //UnityEngine.Debug.LogWarning("1realCount:"+ realCount);
  467. netMeshData.netAtlasUVList.Capacity = (int)realCount;
  468. for (int i = 0; i < realCount; i++)
  469. {
  470. //UnityEngine.Debug.LogError(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  471. netMeshData.netAtlasUVList.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  472. bufferOffset += sizeof(UInt16);
  473. }
  474. //atlasDices size
  475. uint atlasIndicesBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  476. if (atlasIndicesBufferLenght > 1000000)
  477. {
  478. lostCount++;
  479. return false;
  480. }
  481. bufferOffset += sizeof(UInt32);
  482. realCount = (int)(atlasIndicesBufferLenght / sizeof(UInt16));
  483. //UnityEngine.Debug.LogWarning("2realCount:" + realCount);
  484. netMeshData.netTrianglesList.Capacity = (int)realCount;
  485. for (int i = 0; i < realCount; i++)
  486. {
  487. netMeshData.netTrianglesList.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  488. bufferOffset += sizeof(UInt16);
  489. }
  490. // 贴图数据
  491. uint atlasImageBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  492. if (atlasImageBufferLenght > 1000000)
  493. {
  494. lostCount++;
  495. return false;
  496. }
  497. bufferOffset += sizeof(UInt32);// y byte size
  498. //netMeshData.netTexture.Capacity = imageYEncoderSize;
  499. _byteBufferList.Clear();
  500. realCount = (int)(atlasImageBufferLenght / sizeof(byte));
  501. //UnityEngine.Debug.LogWarning("3 realCount:" + realCount);
  502. _byteBufferList.Capacity = (int)realCount;
  503. //byte temp;
  504. for (int i = 0; i < realCount; i++)
  505. {
  506. _byteBufferList.Add(_streamDataBuffer.GetBuffer()[bufferOffset++]);
  507. }
  508. DecodeImageMat(_byteBufferList.ToArray(), out rgbMat);
  509. //nosexyzrgb
  510. uint noseXyzrgbBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  511. if (noseXyzrgbBufferLenght > 1000000)
  512. {
  513. lostCount++;
  514. return false;
  515. }
  516. bufferOffset += sizeof(UInt32);
  517. realCount = (int)(noseXyzrgbBufferLenght / sizeof(short));
  518. _shortBufferList.Clear();
  519. _shortBufferList.TrimExcess();
  520. //UnityEngine.Debug.LogWarning("4 realCount:" + realCount);
  521. _shortBufferList.Capacity = (int)realCount;
  522. // nosexyzrgb 内容
  523. for (int i = 0; i < realCount; i++)
  524. {
  525. _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  526. bufferOffset += sizeof(Int16);
  527. }
  528. uint vertexCount = (uint)realCount / 6;
  529. netMeshData.netSideVertexList.Capacity = (int)vertexCount * 3;
  530. netMeshData.netColorList.Capacity = (int)vertexCount * 3;
  531. // 顶点数据和颜色
  532. for (int i = 0; i < vertexCount; i++)
  533. {
  534. netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 0]);
  535. netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 1]);
  536. netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 2]);
  537. netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 3]);
  538. netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 4]);
  539. netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 5]);
  540. }
  541. //nosexyzrgb
  542. uint noseIndicesBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  543. if (noseIndicesBufferLenght > 1000000)
  544. {
  545. lostCount++;
  546. return false;
  547. }
  548. bufferOffset += sizeof(UInt32);
  549. realCount = (int)(noseIndicesBufferLenght / sizeof(ushort));
  550. // UnityEngine.Debug.LogWarning("5 realCount:" + realCount);
  551. netMeshData.netNoseTriangles.Capacity = (int)realCount;
  552. for (int i = 0; i < realCount; i++)
  553. {
  554. netMeshData.netNoseTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  555. bufferOffset += sizeof(UInt16);
  556. }
  557. return true;
  558. }
  559. private bool CheckSum(Dictionary<int, PackageCheck> packageCheckDic, ulong checksums)
  560. {
  561. UInt64 checkNumLocak = 0;
  562. packageCheckDic.Values.ToList().ForEach(pcidValue => { checkNumLocak += (ulong)pcidValue.frameLenght; checkNumLocak += (ulong)pcidValue.frameLastByteValue; });
  563. //UnityEngine.Debug.LogError(checkNumLocak);
  564. if (checkNumLocak == checksums)
  565. {
  566. return true;
  567. }
  568. return false;
  569. }
  570. Stopwatch watch = new Stopwatch();
  571. private void DecodeImageMat(byte[] imageRgbByte, out Mat imageRGBMat)
  572. {
  573. MatOfByte matOfByte = new MatOfByte(imageRgbByte);
  574. Mat yuv;
  575. // using (ProTimer p = new ProTimer("Imgcodecs.imdecode"))
  576. {
  577. //p._runTime
  578. watch.Start();
  579. yuv = Imgcodecs.imdecode(matOfByte, -1);
  580. watch.Stop();
  581. _decodeTime = watch.ElapsedMilliseconds;
  582. watch.Reset();
  583. }
  584. imageRGBMat = new Mat(yuv.rows(), yuv.cols(), CvType.CV_8UC3);
  585. Imgproc.cvtColor(yuv, imageRGBMat, Imgproc.COLOR_BGR2RGB);
  586. }
  587. }
  588. }