AppManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using CScript.Utilities;
  5. using CScript.Net;
  6. using CScript.Entity;
  7. using System;
  8. using System.IO;
  9. using UnityEngine.UI;
  10. using OpenCVForUnity.UnityUtils;
  11. using OpenCVForUnity.CoreModule;
  12. using OpenCVForUnity.ImgprocModule;
  13. using OpenCVForUnity.ImgcodecsModule;
  14. using SC.XR.Unity;
  15. using System.Text;
  16. namespace CScript.App
  17. {
  18. public enum ConnectType
  19. {
  20. None = 0,
  21. TCPServer = 1,
  22. Quic =2,
  23. }
  24. public class AppManager : MonoSingleton<AppManager>
  25. {
  26. Server _server;
  27. public ConnectType connectType = ConnectType.None;
  28. public Queue<NetMeshData> meshQueue = new Queue<NetMeshData>();
  29. private Dictionary<NetMeshData, Mat> _meshDataMatDic = new Dictionary<NetMeshData, Mat>();
  30. public GameObject MeshPoint;
  31. public Material meshFrontMaterial;
  32. public Material meshSideMaterial;
  33. public GameObject meshFrontPoint;
  34. public GameObject meshSidePoint;
  35. public GameObject meshFrontPointNew;
  36. public GameObject meshSidePointNew;
  37. public bool isRunHolographic;
  38. private Renderer _meshFrontRender;
  39. private MeshFilter _meshFrontFilter;
  40. private Renderer __meshFrontRenderChild;
  41. private MeshFilter _meshFrontFilterChild;
  42. private Renderer _meshSideRender;
  43. private MeshFilter _meshSideFilter;
  44. private NetMeshData _currMeshData;
  45. private Queue<byte[]> _meshDatas;
  46. private string configPath = "/Config/ConfigData.json";
  47. ConfigData configData;
  48. [SerializeField]
  49. private Transform meshPoint;
  50. IEnumerator renderThreadCoroutine;
  51. Mat _rgbMat;
  52. public Texture2D _tex;
  53. private bool _gotMeshData = false;
  54. public NetMeshData CurrMeshData
  55. {
  56. get
  57. {
  58. return _currMeshData;
  59. }
  60. private set
  61. {
  62. _currMeshData = value;
  63. if (_currMeshData != null)
  64. {
  65. _rgbMat = _meshDataMatDic[_currMeshData];
  66. // Imgcodecs.imwrite(Application.dataPath+"xxx.jpg", _rgbMat);
  67. if (_tex == null || _tex.width != _rgbMat.cols() || _tex.height != _rgbMat.rows())
  68. {
  69. _tex = new Texture2D(_rgbMat.cols(), _rgbMat.rows(), TextureFormat.RGB24, false);
  70. }
  71. _meshDataMatDic.Remove(_currMeshData);
  72. DrawModel(_currMeshData);
  73. CurrMeshData = null;
  74. ReadMeshData();
  75. }
  76. }
  77. }
  78. private void Awake()
  79. {
  80. _server = Server.Instance;
  81. _meshFrontFilter = AppConfig.isNewMeshData ? meshFrontPointNew.AddComponent<MeshFilter>() : meshFrontPoint.AddComponent<MeshFilter>();
  82. _meshFrontRender = AppConfig.isNewMeshData ? meshFrontPointNew.AddComponent<MeshRenderer>() : meshFrontPoint.AddComponent<MeshRenderer>();
  83. _meshFrontRender.material = meshFrontMaterial;
  84. _meshSideFilter = AppConfig.isNewMeshData ? meshSidePointNew.AddComponent<MeshFilter>() : meshSidePoint.AddComponent<MeshFilter>();
  85. _meshSideRender = AppConfig.isNewMeshData ? meshSidePointNew.AddComponent<MeshRenderer>() : meshSidePoint.AddComponent<MeshRenderer>();
  86. _meshSideRender.material = meshSideMaterial;
  87. _currMeshData = null;
  88. Net.NetDistribute.Instance.drawMeshAction += DrawMeshHandler;
  89. Net.NetDistribute.Instance.OnConnectAction += ConnectSuccess;
  90. if (AppConfig.isNewMeshData)
  91. {
  92. //connectType = ConnectType.TCPServer;
  93. }
  94. }
  95. bool isConnected;
  96. private void OnDestroy()
  97. {
  98. _server.Close();
  99. Net.NetDistribute.Instance.OnConnectAction -= ConnectSuccess;
  100. Net.NetDistribute.Instance.drawMeshAction -= DrawMeshHandler;
  101. }
  102. private byte[] PackMessage(NetMessage message)
  103. {
  104. byte[] package = null;
  105. using (MemoryStream ms = new MemoryStream())
  106. {
  107. ProtoBuf.Serializer.Serialize(ms, message);
  108. package = new byte[ms.Length];
  109. Buffer.BlockCopy(ms.GetBuffer(), 0, package, 0, (int)ms.Length);
  110. }
  111. return package;
  112. }
  113. void Start()
  114. {
  115. renderThreadCoroutine = CallAtEndOfFrames();
  116. _gotMeshData = false;
  117. Texture2D texture2D = Resources.Load("uvMap") as Texture2D;
  118. Mat imgMat = new Mat(texture2D.height, texture2D.width, CvType.CV_8UC4);
  119. Utils.texture2DToMat(texture2D, imgMat);
  120. ConectServerHandler();
  121. }
  122. private void ConectServerHandler()
  123. {
  124. // _server.Init("127.0.0.1", 5555);
  125. _server.Init("192.168.50.17", 6666);
  126. _server.Connect();
  127. }
  128. private void ConnectSuccess(bool result, string reason)
  129. {
  130. if (result)
  131. {
  132. isConnected = true;
  133. }
  134. }
  135. /// <summary>
  136. /// Calls at end of frames.
  137. /// </summary>
  138. private IEnumerator CallAtEndOfFrames()
  139. {
  140. while (true)
  141. {
  142. // Wait until all frame rendering is done
  143. yield return new WaitForEndOfFrame();
  144. if (_rgbMat != null && _tex != null)
  145. {
  146. Utils.matToTextureInRenderThread(_rgbMat, _tex);
  147. }
  148. }
  149. }
  150. private float imageDecodeTime;
  151. private void DrawMeshHandler(NetMeshData meshData, Mat rgbMat, int totalByte,float imageDecodeTime,int lostCount)
  152. {
  153. this.imageDecodeTime = imageDecodeTime;
  154. //this._rgbMat = rgbMat;
  155. // if (meshQueue.Count == 0)
  156. {
  157. meshQueue.Enqueue(meshData);
  158. _meshDataMatDic.Add(meshData, rgbMat);
  159. if (CurrMeshData == null)
  160. {
  161. _gotMeshData = true;
  162. }
  163. }
  164. }
  165. private void ReadMeshData()
  166. {
  167. if (CurrMeshData == null)
  168. {
  169. if (meshQueue.Count > 0)
  170. {
  171. CurrMeshData = meshQueue.Dequeue();
  172. }
  173. }
  174. }
  175. void Update()
  176. {
  177. if (_gotMeshData)
  178. {
  179. ReadMeshData();
  180. }
  181. }
  182. void DrawModel(NetMeshData meshData, bool isFront = true)
  183. {
  184. //Debug.LogWarning("DrawModel:"+ meshData.uvList.Length);
  185. if (_meshFrontFilter.sharedMesh != null)
  186. {
  187. _meshFrontFilter.sharedMesh.Clear();
  188. }
  189. _meshFrontFilter.sharedMesh = DrawMesh1(meshData);
  190. Utils.fastMatToTexture2D(_rgbMat, _tex);
  191. meshFrontMaterial.mainTexture = _tex;
  192. _meshFrontFilter.sharedMesh.uv = meshData.uvList;//uvArray;// meshData.uvList;
  193. _meshFrontFilter.sharedMesh.triangles = meshData.Triangles1;// triangles;// meshData.Triangles1;
  194. _meshFrontFilter.sharedMesh.colors = null;
  195. if (_meshSideFilter.sharedMesh != null)
  196. {
  197. _meshSideFilter.sharedMesh.Clear();
  198. }
  199. _meshSideFilter.sharedMesh = DrawMesh2(meshData);
  200. _meshSideFilter.sharedMesh.triangles = meshData.Triangles2;
  201. _meshSideFilter.sharedMesh.colors = meshData.colorList;
  202. }
  203. Mesh mesh1;
  204. Mesh mesh1Child;
  205. Mesh mesh2;
  206. Mesh DrawMesh1(NetMeshData meshData)
  207. {
  208. if (mesh1 == null)
  209. {
  210. mesh1 = new Mesh();
  211. mesh1.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
  212. }
  213. mesh1.Clear();
  214. mesh1.vertices = meshData.frontVertexList;// meshData.frontVertexList;// newVertex;
  215. return mesh1;
  216. }
  217. Mesh DrawMesh2(NetMeshData meshData, bool isFront = true)
  218. {
  219. if (mesh2 == null)
  220. {
  221. mesh2 = new Mesh();
  222. }
  223. mesh2.Clear();
  224. mesh2.vertices = meshData.sideVertexList;// newVertex;
  225. return mesh2;
  226. }
  227. public void SaveConfig(string Path, string information)
  228. {
  229. FileStream aFile = new FileStream(@"" + Path, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite);
  230. StreamWriter sw = new StreamWriter(aFile, Encoding.UTF8);
  231. sw.WriteLine(information);
  232. sw.Close();
  233. sw.Dispose();
  234. }
  235. }
  236. }