SyncCameraTransform.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Netcode;
  5. using UnityEngine;
  6. namespace Rokid.MRC
  7. {
  8. //相机参数
  9. public struct CameraParam : INetworkSerializable
  10. {
  11. //相机长宽
  12. public Vector2 pixelXY;
  13. public Vector2 pixel;
  14. public float fov;
  15. public float focalLength;
  16. public float nearClipPlane;
  17. public float farClipPlane;
  18. public int frameRate;
  19. public int bitRate;
  20. public CameraParam(Camera cam, int frameRate, int bitRate)
  21. {
  22. pixelXY = new Vector2(cam.pixelRect.x, cam.pixelRect.y);
  23. pixel = new Vector2(cam.pixelRect.width, cam.pixelRect.height);
  24. fov = cam.fieldOfView;
  25. focalLength = cam.focalLength;
  26. nearClipPlane = cam.nearClipPlane;
  27. farClipPlane = cam.farClipPlane;
  28. this.frameRate = frameRate;
  29. this.bitRate = bitRate;
  30. }
  31. public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
  32. {
  33. serializer.SerializeValue(ref pixelXY);
  34. serializer.SerializeValue(ref pixel);
  35. serializer.SerializeValue(ref fov);
  36. serializer.SerializeValue(ref focalLength);
  37. serializer.SerializeValue(ref nearClipPlane);
  38. serializer.SerializeValue(ref farClipPlane);
  39. serializer.SerializeValue(ref frameRate);
  40. serializer.SerializeValue(ref bitRate);
  41. }
  42. }
  43. public class SyncCameraTransform : NetworkBehaviour
  44. {
  45. private NetworkVariable<Vector3> syncPostion;
  46. private NetworkVariable<Quaternion> syncRotation;
  47. private NetworkVariable<Vector3> syncScale;
  48. private NetworkVariable<CameraParam> cameraInfo;
  49. public float deltaTime = 0.1f;
  50. private float endTime;
  51. public float interpolationTime = 0.1f;
  52. public GameObject clientCamera;
  53. void Awake()
  54. {
  55. DontDestroyOnLoad(gameObject);
  56. syncPostion = new NetworkVariable<Vector3>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
  57. syncRotation = new NetworkVariable<Quaternion>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
  58. syncScale = new NetworkVariable<Vector3>(Vector3.one, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
  59. cameraInfo = new NetworkVariable<CameraParam>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
  60. //syncPostion.OnValueChanged += (oldVal, newVal) =>{
  61. // Debug.Log($"LocalTime : {NetworkManager.Singleton.LocalTime.Time} ServerTime : {NetworkManager.Singleton.ServerTime.Time}");
  62. //};
  63. }
  64. void Update()
  65. {
  66. ServerSync();
  67. }
  68. public override void OnNetworkSpawn()
  69. {
  70. base.OnNetworkSpawn();
  71. if(NetworkManager.Singleton.IsConnectedClient && IsOwner)
  72. {
  73. //手机端,设置参数
  74. //CameraParam param = new CameraParam(Camera.main);
  75. //cameraInfo.Value = param;
  76. }
  77. else if(IsServer)
  78. {
  79. if(clientCamera != null)
  80. {
  81. //眼镜端,设置其下的摄像机参数,或者手动生成一个摄像机,同时设置相机参数
  82. cameraInfo.OnValueChanged += (oldParam, newParam) =>
  83. {
  84. //Rect pRect = new Rect(newParam.pixelXY, newParam.pixelHeightWidth);
  85. clientCamera.GetComponent<CameraController>().SetCameraParam(newParam);
  86. };
  87. }
  88. }
  89. }
  90. public override void OnNetworkDespawn()
  91. {
  92. base.OnNetworkDespawn();
  93. }
  94. private void ServerSync()
  95. {
  96. if(NetworkManager.Singleton.IsConnectedClient)
  97. {
  98. //客户端
  99. if(CheckDeltaTime())
  100. {
  101. //syncPostion.Value = ClientCoordinateManager.Instance.GetSyncCameraPos();
  102. //syncRotation.Value = ClientCoordinateManager.Instance.GetSyncCameraRot();
  103. //syncScale.Value = Vector3.one;
  104. }
  105. }
  106. else if(IsServer)
  107. {
  108. //服务端
  109. transform.position = Vector3.Lerp(transform.position, syncPostion.Value, interpolationTime);
  110. transform.localScale = Vector3.Lerp(transform.localScale, syncScale.Value, interpolationTime);
  111. transform.rotation = Quaternion.Slerp(transform.rotation, syncRotation.Value, interpolationTime);
  112. }
  113. }
  114. bool CheckDeltaTime()
  115. {
  116. if(Time.time >= endTime)
  117. {
  118. endTime = Time.time + deltaTime;
  119. return true;
  120. }
  121. return false;
  122. }
  123. }
  124. }