INTERPOLATE









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

Featured Snippets


File name: CubeLerp.cs Copy
35     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
36     {
37         if (stream.isWriting)
38         {
39             Vector3 pos = transform.localPosition;
40             Quaternion rot = transform.localRotation;
41             stream.Serialize(ref pos);
42             stream.Serialize(ref rot);
43         }
44         else
45         {
46             // Receive latest state information
47             Vector3 pos = Vector3.zero;
48             Quaternion rot = Quaternion.identity;
49
50             stream.Serialize(ref pos);
51             stream.Serialize(ref rot);
52
53             latestCorrectPos = pos; // save this to move towards it in FixedUpdate()
54             onUpdatePos = transform.localPosition; // we interpolate from here to latestCorrectPos
55             fraction = 0; // reset the fraction we alreay moved. see Update()
56
57             transform.localRotation = rot; // this sample doesn't smooth rotation
58         }
59     }
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
163     private float GetInterpolateHelpBoxHeight()
164     {
165         return PhotonGUI.RichLabel.CalcHeight(new GUIContent(INTERPOLATE_HELP), Screen.width - 54) + 35;
166     }
File name: PhotonTransformViewEditor.cs Copy
264         PhotonTransformViewPositionModel.InterpolateOptions interpolateOption)
265     {
266         DrawPropertyWithHelpIcon(ref propertyRect, ref this.m_InterpolateHelpOpen, interpolatePositionProperty, INTERPOLATE_TOOLTIP);
267         DrawHelpBox(ref propertyRect, this.m_InterpolateHelpOpen, GetInterpolateHelpBoxHeight(), INTERPOLATE_HELP, INTERPOLATE_HELP_URL);
268
269         switch (interpolateOption)
270         {
271             case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
272                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_PositionModel.InterpolateMoveTowardsSpeed"),
273                     new GUIContent("MoveTowards Speed"));
274                 propertyRect.y += EDITOR_LINE_HEIGHT;
275                 break;
276
277             case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
278                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_PositionModel.InterpolateLerpSpeed"), new GUIContent("Lerp Speed"));
279                 propertyRect.y += EDITOR_LINE_HEIGHT;
280                 break;
281
282             /*case PhotonTransformViewPositionModel.InterpolateOptions.MoveTowardsComplex:
283                 Rect curveRect = new Rect(propertyRect.xMin, propertyRect.yMin, propertyRect.width - 100, propertyRect.height);
284                 EditorGUI.PropertyField(curveRect, serializedObject.FindProperty("m_PositionModel.InterpolateSpeedCurve"), new GUIContent("MoveTowards Speed Curve"));
285
286                 Rect labelRect = new Rect(propertyRect.xMax - 95, propertyRect.yMin, 10, propertyRect.height);
287                 GUI.Label(labelRect, "x");
288
289                 Rect multiplierRect = new Rect(propertyRect.xMax - 80, propertyRect.yMin, 80, propertyRect.height);
290                 EditorGUI.PropertyField(multiplierRect, serializedObject.FindProperty("m_PositionModel.InterpolateMoveTowardsSpeed"), GUIContent.none);
291                 propertyRect.y += EDITOR_LINE_HEIGHT;
292
293                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_PositionModel.InterpolateMoveTowardsAcceleration"),
294                     new GUIContent("Acceleration"));
295                 propertyRect.y += EDITOR_LINE_HEIGHT;
296
297                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_PositionModel.InterpolateMoveTowardsDeceleration"),
298                     new GUIContent("Deceleration"));
299                 propertyRect.y += EDITOR_LINE_HEIGHT;
300                 break;*/
301         }
302     }
File name: PhotonTransformViewEditor.cs Copy
309     private void DrawSynchronizeRotationData()
310     {
311         if (this.m_SynchronizeRotationProperty == null || this.m_SynchronizeRotationProperty.boolValue == false)
312         {
313             return;
314         }
315
316         SerializedProperty interpolateRotationProperty = serializedObject.FindProperty("m_RotationModel.InterpolateOption");
317         PhotonTransformViewRotationModel.InterpolateOptions interpolateOption =
318             (PhotonTransformViewRotationModel.InterpolateOptions) interpolateRotationProperty.enumValueIndex;
319
320         float containerHeight = 20;
321
322         switch (interpolateOption)
323         {
324             case PhotonTransformViewRotationModel.InterpolateOptions.RotateTowards:
325             case PhotonTransformViewRotationModel.InterpolateOptions.Lerp:
326                 containerHeight += EDITOR_LINE_HEIGHT;
327                 break;
328         }
329
330         if (this.m_InterpolateRotationHelpOpen == true)
331         {
332             containerHeight += GetInterpolateHelpBoxHeight();
333         }
334
335         Rect rect = PhotonGUI.ContainerBody(containerHeight);
336         Rect propertyRect = new Rect(rect.xMin + 5, rect.yMin + 2, rect.width - 10, EditorGUIUtility.singleLineHeight);
337
338         DrawPropertyWithHelpIcon(ref propertyRect, ref this.m_InterpolateRotationHelpOpen, interpolateRotationProperty, INTERPOLATE_TOOLTIP);
339         DrawHelpBox(ref propertyRect, this.m_InterpolateRotationHelpOpen, GetInterpolateHelpBoxHeight(), INTERPOLATE_HELP, INTERPOLATE_HELP_URL);
340
341         switch (interpolateOption)
342         {
343             case PhotonTransformViewRotationModel.InterpolateOptions.RotateTowards:
344                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_RotationModel.InterpolateRotateTowardsSpeed"),
345                     new GUIContent("RotateTowards Speed"));
346                 break;
347             case PhotonTransformViewRotationModel.InterpolateOptions.Lerp:
348                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_RotationModel.InterpolateLerpSpeed"), new GUIContent("Lerp Speed"));
349                 break;
350         }
351     }
File name: PhotonTransformViewEditor.cs Copy
358     private void DrawSynchronizeScaleData()
359     {
360         if (this.m_SynchronizeScaleProperty == null || this.m_SynchronizeScaleProperty.boolValue == false)
361         {
362             return;
363         }
364
365         SerializedProperty interpolateScaleProperty = serializedObject.FindProperty("m_ScaleModel.InterpolateOption");
366         PhotonTransformViewScaleModel.InterpolateOptions interpolateOption = (PhotonTransformViewScaleModel.InterpolateOptions) interpolateScaleProperty.enumValueIndex;
367
368         float containerHeight = EDITOR_LINE_HEIGHT;
369
370         switch (interpolateOption)
371         {
372             case PhotonTransformViewScaleModel.InterpolateOptions.MoveTowards:
373             case PhotonTransformViewScaleModel.InterpolateOptions.Lerp:
374                 containerHeight += EDITOR_LINE_HEIGHT;
375                 break;
376         }
377
378         if (this.m_InterpolateScaleHelpOpen == true)
379         {
380             containerHeight += GetInterpolateHelpBoxHeight();
381         }
382
383         Rect rect = PhotonGUI.ContainerBody(containerHeight);
384         Rect propertyRect = new Rect(rect.xMin + 5, rect.yMin + 2, rect.width - 10, EditorGUIUtility.singleLineHeight);
385
386         DrawPropertyWithHelpIcon(ref propertyRect, ref this.m_InterpolateScaleHelpOpen, interpolateScaleProperty, INTERPOLATE_TOOLTIP);
387         DrawHelpBox(ref propertyRect, this.m_InterpolateScaleHelpOpen, GetInterpolateHelpBoxHeight(), INTERPOLATE_HELP, INTERPOLATE_HELP_URL);
388
389         switch (interpolateOption)
390         {
391             case PhotonTransformViewScaleModel.InterpolateOptions.MoveTowards:
392                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_ScaleModel.InterpolateMoveTowardsSpeed"),
393                     new GUIContent("MoveTowards Speed"));
394                 break;
395             case PhotonTransformViewScaleModel.InterpolateOptions.Lerp:
396                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_ScaleModel.InterpolateLerpSpeed"), new GUIContent("Lerp Speed"));
397                 break;
398         }
399     }
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
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     }
File name: PhotonTransformViewPositionModel.cs Copy
35     public AnimationCurve InterpolateSpeedCurve = new AnimationCurve( new Keyframe[] {
36                                                                               new Keyframe( -1, 0, 0, Mathf.Infinity ),
37                                                                               new Keyframe( 0, 1, 0, 0 ),
38                                                                               new Keyframe( 1, 1, 0, 1 ),
39                                                                               new Keyframe( 4, 4, 1, 0 ) } );

INTERPOLATE 126 lượt xem

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