/// <summary> /// 同步动画 /// </summary> /// <param name="type">类型</param> /// <param name="objId">对象id, 数字人时穿null</param> /// <param name="animId">动画id</param> public void SyncAnimation(SyncType type, string objId, int animId) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SyncAnim syncAnim = new SyncAnim(); syncAnim.type = type; if (type == SyncType.VirtualMan) { syncAnim.id = this.uid; } else { syncAnim.id = objId; } syncAnim.amimid = animId; syncAnim.time = TimeHelper.GetTimestamp(); byte[] cmd = EncodeStruct <SyncAnim>(syncAnim); SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncAnim, cmd); }
/// <summary> /// Syncs the rotation. /// </summary> /// <returns>The rotation.</returns> /// <param name="targetId">Target identifier.</param> /// <param name="type">Type.</param> /// <param name="objId">Object identifier.</param> /// <param name="rotation">Rotation.</param> public void SyncRotation(string targetId, SyncType type, string objId, Vector3 rotation) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SyncRotate headRotate = new SyncRotate(); headRotate.type = type; headRotate.id = objId; headRotate.rx = rotation.x; headRotate.ry = rotation.y; headRotate.rz = rotation.z; headRotate.time = TimeHelper.GetTimestamp(); //Debug.Log("SyncRotation type:" + type + "objid:" + objId + " rot:" + rotation + " time:" + headRotate.time); if (_lastRotTime >= headRotate.time) { Debug.Log("SyncRotation 顺序错误 cur:" + headRotate.time + " last:" + _lastRotTime); } _lastRotTime = headRotate.time; byte[] cmd = EncodeStruct <SyncRotate>(headRotate); if (targetId == null) { SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncRotate, cmd); } else { SendCmdMessage(mqttHelper, targetId, NetCmdIdClient.SyncRotate, cmd); } }
/// <summary> /// 同步位置 /// </summary> /// <param name="targetId">目标id</param> /// <param name="type">同步类型</param> /// <param name="objId">对象id</param> /// <param name="position">位置</param> public void SyncPosition(string targetId, SyncType type, string objId, Vector3 position) { string rid; MqttHelper mqttHelper = ChooseMqtt(type, out rid); if (mqttHelper == null) { return; } SyncPos headPos = new SyncPos(); headPos.type = type; headPos.id = objId; headPos.px = position.x; headPos.py = position.y; headPos.pz = position.z; headPos.time = TimeHelper.GetTimestamp(); //Debug.Log("SyncPosition type:" + type + "objid:" + objId + " pos:" + position + " time:" + headPos.time); if (_lastPosTime >= headPos.time) { //Debug.Log("SyncPosition 顺序错误 cur:" + headPos.time + " last:" + _lastPosTime); } _lastPosTime = headPos.time; byte[] cmd = EncodeStruct <SyncPos>(headPos); if (targetId == null) { SendCmdMessage(mqttHelper, rid, NetCmdIdClient.SyncPos, cmd); } else { SendCmdMessage(mqttHelper, targetId, NetCmdIdClient.SyncPos, cmd); } }
public void TestAnim() { Debug.Log("TestAnim"); NetHelper.Instance.TestJson(); String[] p = new String[1]; p[0] = "" + TimeHelper.GetTimestamp(); SyncInterface.Instance.SyncOtherCmd("Test", p); }
IEnumerator UploadAnchor() { if (state != SpectatorViewState.Init) { this.anchorName = "anchor_" + TimeHelper.GetTimestamp().ToString("f1"); WWWForm form = new WWWForm(); #if !UNITY_EDITOR && UNITY_WSA form.AddBinaryData("file", GameAnchorManager.Instance.AnchorData, this.anchorName); #else byte[] anchordata = new byte[10000]; for (int i = 0; i < 10000; i++) { anchordata[i] = 6; } form.AddBinaryData("file", new byte[10000], this.anchorName); #endif form.AddField("rid", roomId); string url = "http://" + SyncInterface.Instance.MrServerAddress + ":" + ServerUrls.HttpPort; UnityWebRequest request = UnityWebRequest.Post(url + "/upload", form); request.SetRequestHeader("rid", roomId); yield return(request.Send()); if (request.isNetworkError) { print("Error: " + request.error); } else { try { if (state == SpectatorViewState.AnchorUploading) { resp = JsonConvert.DeserializeObject <UploadAnchorResp>(request.downloadHandler.text); NetHelper.Instance.UploadAnchorDone(resp.File.downloadpath, MrShareData._instance.FloorY); SetStates(SpectatorViewState.AnchorPrepared); } else { Debug.Log("SV#UploadAnchor#Wrong State. Current State:" + state.ToString()); } } catch (Exception e) { print(e.ToString()); } print("Request Response: " + request.downloadHandler.text); } } }
void FixedUpdate() { if (IsLocolPlayer && clientType == ClientType.SpectatorViewHoloLens) { string uid = SpectatorViewManager._instance.getSpectatorViewPcId(); if (uid == null) { return; } double curTime = TimeHelper.GetTimestamp(); Vector3 curPos = mainCamTransform.position; bool isNeedSync = (_lastSyncPos.Equals(Vector3.zero) || Vector3.Distance(_lastPos, curPos) > needSyncDistance); // 发送位置 if ((curTime - _lastSyncPosTime) > needSyncTime || isNeedSync) { head.position = mainCamTransform.position; //同步相对于评审物体的坐标 Vector3 p = MrShareData._instance.RelativeTransform.InverseTransformPoint(head.position); if (Math.Abs(p.x) > 20 || Math.Abs(p.y) > 20 || Math.Abs(p.z) > 20) { return; } _lastSyncPosTime = curTime; _lastSyncPos = curPos; NetHelper.Instance.SyncPosition(uid, SyncType.VirtualMan, UserID, p); } Vector3 curRot = mainCamTransform.rotation.eulerAngles; isNeedSync = (_lastSyncRot.Equals(Vector3.zero) || Mathf.Abs(curRot.y - _lastSyncRot.y) > needSyncAngle); // 发送旋转 if ((curTime - _lastSyncRotTime) > needSyncTime || isNeedSync) { head.rotation = mainCamTransform.rotation; //同步相对于评审物体的旋转 Vector3 r = head.localEulerAngles; _lastSyncRotTime = curTime; _lastSyncRot = curRot; NetHelper.Instance.SyncRotation(uid, SyncType.VirtualMan, UserID, r); } } }
void Update() { if (!IsLocolPlayer) { // 更新位置 if (headPisitionQueue.Count() > 0) { // 包过多则丢包 if (headPisitionQueue.Count() > 60) { while (headPisitionQueue.Count() > 5) { headPisitionQueue.Dequeue(); } _lastUpdatePosTime = 0; } PosBean pr = headPisitionQueue.Peek(); while ((pr.time - _lastUpdatePosTime) < 0 && headPisitionQueue.Count() > 0) { Debug.Log("错误顺序 curTime:" + pr.time + " lastTime:" + _lastUpdatePosTime); headPisitionQueue.Dequeue(); pr = headPisitionQueue.Peek(); } if (_lastUpdatePosTime == 0) { _lastUpdatePosTime = pr.time - 0.04; } double curTime = TimeHelper.GetTimestamp(); if ((curTime - _lastUpdatePosLocalTime) > ((pr.time - _lastUpdatePosTime) - 10)) { // Debug.Log("VirtualManPlayer qeuelen:" + headPisitionQueue.Count() + " curTime:" + curTime + " prTime:" + pr.time // + " localLerp:" + (curTime - _lastUpdateTime) + " reomteLerp:" + (pr.time - _lastPosTime) + " curPos:" + head.position); pr = headPisitionQueue.Dequeue(); if (Math.Abs(FF(_lastPos.x) - FF(pr.pos.x)) < 0.2 && Math.Abs(FF(_lastPos.y) - FF(pr.pos.y)) < 0.2 && Math.Abs(FF(_lastPos.z) - FF(pr.pos.z)) < 0.2) { } else { iTween.MoveTo(gameObject, MrShareData._instance.RelativeTransform.TransformPoint(pr.pos), (float)(pr.time - _lastUpdatePosTime) / 1000); } _lastPos = pr.pos; _lastUpdatePosTime = pr.time; _lastUpdatePosLocalTime = curTime; } } // 更新姿态 if (headRotationQueue.Count() > 0) { // 包过多则丢包 if (headRotationQueue.Count() > 60) { while (headRotationQueue.Count() > 5) { headRotationQueue.Dequeue(); } _lastUpdateRotTime = 0; } RotBean r = headRotationQueue.Peek(); while ((r.time - _lastUpdateRotTime) < 0 && headRotationQueue.Count() > 0) { Debug.Log("错误顺序 curTime:" + r.time + " lastTime:" + _lastUpdateRotTime); headRotationQueue.Dequeue(); r = headRotationQueue.Peek(); } if (_lastUpdateRotTime == 0) { _lastUpdateRotTime = r.time - 0.04; } double curTime = TimeHelper.GetTimestamp(); if ((curTime - _lastUpdateRotLocalTime) > ((r.time - _lastUpdateRotTime) - 10)) { r = headRotationQueue.Dequeue(); if (FFR(_lastRot.x) == FFR(r.rot.x) && FFR(_lastRot.y) == FFR(r.rot.y) && FFR(_lastRot.z) == FFR(r.rot.z)) { } else { head.localEulerAngles = r.rot; } _lastRot = r.rot; _lastUpdateRotTime = r.time; _lastUpdateRotLocalTime = curTime; } } } }
void Update() { if (!IsLocolPlayer) { // 更新位置 if (headPisitionQueue.Count() > 0) { // 包过多则丢包 if (headPisitionQueue.Count() > 60) { while (headPisitionQueue.Count() > 5) { headPisitionQueue.Dequeue(); } _lastUpdatePosTime = 0; } PosBean pr = headPisitionQueue.Peek(); while ((pr.time - _lastUpdatePosTime) < 0 && headPisitionQueue.Count() > 0) { Debug.Log("错误顺序 curTime:" + pr.time + " lastTime:" + _lastUpdatePosTime); headPisitionQueue.Dequeue(); pr = headPisitionQueue.Peek(); } if (_lastUpdatePosTime == 0) { _lastUpdatePosTime = pr.time - 0.04; } double curTime = TimeHelper.GetTimestamp(); if ((curTime - _lastUpdatePosLocalTime) > ((pr.time - _lastUpdatePosTime) - 10)) { // Debug.Log("VirtualManPlayer qeuelen:" + headPisitionQueue.Count() + " curTime:" + curTime + " prTime:" + pr.time // + " localLerp:" + (curTime - _lastUpdateTime) + " reomteLerp:" + (pr.time - _lastPosTime) + " curPos:" + head.position); pr = headPisitionQueue.Dequeue(); Vector3 posHead = HoloWorldSync.Instance.WorldRoot.transform.TransformPoint(pr.pos);//MrShareData._instance.RelativeTransform.TransformPoint(pr.pos); #if !UNITY_EDITOR && UNITY_IPHONE posHead = new Vector3(posHead.x, MrShareData._instance.FloorY * 2, posHead.z); #else posHead = new Vector3(posHead.x, MrShareData._instance.FloorY, posHead.z); #endif iTween.MoveTo(head.gameObject, posHead, (float)(pr.time - _lastUpdatePosTime) / 1000); body.localEulerAngles = new Vector3(0, head.localEulerAngles.y, 0); #if !UNITY_EDITOR && UNITY_IPHONE body.localPosition = new Vector3(head.localPosition.x, MrShareData._instance.FloorY * 2, head.localPosition.z); #else body.localPosition = new Vector3(head.localPosition.x, MrShareData._instance.FloorY, head.localPosition.z); #endif // Debug.Log ("VirtualManPlayer position:" + head.position.ToString() + " worldposition:" + HoloWorldSync.Instance.WorldRoot.transform.position.ToString() + //" localScale:" + head.localScale.ToString() + " recvposition:" + pr.pos.ToString()); _lastPos = pr.pos; _lastUpdatePosTime = pr.time; _lastUpdatePosLocalTime = curTime; } } // 更新姿态 if (headRotationQueue.Count() > 0) { // 包过多则丢包 if (headRotationQueue.Count() > 60) { while (headRotationQueue.Count() > 5) { headRotationQueue.Dequeue(); } _lastUpdateRotTime = 0; } RotBean r = headRotationQueue.Peek(); while ((r.time - _lastUpdateRotTime) < 0 && headRotationQueue.Count() > 0) { Debug.Log("错误顺序 curTime:" + r.time + " lastTime:" + _lastUpdateRotTime); headRotationQueue.Dequeue(); r = headRotationQueue.Peek(); } if (_lastUpdateRotTime == 0) { _lastUpdateRotTime = r.time - 0.04; } double curTime = TimeHelper.GetTimestamp(); if ((curTime - _lastUpdateRotLocalTime) > ((r.time - _lastUpdateRotTime) - 10)) { r = headRotationQueue.Dequeue(); head.localEulerAngles = r.rot; body.localEulerAngles = new Vector3(0, head.localEulerAngles.y, 0); _lastUpdateRotTime = r.time; _lastUpdateRotLocalTime = curTime; //Debug.Log ("VirtualManPlayer localEulerAngles:" + head.localEulerAngles.ToString() // + " body.localEulerAngles:" + body.localEulerAngles.ToString() // + " recvrotation:" + r.rot.ToString()); } } // 更新动画 if (animQueue.Count() > 0) { AnimBean anim = animQueue.Dequeue(); VirtualManAnimatorChange(anim.animId); } } else { //走路动画 if (canCheck) { StartCoroutine(WalkCheck()); } } }
void FixedUpdate() { if (IsLocolPlayer) { double curTime = TimeHelper.GetTimestamp(); Vector3 curPos = mainCamTransform.position; bool isNeedSync = (_lastSyncPos.Equals(Vector3.zero) || Vector3.Distance(_lastPos, curPos) > needSyncDistance); // 发送位置 if ((curTime - _lastSyncPosTime) > needSyncTime || isNeedSync) { head.position = mainCamTransform.position; body.localPosition = new Vector3(head.localPosition.x, MrShareData._instance.FloorY, head.localPosition.z); //同步相对于评审物体的坐标 //Vector3 p = MrShareData._instance.RelativeTransform.InverseTransformPoint(head.position); Vector3 p = HoloWorldSync.Instance.WorldRoot.transform.InverseTransformPoint(head.position); if (Math.Abs(p.x) > 20 || Math.Abs(p.y) > 20 || Math.Abs(p.z) > 20) { return; } _lastSyncPosTime = curTime; _lastSyncPos = curPos; NetHelper.Instance.SyncPosition(SyncType.VirtualMan, UserID, p); } Vector3 curRot = mainCamTransform.rotation.eulerAngles; isNeedSync = (_lastSyncRot.Equals(Vector3.zero) || Mathf.Abs(curRot.y - _lastSyncRot.y) > needSyncAngle); // 发送旋转 if ((curTime - _lastSyncRotTime) > needSyncTime || isNeedSync) { head.rotation = mainCamTransform.rotation; body.localEulerAngles = new Vector3(0, head.localEulerAngles.y, 0); //同步相对于评审物体的旋转 Vector3 r = head.localEulerAngles; _lastSyncRotTime = curTime; _lastSyncRot = curRot; NetHelper.Instance.SyncRotation(SyncType.VirtualMan, UserID, r); } // 发送动画 int ani = -1; if (!isSit && MrShareData._instance.SourceDetected) { MrShareData._instance.SourceDetected = false; ani = (int)(ManAni.greet); print("send greet"); } else if (!isSit && head.transform.localPosition.y - MrShareData._instance.FloorY < 1.2f) { isSit = true; ani = (int)(ManAni.sit); print("send sit"); } else if (isSit && head.transform.localPosition.y - MrShareData._instance.FloorY > 1.35f) { isSit = false; ani = (int)(ManAni.sitstand); print("send sitstand"); } if (ani != -1) { NetHelper.Instance.SyncAnimation(SyncType.VirtualMan, UserID, ani); } } // 显示视线 if (!IsLocolPlayer) { RaycastHit hitInfo; if (Physics.Raycast(head.position, head.forward, out hitInfo, 10, 1 << 10)) { //Debug.DrawLine(head.localPosition, hitInfo.point,Color.red);//划出射线,只有在scene视图中才能看到 hitPoint = hitInfo.point; lr.enabled = true; UpadateLaser(); offsetX -= 0.05f; laserMat.SetTextureOffset("_MainTex", new Vector2(offsetX, 0)); } else { lr.enabled = false; } } }
public void Test(string time) { Debug.Log("VM#Test"); ShowLog._instance.Log("recv log time:" + time + " cur:" + TimeHelper.GetTimestamp() + ""); }
public void TestPosRot() { Debug.Log("TestPosRot"); string uid = "testposrotuserid_" + (int)ClientType.Hololens; if (posRotLooping) { posRotLooping = false; OperationBean op = new OperationBean(); op.op = OperationBean.OpId.UserLeave; op.param = uid; operationQueue.Enqueue(op); } else { posRotLooping = true; OperationBean op = new OperationBean(); op.op = OperationBean.OpId.UserEnter; op.param = uid; operationQueue.Enqueue(op); new Task(async() => { while (posRotLooping) { VirtualManPlayer player; PlayerUserList.TryGetValue(uid, out player); if (player) { if (testx > 5) { moveLeft = true; player.onRecvAnimation((int)VirtualManPlayer.ManAni.walk, TimeHelper.GetTimestamp()); player.onRecvAnimation((int)VirtualManPlayer.ManAni.sit, TimeHelper.GetTimestamp()); NetHelper.Instance.SyncAnimation(SyncType.VirtualMan, this.mineUid, (int)VirtualManPlayer.ManAni.walk); NetHelper.Instance.SyncAnimation(SyncType.VirtualMan, this.mineUid, (int)VirtualManPlayer.ManAni.sit); } if (testx < -5) { moveLeft = false; player.onRecvAnimation((int)VirtualManPlayer.ManAni.sitstand, TimeHelper.GetTimestamp()); player.onRecvAnimation((int)VirtualManPlayer.ManAni.greet, TimeHelper.GetTimestamp()); NetHelper.Instance.SyncAnimation(SyncType.VirtualMan, this.mineUid, (int)VirtualManPlayer.ManAni.sitstand); NetHelper.Instance.SyncAnimation(SyncType.VirtualMan, this.mineUid, (int)VirtualManPlayer.ManAni.sitstand); } if (moveLeft) { testx -= 0.1f; } else { testx += 0.1f; } player.OnRecvPosition(new Vector3(testx, 0, 0), TimeHelper.GetTimestamp()); player.OnRecvRotation(new Vector3(-1, 0, 0), TimeHelper.GetTimestamp()); NetHelper.Instance.SyncPosition(SyncType.VirtualMan, mineUid, new Vector3(testx, 0, 0)); NetHelper.Instance.SyncRotation(SyncType.VirtualMan, mineUid, new Vector3(1, 0, 0)); } await Task.Delay(40); } }).Start(); } }