|
1 | 1 | [TOC] |
2 | 2 |
|
3 | | -# MGS.UCurve |
| 3 | +# MGS.MonoCurve |
4 | 4 |
|
5 | 5 | ## Summary |
6 | 6 |
|
7 | | -- Smooth 3D curve for Unity project develop. |
| 7 | +- Smooth 3D curve component for Unity project develop. |
8 | 8 |
|
9 | 9 | ## Environment |
10 | 10 |
|
|
18 | 18 | ## Implemented |
19 | 19 |
|
20 | 20 | ```C# |
21 | | -public class SinCurve : ITimeCurve{} |
22 | | -public class EllipseCurve : ITimeCurve{} |
23 | | -public class HelixCurve : ITimeCurve{} |
24 | | -public class BezierCurve : ITimeCurve{} |
25 | | -public class HermiteCurve : ITimeCurve{} |
| 21 | +public class MonoSinCurve : MonoCurve{} |
| 22 | +public class MonoEllipseCurve : MonoCurve{} |
| 23 | +public class MonoHelixCurve : MonoCurve{} |
| 24 | +public class MonoBezierCurve : MonoCurve{} |
| 25 | +public class MonoHermiteCurve : MonoCurve{} |
26 | 26 | ``` |
27 | 27 |
|
28 | 28 | ## Technology |
29 | 29 |
|
30 | | -### Hermite Polynomial Structure |
| 30 | +### Transform |
31 | 31 |
|
32 | 32 | ```C# |
33 | | - |
| 33 | +//World to local position. |
| 34 | +return transform.TransformPoint(worldPos); |
| 35 | +//Local to world position. |
| 36 | +transform.InverseTransformPoint(localPos); |
| 37 | + |
| 38 | +//World to local vector. |
| 39 | +return transform.TransformPoint(worldVector); |
| 40 | +//Local to world vector. |
| 41 | +transform.InverseTransformPoint(localVector); |
34 | 42 | ``` |
35 | 43 |
|
36 | | -### Tangent Smooth |
| 44 | +### Calculus |
37 | 45 |
|
38 | 46 | ```C# |
39 | | - |
| 47 | +//Evaluate length of MonoSinCurve. |
| 48 | +var halfPI = Mathf.PI * 0.5f; |
| 49 | +var angularAbs = Mathf.Abs(args.angular); |
| 50 | +var piece = Vector2.Distance(Vector2.zero, new Vector2(halfPI / angularAbs, args.amplitude)); |
| 51 | +var pieces = piece * angularAbs; |
| 52 | +var segments = Mathf.RoundToInt(radian / halfPI); |
| 53 | +return pieces * segments; |
| 54 | + |
| 55 | +//Evaluate length of MonoEllipseCurve. |
| 56 | +var ratio = Mathf.Abs(radian) / (Mathf.PI * 2); |
| 57 | +if (args.semiMinorAxis == 0 || args.semiMajorAxis == 0) |
| 58 | +{ |
| 59 | + return 2 * Mathf.Abs(args.semiMinorAxis + args.semiMajorAxis) * ratio; |
| 60 | +} |
| 61 | +var minor = Mathf.Abs(args.semiMinorAxis); |
| 62 | +var major = Mathf.Abs(args.semiMajorAxis); |
| 63 | +var a = Mathf.Max(minor, major); |
| 64 | +var b = Mathf.Min(minor, major); |
| 65 | +return (2 * Mathf.PI * b + 4 * (a - b)) * ratio; |
| 66 | + |
| 67 | +//Evaluate length of MonoHelixCurve, MonoBezierCurve and MonoHermiteCurve. |
| 68 | +var length = 0f; |
| 69 | +var t = 0f; |
| 70 | +var p0 = EvaluateNormalized(t); |
| 71 | +while (t < 1.0f) |
| 72 | +{ |
| 73 | + t = Mathf.Min(t + differ, 1.0f); |
| 74 | + var p1 = EvaluateNormalized(t); |
| 75 | + length += Vector3.Distance(p0, p1); |
| 76 | + p0 = p1; |
| 77 | +} |
| 78 | +return length; |
40 | 79 | ``` |
41 | 80 |
|
42 | 81 | ## Usage |
43 | 82 |
|
44 | | -- New a Curve and Set Args to it. |
45 | | - |
46 | | -```C# |
47 | | -var curve = new HermiteCurve(); |
48 | | -curve.AddFrames(frames); |
49 | | -curve.SmoothTangents();//If need SmoothTangents Auto. |
| 83 | +- Attach mono curve component to a game object. |
| 84 | +- Adjust the args of curve component or edit curve in scene editor. |
| 85 | + |
| 86 | +```tex |
| 87 | +Select the MonoBezierCurve and drag the handle to adjust the anchor to see effect. |
| 88 | +Press and hold the ALT into Tangent Edit mode, drag the handle to adjust the tangent of anchor. |
| 89 | +If the start and end points are close, they will stick together. |
| 90 | +
|
| 91 | +Select the MonoHermiteCurve and drag the handle to adjust the anchor to see effect. |
| 92 | +Press and hold the CTRL into Insert Edit mode, click the green handle to add a new anchor. |
| 93 | +Press and hold the CTRL+SHIFT into Remove Edit mode, click the red handle to remove a anchor. |
| 94 | +If do not use Auto Smooth, |
| 95 | +Press and hold the ALT into Tangent Edit mode, drag the handle to adjust the tangent of anchor. |
| 96 | +Press and hold the ALT+SHIFT into InOutTangent Edit mode, drag the handle to adjust the in out tangents of anchor. |
| 97 | +If the start and end points are close, they will stick together. |
50 | 98 | ``` |
51 | 99 |
|
52 | | -- Use time lapse to Evaluate target Vector3. |
| 100 | + |
| 101 | + |
| 102 | +- Evaluate point on the mono curve. |
53 | 103 |
|
54 | 104 | ```C# |
55 | | -var p0 = curve.Evaluate(frames[0].time); |
56 | | -for (float t = frames[0].time; t < frames[frames.Length - 1].time; t += delta) |
| 105 | +//Evaluate point on the mono curve at length. |
| 106 | +var len = 0f; |
| 107 | +var p0 = Target.Evaluate(len); |
| 108 | +while (len < Target.Length) |
| 109 | +{ |
| 110 | + len = Mathf.Min(len + 0.01f, Target.Length); |
| 111 | + var p1 = Target.Evaluate(len); |
| 112 | + //Just for demo, you can use p0,p1 to do more things. |
| 113 | + Gizmos.DrawLine(p0, p1); |
| 114 | + p0 = p1; |
| 115 | +} |
| 116 | + |
| 117 | +//Evaluate the curve at normalized time int the range[0,1]. |
| 118 | +var t = 0f; |
| 119 | +var p0 = EvaluateNormalized(t); |
| 120 | +while (t < 1.0f) |
57 | 121 | { |
58 | | - var p1 = curve.Evaluate(t); |
| 122 | + t = Mathf.Min(t + differ, 1.0f); |
| 123 | + var p1 = EvaluateNormalized(t); |
59 | 124 | //Just for demo, you can use p0,p1 to do more things. |
60 | | - Gizmos.DrawLine(transform.TransformPoint(p0), transform.TransformPoint(p1)); |
| 125 | + Gizmos.DrawLine(p0, p1); |
61 | 126 | p0 = p1; |
62 | 127 | } |
63 | 128 | ``` |
64 | 129 |
|
65 | 130 | ## Demo |
66 | 131 |
|
67 | | -- Demos in the path "MGS.Packages/UCurve/Demo/" provide reference to you. |
| 132 | +- Demos in the path "MGS.Packages/Curve/Demo/" provide reference to you. |
68 | 133 |
|
69 | 134 | ## Preview |
70 | 135 |
|
|
0 commit comments