博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NETWORK_同步位置信息
阅读量:6359 次
发布时间:2019-06-23

本文共 3587 字,大约阅读时间需要 11 分钟。

using UnityEngine;

using System.Collections;

public class NetworkInterpolatedTransform : MonoBehaviour {

 
 public double interpolationBackTime = 0.1;
 
 internal struct  State
 {
  internal double timestamp;
  internal Vector3 pos;
  internal Quaternion rot;
 }

 // We store twenty states with "playback" information

 State[] m_BufferedState = new State[20];
 // Keep track of what slots are used
 int m_TimestampCount;
 
 void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
 {
  // Always send transform (depending on reliability of the network view)
  if (stream.isWriting)
  {
   Vector3 pos = transform.localPosition;
   Quaternion rot = transform.localRotation;
   stream.Serialize(ref pos);
   stream.Serialize(ref rot);
  }
  // When receiving, buffer the information
  else
  {
   // Receive latest state information
   Vector3 pos = Vector3.zero;
   Quaternion rot = Quaternion.identity;
   stream.Serialize(ref pos);
   stream.Serialize(ref rot);
   
   // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1
   for (int i=m_BufferedState.Length-1;i>=1;i--)
   {
    m_BufferedState[i] = m_BufferedState[i-1];
   }
   
   // Save currect received state as 0 in the buffer, safe to overwrite after shifting
   State state;
   state.timestamp = info.timestamp;
   state.pos = pos;
   state.rot = rot;
   m_BufferedState[0] = state;
   
   // Increment state count but never exceed buffer size
   m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);

   // Check integrity, lowest numbered state in the buffer is newest and so on

   for (int i=0;i<m_TimestampCount-1;i++)
   {
    if (m_BufferedState[i].timestamp < m_BufferedState[i+1].timestamp)
     Debug.Log("State inconsistent");
   }
   
   //Debug.Log("stamp: " + info.timestamp + "my time: " + Network.time + "delta: " + (Network.time - info.timestamp));
  }
 }
 
 // This only runs where the component is enabled, which is only on remote peers (server/clients)
 void Update () {
  double currentTime = Network.time;
  double interpolationTime = currentTime - interpolationBackTime;
  // We have a window of interpolationBackTime where we basically play
  // By having interpolationBackTime the average ping, you will usually use interpolation.
  // And only if no more data arrives we will use extrapolation
  
  // Use interpolation
  // Check if latest state exceeds interpolation time, if this is the case then
  // it is too old and extrapolation should be used
  if (m_BufferedState[0].timestamp > interpolationTime)
  {
   for (int i=0;i<m_TimestampCount;i++)
   {
    // Find the state which matches the interpolation time (time+0.1) or use last state
    if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount-1)
    {
     // The state one slot newer (<100ms) than the best playback state
     State rhs = m_BufferedState[Mathf.Max(i-1, 0)];
     // The best playback state (closest to 100 ms old (default time))
     State lhs = m_BufferedState[i];
     
     // Use the time between the two slots to determine if interpolation is necessary
     double length = rhs.timestamp - lhs.timestamp;
     float t = 0.0F;
     // As the time difference gets closer to 100 ms t gets closer to 1 in
     // which case rhs is only used
     if (length > 0.0001)
      t = (float)((interpolationTime - lhs.timestamp) / length);
     
     // if t=0 => lhs is used directly
     transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
     transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
     return;
    }
   }
  }
  // Use extrapolation. Here we do something really simple and just repeat the last
  // received state. You can do clever stuff with predicting what should happen.
  else
  {
   State latest = m_BufferedState[0];
   
   transform.localPosition = latest.pos;
   transform.localRotation = latest.rot;
  }
 }
}

转载地址:http://wfbma.baihongyu.com/

你可能感兴趣的文章
铁路开通WiFi“钱景”不明
查看>>
Facebook申请专利 或让好友及陌生人相互拼车
查看>>
电力“十三五”规划:地面光伏与分布式的分水岭
查看>>
美联社再告FBI:要求公开请黑客解锁iPhone花费
查看>>
三星电子出售希捷和夏普等四家公司股份
查看>>
任志远:当云计算遇上混合云
查看>>
思科联手发那科 用物联网技术打造无人工厂
查看>>
智慧城市首要在政府利用大数据的智慧
查看>>
2015年物联网行业:巨头展开专利大战
查看>>
以自动化测试撬动遗留系统
查看>>
网络安全初创公司存活之道
查看>>
《图解CSS3:核心技术与案例实战》——1.2节浏览器对CSS3的支持状况
查看>>
《Android应用开发》——2.4节应用类
查看>>
继 One Step 后,锤子科技 Big Bang 正式开源
查看>>
《数据科学:R语言实现》——2.5 使用Excel文件
查看>>
《淘宝店铺设计装修一册通》一2.5 抠图工具的简单运用
查看>>
《音乐达人秀:Adobe Audition实战200例》——实例4 收音机音乐节目转录到电脑里...
查看>>
《JavaScript应用程序设计》一一3.1 过时的类继承
查看>>
Amazon 推出 API 网关使用计划
查看>>
互联网流量超出路由器上限 或致全球断网
查看>>