EXTRAPOLATE









How do I use E X T R A P O L A T E
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: HubGui.cs Copy
28     void OnGUI()
29     {
30         GUI.skin = this.Skin;
31         GUILayout.Space(10);
32
33         GUILayout.BeginHorizontal();
34         GUILayout.Space(10);
35         scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37         GUILayout.Label("Basics", m_Headline);
38         if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39         {
40             demoDescription = "Demo Boxes\n\nUses ConnectAndJoinRandom script.\n(joins a random room or creates one)\n\nInstantiates simple prefab.\nSynchronizes positions without smoothing.";
41             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42         }
43         if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44         {
45             demoDescription = "Demo Worker\n\nJoins the default lobby and shows existing rooms.\nLets you create or join a room.\nInstantiates an animated character.\nSynchronizes position and animation state of character with smoothing.\nImplements simple in-room Chat via RPC calls.";
46             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47         }
48         if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49         {
50             demoDescription = "Movement Smoothing\n\nUses ConnectAndJoinRandom script.\nShows several basic ways to update positions of remote objects.";
51             demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52         }
53
54         GUILayout.Label("Advanced", m_Headline);
55         if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56         {
57             demoDescription = "Ownership Transfer\n\nShows how to transfer the ownership of a PhotonView.\nThe owner will send position updates of the GameObject.\nTransfer can be edited per PhotonView and set to Fixed (no transfer), Request (owner has to agree) or Takeover (owner can't object).";
58             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59             this.webLink = new DemoBtn();
60         }
61         if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62         {
63             demoDescription = "Pickup, Teams, Scores\n\nUses ConnectAndJoinRandom script.\nImplements item pickup with RPCs.\nUses Custom Properties for Teams.\nCounts score per player and team.\nUses PhotonPlayer extension methods for easy Custom Property access.";
64             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65             this.webLink = new DemoBtn();
66         }
67
68         GUILayout.Label("Feature Demos", m_Headline);
69         if (GUILayout.Button("Chat", GUILayout.Width(280)))
70         {
71             demoDescription = "Chat\n\nUses the Chat API (now part of PUN).\nSimple UI.\nYou can enter any User ID.\nAutomatically subscribes some channels.\nAllows simple commands via text.\n\nRequires configuration of Chat App ID in scene.";
72             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73             this.webLink = new DemoBtn();
74         }
75         if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76         {
77             demoDescription = "RPG Movement\n\nDemonstrates how to use the PhotonTransformView component to synchronize position updates smoothly using inter- and extrapolation.\n\nThis demo also shows how to setup a Mecanim Animator to update animations automatically based on received position updates (without sending explicit animation updates).";
78             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79             this.webLink = new DemoBtn();
80         }
81         if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82         {
83             demoDescription = "Mecanim Animations\n\nThis demo shows how to use the PhotonAnimatorView component to easily synchronize Mecanim animations.\n\nIt also demonstrates another feature of the PhotonTransformView component which gives you more control how position updates are inter-/extrapolated by telling the component how fast the object moves and turns using SetSynchronizedValues().";
84             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85             this.webLink = new DemoBtn();
86         }
87         if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88         {
89             demoDescription = "2D Game Demo\n\nSynchronizes animations, positions and physics in a 2D scene.";
90             this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91             this.webLink = new DemoBtn();
92         }
93         if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94         {
95             demoDescription = "Friends & Authentication\n\nShows connect with or without (server-side) authentication.\n\nAuthentication requires minor server-side setup (in Dashboard).\n\nOnce connected, you can find (made up) friends.\nJoin a room just to see how that gets visible in friends list.";
96             this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97             this.webLink = new DemoBtn();
98         }
99
100         GUILayout.Label("Tutorial", m_Headline);
101         if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102         {
103             demoDescription = "Marco Polo Tutorial\n\nFinal result you could get when you do the Marco Polo Tutorial.\nSlightly modified to be more compatible with this package.";
104             this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105             this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106         }
107         GUILayout.EndScrollView();
108
109         GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110         GUILayout.Label(demoDescription);
111         GUILayout.Space(10);
112         if (!string.IsNullOrEmpty(this.demoBtn.Text))
113         {
114             if (GUILayout.Button(this.demoBtn.Text))
115             {
116                 Application.LoadLevel(this.demoBtn.Link);
117             }
118         }
119         if (!string.IsNullOrEmpty(this.webLink.Text))
120         {
121             if (GUILayout.Button(this.webLink.Text))
122             {
123                 Application.OpenURL(this.webLink.Link);
124             }
125         }
126         GUILayout.EndVertical();
127
128
129         GUILayout.EndHorizontal();
130     }
File name: PhotonTransformViewEditor.cs Copy
96     private void DrawSynchronizePositionData()
97     {
98         if (this.m_SynchronizePositionProperty == null || this.m_SynchronizePositionProperty.boolValue == false)
99         {
100             return;
101         }
102
103         SerializedProperty interpolatePositionProperty = serializedObject.FindProperty("m_PositionModel.InterpolateOption");
104         PhotonTransformViewPositionModel.InterpolateOptions interpolateOption = (PhotonTransformViewPositionModel.InterpolateOptions)interpolatePositionProperty.enumValueIndex;
105
106         SerializedProperty extrapolatePositionProperty = serializedObject.FindProperty("m_PositionModel.ExtrapolateOption");
107         PhotonTransformViewPositionModel.ExtrapolateOptions extrapolateOption = (PhotonTransformViewPositionModel.ExtrapolateOptions)extrapolatePositionProperty.enumValueIndex;
108
109         float containerHeight = 155;
110
111         switch (interpolateOption)
112         {
113             case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
114             case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
115                 containerHeight += EDITOR_LINE_HEIGHT;
116                 break;
117             /*case PhotonTransformViewPositionModel.InterpolateOptions.MoveTowardsComplex:
118                 containerHeight += EDITOR_LINE_HEIGHT*3;
119                 break;*/
120         }
121
122         if (extrapolateOption != PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled)
123         {
124             containerHeight += EDITOR_LINE_HEIGHT;
125         }
126
127         switch (extrapolateOption)
128         {
129             case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
130                 containerHeight += EDITOR_LINE_HEIGHT;
131                 break;
132         }
133
134         if (this.m_InterpolateHelpOpen == true)
135         {
136             containerHeight += GetInterpolateHelpBoxHeight();
137         }
138
139         if (this.m_ExtrapolateHelpOpen == true)
140         {
141             containerHeight += GetExtrapolateHelpBoxHeight();
142         }
143
144         // removed Gizmo Options. -3 lines, -1 splitter
145         containerHeight -= EDITOR_LINE_HEIGHT * 2;
146
147         Rect rect = PhotonGUI.ContainerBody(containerHeight);
148
149         Rect propertyRect = new Rect(rect.xMin + 5, rect.yMin + 2, rect.width - 10, EditorGUIUtility.singleLineHeight);
150
151         DrawTeleport(ref propertyRect);
152         DrawSplitter(ref propertyRect);
153
154         DrawSynchronizePositionDataInterpolation(ref propertyRect, interpolatePositionProperty, interpolateOption);
155         DrawSplitter(ref propertyRect);
156
157         DrawSynchronizePositionDataExtrapolation(ref propertyRect, extrapolatePositionProperty, extrapolateOption);
158         DrawSplitter(ref propertyRect);
159
160         DrawSynchronizePositionDataGizmos(ref propertyRect);
161     }
File name: PhotonTransformViewEditor.cs Copy
168     private float GetExtrapolateHelpBoxHeight()
169     {
170         return PhotonGUI.RichLabel.CalcHeight(new GUIContent(EXTRAPOLATE_HELP), Screen.width - 54) + 35;
171     }
File name: PhotonTransformViewEditor.cs Copy
232     private void DrawSynchronizePositionDataExtrapolation(ref Rect propertyRect, SerializedProperty extrapolatePositionProperty, PhotonTransformViewPositionModel.ExtrapolateOptions extrapolateOption)
233     {
234         DrawPropertyWithHelpIcon(ref propertyRect, ref this.m_ExtrapolateHelpOpen, extrapolatePositionProperty, EXTRAPOLATE_TOOLTIP);
235         DrawHelpBox(ref propertyRect, this.m_ExtrapolateHelpOpen, GetExtrapolateHelpBoxHeight(), EXTRAPOLATE_HELP, EXTRAPOLATE_HELP_URL);
236
237         if (extrapolateOption != PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled)
238         {
239             EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_PositionModel.ExtrapolateIncludingRoundTripTime"));
240             propertyRect.y += EDITOR_LINE_HEIGHT;
241         }
242
243         switch (extrapolateOption)
244         {
245             case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
246                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_PositionModel.ExtrapolateSpeed"));
247                 propertyRect.y += EDITOR_LINE_HEIGHT;
248                 break;
249         }
250     }
File name: PhotonTransformViewPositionControl.cs Copy
54     public Vector3 UpdatePosition( Vector3 currentPosition )
55     {
56         Vector3 targetPosition = GetNetworkPosition() + GetExtrapolatedPositionOffset();
57
58         switch( m_Model.InterpolateOption )
59         {
60         case PhotonTransformViewPositionModel.InterpolateOptions.Disabled:
61             if( m_UpdatedPositionAfterOnSerialize == false )
62             {
63                 currentPosition = targetPosition;
64                 m_UpdatedPositionAfterOnSerialize = true;
65             }
66             break;
67         case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
68             currentPosition = Vector3.MoveTowards( currentPosition, targetPosition, Time.deltaTime * m_Model.InterpolateMoveTowardsSpeed );
69             break;
70         case PhotonTransformViewPositionModel.InterpolateOptions.EstimatedSpeed:
71             int positionsCount = Mathf.Min( 1, m_OldNetworkPositions.Count );
72             float estimatedSpeed = Vector3.Distance( m_NetworkPosition, GetOldestStoredNetworkPosition() ) / positionsCount;
73             currentPosition = Vector3.MoveTowards( currentPosition, targetPosition, Time.deltaTime * estimatedSpeed );
74             break;
75         case PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues:
76             if( m_SynchronizedSpeed.magnitude == 0 )
77             {
78                 currentPosition = targetPosition;
79             }
80             else
81             {
82                 currentPosition = Vector3.MoveTowards( currentPosition, targetPosition, Time.deltaTime * m_SynchronizedSpeed.magnitude );
83             }
84             break;
85         case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
86             currentPosition = Vector3.Lerp( currentPosition, targetPosition, Time.deltaTime * m_Model.InterpolateLerpSpeed );
87             break;
88         /*case PhotonTransformViewPositionModel.InterpolateOptions.MoveTowardsComplex:
89             float distanceToTarget = Vector3.Distance( currentPosition, targetPosition );
90             float targetSpeed = m_Model.InterpolateSpeedCurve.Evaluate( distanceToTarget ) * m_Model.InterpolateMoveTowardsSpeed;
91
92             if( targetSpeed > m_CurrentSpeed )
93             {
94                 m_CurrentSpeed = Mathf.MoveTowards( m_CurrentSpeed, targetSpeed, Time.deltaTime * m_Model.InterpolateMoveTowardsAcceleration );
95             }
96             else
97             {
98                 m_CurrentSpeed = Mathf.MoveTowards( m_CurrentSpeed, targetSpeed, Time.deltaTime * m_Model.InterpolateMoveTowardsDeceleration );
99             }
100
101             //Debug.Log( m_CurrentSpeed + " - " + targetSpeed + " - " + transform.localPosition + " - " + targetPosition );
102
103             currentPosition = Vector3.MoveTowards( currentPosition, targetPosition, Time.deltaTime * m_CurrentSpeed );
104             break;*/
105         }
106
107         if( m_Model.TeleportEnabled == true )
108         {
109             if( Vector3.Distance( currentPosition, GetNetworkPosition() ) > m_Model.TeleportIfDistanceGreaterThan )
110             {
111                 currentPosition = GetNetworkPosition();
112             }
113         }
114
115         return currentPosition;
116     }
File name: PhotonTransformViewPositionControl.cs Copy
132     public Vector3 GetExtrapolatedPositionOffset()
133     {
134         float timePassed = (float)( PhotonNetwork.time - m_LastSerializeTime );
135
136         if( m_Model.ExtrapolateIncludingRoundTripTime == true )
137         {
138             timePassed += (float)PhotonNetwork.GetPing() / 1000f;
139         }
140
141         Vector3 extrapolatePosition = Vector3.zero;
142
143         switch( m_Model.ExtrapolateOption )
144         {
145         case PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues:
146             Quaternion turnRotation = Quaternion.Euler( 0, m_SynchronizedTurnSpeed * timePassed, 0 );
147             extrapolatePosition = turnRotation * ( m_SynchronizedSpeed * timePassed );
148             break;
149         case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
150             Vector3 moveDirection = ( m_NetworkPosition - GetOldestStoredNetworkPosition() ).normalized;
151
152             extrapolatePosition = moveDirection * m_Model.ExtrapolateSpeed * timePassed;
153             break;
154         case PhotonTransformViewPositionModel.ExtrapolateOptions.EstimateSpeedAndTurn:
155             Vector3 moveDelta = ( m_NetworkPosition - GetOldestStoredNetworkPosition() ) * PhotonNetwork.sendRateOnSerialize;
156             extrapolatePosition = moveDelta * timePassed;
157             break;
158         }
159
160         return extrapolatePosition;
161     }
File name: PhotonTransformViewPositionControl.cs Copy
183     void SerializeData( Vector3 currentPosition, PhotonStream stream, PhotonMessageInfo info )
184     {
185         stream.SendNext( currentPosition );
186
187         if( m_Model.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues ||
188             m_Model.InterpolateOption == PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues )
189         {
190             stream.SendNext( m_SynchronizedSpeed );
191             stream.SendNext( m_SynchronizedTurnSpeed );
192         }
193     }
File name: PhotonTransformViewPositionControl.cs Copy
195     void DeserializeData( PhotonStream stream, PhotonMessageInfo info )
196     {
197         m_OldNetworkPositions.Enqueue( m_NetworkPosition );
198
199         while( m_OldNetworkPositions.Count > m_Model.ExtrapolateNumberOfStoredPositions )
200         {
201             m_OldNetworkPositions.Dequeue();
202         }
203
204         m_NetworkPosition = (Vector3)stream.ReceiveNext();
205
206         if( m_Model.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.SynchronizeValues ||
207             m_Model.InterpolateOption == PhotonTransformViewPositionModel.InterpolateOptions.SynchronizeValues )
208         {
209             m_SynchronizedSpeed = (Vector3)stream.ReceiveNext();
210             m_SynchronizedTurnSpeed = (float)stream.ReceiveNext();
211         }
212     }

EXTRAPOLATE 149 lượt xem

Gõ tìm kiếm nhanh...