using System.Collections; using System.Collections.Generic; using UnityEngine; using XFramework; public class VRPersonSyncHandlerCtrl : MonoBehaviour, ISyncHand { private Transform headTrans; private Transform leftHandTrans; private Transform rightHandTrans; float interval;//帧同步数据正常情况下应该进入的时间间隔 bool lerpTrans;//是否开启lerp同步 bool lerping = false;//当前数据是否需要执行Lerp float lerpTimer;//开启Lerp时的计时器 用于计算每帧的位置 //Lerp 时记录接到数据时当前同步物体的所有位置和角度 TransGesture currentHead; TransGesture currentLH; TransGesture currentRH; Text3DCtrl Text3DCtrl_showName; //接收到的同步数据 PersonSyncData personSyncData; [SerializeField] string soleName; // Start is called before the first frame update void Awake() { headTrans = this.transform.FindChildTrans("Head"); leftHandTrans = this.transform.FindChildTrans("LeftHand"); rightHandTrans = this.transform.FindChildTrans("RightHand"); Text3DCtrl_showName = this.GetComponentInChildren(); } /// /// 设置学员信息 确定同步时的唯一名称和显示的名字 /// 加入同步处理队列 /// /// /// public void SetUser(string user, string ip) { Text3DCtrl_showName.SetName(user); soleName = $"VRPerson_{ip}";//名称创建时根据IP自动获取 JoinHandList(); } public string GetSoleName() { return soleName; } public void HandleSyncData(object msg, string msgType = null) { //if (msgType == "Trans")//位置同步处理 人物只有位置同步需要处理 无需使用消息标签 //{ personSyncData = (PersonSyncData)msg; CheckDataForActive(); if (!lerpTrans)//未开启Lerp 模式时直接同步 { SyncData(); } else//重置计时器 记录当前位置 等待FixedUpdate Lerp { MarkCurrentPos(); lerpTimer = 0; lerping = true; } //} } //检查数据 显示隐藏无效数据对应的部位 private void CheckDataForActive() { if (personSyncData.head.GetPos() == Vector3.zero && headTrans.gameObject.activeSelf)//无有效同步数据时 隐藏物体 { headTrans.gameObject.SetActive(false); } else if (personSyncData.head.GetPos() != Vector3.zero && !headTrans.gameObject.activeSelf) { headTrans.gameObject.SetActive(true); } if (personSyncData.LH.GetPos() == Vector3.zero && leftHandTrans.gameObject.activeSelf)//无有效同步数据时 隐藏物体 { leftHandTrans.gameObject.SetActive(false); } else if (personSyncData.LH.GetPos() != Vector3.zero && !leftHandTrans.gameObject.activeSelf) { leftHandTrans.gameObject.SetActive(true); } if (personSyncData.RH.GetPos() == Vector3.zero && rightHandTrans.gameObject.activeSelf)//无有效同步数据时 隐藏物体 { rightHandTrans.gameObject.SetActive(false); } else if (personSyncData.RH.GetPos() != Vector3.zero && !rightHandTrans.gameObject.activeSelf) { rightHandTrans.gameObject.SetActive(true); } } //直接同步位置 private void SyncData() { headTrans.transform.localPosition = personSyncData.head.GetPos(); headTrans.transform.eulerAngles = personSyncData.head.GetRotAnlgle(); leftHandTrans.transform.localPosition = personSyncData.LH.GetPos(); leftHandTrans.transform.eulerAngles = personSyncData.LH.GetRotAnlgle(); rightHandTrans.transform.localPosition = personSyncData.RH.GetPos(); rightHandTrans.transform.eulerAngles = personSyncData.RH.GetRotAnlgle(); } //Lerp同步位置 private void LerpSyncData(float progress) { //headTrans.transform.localPosition = Vector3.Lerp(currentHead.GetPos(), personSyncData.head.GetPos(),progress); //headTrans.transform.eulerAngles = Vector3.Lerp(currentHead.GetRotAnlgle(), personSyncData.head.GetRotAnlgle(), progress); //leftHandTrans.transform.localPosition = Vector3.Lerp(currentLH.GetPos(), personSyncData.LH.GetPos(), progress); //leftHandTrans.transform.eulerAngles = Vector3.Lerp(currentLH.GetRotAnlgle(), personSyncData.LH.GetRotAnlgle(),progress); //rightHandTrans.transform.localPosition = Vector3.Lerp(currentRH.GetPos(),personSyncData.RH.GetPos(),progress); //rightHandTrans.transform.eulerAngles = Vector3.Lerp(currentRH.GetRotAnlgle(), personSyncData.RH.GetRotAnlgle(), progress) ; LerpOneTrans(headTrans, currentHead, personSyncData.head, progress); LerpOneTrans(leftHandTrans, currentLH, personSyncData.LH, progress); LerpOneTrans(rightHandTrans, currentRH, personSyncData.RH, progress); } private void LerpOneTrans( Transform target,TransGesture a,TransGesture b,float progress) { target.localPosition = Vector3.Lerp(a.GetPos(), b.GetPos(), progress); target.transform.localRotation =Quaternion.Lerp(Quaternion.Euler(a.GetRotAnlgle()), Quaternion.Euler(b.GetRotAnlgle()), progress); } public void JoinHandList() { SyncGOManager.Instance.AddHandler(this); } public void ExitHandList() { SyncGOManager.Instance.RemoHandler(this); } private void FixedUpdate() { if (lerpTrans&& lerping) { if (lerpTimer <= interval) { lerpTimer += Time.fixedUnscaledDeltaTime; LerpSyncData(lerpTimer / interval); } else//Lerp完成了 { lerping = false; } } } //记录当前位置 private void MarkCurrentPos() { currentHead = new TransGesture(headTrans.localPosition, headTrans.localEulerAngles); currentLH = new TransGesture(leftHandTrans.localPosition, leftHandTrans.localEulerAngles); currentRH = new TransGesture(rightHandTrans.localPosition, rightHandTrans.localEulerAngles); } public void InitInterval(float time) { interval = time; if (interval > 0.04f) lerpTrans = true; } }