@@ -75,6 +75,10 @@ public SplineAutoConstructMode autoConstructMode
75
75
}
76
76
}
77
77
}
78
+
79
+ private Vector3 [ ] autoConstructedSplineRhs ;
80
+ private Vector3 [ ] autoConstructedSplineControlPoints ;
81
+ private float [ ] autoConstructedSplineTmp ;
78
82
79
83
[ SerializeField , HideInInspector ]
80
84
[ UnityEngine . Serialization . FormerlySerializedAs ( "Internal_AutoCalculateNormals" ) ]
@@ -1048,57 +1052,56 @@ public void AutoConstructSpline()
1048
1052
return ;
1049
1053
}
1050
1054
1051
- Vector3 [ ] rhs ;
1052
- if ( m_loop )
1053
- rhs = new Vector3 [ n + 1 ] ;
1054
- else
1055
- rhs = new Vector3 [ n ] ;
1055
+ int rhsLength = m_loop ? n + 1 : n ;
1056
+ if ( autoConstructedSplineRhs == null || autoConstructedSplineRhs . Length != rhsLength )
1057
+ autoConstructedSplineRhs = new Vector3 [ rhsLength ] ;
1058
+ if ( autoConstructedSplineControlPoints == null || rhsLength != autoConstructedSplineControlPoints . Length )
1059
+ autoConstructedSplineControlPoints = new Vector3 [ rhsLength ] ; // Solution vector
1060
+ if ( autoConstructedSplineTmp == null || rhsLength != autoConstructedSplineTmp . Length )
1061
+ autoConstructedSplineTmp = new float [ rhsLength ] ; // Temp workspace
1062
+
1056
1063
1057
1064
for ( int i = 1 ; i < n - 1 ; i ++ )
1058
- rhs [ i ] = 4 * endPoints [ i ] . position + 2 * endPoints [ i + 1 ] . position ;
1065
+ autoConstructedSplineRhs [ i ] = 4 * endPoints [ i ] . position + 2 * endPoints [ i + 1 ] . position ;
1059
1066
1060
- rhs [ 0 ] = endPoints [ 0 ] . position + 2 * endPoints [ 1 ] . position ;
1067
+ autoConstructedSplineRhs [ 0 ] = endPoints [ 0 ] . position + 2 * endPoints [ 1 ] . position ;
1061
1068
1062
1069
if ( ! m_loop )
1063
- rhs [ n - 1 ] = ( 8 * endPoints [ n - 1 ] . position + endPoints [ n ] . position ) * 0.5f ;
1070
+ autoConstructedSplineRhs [ n - 1 ] = ( 8 * endPoints [ n - 1 ] . position + endPoints [ n ] . position ) * 0.5f ;
1064
1071
else
1065
1072
{
1066
- rhs [ n - 1 ] = 4 * endPoints [ n - 1 ] . position + 2 * endPoints [ n ] . position ;
1067
- rhs [ n ] = ( 8 * endPoints [ n ] . position + endPoints [ 0 ] . position ) * 0.5f ;
1073
+ autoConstructedSplineRhs [ n - 1 ] = 4 * endPoints [ n - 1 ] . position + 2 * endPoints [ n ] . position ;
1074
+ autoConstructedSplineRhs [ n ] = ( 8 * endPoints [ n ] . position + endPoints [ 0 ] . position ) * 0.5f ;
1068
1075
}
1069
1076
1070
1077
// Get first control points
1071
- int rhsLength = rhs . Length ;
1072
- Vector3 [ ] controlPoints = new Vector3 [ rhsLength ] ; // Solution vector
1073
- float [ ] tmp = new float [ rhsLength ] ; // Temp workspace
1074
-
1075
1078
float b = 2f ;
1076
- controlPoints [ 0 ] = rhs [ 0 ] / b ;
1079
+ autoConstructedSplineControlPoints [ 0 ] = autoConstructedSplineRhs [ 0 ] / b ;
1077
1080
for ( int i = 1 ; i < rhsLength ; i ++ ) // Decomposition and forward substitution
1078
1081
{
1079
1082
float val = 1f / b ;
1080
- tmp [ i ] = val ;
1083
+ autoConstructedSplineTmp [ i ] = val ;
1081
1084
b = ( i < rhsLength - 1 ? 4f : 3.5f ) - val ;
1082
- controlPoints [ i ] = ( rhs [ i ] - controlPoints [ i - 1 ] ) / b ;
1085
+ autoConstructedSplineControlPoints [ i ] = ( autoConstructedSplineRhs [ i ] - autoConstructedSplineControlPoints [ i - 1 ] ) / b ;
1083
1086
}
1084
1087
1085
1088
for ( int i = 1 ; i < rhsLength ; i ++ )
1086
- controlPoints [ rhsLength - i - 1 ] -= tmp [ rhsLength - i ] * controlPoints [ rhsLength - i ] ; // Backsubstitution
1089
+ autoConstructedSplineControlPoints [ rhsLength - i - 1 ] -= autoConstructedSplineTmp [ rhsLength - i ] * autoConstructedSplineControlPoints [ rhsLength - i ] ; // Back substitution
1087
1090
1088
1091
for ( int i = 0 ; i < n ; i ++ )
1089
1092
{
1090
1093
// First control point
1091
- endPoints [ i ] . followingControlPointPosition = controlPoints [ i ] ;
1094
+ endPoints [ i ] . followingControlPointPosition = autoConstructedSplineControlPoints [ i ] ;
1092
1095
1093
1096
if ( m_loop )
1094
- endPoints [ i + 1 ] . precedingControlPointPosition = 2 * endPoints [ i + 1 ] . position - controlPoints [ i + 1 ] ;
1097
+ endPoints [ i + 1 ] . precedingControlPointPosition = 2 * endPoints [ i + 1 ] . position - autoConstructedSplineControlPoints [ i + 1 ] ;
1095
1098
else
1096
1099
{
1097
1100
// Second control point
1098
1101
if ( i < n - 1 )
1099
- endPoints [ i + 1 ] . precedingControlPointPosition = 2 * endPoints [ i + 1 ] . position - controlPoints [ i + 1 ] ;
1102
+ endPoints [ i + 1 ] . precedingControlPointPosition = 2 * endPoints [ i + 1 ] . position - autoConstructedSplineControlPoints [ i + 1 ] ;
1100
1103
else
1101
- endPoints [ i + 1 ] . precedingControlPointPosition = ( endPoints [ n ] . position + controlPoints [ n - 1 ] ) * 0.5f ;
1104
+ endPoints [ i + 1 ] . precedingControlPointPosition = ( endPoints [ n ] . position + autoConstructedSplineControlPoints [ n - 1 ] ) * 0.5f ;
1102
1105
}
1103
1106
}
1104
1107
@@ -1501,4 +1504,4 @@ internal void Reset()
1501
1504
}
1502
1505
#endif
1503
1506
}
1504
- }
1507
+ }
0 commit comments