Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Photon.Pun;
  4. using UnityEngine;
  5.  
  6. public class StateBufferModel_2 : MonoBehaviourPun, IPunObservable
  7. {
  8.     [SerializeField] private float InterpolationBackTime = 0.1f;//Default 0.1, one tenth of a second
  9.     [SerializeField] private float ExtrapolationLimit = 0.5f;
  10.    
  11.     private struct State
  12.     {
  13.         public double Timestamp;
  14.         public Vector3 Position;
  15.         public Quaternion Rotation;
  16.  
  17.         public State(double timestamp, Vector3 position,Quaternion rotation)
  18.         {
  19.             Timestamp = timestamp;
  20.             Position = position;
  21.             Rotation = rotation;
  22.         }
  23.     }
  24.  
  25.     private State[] m_stateBuffer = new State[20];
  26.     private int m_stateCount = 0;
  27.     private Vector3 m_localPosition = new Vector3(0,0,0);
  28.  
  29.     private void Update()
  30.     {
  31.         if (!photonView.IsMine && m_stateCount != 0)
  32.         {
  33.             StateInterpolation();
  34.         }
  35.     }
  36.  
  37.     private void StateInterpolation()
  38.     {
  39.         var currentTime = PhotonNetwork.Time;
  40.         var interpolationTime = currentTime - InterpolationBackTime;
  41.  
  42.         if (m_stateBuffer[0].Timestamp > interpolationTime)
  43.         {
  44.             Debug.Log("Interpolation");
  45.             for(var i = 0; i < m_stateCount;i++)
  46.             {
  47.                 //closest state that matches network Time or use oldest state
  48.                 if (m_stateBuffer[i].Timestamp <= interpolationTime || i == m_stateCount - 1)
  49.                 {
  50.                     //closest to Network
  51.                     var lhs = m_stateBuffer[i];
  52.                     //one newer
  53.                     var rhs = m_stateBuffer[Mathf.Max(i - 1, 0)];
  54.                     //time between
  55.                     var length = rhs.Timestamp - lhs.Timestamp;
  56.  
  57.                     var t = 0f;
  58.                     if (t > 0.0001)
  59.                     {
  60.                         t = (float) ((interpolationTime - lhs.Timestamp) / length);
  61.                     }
  62.  
  63.                     transform.position = Vector3.Lerp(lhs.Position, rhs.Position, t);
  64.                     transform.rotation = Quaternion.Slerp(lhs.Rotation, rhs.Rotation, t);
  65.                     break;
  66.                 }
  67.             }
  68.         }
  69.         else
  70.         {
  71.             Debug.Log("Extrapolation");
  72.             var latestState = m_stateBuffer[0];
  73.  
  74.             var extrapolationLength = (float)(interpolationTime - latestState.Timestamp);
  75.             if (extrapolationLength < ExtrapolationLimit)
  76.             {
  77.                 transform.position = latestState.Position * extrapolationLength;
  78.                 transform.rotation = latestState.Rotation;
  79.             }
  80.         }
  81.                  
  82.     }
  83.  
  84.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  85.     {
  86.         if (stream.IsWriting)
  87.         {
  88.             stream.SendNext(transform.position);
  89.             stream.SendNext(transform.rotation);
  90.         }
  91.         else
  92.         {
  93.             var pos = (Vector3) stream.ReceiveNext();
  94.             var rot = (Quaternion) stream.ReceiveNext();
  95.            
  96.             var newState = new State(info.SentServerTime,pos,rot);
  97.             BufferState(newState);
  98.            
  99.             for (int i=0; i<m_stateCount-1; i++){
  100.                 if (m_stateBuffer[i].Timestamp < m_stateBuffer[i+1].Timestamp)
  101.                     Debug.Log("State inconsistent");
  102.             }
  103.         }
  104.     }
  105.  
  106.     /// <summary>
  107.     /// Shift the states to the right.Storing newst at 0.
  108.     /// </summary>
  109.     /// <param name="state"></param>
  110.     private void BufferState(State state)
  111.     {
  112.         for (var i = m_stateBuffer.Length - 1; i > 0; i--)
  113.         {
  114.             m_stateBuffer[i] = m_stateBuffer[i - 1];
  115.         }
  116.  
  117.         //Newest State
  118.         m_stateBuffer[0] = state;
  119.  
  120.         m_stateCount = Mathf.Min(m_stateCount + 1, m_stateBuffer.Length);
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement