CloudPoint.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. public class CloudPoint : MonoBehaviour
  7. {
  8. // Start is called before the first frame update
  9. private GameObject pointCloud;
  10. private List<GameObject> headAvatarList;
  11. private List<MeshFilter> headMeshFilterList;
  12. public Material matVertex;
  13. public VyncCelect _VyncCloud;
  14. public int offsetZ=2;
  15. public float headPointSize=0;//0.005
  16. public float bodyPointSize=0;//0.01f
  17. public TextMesh _fpsText;
  18. public TextMesh _SizeText;
  19. public string ip;
  20. // public WebRtcManager _WebRtcManager;
  21. void Start()
  22. {
  23. headAvatarList=new List<GameObject>();
  24. headMeshFilterList=new List<MeshFilter>();
  25. pointCloud = new GameObject ("avatar");
  26. pointCloud.transform.position = new Vector3(-1.063f, -0.427f, -1.688f);
  27. pointCloud.transform.parent = transform;
  28. _VyncCloud = VyncCelect.GetInstance();
  29. _VyncCloud.HOST = ip;
  30. _VyncCloud.StartThread();
  31. _fpsText.gameObject.SetActive(false);
  32. _SizeText.gameObject.SetActive(false);
  33. StartCoroutine("renderCloud");
  34. }
  35. void OnDestroy()
  36. {
  37. Debug.Log("OnDestroy");
  38. StopCoroutine("renderCloud");
  39. if(_VyncCloud!=null)
  40. _VyncCloud.destory();
  41. // _WebRtcManager.leaveRoom();
  42. }
  43. private bool isLost;
  44. public void clear(bool vis)
  45. {
  46. if(!isLost&&!vis||vis==isLost)
  47. return;
  48. isLost = vis;
  49. pointCloud.SetActive(vis);
  50. }
  51. private bool init=false;
  52. private void Update()
  53. {
  54. _VyncCloud.offsetZ = offsetZ;
  55. _VyncCloud.headPointSize = headPointSize;
  56. _VyncCloud.bodyPointSize = bodyPointSize;
  57. _fpsText.text = _VyncCloud.timeDelay.ToString()+"ms Fps:"+1000/_VyncCloud.timeDelay;
  58. _SizeText.text ="SNCount:"+_VyncCloud._curDeviceSN_Array_NUM+ " curSN:" + _VyncCloud._curDeviceSN_NUM;
  59. }
  60. public void initSocket()
  61. {
  62. if (init)
  63. {
  64. // _WebRtcManager.MicrophoneMute();
  65. }
  66. if (init)
  67. {
  68. // _WebRtcManager.SpeakerMute();
  69. }
  70. if (init)
  71. {
  72. // _WebRtcManager.ToggleMic1();
  73. }
  74. if (!init)
  75. {
  76. init = true;
  77. #if UNITY_ANDROID
  78. // _WebRtcManager.initSocket(ip);
  79. // Invoke("initUser",2);
  80. #endif
  81. }
  82. }
  83. public void initUser()
  84. {
  85. return;
  86. #if UNITY_ANDROID
  87. // _WebRtcManager.loginUser(_VyncCloud._curDeviceSN_NUM);
  88. Invoke("jionORcreateRoom",2);
  89. #endif
  90. }
  91. public void jionORcreateRoom()
  92. {
  93. #if UNITY_ANDROID
  94. // _WebRtcManager.createOrJoinRoom();
  95. #endif
  96. }
  97. IEnumerator renderCloud()
  98. {
  99. while (true)
  100. {
  101. if (!_VyncCloud.isUpdatae)
  102. {
  103. //Debug.Log("初始没完成 ");
  104. yield return null;
  105. continue;
  106. }
  107. if (headAvatarList.Count > _VyncCloud.headPointGroups)
  108. {
  109. for (int i=_VyncCloud.headPointGroups;i<headAvatarList.Count;i++)
  110. {
  111. Destroy(headAvatarList[i]);
  112. Destroy(headMeshFilterList[i]);
  113. }
  114. //Debug.Log("xjytest:headAvatarList: 第"+_VyncCloud.headPointGroups+"到"+(headAvatarList.Count-_VyncCloud.headPointGroups));
  115. headAvatarList.RemoveRange( _VyncCloud.headPointGroups,headAvatarList.Count-_VyncCloud.headPointGroups);
  116. headMeshFilterList.RemoveRange( _VyncCloud.headPointGroups,headMeshFilterList.Count-_VyncCloud.headPointGroups);
  117. }
  118. for (int i = 0; i < _VyncCloud.headPointGroups; i++)
  119. {
  120. CreateHeadPointObj(i);
  121. }
  122. yield return null;
  123. _VyncCloud.isUpdatae = false;
  124. }
  125. }
  126. void CreateHeadPointObj(int meshInd)
  127. {
  128. // Debug.Log("开始渲染 ");
  129. if (headAvatarList.Count>meshInd&&headAvatarList[meshInd] != null)
  130. {
  131. //Debug.Log("xjytest:loadhead_"+meshInd.ToString()+" headAvatarList:"+headAvatarList.Count);
  132. CreateMesh(meshInd, headMeshFilterList[meshInd].mesh);
  133. }
  134. else
  135. {
  136. // Debug.Log("xjytest:head_"+meshInd.ToString()+" headAvatarList:"+headAvatarList.Count);
  137. GameObject obj = new GameObject("head_" + meshInd.ToString());
  138. MeshFilter _MeshFilter = obj.AddComponent<MeshFilter>();
  139. MeshRenderer _MeshRenderer= obj.AddComponent<MeshRenderer>();
  140. headAvatarList.Add(obj);
  141. headMeshFilterList.Add(_MeshFilter);
  142. obj.transform.parent = pointCloud.transform;
  143. obj.transform.localPosition=new Vector3(0,0,0);
  144. _MeshFilter.mesh = CreateMesh(meshInd);
  145. _MeshRenderer.material = matVertex;
  146. _MeshRenderer.material.SetMatrix("_Transform", transform.localToWorldMatrix);
  147. }
  148. // matVertex.SetFloat("_Size", pointSize);
  149. }
  150. private int[] indecies;
  151. private Vector3[] myPoints;
  152. private Color[] myColors;
  153. private int indexnum = 0;
  154. Mesh CreateMesh(int id,Mesh _mesh = null)
  155. {
  156. indexnum++;
  157. if (_mesh == null)
  158. {
  159. _mesh = new Mesh();
  160. indecies = new int[_VyncCloud.limitPoints];
  161. for (int i = 0; i < _VyncCloud.limitPoints; ++i)
  162. {
  163. indecies[i] = i;
  164. }
  165. //WriteIntoTxt(_VyncCloud.fileStr);
  166. }
  167. // Debug.Log("xjytest:_VyncCloud.headpostionList:"+_VyncCloud.headpostionList.Count+"_VyncCloud.headcolorList[id]:"+_VyncCloud.headcolorList.Count+" xjytest id: "+id+" meshid:"+_mesh.name+indexnum);
  168. _mesh.vertices =_VyncCloud.headpostionList[id];
  169. _mesh.colors = _VyncCloud.headcolorList[id];
  170. _mesh.SetIndices(indecies, MeshTopology.Triangles, 0);
  171. return _mesh;
  172. }
  173. }