11//========= Copyright 2017, HTC Corporation. All rights reserved. ===========
22
3+ using System . Collections . Generic ;
34using UnityEngine ;
45
56namespace HTC . UnityPlugin . ViveShare
67{
78 public class ViveShare_SyncPose : ViveShare_SyncBase
89 {
9- // transform from network
10- private float currTimeStamp = 0 ;
11- private Vector3 currPosition = Vector3 . zero ;
12- private Vector3 currEulerRotation = Vector3 . zero ;
10+ private class State
11+ {
12+ public float timeStamp ;
13+ public Vector3 position ;
14+ public Quaternion rotation ;
15+ }
16+
17+ private float lastSyncTime = 0 ;
18+ public bool doInterpolate = true ;
19+ private float interpolateStep = 0.0f ;
20+
21+ private List < State > states = new List < State > ( ) ;
1322
1423 //-----------------------------------------------------------------------------
1524 // state update
1625 //-----------------------------------------------------------------------------
1726
18- public override void OnEnable ( )
19- {
20- base . OnEnable ( ) ;
21-
22- currPosition = transform . position ;
23- currEulerRotation = transform . eulerAngles ;
24- }
25-
2627 public void FixedUpdate ( )
2728 {
28- // update pose if this object has no authority
29- if ( identity . hasAuthority == false )
29+ // return if this object has authority
30+ if ( identity . hasAuthority )
31+ return ;
32+
33+ if ( doInterpolate && states . Count >= 2 )
3034 {
31- transform . position = currPosition ;
32- transform . eulerAngles = currEulerRotation ;
35+ transform . position = Vector3 . Lerp ( states [ 0 ] . position , states [ 1 ] . position , interpolateStep ) ;
36+ transform . rotation = Quaternion . Slerp ( states [ 0 ] . rotation , states [ 1 ] . rotation , interpolateStep ) ;
37+
38+ interpolateStep += sendInterval / Time . fixedDeltaTime ;
39+ if ( interpolateStep >= 1 )
40+ {
41+ interpolateStep = 0 ;
42+ states . RemoveAt ( 0 ) ;
43+ }
3344 }
3445 }
3546
@@ -39,16 +50,37 @@ public void FixedUpdate()
3950
4051 public override object [ ] GenerateParamList ( )
4152 {
42- object [ ] param = { transform . position , transform . eulerAngles } ;
53+ object [ ] param = { transform . position , transform . rotation } ;
4354 return param ;
4455 }
4556
4657 public override void SetParams ( float timeStamp , object [ ] paramList )
4758 {
48- // record newly received state
49- currTimeStamp = timeStamp ;
50- currPosition = ( Vector3 ) paramList [ 0 ] ;
51- currEulerRotation = ( Vector3 ) paramList [ 1 ] ;
59+ // ignore out-of-order packets
60+ if ( timeStamp <= lastSyncTime )
61+ {
62+ return ;
63+ }
64+
65+ Vector3 pos = ( Vector3 ) paramList [ 0 ] ;
66+ Quaternion rot = ( Quaternion ) paramList [ 1 ] ;
67+
68+ if ( ! doInterpolate )
69+ {
70+ transform . position = pos ;
71+ transform . rotation = rot ;
72+ }
73+ else
74+ {
75+ State newState = new State ( ) ;
76+ newState . timeStamp = timeStamp ;
77+ newState . position = pos ;
78+ newState . rotation = rot ;
79+
80+ states . Add ( newState ) ;
81+ }
82+
83+ lastSyncTime = timeStamp ;
5284 }
5385 }
5486}
0 commit comments