-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatternBuilder.cs
222 lines (182 loc) · 7.32 KB
/
PatternBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
namespace Sensation {
public enum Connection {
NotDefined,
Straight,
Smooth,
Flat
}
public class TrackBuilder {
private class KeyframeBuilder {
private class Tangent {
public float angleTangens = float.NaN;
public float time = float.NaN;
public float value = float.NaN;
public float angle {
get {
return (float)Math.Atan(angleTangens);
}
set {
angleTangens = (float)Math.Tan(value);
}
}
public Tangent(float angleTangens) {
this.angleTangens = angleTangens;
}
}
public float time;
public float value;
public Connection leftConnection = Connection.NotDefined;
public Connection rightConnection = Connection.NotDefined;
private Tangent inTangent;
private Tangent outTangent;
public KeyframeBuilder previous;
public KeyframeBuilder next;
public KeyframeBuilder(float time, float value, Connection leftConnection, Connection rightConnection) {
this.time = time;
this.value = value;
this.leftConnection = leftConnection;
this.rightConnection = rightConnection;
}
public KeyframeBuilder(float time, float value, float inAngle, float outAngle) {
this.time = time;
this.value = value;
this.inTangent = new Tangent(inAngle);
this.outTangent = new Tangent(outAngle);
}
private float DeltaTime(float from, float to) {
var delta = to - from;
if (Math.Abs(delta) < 0.0001f) {
delta = 0.0001f * Math.Sign(delta);
}
return delta;
}
public Track.Keyframe Build() {
// calculate tangents for straight or flat connection (tan(angle))
if (previous != null) {
if (leftConnection != Connection.NotDefined) {
var angleTangens = leftConnection == Connection.Flat ? 0 : (value - previous.value) / DeltaTime(previous.time, time);
inTangent = new Tangent(angleTangens);
}
} else {
inTangent = null;
leftConnection = Connection.NotDefined;
}
if (next != null) {
if (rightConnection != Connection.NotDefined) {
var angleTangens = rightConnection == Connection.Flat ? 0 : (next.value - value) / DeltaTime(time, next.time);
outTangent = new Tangent(angleTangens);
}
} else {
outTangent = null;
rightConnection = Connection.NotDefined;
}
// smooth
if (inTangent != null && outTangent != null) {
// the closer to a point, the higher the weight
var outAngleWeight = DeltaTime(previous.time, time) / DeltaTime(previous.time, next.time);
var inAngleWeight = 1 - outAngleWeight;
var averageAngle = inTangent.angle * inAngleWeight + outTangent.angle * outAngleWeight;
if (leftConnection == Connection.Smooth) {
inTangent.angle = averageAngle;
}
if (rightConnection == Connection.Smooth) {
outTangent.angle = averageAngle;
}
}
// calculate tangents
if (inTangent != null) {
var tangentTime = DeltaTime(previous.time, time) / 3f;
inTangent.time = time - tangentTime;
inTangent.value = value - tangentTime * inTangent.angleTangens;
}
if (outTangent != null) {
var tangentTime = DeltaTime(time, next.time) / 3f;
outTangent.time = time + tangentTime;
outTangent.value = value + tangentTime * outTangent.angleTangens;
}
// build result
var keyframe = new Track.Keyframe();
keyframe.ControlPoint = BuildPoint(time, value);
if (inTangent != null) {
keyframe.InTangentStart = BuildPoint(inTangent.time, inTangent.value);
}
if (outTangent != null) {
keyframe.OutTangentEnd = BuildPoint(outTangent.time, outTangent.value);
}
return keyframe;
}
private Track.Keyframe.Point BuildPoint(float time, float value) {
var point = new Track.Keyframe.Point();
point.Time = time;
point.Value = value;
return point;
}
}
private Vibration.Region region;
private int actorIndex;
private SortedDictionary<float, KeyframeBuilder> keyframeBuilders = new SortedDictionary<float, KeyframeBuilder>();
public TrackBuilder(Vibration.Region region, int actorIndex) {
this.region = region;
this.actorIndex = actorIndex;
}
public TrackBuilder AddKeyframe(float time, float value) {
return AddKeyframe(time, value, Connection.Smooth);
}
public TrackBuilder AddKeyframe(float time, float value, Connection connection) {
return AddKeyframe(time, value, connection, connection);
}
public TrackBuilder AddKeyframe(float time, float value, Connection leftConnection, Connection rightConnection) {
return AddKeyframeBuilder(new KeyframeBuilder(time, value, leftConnection, rightConnection));
}
public TrackBuilder AddKeyframe(float time, float value, float inAngle, float outAngle) {
return AddKeyframeBuilder(new KeyframeBuilder(time, value, inAngle, outAngle));
}
private TrackBuilder AddKeyframeBuilder(KeyframeBuilder builder) {
keyframeBuilders.Add(builder.time, builder);
return this;
}
public Track Build() {
KeyframeBuilder previousKeyframeBuilder = null;
foreach (var builder in keyframeBuilders.Values) {
builder.previous = previousKeyframeBuilder;
if (previousKeyframeBuilder != null) {
previousKeyframeBuilder.next = builder;
}
previousKeyframeBuilder = builder;
}
var keyframes = new List<Track.Keyframe>();
foreach (var builder in keyframeBuilders.Values) {
keyframes.Add(builder.Build());
}
var track = new Track();
track.TargetRegion = region;
track.ActorIndex = actorIndex;
track.Keyframes = keyframes.ToArray();
return track;
}
}
public class PatternBuilder {
private List<TrackBuilder> trackBuilders = new List<TrackBuilder>();
private string identifier;
public PatternBuilder(string identifier) {
this.identifier = identifier;
}
public TrackBuilder AddTrack(Vibration.Region region, int actorIndex) {
var builder = new TrackBuilder(region, actorIndex);
trackBuilders.Add(builder);
return builder;
}
public LoadPattern Build() {
var pattern = new LoadPattern();
pattern.Identifier = identifier;
var tracks = new List<Track>();
foreach (var builder in trackBuilders) {
tracks.Add(builder.Build());
}
pattern.Tracks = tracks.ToArray();
return pattern;
}
}
}