Skip to content

Commit

Permalink
Performance optimization for [0,1] range normalizedT calculation in l…
Browse files Browse the repository at this point in the history
…ooping splines
  • Loading branch information
yasirkula committed Feb 1, 2023
1 parent 0243225 commit 40fa038
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 56 deletions.
35 changes: 5 additions & 30 deletions Plugins/BezierSolution/BezierSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,7 @@ public Vector3 GetPoint( float normalizedT )
return endPoints[endPoints.Count - 1].position;
}
else
{
while( normalizedT < 0f )
normalizedT += 1f;
while( normalizedT >= 1f )
normalizedT -= 1f;
}
normalizedT = ( ( normalizedT % 1f ) + 1f ) % 1f;

float t = normalizedT * ( m_loop ? endPoints.Count : ( endPoints.Count - 1 ) );

Expand Down Expand Up @@ -663,12 +658,7 @@ public Vector3 GetTangent( float normalizedT )
}
}
else
{
while( normalizedT < 0f )
normalizedT += 1f;
while( normalizedT >= 1f )
normalizedT -= 1f;
}
normalizedT = ( ( normalizedT % 1f ) + 1f ) % 1f;

float t = normalizedT * ( m_loop ? endPoints.Count : ( endPoints.Count - 1 ) );

Expand Down Expand Up @@ -701,12 +691,7 @@ public Vector3 GetNormal( float normalizedT )
return endPoints[endPoints.Count - 1].normal;
}
else
{
while( normalizedT < 0f )
normalizedT += 1f;
while( normalizedT >= 1f )
normalizedT -= 1f;
}
normalizedT = ( ( normalizedT % 1f ) + 1f ) % 1f;

float t = normalizedT * ( m_loop ? endPoints.Count : ( endPoints.Count - 1 ) );

Expand Down Expand Up @@ -757,12 +742,7 @@ public BezierPoint.ExtraData GetExtraData( float normalizedT, ExtraDataLerpFunct
return endPoints[endPoints.Count - 1].extraData;
}
else
{
while( normalizedT < 0f )
normalizedT += 1f;
while( normalizedT >= 1f )
normalizedT -= 1f;
}
normalizedT = ( ( normalizedT % 1f ) + 1f ) % 1f;

float t = normalizedT * ( m_loop ? endPoints.Count : ( endPoints.Count - 1 ) );

Expand Down Expand Up @@ -816,12 +796,7 @@ public Segment GetSegmentAt( float normalizedT )
return new Segment( endPoints[endPoints.Count - 2], endPoints[endPoints.Count - 1], 1f );
}
else
{
while( normalizedT < 0f )
normalizedT += 1f;
while( normalizedT >= 1f )
normalizedT -= 1f;
}
normalizedT = ( ( normalizedT % 1f ) + 1f ) % 1f;

float t = normalizedT * ( m_loop ? endPoints.Count : ( endPoints.Count - 1 ) );

Expand Down
28 changes: 4 additions & 24 deletions Plugins/BezierSolution/Other/BezierDataStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,7 @@ public float GetNormalizedTAtPercentage( float percentage )
return 1f;
}
else
{
while( percentage < 0f )
percentage += 1f;
while( percentage >= 1f )
percentage -= 1f;
}
percentage = ( ( percentage % 1f ) + 1f ) % 1f;

float indexRaw = ( uniformNormalizedTs.Length - 1 ) * percentage;
int index = (int) indexRaw;
Expand All @@ -168,12 +163,7 @@ public float GetPercentageAtNormalizedT( float normalizedT )
return 1f;
}
else
{
while( normalizedT < 0f )
normalizedT += 1f;
while( normalizedT >= 1f )
normalizedT -= 1f;
}
normalizedT = ( ( normalizedT % 1f ) + 1f ) % 1f;

// Perform binary search
int lowerBound = 0;
Expand Down Expand Up @@ -227,12 +217,7 @@ public BezierPoint.ExtraData GetExtraData( float percentage, ExtraDataLerpFuncti
return extraDatas[extraDatas.Length - 1];
}
else
{
while( percentage < 0f )
percentage += 1f;
while( percentage >= 1f )
percentage -= 1f;
}
percentage = ( ( percentage % 1f ) + 1f ) % 1f;

float t = percentage * ( loop ? extraDatas.Length : ( extraDatas.Length - 1 ) );

Expand All @@ -255,12 +240,7 @@ private Vector3 LerpArray( Vector3[] array, float percentage )
return array[array.Length - 1];
}
else
{
while( percentage < 0f )
percentage += 1f;
while( percentage >= 1f )
percentage -= 1f;
}
percentage = ( ( percentage % 1f ) + 1f ) % 1f;

float t = percentage * ( loop ? array.Length : ( array.Length - 1 ) );

Expand Down
2 changes: 1 addition & 1 deletion Plugins/BezierSolution/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Bezier Solution =
= Bezier Solution (v2.3.3) =

Online documentation & example code available at: https://github.com/yasirkula/UnityBezierSolution
E-mail: [email protected]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.beziersolution",
"displayName": "Bezier Solution",
"version": "2.3.2",
"version": "2.3.3",
"documentationUrl": "https://github.com/yasirkula/UnityBezierSolution",
"changelogUrl": "https://github.com/yasirkula/UnityBezierSolution/releases",
"licensesUrl": "https://github.com/yasirkula/UnityBezierSolution/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 40fa038

Please sign in to comment.