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 { Server _server; public ConnectType connectType = ConnectType.None; public Queue meshQueue = new Queue(); private Dictionary _meshDataMatDic = new Dictionary(); 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 _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() : meshFrontPoint.AddComponent(); _meshFrontRender = AppConfig.isNewMeshData ? meshFrontPointNew.AddComponent() : meshFrontPoint.AddComponent(); _meshFrontRender.material = meshFrontMaterial; _meshSideFilter = AppConfig.isNewMeshData ? meshSidePointNew.AddComponent() : meshSidePoint.AddComponent(); _meshSideRender = AppConfig.isNewMeshData ? meshSidePointNew.AddComponent() : meshSidePoint.AddComponent(); _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; } } /// /// Calls at end of frames. /// 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(); } } }