Skip to content

Commit 373ed4c

Browse files
committed
Add documents
1 parent 87d567e commit 373ed4c

File tree

9 files changed

+484
-122
lines changed

9 files changed

+484
-122
lines changed
20 KB
Loading
6.6 KB
Loading

README.md

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[TOC]
22

3-
# MGS.UCurve
3+
# MGS.MonoCurve
44

55
## Summary
66

7-
- Smooth 3D curve for Unity project develop.
7+
- Smooth 3D curve component for Unity project develop.
88

99
## Environment
1010

@@ -18,53 +18,118 @@
1818
## Implemented
1919

2020
```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{}
2626
```
2727

2828
## Technology
2929

30-
### Hermite Polynomial Structure
30+
### Transform
3131

3232
```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);
3442
```
3543

36-
### Tangent Smooth
44+
### Calculus
3745

3846
```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;
4079
```
4180

4281
## Usage
4382

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.
5098
```
5199

52-
- Use time lapse to Evaluate target Vector3.
100+
101+
102+
- Evaluate point on the mono curve.
53103

54104
```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)
57121
{
58-
var p1 = curve.Evaluate(t);
122+
t = Mathf.Min(t + differ, 1.0f);
123+
var p1 = EvaluateNormalized(t);
59124
//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);
61126
p0 = p1;
62127
}
63128
```
64129

65130
## Demo
66131

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.
68133

69134
## Preview
70135

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
[TOC]
2+
3+
# MGS.Curve.dll
4+
5+
## Summary
6+
7+
- Smooth 3D curve for Unity project develop.
8+
9+
## Environment
10+
11+
- .Net Framework 3.5 or above.
12+
- Unity 5.0 or above.
13+
14+
## Platform
15+
16+
- Windows
17+
18+
## Implemented
19+
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{}
26+
```
27+
28+
## Technology
29+
30+
### Bezier Polynomial
31+
32+
```C#
33+
return Mathf.Pow(1 - t, 3) * anchor.from + 3 * t * Mathf.Pow(1 - t, 2) * anchor.frTangent +3 * (1 - t) * Mathf.Pow(t, 2) * anchor.toTangent + Mathf.Pow(t, 3) * anchor.to;
34+
```
35+
36+
### Hermite Polynomial
37+
38+
```C#
39+
/* Designed By Mogoson.
40+
* Hermite Polynomial Structure
41+
* Base: H(t) = v0a0(t) + v1a1(t) + m0b0(t) + m1b1(t)
42+
*
43+
* t-t0 t-t1 2
44+
* a0(t) = (1+2------)(------)
45+
* t1-t0 t0-t1
46+
*
47+
* t-t1 t-t0 2
48+
* a1(t) = (1+2------)(------)
49+
* t0-t1 t1-t0
50+
*
51+
* t-t1 2
52+
* b0(t) = (t-t0)(------)
53+
* t0-t1
54+
*
55+
* t-t0 2
56+
* b1(t) = (t-t1)(------)
57+
* t1-t0
58+
*
59+
* Let: d0 = t-t0, d1 = t-t1, d = t0-t1
60+
*
61+
* d0 d1
62+
* q0 = ---- , q1 = ----
63+
* d d
64+
*
65+
* t-t1 2 d1 2 2 t-t0 2 d0 2 2
66+
* p0 = (------) = (----) = q1 , p1 = (------) = (----) = q0
67+
* t0-t1 d t1-t0 -d
68+
*
69+
* Get: H(t) = (1-2q0)v0p0 + (1+2q1)v1p1 + mod0p0 + m1d1p1
70+
*/
71+
72+
var d0 = t - t0;
73+
var d1 = t - t1;
74+
var d = t0 - t1;
75+
76+
var q0 = d0 / d;
77+
var q1 = d1 / d;
78+
79+
var p0 = q1 * q1;
80+
var p1 = q0 * q0;
81+
82+
return (1 - 2 * q0) * v0 * p0 + (1 + 2 * q1) * v1 * p1 + m0 * d0 * p0 + m1 * d1 * p1;
83+
```
84+
85+
### Tangent Smooth
86+
87+
```C#
88+
//Designed By Mogoson.
89+
KeyFrame k0, k1, k2;
90+
if (index == 0 || index == frames.Count - 1)
91+
{
92+
if (frames[0].value != frames[frames.Count - 1].value)
93+
{
94+
var frame = frames[index];
95+
frame.inTangent = frame.outTangent = Vector3.zero;
96+
frames[index] = frame;
97+
return;
98+
}
99+
100+
k0 = frames[frames.Count - 2];
101+
k1 = frames[index];
102+
k2 = frames[1];
103+
104+
if (index == 0)
105+
{
106+
k0.time -= frames[frames.Count - 1].time;
107+
}
108+
else
109+
{
110+
k2.time += frames[frames.Count - 1].time;
111+
}
112+
}
113+
else
114+
{
115+
k0 = frames[index - 1];
116+
k1 = frames[index];
117+
k2 = frames[index + 1];
118+
}
119+
120+
var weight01 = (1 + weight) / 2;
121+
var weight12 = (1 - weight) / 2;
122+
var t01 = (k1.value - k0.value) / (k1.time - k0.time);
123+
var t12 = (k2.value - k1.value) / (k2.time - k1.time);
124+
k1.inTangent = k1.outTangent = t01 * weight01 + t12 * weight12;
125+
frames[index] = k1;
126+
```
127+
128+
## Usage
129+
130+
- New a Curve and Set Args.
131+
132+
```C#
133+
var curve = new HermiteCurve();
134+
curve.AddFrames(frames);
135+
curve.SmoothTangents();//If need SmoothTangents Auto.
136+
```
137+
138+
- Evaluate the curve at time.
139+
140+
```C#
141+
var p0 = curve.Evaluate(frames[0].time);
142+
for (float t = frames[0].time; t < frames[frames.Length - 1].time; t += delta)
143+
{
144+
var p1 = curve.Evaluate(t);
145+
//Just for demo, you can use p0,p1 to do more things.
146+
Gizmos.DrawLine(transform.TransformPoint(p0), transform.TransformPoint(p1));
147+
p0 = p1;
148+
}
149+
```
150+
151+
------
152+
153+
[Previous](../../README.md)
154+
155+
------
156+
157+
Copyright © 2021 Mogoson. mogoson@outlook.com

UnityProject/Assets/MGS.Packages/Curve/Plugins/MGS.Curve.md.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)