-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMovinShape.cs
551 lines (391 loc) · 17.8 KB
/
MovinShape.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
using Unity.UIWidgets.ui;
using UnityEngine;
using UnityEngine.Rendering;
using Color = UnityEngine.Color;
namespace Unity.UIWidgets.Movin {
public class BezierPathSegment {
public Vector2 P0;
public Vector2 P1;
public Vector2 P2;
}
public class SolidFill {
public Color Color;
}
public class PathProperties {
public Stroke Stroke;
}
public class Stroke {
public Color Color;
public float HalfThickness;
}
public class Shape {
public SolidFill Fill;
public PathProperties PathProps;
public Matrix3 FillTransform;
}
public class MovinShape {
public Transform transform;
public MovinShapeSlave[] slaves;
public BodymovinShape content;
public BodyPoint[] points;
public BodyPoint[] startPoints;
public BodyPoint[] endPoints;
public BezierPathSegment[] segments;
public Movin movin;
public MovinLayer layer;
public bool closed;
public PathProperties props;
public SolidFill fill;
public Stroke stroke;
public Shape shape;
public bool animated = false;
public bool strokeColorAnimated = false;
public bool fillColorAnimated = false;
public MotionProps motion;
public MotionProps mstrokec;
public MotionProps mfillc;
public Vector3 currentStrokeColor;
public Vector3 currentFillColor;
public BodymovinAnimatedShapeProperties[] motionSet;
public MovinShape() { }
public MovinShape(MovinLayer layer, BodymovinShape content) {
this.content = content;
if (content.paths == null || content.paths.Length < 1) {
Debug.Log("DON'T DRAW SHAPE -> NO PTS");
return;
}
this.layer = layer;
movin = layer.movin;
Transform parent = layer.transform;
/* FIRST SHAPE PROPS */
points = (BodyPoint[]) content.paths[0].points.Clone();
motionSet = content.paths[0].animSets;
closed = content.paths[0].closed;
/* ANIM SETUP */
MotionSetup(ref animated, ref motion, motionSet);
MotionSetup(ref strokeColorAnimated, ref mstrokec, content.strokeColorSets);
MotionSetup(ref fillColorAnimated, ref mfillc, content.fillColorSets);
/* GAMEOBJECT, MESH, MATERIAL */
transform = new RectTransform();
transform.SetParent(parent, false);
transform.localPosition = -layer.content.anchorPoint;
/* SETUP VECTOR */
Color stClr = (content.strokeColor == null)
? new Color(1, 1, 1)
: new Color(content.strokeColor[0], content.strokeColor[1], content.strokeColor[2]);
Color flClr = (content.fillColor == null)
? new Color(1, 1, 1)
: new Color(content.fillColor[0], content.fillColor[1], content.fillColor[2]);
currentStrokeColor = new Vector3(stClr.r, stClr.g, stClr.b);
currentFillColor = new Vector3(flClr.r, flClr.g, flClr.b);
fill = content.fillHidden || content.fillColor == null ? null : new SolidFill() {Color = flClr};
stroke = content.strokeHidden || content.strokeColor == null
? null
: new Stroke() {Color = stClr, HalfThickness = content.strokeWidth * movin.strokeWidth};
props = new PathProperties() {Stroke = stroke};
shape = new Shape() {
Fill = fill,
PathProps = props,
FillTransform = Matrix3.I()
};
UpdateMesh();
// ADDITIONAL SHAPE PATHS
slaves = new MovinShapeSlave[content.paths.Length - 1];
for (int i = 1; i <= slaves.Length; i++)
slaves[i - 1] = new MovinShapeSlave(this, content.paths[i], movin.strokeWidth);
}
public void UpdateSegments(BodyPoint[] pts, ref BezierPathSegment[] segs) {
float y = -1f;
for (int i = 0; i < pts.Length; i++) {
BodyPoint point = pts[i];
// Next point...
bool last = i >= pts.Length - 1;
BodyPoint nextPoint = last ? pts[0] : pts[i + 1];
// UPDATE segment
segs[i].P0.x = point.p.x;
segs[i].P0.y = point.p.y * y;
segs[i].P1.x = point.p.x + point.o.x;
segs[i].P1.y = (point.p.y + point.o.y) * y;
segs[i].P2.x = nextPoint.p.x + nextPoint.i.x;
segs[i].P2.y = (nextPoint.p.y + nextPoint.i.y) * y;
}
int l = segs.Length - 1;
segs[l].P0.x = pts[0].p.x;
segs[l].P0.y = pts[0].p.y * y;
segs[l].P1.x = segs[l].P1.y = segs[l].P2.x = segs[l].P2.y = 0;
}
public BezierPathSegment[] ConvertPointsToSegments(BodyPoint[] pts) {
float y = -1f;
int cnt = pts.Length + (closed ? 1 : 0);
BezierPathSegment[] segs = new BezierPathSegment[cnt];
int i = 0;
for (int j = 0; j < pts.Length; j++) {
BodyPoint point = pts[j];
// Next point...
bool last = i >= pts.Length - 1;
BodyPoint nextPoint = last ? pts[0] : pts[i + 1];
// Make segment
BezierPathSegment s = new BezierPathSegment() {
P0 = new Vector2(point.p.x, point.p.y * y),
P1 = new Vector2((point.p.x + point.o.x), (point.p.y + point.o.y) * y),
P2 = new Vector2((nextPoint.p.x + nextPoint.i.x), (nextPoint.p.y + nextPoint.i.y) * y)
};
segs[i] = s;
i += 1;
}
if (pts.Length > 0 && i == cnt - 1) {
BezierPathSegment final = new BezierPathSegment() {
P0 = new Vector2(pts[0].p.x, pts[0].p.y * y)
};
segs[i] = final;
}
return segs;
}
public void Update(float frame) {
/* ----- ANIM PROPS ----- */
if (animated && !motion.completed) UpdateProperty(frame, ref motion, motionSet);
if (strokeColorAnimated && !mstrokec.completed)
UpdateProperty(frame, ref mstrokec, content.strokeColorSets);
if (fillColorAnimated && !mfillc.completed) UpdateProperty(frame, ref mfillc, content.fillColorSets);
if (slaves == null) return;
for (int i = 0; i < slaves.Length; i++) slaves[i].Update(frame);
}
public void UpdateOpacity(float opacity) {
}
public void UpdateProperty(float frame, ref MotionProps m, BodymovinAnimatedShapeProperties[] set) {
/* ----- CHECK FOR COMPLETE ----- */
if (m.keys <= 0) {
//Debug.Log(">>> NO PROP KEYS TO ANIMATE!");
m.completed = true;
return;
}
if (frame >= m.endFrame) {
if (set == null || m.key + 1 == set.Length - 1) {
m.completed = true;
//Debug.Log("****** Prop Animation done! ******");
return;
}
SetKeyframe(ref m, set, m.key + 1);
}
/* ----- PERCENT KEYFRAME COMPLETE ----- */
m.percent = (frame - m.startFrame) / (m.endFrame - m.startFrame);
/* ----- CUBIC BEZIER EASE ----- */
float ease = Ease.CubicBezier(Vector2.zero, m.currentOutTangent, m.nextInTangent, Vector2.one, m.percent);
/* ----- UPDATE POINTS ----- */
for (int i = 0; i < points.Length; i++)
if (m.percent < 0) {
// BACK TO START OF KEYFRAME
points[i].p = startPoints[i].p;
points[i].i = startPoints[i].i;
points[i].o = startPoints[i].o;
}
else {
points[i].p = startPoints[i].p + ((endPoints[i].p - startPoints[i].p) * ease);
points[i].i = startPoints[i].i + ((endPoints[i].i - startPoints[i].i) * ease);
points[i].o = startPoints[i].o + ((endPoints[i].o - startPoints[i].o) * ease);
}
// Debug.Log("Shape anim fr: " + frame + " m.percent: " + m.percent);
/* ----- UPDATE MESH ----- */
UpdateMesh();
}
public void UpdateProperty(float frame, ref MotionProps m, BodymovinAnimatedProperties[] set) {
/* ----- CHECK FOR COMPLETE ----- */
if (m.keys <= 0) {
//Debug.Log(">>> NO PROP KEYS TO ANIMATE!");
m.completed = true;
return;
}
if (frame >= m.endFrame) {
if (m.key + 1 == set.Length - 1) {
m.completed = true;
//Debug.Log("****** Prop Animation done! ******");
return;
}
while (frame >= m.endFrame) {
// Debug.Log("fr > end, eq: " + frame + " - " + m.startFrame + " / (" + m.endFrame + " - " + m.startFrame + ") keyframe: " + m.key );
SetKeyframe(ref m, set, m.key + 1);
if (m.key == 0) break;
}
}
/* ----- PERCENT KEYFRAME COMPLETE ----- */
m.percent = (frame - m.startFrame) / (m.endFrame - m.startFrame);
/* ----- CUBIC BEZIER EASE ----- */
//Debug.Log("to: " + m.currentOutTangent + " ti: " + m.nextInTangent);
float ease = Ease.CubicBezier(Vector2.zero, m.currentOutTangent, m.nextInTangent, Vector2.one, m.percent);
/* ----- UPDATE PROPERTY ----- */
if (set == content.strokeColorSets) {
Color c = stroke.Color;
Vector3 v = Value3(m, ease);
currentStrokeColor = v;
c.r = v.x;
c.g = v.y;
c.b = v.z;
UpdateStrokeColor(c);
}
else if (set == content.fillColorSets) {
//Debug.Log("diff: " + (set[m.key].e.x - set[m.key].s.x).ToString("F4") + " fnl: " + (set[m.key].s + ((set[m.key].e - set[m.key].s) * ease)) + " percent: " + m.percent + " ease: " + ease);
Color c = fill.Color;
Vector3 v = Value3(m, ease);
currentFillColor = v;
c.r = v.x;
c.g = v.y;
c.b = v.z;
UpdateFillColor(c);
}
}
public void UpdateStrokeColor(Color c) {
stroke.Color = c;
props.Stroke = stroke;
if (slaves == null) return;
for (int i = 0; i < slaves.Length; i++) slaves[i].UpdateStrokeColor(c);
}
public void UpdateFillColor(Color c) {
fill.Color = c;
shape.Fill = fill;
if (slaves == null) return;
for (int i = 0; i < slaves.Length; i++) slaves[i].UpdateFillColor(c);
}
public void UpdateAnchor(Vector3 a) {
transform.localPosition = -a;
if (slaves == null) return;
for (int i = 0; i < slaves.Length; i++) slaves[i].UpdateAnchor(a);
}
public Vector3 Value3(MotionProps m, float ease) {
return (m.percent < 0 || m.percent > 1)
? m.startValue
: m.startValue + ((m.endValue - m.startValue) * ease);
}
//public Vector3 Value3b(MotionProps m, BodymovinAnimatedProperties[] set, float ease)
//{
// float x = m.percent < 0 ?
// set[m.key].s.x : set[m.key].s.x + ((set[m.key].e.x - set[m.key].s.x) * ease);
// float y = m.percent < 0 ?
// set[m.key].s.y : set[m.key].s.y + ((set[m.key].e.y - set[m.key].s.y) * ease);
// float z = m.percent < 0 ?
// set[m.key].s.z : set[m.key].s.z + ((set[m.key].e.z - set[m.key].s.z) * ease);
// return new Vector3(x, y, z);
//}
public void ResetKeyframes() {
if (animated) SetKeyframe(ref motion, motionSet, 0);
if (strokeColorAnimated) SetKeyframe(ref mstrokec, content.strokeColorSets, 0);
if (fillColorAnimated) SetKeyframe(ref mfillc, content.fillColorSets, 0);
if (slaves == null) return;
for (int i = 0; i < slaves.Length; i++) slaves[i].ResetKeyframes();
}
/* ----- MOTION SETUP ------ */
public void MotionSetup(ref bool b, ref MotionProps prop, BodymovinAnimatedProperties[] set) {
b = set != null && set.Length > 0;
if (b) {
prop = new MotionProps {keys = set.Length};
SetKeyframe(ref prop, set, 0);
}
}
public void MotionSetup(ref bool b, ref MotionProps prop, BodymovinAnimatedShapeProperties[] set) {
b = set != null && set.Length > 0;
if (b) {
prop = new MotionProps {keys = set.Length};
SetKeyframe(ref prop, set, 0);
}
}
/* ----- KEYFRAME SETTERS ----- */
public void SetKeyframe(ref MotionProps prop, BodymovinAnimatedProperties[] set, int k = 0) {
prop.completed = false;
if (prop.keys <= 0 || set == null) return;
if (k >= prop.keys) k = 0;
prop.key = k;
prop.startFrame = set[k].t;
prop.endFrame = set.Length > k + 1 ? set[k + 1].t : prop.startFrame;
prop.currentOutTangent = set[k].o;
prop.nextInTangent = set[k].i;
prop.startValue = set[k].s;
prop.endValue = set[k].e;
}
public void SetKeyframe(ref MotionProps prop, BodymovinAnimatedShapeProperties[] set, int k = 0) {
prop.completed = false;
if (prop.keys <= 0 || set == null) return;
if (k >= prop.keys) k = 0;
prop.key = k;
prop.startFrame = set[k].t;
prop.endFrame = set.Length > k ? set[k + 1].t : prop.startFrame;
prop.currentOutTangent = set[k].o;
prop.nextInTangent = set[k].i;
if (set == motionSet) {
startPoints = set[k].pts[0];
endPoints = set[k].pts[1];
}
}
/* ----- UPDATE MESH ----- */
public void UpdateMesh() {
if (segments == null)
segments = ConvertPointsToSegments(points);
else
UpdateSegments(points, ref segments);
}
/* ---- BLENDING ---- */
public void CreateBlendKeyframe(BodymovinShape blendContent, float duration, Vector2[] ease) {
/* SHAPE PATH */
animated = true;
CreatePathKeyframe(ref motion, 0, duration + 0, ease,
(BodyPoint[]) blendContent.paths[0].points.Clone()
);
/* STROKE + FILL COLORS */
Vector3 stClr = (blendContent.strokeColor == null)
? Vector3.one
: new Vector3(blendContent.strokeColor[0], blendContent.strokeColor[1], blendContent.strokeColor[2]);
strokeColorAnimated = true;
CreateKeyframe(ref mstrokec, 0, duration, ease, currentStrokeColor, stClr);
Vector3 flClr = (blendContent.fillColor == null)
? Vector3.one
: new Vector3(blendContent.fillColor[0], blendContent.fillColor[1], blendContent.fillColor[2]);
fillColorAnimated = true;
CreateKeyframe(ref mfillc, 0, duration, ease, currentFillColor, flClr);
if (slaves == null) return;
for (int i = 1; i <= slaves.Length; i++) {
slaves[i - 1].animated = true;
slaves[i - 1].CreatePathKeyframe(ref slaves[i - 1].motion, 0, duration + 0, ease,
(BodyPoint[]) blendContent.paths[i].points.Clone()
);
}
}
public void CreateKeyframe(ref MotionProps prop, float start, float end,
Vector2[] ease, Vector3 startValue, Vector3 endValue, int k = 0) {
prop.completed = false;
prop.keys = 1;
prop.key = k;
prop.startFrame = start;
prop.endFrame = end;
prop.currentOutTangent = ease[0];
prop.nextInTangent = ease[1];
prop.startValue = startValue;
prop.endValue = endValue;
}
public void CreatePathKeyframe(ref MotionProps prop, float start, float end, Vector2[] ease, BodyPoint[] pts,
int k = 0) {
prop.completed = false;
prop.keys = 1;
prop.key = k;
prop.startFrame = start;
prop.endFrame = end;
prop.currentOutTangent = ease[0];
prop.nextInTangent = ease[1];
startPoints = points;
endPoints = pts;
}
public void UpdateLayersWithContent(BodymovinShape s) {
content = s;
points = (BodyPoint[]) content.paths[0].points.Clone();
motionSet = content.paths[0].animSets;
MotionSetup(ref animated, ref motion, motionSet);
MotionSetup(ref strokeColorAnimated, ref mstrokec, content.strokeColorSets);
MotionSetup(ref fillColorAnimated, ref mfillc, content.fillColorSets);
transform.localPosition = -layer.content.anchorPoint;
if (slaves == null) return;
for (int i = 1; i <= slaves.Length; i++) {
slaves[i - 1].transform.localPosition = -layer.content.anchorPoint;
slaves[i - 1].points = (BodyPoint[]) content.paths[i].points.Clone();
slaves[i - 1].motionSet = content.paths[i].animSets;
slaves[i - 1].MotionSetup(ref animated, ref motion, motionSet);
}
}
}
}