Lerping Vectorisity spline
I am drawing a graph using Vectorisity. Every time I get a new measurement I would like to interpolate between the two states of the graph, the old data and the new. I am doing this using a coroutine, however it isn't working yet.
IEnumerator DrawGraph(Vector3[] coordinates, float duration)
{
float startTime = Time.time;
float endTime = startTime + duration;
while (Time.time <= endTime)
{
float t = (Time.time - startTime) / duration;
for(int i = 0; i < coordinates.Length; i++)
{
coordinates[i] = Vector3.Lerp(previousCoordinateArray[i], coordinates[i], t);
}
GraphSpline.MakeSpline(coordinates, coordinates.Length, false);
GraphSpline.Draw3D();
yield return null;
}
}
Could you suggest me any fixes?
Comment
It is not interpolating between the coordinates, it just snaps from one state to another.
Try replacing your yield return with:
yield return new WaitForEndOfFrame ();
It is still not working unfortunately. Here is a little bit larger snippet of my current code:
void DrawSpline(List<Vector3> coordinates)
{
Vector3[] coordinateArray = coordinates.ToArray();
if(previousCoordinateArray == null)
{
GraphSpline = new VectorLine("GraphSpline", new List<Vector3>(coordinates.Count + 1), GraphTexture, GraphLineWidth, LineType.Continuous);
GraphSpline.joins = Joins.Weld;
GraphSpline.continuousTexture = true;
GraphSpline.$$anonymous$$akeSpline(coordinateArray, coordinateArray.Length, false);
GraphSpline.Draw3D();
}
else
{
//CreateStaticGraph(coordinateArray);
StartCoroutine(CreateGraph(coordinateArray, 5f));
}
previousCoordinateArray = coordinateArray;
}
IEnumerator CreateGraph(Vector3[] coordinates, float duration)
{
float startTime = Time.time;
float endTime = startTime + duration;
while (Time.time <= endTime)
{
float t = (Time.time - startTime) / duration;
for(int i = 0; i < coordinates.Length; i++)
{
coordinates[i] = Vector3.Lerp(previousCoordinateArray[i], coordinates[i], t);
}
GraphSpline.$$anonymous$$akeSpline(coordinates, coordinates.Length, false);
GraphSpline.Draw3D();
yield return new WaitForEndOfFrame();
}
}
Looks like you might've been assigning the coordinates in reverse order.
What does it do if you try this:
IEnumerator CreateGraph(Vector3[] coordinates, float duration)
{
float startTime = Time.time;
float currentTime = Time.time;
float endTime = currentTime + duration;
while (currentTime <= endTime)
{
float t = (currentTime - startTime) / duration;
for(int i = 0; i < coordinates.Length; i++)
{
previousCoordinateArray[i] = Vector3.Lerp(previousCoordinateArray[i], coordinates[i], t);
}
GraphSpline.$$anonymous$$akeSpline(previousCoordinateArray, previousCoordinateArray.Length, false);
GraphSpline.Draw3D();
yield return new WaitForEndOfFrame();
// Resample the time after that yield finishes
currentTime = Time.time;
}
// Snap these in case we have a ti$$anonymous$$g issue
GraphSpline.$$anonymous$$akeSpline(coordinates, coordinates.Length, false);
GraphSpline.Draw3D();
}
I have made it work, I needed to store the vectors from the lerping in a temporary array.
IEnumerator CreateGraph(Vector3[] coordinates, float duration)
{
float startTime = Time.time;
float endTime = startTime + duration;
while (Time.time <= endTime)
{
float t = (Time.time - startTime) / duration;
for(int i = 0; i < coordinates.Length; i++)
{
tempCoordinateArray[i] = Vector3.Lerp(previousCoordinateArray[i], coordinates[i], t);
}
GraphSpline.$$anonymous$$akeSpline(tempCoordinateArray, coordinates.Length, false);
GraphSpline.Draw3D();
tempCoordinateArray = previousCoordinateArray;
yield return new WaitForEndOfFrame();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612100003im_/https://answers.unity.com/themes/thub/images/avi.jpg)