123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class CloudPoint : MonoBehaviour
- {
- // Start is called before the first frame update
- private GameObject pointCloud;
- private List<GameObject> headAvatarList;
- private List<MeshFilter> headMeshFilterList;
- public Material matVertex;
- public VyncCelect _VyncCloud;
- public int offsetZ=2;
- public float headPointSize=0;//0.005
- public float bodyPointSize=0;//0.01f
- public TextMesh _fpsText;
- public TextMesh _SizeText;
-
- public string ip;
- // public WebRtcManager _WebRtcManager;
- void Start()
- {
- headAvatarList=new List<GameObject>();
- headMeshFilterList=new List<MeshFilter>();
- pointCloud = new GameObject ("avatar");
- pointCloud.transform.position = new Vector3(-1.063f, -0.427f, -1.688f);
- pointCloud.transform.parent = transform;
- _VyncCloud = VyncCelect.GetInstance();
-
- _VyncCloud.HOST = ip;
- _VyncCloud.StartThread();
- _fpsText.gameObject.SetActive(false);
- _SizeText.gameObject.SetActive(false);
- StartCoroutine("renderCloud");
- }
-
- void OnDestroy()
- {
- Debug.Log("OnDestroy");
- StopCoroutine("renderCloud");
- if(_VyncCloud!=null)
- _VyncCloud.destory();
- // _WebRtcManager.leaveRoom();
- }
- private bool isLost;
- public void clear(bool vis)
- {
- if(!isLost&&!vis||vis==isLost)
- return;
- isLost = vis;
- pointCloud.SetActive(vis);
- }
- private bool init=false;
- private void Update()
- {
-
- _VyncCloud.offsetZ = offsetZ;
- _VyncCloud.headPointSize = headPointSize;
- _VyncCloud.bodyPointSize = bodyPointSize;
- _fpsText.text = _VyncCloud.timeDelay.ToString()+"ms Fps:"+1000/_VyncCloud.timeDelay;
- _SizeText.text ="SNCount:"+_VyncCloud._curDeviceSN_Array_NUM+ " curSN:" + _VyncCloud._curDeviceSN_NUM;
- }
- public void initSocket()
- {
- if (init)
- {
- // _WebRtcManager.MicrophoneMute();
- }
- if (init)
- {
- // _WebRtcManager.SpeakerMute();
- }
- if (init)
- {
- // _WebRtcManager.ToggleMic1();
- }
- if (!init)
- {
- init = true;
- #if UNITY_ANDROID
- // _WebRtcManager.initSocket(ip);
- // Invoke("initUser",2);
- #endif
- }
-
- }
-
- public void initUser()
- {
- return;
- #if UNITY_ANDROID
- // _WebRtcManager.loginUser(_VyncCloud._curDeviceSN_NUM);
- Invoke("jionORcreateRoom",2);
- #endif
- }
-
- public void jionORcreateRoom()
- {
- #if UNITY_ANDROID
- // _WebRtcManager.createOrJoinRoom();
- #endif
- }
- IEnumerator renderCloud()
- {
- while (true)
- {
-
- if (!_VyncCloud.isUpdatae)
- {
- //Debug.Log("初始没完成 ");
- yield return null;
- continue;
- }
- if (headAvatarList.Count > _VyncCloud.headPointGroups)
- {
- for (int i=_VyncCloud.headPointGroups;i<headAvatarList.Count;i++)
- {
- Destroy(headAvatarList[i]);
- Destroy(headMeshFilterList[i]);
- }
- //Debug.Log("xjytest:headAvatarList: 第"+_VyncCloud.headPointGroups+"到"+(headAvatarList.Count-_VyncCloud.headPointGroups));
- headAvatarList.RemoveRange( _VyncCloud.headPointGroups,headAvatarList.Count-_VyncCloud.headPointGroups);
- headMeshFilterList.RemoveRange( _VyncCloud.headPointGroups,headMeshFilterList.Count-_VyncCloud.headPointGroups);
- }
-
- for (int i = 0; i < _VyncCloud.headPointGroups; i++)
- {
- CreateHeadPointObj(i);
- }
- yield return null;
- _VyncCloud.isUpdatae = false;
- }
- }
- void CreateHeadPointObj(int meshInd)
- {
- // Debug.Log("开始渲染 ");
- if (headAvatarList.Count>meshInd&&headAvatarList[meshInd] != null)
- {
- //Debug.Log("xjytest:loadhead_"+meshInd.ToString()+" headAvatarList:"+headAvatarList.Count);
- CreateMesh(meshInd, headMeshFilterList[meshInd].mesh);
- }
- else
- {
- // Debug.Log("xjytest:head_"+meshInd.ToString()+" headAvatarList:"+headAvatarList.Count);
- GameObject obj = new GameObject("head_" + meshInd.ToString());
- MeshFilter _MeshFilter = obj.AddComponent<MeshFilter>();
- MeshRenderer _MeshRenderer= obj.AddComponent<MeshRenderer>();
- headAvatarList.Add(obj);
- headMeshFilterList.Add(_MeshFilter);
- obj.transform.parent = pointCloud.transform;
- obj.transform.localPosition=new Vector3(0,0,0);
- _MeshFilter.mesh = CreateMesh(meshInd);
- _MeshRenderer.material = matVertex;
- _MeshRenderer.material.SetMatrix("_Transform", transform.localToWorldMatrix);
- }
- // matVertex.SetFloat("_Size", pointSize);
- }
-
- private int[] indecies;
- private Vector3[] myPoints;
- private Color[] myColors;
- private int indexnum = 0;
- Mesh CreateMesh(int id,Mesh _mesh = null)
- {
- indexnum++;
- if (_mesh == null)
- {
- _mesh = new Mesh();
- indecies = new int[_VyncCloud.limitPoints];
-
- for (int i = 0; i < _VyncCloud.limitPoints; ++i)
- {
- indecies[i] = i;
- }
- //WriteIntoTxt(_VyncCloud.fileStr);
- }
- // Debug.Log("xjytest:_VyncCloud.headpostionList:"+_VyncCloud.headpostionList.Count+"_VyncCloud.headcolorList[id]:"+_VyncCloud.headcolorList.Count+" xjytest id: "+id+" meshid:"+_mesh.name+indexnum);
-
- _mesh.vertices =_VyncCloud.headpostionList[id];
- _mesh.colors = _VyncCloud.headcolorList[id];
-
- _mesh.SetIndices(indecies, MeshTopology.Triangles, 0);
- return _mesh;
- }
-
- }
|