123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using CScript.Utilities;
- using CScript.Net;
- using CScript.Entity;
- using System;
- using System.IO;
- using UnityEngine.UI;
- using OpenCVForUnity.UnityUtils;
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.ImgprocModule;
- using OpenCVForUnity.ImgcodecsModule;
- using SC.XR.Unity;
- using System.Text;
- namespace CScript.App
- {
- public enum ConnectType
- {
- None = 0,
- TCPServer = 1,
- Quic =2,
- }
- public class AppManager : MonoSingleton<AppManager>
- {
- Server _server;
- public ConnectType connectType = ConnectType.None;
- public Queue<NetMeshData> meshQueue = new Queue<NetMeshData>();
- private Dictionary<NetMeshData, Mat> _meshDataMatDic = new Dictionary<NetMeshData, Mat>();
- public GameObject MeshPoint;
- public Material meshFrontMaterial;
- public Material meshSideMaterial;
- public GameObject meshFrontPoint;
- public GameObject meshSidePoint;
- public GameObject meshFrontPointNew;
- public GameObject meshSidePointNew;
- public bool isRunHolographic;
- private Renderer _meshFrontRender;
- private MeshFilter _meshFrontFilter;
- private Renderer __meshFrontRenderChild;
- private MeshFilter _meshFrontFilterChild;
- private Renderer _meshSideRender;
- private MeshFilter _meshSideFilter;
- private NetMeshData _currMeshData;
- private Queue<byte[]> _meshDatas;
- private string configPath = "/Config/ConfigData.json";
- ConfigData configData;
- [SerializeField]
- private Transform meshPoint;
- IEnumerator renderThreadCoroutine;
- Mat _rgbMat;
- public Texture2D _tex;
- private bool _gotMeshData = false;
- public NetMeshData CurrMeshData
- {
- get
- {
- return _currMeshData;
- }
- private set
- {
- _currMeshData = value;
- if (_currMeshData != null)
- {
- _rgbMat = _meshDataMatDic[_currMeshData];
- // Imgcodecs.imwrite(Application.dataPath+"xxx.jpg", _rgbMat);
- if (_tex == null || _tex.width != _rgbMat.cols() || _tex.height != _rgbMat.rows())
- {
- _tex = new Texture2D(_rgbMat.cols(), _rgbMat.rows(), TextureFormat.RGB24, false);
- }
- _meshDataMatDic.Remove(_currMeshData);
- DrawModel(_currMeshData);
- CurrMeshData = null;
- ReadMeshData();
- }
- }
- }
- private void Awake()
- {
-
- _server = Server.Instance;
- _meshFrontFilter = AppConfig.isNewMeshData ? meshFrontPointNew.AddComponent<MeshFilter>() : meshFrontPoint.AddComponent<MeshFilter>();
- _meshFrontRender = AppConfig.isNewMeshData ? meshFrontPointNew.AddComponent<MeshRenderer>() : meshFrontPoint.AddComponent<MeshRenderer>();
- _meshFrontRender.material = meshFrontMaterial;
- _meshSideFilter = AppConfig.isNewMeshData ? meshSidePointNew.AddComponent<MeshFilter>() : meshSidePoint.AddComponent<MeshFilter>();
- _meshSideRender = AppConfig.isNewMeshData ? meshSidePointNew.AddComponent<MeshRenderer>() : meshSidePoint.AddComponent<MeshRenderer>();
- _meshSideRender.material = meshSideMaterial;
- _currMeshData = null;
- Net.NetDistribute.Instance.drawMeshAction += DrawMeshHandler;
- Net.NetDistribute.Instance.OnConnectAction += ConnectSuccess;
- if (AppConfig.isNewMeshData)
- {
- //connectType = ConnectType.TCPServer;
- }
- }
- bool isConnected;
- private void OnDestroy()
- {
- _server.Close();
- Net.NetDistribute.Instance.OnConnectAction -= ConnectSuccess;
- Net.NetDistribute.Instance.drawMeshAction -= DrawMeshHandler;
- }
- private byte[] PackMessage(NetMessage message)
- {
- byte[] package = null;
- using (MemoryStream ms = new MemoryStream())
- {
- ProtoBuf.Serializer.Serialize(ms, message);
- package = new byte[ms.Length];
- Buffer.BlockCopy(ms.GetBuffer(), 0, package, 0, (int)ms.Length);
- }
- return package;
- }
- void Start()
- {
- renderThreadCoroutine = CallAtEndOfFrames();
- _gotMeshData = false;
- Texture2D texture2D = Resources.Load("uvMap") as Texture2D;
- Mat imgMat = new Mat(texture2D.height, texture2D.width, CvType.CV_8UC4);
- Utils.texture2DToMat(texture2D, imgMat);
- ConectServerHandler();
- }
- private void ConectServerHandler()
- {
- // _server.Init("127.0.0.1", 5555);
- _server.Init("192.168.50.17", 6666);
- _server.Connect();
- }
- private void ConnectSuccess(bool result, string reason)
- {
- if (result)
- {
- isConnected = true;
- }
- }
- /// <summary>
- /// Calls at end of frames.
- /// </summary>
- private IEnumerator CallAtEndOfFrames()
- {
- while (true)
- {
- // Wait until all frame rendering is done
- yield return new WaitForEndOfFrame();
- if (_rgbMat != null && _tex != null)
- {
- Utils.matToTextureInRenderThread(_rgbMat, _tex);
- }
- }
- }
- private float imageDecodeTime;
- private void DrawMeshHandler(NetMeshData meshData, Mat rgbMat, int totalByte,float imageDecodeTime,int lostCount)
- {
- this.imageDecodeTime = imageDecodeTime;
- //this._rgbMat = rgbMat;
- // if (meshQueue.Count == 0)
- {
- meshQueue.Enqueue(meshData);
- _meshDataMatDic.Add(meshData, rgbMat);
- if (CurrMeshData == null)
- {
- _gotMeshData = true;
- }
- }
- }
- private void ReadMeshData()
- {
- if (CurrMeshData == null)
- {
- if (meshQueue.Count > 0)
- {
- CurrMeshData = meshQueue.Dequeue();
- }
- }
- }
- void Update()
- {
- if (_gotMeshData)
- {
- ReadMeshData();
- }
- }
- void DrawModel(NetMeshData meshData, bool isFront = true)
- {
- //Debug.LogWarning("DrawModel:"+ meshData.uvList.Length);
- if (_meshFrontFilter.sharedMesh != null)
- {
- _meshFrontFilter.sharedMesh.Clear();
- }
- _meshFrontFilter.sharedMesh = DrawMesh1(meshData);
- Utils.fastMatToTexture2D(_rgbMat, _tex);
- meshFrontMaterial.mainTexture = _tex;
- _meshFrontFilter.sharedMesh.uv = meshData.uvList;//uvArray;// meshData.uvList;
- _meshFrontFilter.sharedMesh.triangles = meshData.Triangles1;// triangles;// meshData.Triangles1;
- _meshFrontFilter.sharedMesh.colors = null;
- if (_meshSideFilter.sharedMesh != null)
- {
- _meshSideFilter.sharedMesh.Clear();
- }
- _meshSideFilter.sharedMesh = DrawMesh2(meshData);
- _meshSideFilter.sharedMesh.triangles = meshData.Triangles2;
- _meshSideFilter.sharedMesh.colors = meshData.colorList;
- }
- Mesh mesh1;
- Mesh mesh1Child;
- Mesh mesh2;
- Mesh DrawMesh1(NetMeshData meshData)
- {
- if (mesh1 == null)
- {
- mesh1 = new Mesh();
- mesh1.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
- }
- mesh1.Clear();
- mesh1.vertices = meshData.frontVertexList;// meshData.frontVertexList;// newVertex;
- return mesh1;
- }
- Mesh DrawMesh2(NetMeshData meshData, bool isFront = true)
- {
- if (mesh2 == null)
- {
- mesh2 = new Mesh();
- }
- mesh2.Clear();
- mesh2.vertices = meshData.sideVertexList;// newVertex;
- return mesh2;
- }
- public void SaveConfig(string Path, string information)
- {
- FileStream aFile = new FileStream(@"" + Path, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite);
- StreamWriter sw = new StreamWriter(aFile, Encoding.UTF8);
- sw.WriteLine(information);
- sw.Close();
- sw.Dispose();
- }
- }
- }
|