1
1
using NUnit . Framework ;
2
2
using System . Diagnostics ;
3
+ using UnityEngine ;
3
4
using Debug = UnityEngine . Debug ;
4
5
5
6
namespace UnitySpring . Tests
@@ -9,21 +10,25 @@ public class PerformanceTest
9
10
const int Count = 100000 ;
10
11
11
12
[ Test ]
12
- public static void ClosedForm ( )
13
+ public static void _ClosedForm ( )
13
14
=> Benchmark ( CreateSprings < ClosedForm . Spring > ( ) , "ClosedForm" ) ;
14
15
15
16
[ Test ]
16
- public static void SemiImplicitEuler ( )
17
+ public static void _SemiImplicitEuler ( )
17
18
=> Benchmark ( CreateSprings < SemiImplicitEuler . Spring > ( ) , "SemiImplicitEuler" ) ;
18
19
19
20
[ Test ]
20
- public static void ExplicitRK4 ( )
21
+ public static void _ExplicitRK4 ( )
21
22
=> Benchmark ( CreateSprings < ExplicitRK4 . Spring > ( ) , "ExplicitRK4" ) ;
22
23
23
24
[ Test ]
24
- public static void VerletIntegration ( )
25
+ public static void _VerletIntegration ( )
25
26
=> Benchmark ( CreateSprings < VerletIntegration . Spring > ( ) , "VerletIntegration" ) ;
26
27
28
+ [ Test ]
29
+ public static void UnitySmoothDamp ( )
30
+ => Benchmark ( CreateSprings < UnitySmoothDamp > ( ) , "UnitySmoothDamp" ) ;
31
+
27
32
static void Benchmark ( SpringBase [ ] springs , string name )
28
33
{
29
34
var sw = new Stopwatch ( ) ;
@@ -36,7 +41,7 @@ static void Benchmark(SpringBase[] springs, string name)
36
41
count ++ ;
37
42
}
38
43
39
- Debug . Log ( $ "[{ name } ] Average time for { Count } spring per frame : { sum / count } ms") ;
44
+ Debug . Log ( $ "[{ name } ] Average time for { Count } spring per frame : { ( sum / count ) . ToString ( "0.000" ) } ms") ;
40
45
41
46
double Step ( )
42
47
{
@@ -60,7 +65,31 @@ double Step()
60
65
61
66
static double GetPreciseTime ( Stopwatch sw )
62
67
{
63
- return 1000.0 * ( double ) sw . ElapsedTicks / Stopwatch . Frequency ;
68
+ return 1000.0 * ( double ) sw . ElapsedTicks / Stopwatch . Frequency ;
69
+ }
70
+ }
71
+
72
+ class UnitySmoothDamp : SpringBase
73
+ {
74
+ float smoothTime = 0.1f ;
75
+ float maxSpeed = Mathf . Infinity ;
76
+
77
+ public override void Reset ( )
78
+ {
79
+ currentValue = startValue ;
80
+ currentVelocity = initialVelocity ;
81
+ }
82
+
83
+ public override void UpdateEndValue ( float value , float velocity )
84
+ {
85
+ endValue = value ;
86
+ currentVelocity = velocity ;
87
+ }
88
+
89
+ public override float Evaluate ( float deltaTime )
90
+ {
91
+ currentValue = Mathf . SmoothDamp ( currentValue , endValue , ref currentVelocity , smoothTime , maxSpeed , deltaTime ) ;
92
+ return currentValue ;
64
93
}
65
94
}
66
- }
95
+ }
0 commit comments