PhotonTransformViewClassic.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="PhotonTransformViewClassic.cs" company="Exit Games GmbH">
  3. // PhotonNetwork Framework for Unity - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Component to synchronize Transforms via PUN PhotonView.
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // ----------------------------------------------------------------------------
  10. namespace Photon.Pun
  11. {
  12. using UnityEngine;
  13. using System.Collections.Generic;
  14. /// <summary>
  15. /// This class helps you to synchronize position, rotation and scale
  16. /// of a GameObject. It also gives you many different options to make
  17. /// the synchronized values appear smooth, even when the data is only
  18. /// send a couple of times per second.
  19. /// Simply add the component to your GameObject and make sure that
  20. /// the PhotonTransformViewClassic is added to the list of observed components
  21. /// </summary>
  22. [AddComponentMenu("Photon Networking/Photon Transform View Classic")]
  23. public class PhotonTransformViewClassic : MonoBehaviourPun, IPunObservable
  24. {
  25. //As this component is very complex, we separated it into multiple classes.
  26. //The PositionModel, RotationModel and ScaleMode store the data you are able to
  27. //configure in the inspector while the "control" objects below are actually moving
  28. //the object and calculating all the inter- and extrapolation
  29. [HideInInspector]
  30. public PhotonTransformViewPositionModel m_PositionModel = new PhotonTransformViewPositionModel();
  31. [HideInInspector]
  32. public PhotonTransformViewRotationModel m_RotationModel = new PhotonTransformViewRotationModel();
  33. [HideInInspector]
  34. public PhotonTransformViewScaleModel m_ScaleModel = new PhotonTransformViewScaleModel();
  35. PhotonTransformViewPositionControl m_PositionControl;
  36. PhotonTransformViewRotationControl m_RotationControl;
  37. PhotonTransformViewScaleControl m_ScaleControl;
  38. PhotonView m_PhotonView;
  39. bool m_ReceivedNetworkUpdate = false;
  40. /// <summary>
  41. /// Flag to skip initial data when Object is instantiated and rely on the first deserialized data instead.
  42. /// </summary>
  43. bool m_firstTake = false;
  44. void Awake()
  45. {
  46. this.m_PhotonView = GetComponent<PhotonView>();
  47. this.m_PositionControl = new PhotonTransformViewPositionControl(this.m_PositionModel);
  48. this.m_RotationControl = new PhotonTransformViewRotationControl(this.m_RotationModel);
  49. this.m_ScaleControl = new PhotonTransformViewScaleControl(this.m_ScaleModel);
  50. }
  51. void OnEnable()
  52. {
  53. m_firstTake = true;
  54. }
  55. void Update()
  56. {
  57. if (this.m_PhotonView == null || this.m_PhotonView.IsMine == true || PhotonNetwork.IsConnectedAndReady == false)
  58. {
  59. return;
  60. }
  61. this.UpdatePosition();
  62. this.UpdateRotation();
  63. this.UpdateScale();
  64. }
  65. void UpdatePosition()
  66. {
  67. if (this.m_PositionModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
  68. {
  69. return;
  70. }
  71. transform.localPosition = this.m_PositionControl.UpdatePosition(transform.localPosition);
  72. }
  73. void UpdateRotation()
  74. {
  75. if (this.m_RotationModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
  76. {
  77. return;
  78. }
  79. transform.localRotation = this.m_RotationControl.GetRotation(transform.localRotation);
  80. }
  81. void UpdateScale()
  82. {
  83. if (this.m_ScaleModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
  84. {
  85. return;
  86. }
  87. transform.localScale = this.m_ScaleControl.GetScale(transform.localScale);
  88. }
  89. /// <summary>
  90. /// These values are synchronized to the remote objects if the interpolation mode
  91. /// or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
  92. /// the current speed (in units/second) and turning speed (in angles/second) so the remote
  93. /// object can use them to predict the objects movement.
  94. /// </summary>
  95. /// <param name="speed">The current movement vector of the object in units/second.</param>
  96. /// <param name="turnSpeed">The current turn speed of the object in angles/second.</param>
  97. public void SetSynchronizedValues(Vector3 speed, float turnSpeed)
  98. {
  99. this.m_PositionControl.SetSynchronizedValues(speed, turnSpeed);
  100. }
  101. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  102. {
  103. this.m_PositionControl.OnPhotonSerializeView(transform.localPosition, stream, info);
  104. this.m_RotationControl.OnPhotonSerializeView(transform.localRotation, stream, info);
  105. this.m_ScaleControl.OnPhotonSerializeView(transform.localScale, stream, info);
  106. if (stream.IsReading == true)
  107. {
  108. this.m_ReceivedNetworkUpdate = true;
  109. // force latest data to avoid initial drifts when player is instantiated.
  110. if (m_firstTake)
  111. {
  112. m_firstTake = false;
  113. if (this.m_PositionModel.SynchronizeEnabled)
  114. {
  115. this.transform.localPosition = this.m_PositionControl.GetNetworkPosition();
  116. }
  117. if (this.m_RotationModel.SynchronizeEnabled)
  118. {
  119. this.transform.localRotation = this.m_RotationControl.GetNetworkRotation();
  120. }
  121. if (this.m_ScaleModel.SynchronizeEnabled)
  122. {
  123. this.transform.localScale = this.m_ScaleControl.GetNetworkScale();
  124. }
  125. }
  126. }
  127. }
  128. }
  129. [System.Serializable]
  130. public class PhotonTransformViewPositionModel
  131. {
  132. public enum InterpolateOptions
  133. {
  134. Disabled,
  135. FixedSpeed,
  136. EstimatedSpeed,
  137. SynchronizeValues,
  138. Lerp
  139. }
  140. public enum ExtrapolateOptions
  141. {
  142. Disabled,
  143. SynchronizeValues,
  144. EstimateSpeedAndTurn,
  145. FixedSpeed,
  146. }
  147. public bool SynchronizeEnabled;
  148. public bool TeleportEnabled = true;
  149. public float TeleportIfDistanceGreaterThan = 3f;
  150. public InterpolateOptions InterpolateOption = InterpolateOptions.EstimatedSpeed;
  151. public float InterpolateMoveTowardsSpeed = 1f;
  152. public float InterpolateLerpSpeed = 1f;
  153. public ExtrapolateOptions ExtrapolateOption = ExtrapolateOptions.Disabled;
  154. public float ExtrapolateSpeed = 1f;
  155. public bool ExtrapolateIncludingRoundTripTime = true;
  156. public int ExtrapolateNumberOfStoredPositions = 1;
  157. }
  158. public class PhotonTransformViewPositionControl
  159. {
  160. PhotonTransformViewPositionModel m_Model;
  161. float m_CurrentSpeed;
  162. double m_LastSerializeTime;
  163. Vector3 m_SynchronizedSpeed = Vector3.zero;
  164. float m_SynchronizedTurnSpeed = 0;
  165. Vector3 m_NetworkPosition;
  166. Queue<Vector3> m_OldNetworkPositions = new Queue<Vector3>();
  167. bool m_UpdatedPositionAfterOnSerialize = true;
  168. public PhotonTransformViewPositionControl(PhotonTransformViewPositionModel model)
  169. {
  170. m_Model = model;
  171. }
  172. Vector3 GetOldestStoredNetworkPosition()
  173. {
  174. Vector3 oldPosition = m_NetworkPosition;
  175. if (m_OldNetworkPositions.Count > 0)
  176. {
  177. oldPosition = m_OldNetworkPositions.Peek();
  178. }
  179. return oldPosition;
  180. }
  181. /// <summary>
  182. /// These values are synchronized to the remote objects if the interpolation mode
  183. /// or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
  184. /// the current speed (in units/second) and turning speed (in angles/second) so the remote
  185. /// object can use them to predict the objects movement.
  186. /// </summary>
  187. /// <param name="speed">The current movement vector of the object in units/second.</param>
  188. /// <param name="turnSpeed">The current turn speed of the object in angles/second.</param>
  189. public void SetSynchronizedValues(Vector3 speed, float turnSpeed)
  190. {
  191. m_SynchronizedSpeed = speed;
  192. m_SynchronizedTurnSpeed = turnSpeed;
  193. }
  194. /// <summary>
  195. /// Calculates the new position based on the values setup in the inspector
  196. /// </summary>
  197. /// <param name="currentPosition">The current position.</param>
  198. /// <returns>The new position.</returns>
  199. public Vector3 UpdatePosition(Vector3 currentPosition)
  200. {
  201. Vector3 targetPosition = GetNetworkPosition() + GetExtrapolatedPositionOffset();
  202. switch (m_Model.InterpolateOption)
  203. {
  204. case PhotonTransformViewPositionModel.InterpolateOptions.Disabled:
  205. if (m_UpdatedPositionAfterOnSerialize == false)
  206. {
  207. currentPosition = targetPosition;
  208. m_UpdatedPositionAfterOnSerialize = true;
  209. }
  210. break;
  211. case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
  212. currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, Time.deltaTime * m_Model.InterpolateMoveTowardsSpeed);
  213. break;
  214. case PhotonTransformViewPositionModel.InterpolateOptions.EstimatedSpeed:
  215. if (m_OldNetworkPositions.Count == 0)
  216. {
  217. // special case: we have no previous updates in memory, so we can't guess a speed!
  218. break;
  219. }
  220. // knowing the last (incoming) position and the one before, we can guess a speed.
  221. // note that the speed is times sendRateOnSerialize! we send X updates/sec, so our estimate has to factor that in.
  222. float estimatedSpeed = (Vector3.Distance(m_NetworkPosition, GetOldestStoredNetworkPosition()) / m_OldNetworkPositions.Count) * PhotonNetwork.SerializationRate;
  223. // move towards the targetPosition (including estimates, if that's active) with the speed calculated from the last updates.
  224. currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, Time.deltaTime * estimatedSpeed);
  225. break;
  226. case PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues:
  227. if (m_SynchronizedSpeed.magnitude == 0)
  228. {
  229. currentPosition = targetPosition;
  230. }
  231. else
  232. {
  233. currentPosition = Vector3.MoveTowards(currentPosition, targetPosition, Time.deltaTime * m_SynchronizedSpeed.magnitude);
  234. }
  235. break;
  236. case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
  237. currentPosition = Vector3.Lerp(currentPosition, targetPosition, Time.deltaTime * m_Model.InterpolateLerpSpeed);
  238. break;
  239. }
  240. if (m_Model.TeleportEnabled == true)
  241. {
  242. if (Vector3.Distance(currentPosition, GetNetworkPosition()) > m_Model.TeleportIfDistanceGreaterThan)
  243. {
  244. currentPosition = GetNetworkPosition();
  245. }
  246. }
  247. return currentPosition;
  248. }
  249. /// <summary>
  250. /// Gets the last position that was received through the network
  251. /// </summary>
  252. /// <returns></returns>
  253. public Vector3 GetNetworkPosition()
  254. {
  255. return m_NetworkPosition;
  256. }
  257. /// <summary>
  258. /// Calculates an estimated position based on the last synchronized position,
  259. /// the time when the last position was received and the movement speed of the object
  260. /// </summary>
  261. /// <returns>Estimated position of the remote object</returns>
  262. public Vector3 GetExtrapolatedPositionOffset()
  263. {
  264. float timePassed = (float)(PhotonNetwork.Time - m_LastSerializeTime);
  265. if (m_Model.ExtrapolateIncludingRoundTripTime == true)
  266. {
  267. timePassed += (float)PhotonNetwork.GetPing() / 1000f;
  268. }
  269. Vector3 extrapolatePosition = Vector3.zero;
  270. switch (m_Model.ExtrapolateOption)
  271. {
  272. case PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues:
  273. Quaternion turnRotation = Quaternion.Euler(0, m_SynchronizedTurnSpeed * timePassed, 0);
  274. extrapolatePosition = turnRotation * (m_SynchronizedSpeed * timePassed);
  275. break;
  276. case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
  277. Vector3 moveDirection = (m_NetworkPosition - GetOldestStoredNetworkPosition()).normalized;
  278. extrapolatePosition = moveDirection * m_Model.ExtrapolateSpeed * timePassed;
  279. break;
  280. case PhotonTransformViewPositionModel.ExtrapolateOptions.EstimateSpeedAndTurn:
  281. Vector3 moveDelta = (m_NetworkPosition - GetOldestStoredNetworkPosition()) * PhotonNetwork.SerializationRate;
  282. extrapolatePosition = moveDelta * timePassed;
  283. break;
  284. }
  285. return extrapolatePosition;
  286. }
  287. public void OnPhotonSerializeView(Vector3 currentPosition, PhotonStream stream, PhotonMessageInfo info)
  288. {
  289. if (m_Model.SynchronizeEnabled == false)
  290. {
  291. return;
  292. }
  293. if (stream.IsWriting == true)
  294. {
  295. SerializeData(currentPosition, stream, info);
  296. }
  297. else
  298. {
  299. DeserializeData(stream, info);
  300. }
  301. m_LastSerializeTime = PhotonNetwork.Time;
  302. m_UpdatedPositionAfterOnSerialize = false;
  303. }
  304. void SerializeData(Vector3 currentPosition, PhotonStream stream, PhotonMessageInfo info)
  305. {
  306. stream.SendNext(currentPosition);
  307. m_NetworkPosition = currentPosition;
  308. if (m_Model.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues ||
  309. m_Model.InterpolateOption == PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues)
  310. {
  311. stream.SendNext(m_SynchronizedSpeed);
  312. stream.SendNext(m_SynchronizedTurnSpeed);
  313. }
  314. }
  315. void DeserializeData(PhotonStream stream, PhotonMessageInfo info)
  316. {
  317. Vector3 readPosition = (Vector3)stream.ReceiveNext();
  318. if (m_Model.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues ||
  319. m_Model.InterpolateOption == PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues)
  320. {
  321. m_SynchronizedSpeed = (Vector3)stream.ReceiveNext();
  322. m_SynchronizedTurnSpeed = (float)stream.ReceiveNext();
  323. }
  324. if (m_OldNetworkPositions.Count == 0)
  325. {
  326. // if we don't have old positions yet, this is the very first update this client reads. let's use this as current AND old position.
  327. m_NetworkPosition = readPosition;
  328. }
  329. // the previously received position becomes the old(er) one and queued. the new one is the m_NetworkPosition
  330. m_OldNetworkPositions.Enqueue(m_NetworkPosition);
  331. m_NetworkPosition = readPosition;
  332. // reduce items in queue to defined number of stored positions.
  333. while (m_OldNetworkPositions.Count > m_Model.ExtrapolateNumberOfStoredPositions)
  334. {
  335. m_OldNetworkPositions.Dequeue();
  336. }
  337. }
  338. }
  339. [System.Serializable]
  340. public class PhotonTransformViewRotationModel
  341. {
  342. public enum InterpolateOptions
  343. {
  344. Disabled,
  345. RotateTowards,
  346. Lerp,
  347. }
  348. public bool SynchronizeEnabled;
  349. public InterpolateOptions InterpolateOption = InterpolateOptions.RotateTowards;
  350. public float InterpolateRotateTowardsSpeed = 180;
  351. public float InterpolateLerpSpeed = 5;
  352. }
  353. public class PhotonTransformViewRotationControl
  354. {
  355. PhotonTransformViewRotationModel m_Model;
  356. Quaternion m_NetworkRotation;
  357. public PhotonTransformViewRotationControl(PhotonTransformViewRotationModel model)
  358. {
  359. m_Model = model;
  360. }
  361. /// <summary>
  362. /// Gets the last rotation that was received through the network
  363. /// </summary>
  364. /// <returns></returns>
  365. public Quaternion GetNetworkRotation()
  366. {
  367. return m_NetworkRotation;
  368. }
  369. public Quaternion GetRotation(Quaternion currentRotation)
  370. {
  371. switch (m_Model.InterpolateOption)
  372. {
  373. default:
  374. case PhotonTransformViewRotationModel.InterpolateOptions.Disabled:
  375. return m_NetworkRotation;
  376. case PhotonTransformViewRotationModel.InterpolateOptions.RotateTowards:
  377. return Quaternion.RotateTowards(currentRotation, m_NetworkRotation, m_Model.InterpolateRotateTowardsSpeed * Time.deltaTime);
  378. case PhotonTransformViewRotationModel.InterpolateOptions.Lerp:
  379. return Quaternion.Lerp(currentRotation, m_NetworkRotation, m_Model.InterpolateLerpSpeed * Time.deltaTime);
  380. }
  381. }
  382. public void OnPhotonSerializeView(Quaternion currentRotation, PhotonStream stream, PhotonMessageInfo info)
  383. {
  384. if (m_Model.SynchronizeEnabled == false)
  385. {
  386. return;
  387. }
  388. if (stream.IsWriting == true)
  389. {
  390. stream.SendNext(currentRotation);
  391. m_NetworkRotation = currentRotation;
  392. }
  393. else
  394. {
  395. m_NetworkRotation = (Quaternion)stream.ReceiveNext();
  396. }
  397. }
  398. }
  399. [System.Serializable]
  400. public class PhotonTransformViewScaleModel
  401. {
  402. public enum InterpolateOptions
  403. {
  404. Disabled,
  405. MoveTowards,
  406. Lerp,
  407. }
  408. public bool SynchronizeEnabled;
  409. public InterpolateOptions InterpolateOption = InterpolateOptions.Disabled;
  410. public float InterpolateMoveTowardsSpeed = 1f;
  411. public float InterpolateLerpSpeed;
  412. }
  413. public class PhotonTransformViewScaleControl
  414. {
  415. PhotonTransformViewScaleModel m_Model;
  416. Vector3 m_NetworkScale = Vector3.one;
  417. public PhotonTransformViewScaleControl(PhotonTransformViewScaleModel model)
  418. {
  419. m_Model = model;
  420. }
  421. /// <summary>
  422. /// Gets the last scale that was received through the network
  423. /// </summary>
  424. /// <returns></returns>
  425. public Vector3 GetNetworkScale()
  426. {
  427. return m_NetworkScale;
  428. }
  429. public Vector3 GetScale(Vector3 currentScale)
  430. {
  431. switch (m_Model.InterpolateOption)
  432. {
  433. default:
  434. case PhotonTransformViewScaleModel.InterpolateOptions.Disabled:
  435. return m_NetworkScale;
  436. case PhotonTransformViewScaleModel.InterpolateOptions.MoveTowards:
  437. return Vector3.MoveTowards(currentScale, m_NetworkScale, m_Model.InterpolateMoveTowardsSpeed * Time.deltaTime);
  438. case PhotonTransformViewScaleModel.InterpolateOptions.Lerp:
  439. return Vector3.Lerp(currentScale, m_NetworkScale, m_Model.InterpolateLerpSpeed * Time.deltaTime);
  440. }
  441. }
  442. public void OnPhotonSerializeView(Vector3 currentScale, PhotonStream stream, PhotonMessageInfo info)
  443. {
  444. if (m_Model.SynchronizeEnabled == false)
  445. {
  446. return;
  447. }
  448. if (stream.IsWriting == true)
  449. {
  450. stream.SendNext(currentScale);
  451. m_NetworkScale = currentScale;
  452. }
  453. else
  454. {
  455. m_NetworkScale = (Vector3)stream.ReceiveNext();
  456. }
  457. }
  458. }
  459. }