/
CubismMotion3Json.cs
683 lines (560 loc) · 22.7 KB
/
CubismMotion3Json.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
using System;
using System.Collections.Generic;
using Live2D.Cubism.Core;
using Live2D.Cubism.Framework.MouthMovement;
using Live2D.Cubism.Rendering;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace Live2D.Cubism.Framework.Json
{
/// <summary>
/// Contains Cubism motion3.json data.
/// </summary>
[Serializable]
// ReSharper disable once ClassCannotBeInstantiated
public sealed class CubismMotion3Json
{
#region Load Methods
/// <summary>
/// Loads a motion3.json asset.
/// </summary>
/// <param name="motion3Json">motion3.json to deserialize.</param>
/// <returns>Deserialized motion3.json on success; <see langword="null"/> otherwise.</returns>
public static CubismMotion3Json LoadFrom(string motion3Json)
{
if (string.IsNullOrEmpty(motion3Json))
{
return null;
}
var cubismMotion3Json = JsonUtility.FromJson<CubismMotion3Json>(motion3Json);
cubismMotion3Json.Meta.FadeInTime = -1.0f;
cubismMotion3Json.Meta.FadeOutTime = -1.0f;
for (var i = 0; i < cubismMotion3Json.Curves.Length; ++i)
{
cubismMotion3Json.Curves[i].FadeInTime = -1.0f;
cubismMotion3Json.Curves[i].FadeOutTime = -1.0f;
}
JsonUtility.FromJsonOverwrite(motion3Json, cubismMotion3Json);
return cubismMotion3Json;
}
/// <summary>
/// Loads a motion3.json asset.
/// </summary>
/// <param name="motion3JsonAsset">motion3.json to deserialize.</param>
/// <returns>Deserialized motion3.json on success; <see langword="null"/> otherwise.</returns>
public static CubismMotion3Json LoadFrom(TextAsset motion3JsonAsset)
{
return (motion3JsonAsset == null)
? null
: LoadFrom(motion3JsonAsset.text);
}
#endregion
#region Json Data
/// <summary>
/// The model3.json format version.
/// </summary>
[SerializeField]
public int Version;
/// <summary>
/// Motion meta info.
/// </summary>
[SerializeField]
public SerializableMeta Meta;
/// <summary>
/// Curves.
/// </summary>
[SerializeField]
public SerializableCurve[] Curves;
/// <summary>
/// User data.
/// </summary>
[SerializeField]
public SerializableUserData[] UserData;
#endregion
#region Constructors
/// <summary>
/// Makes construction only possible through factories.
/// </summary>
private CubismMotion3Json()
{
}
#endregion
/// <summary>
/// Converts motion curve segments into <see cref="Keyframe"/>s.
/// </summary>
/// <param name="segments">Data to convert.</param>
/// <returns>Keyframes.</returns>
public static Keyframe[] ConvertCurveSegmentsToKeyframes(float[] segments)
{
// Return early on invalid input.
if (segments.Length < 1)
{
return new Keyframe[0];
}
// Initialize container for keyframes.
var keyframes = new List<Keyframe> { new Keyframe(segments[0], segments[1]) };
// Parse segments.
for (var i = 2; i < segments.Length;)
{
Parsers[segments[i]](segments, keyframes, ref i);
}
// Return result.
return keyframes.ToArray();
}
/// <summary>
/// Converts stepped curves to liner curves.
/// </summary>
/// <param name="curve">Data to convert.</param>
/// <returns>Animation curve.</returns>
public static AnimationCurve ConvertSteppedCurveToLinerCurver(CubismMotion3Json.SerializableCurve curve, float poseFadeInTime)
{
poseFadeInTime = (poseFadeInTime < 0) ? 0.5f : poseFadeInTime;
var segments = curve.Segments;
var segmentsCount = 2;
for(var index = 2; index < curve.Segments.Length; index += 3)
{
// if current segment type is stepped and
// next segment type is stepped or next segment is last segment
// then convert segment type to liner.
var currentSegmentTypeIsStepped = (curve.Segments[index] == 2);
var currentSegmentIsLast = (index == (curve.Segments.Length - 3));
var nextSegmentTypeIsStepped = (currentSegmentIsLast) ? false : (curve.Segments[index + 3] == 2);
var nextSegmentIsLast = (currentSegmentIsLast) ? false : ((index + 3) == (curve.Segments.Length - 3));
if ( currentSegmentTypeIsStepped && (nextSegmentTypeIsStepped || nextSegmentIsLast) )
{
Array.Resize(ref segments, segments.Length + 3);
segments[segmentsCount + 0] = 0;
segments[segmentsCount + 1] = curve.Segments[index + 1];
segments[segmentsCount + 2] = curve.Segments[index - 1];
segments[segmentsCount + 3] = 0;
segments[segmentsCount + 4] = curve.Segments[index + 1] + poseFadeInTime;
segments[segmentsCount + 5] = curve.Segments[index + 2];
segmentsCount += 6;
}
else if(curve.Segments[index] == 1)
{
segments[segmentsCount + 0] = curve.Segments[index + 0];
segments[segmentsCount + 1] = curve.Segments[index + 1];
segments[segmentsCount + 2] = curve.Segments[index + 2];
segments[segmentsCount + 3] = curve.Segments[index + 3];
segments[segmentsCount + 4] = curve.Segments[index + 4];
segments[segmentsCount + 5] = curve.Segments[index + 5];
segments[segmentsCount + 6] = curve.Segments[index + 6];
index += 4;
segmentsCount += 7;
}
else
{
segments[segmentsCount + 0] = curve.Segments[index + 0];
segments[segmentsCount + 1] = curve.Segments[index + 1];
segments[segmentsCount + 2] = curve.Segments[index + 2];
segmentsCount += 3;
}
}
return new AnimationCurve(ConvertCurveSegmentsToKeyframes(segments));
}
/// <summary>
/// Instantiates an <see cref="AnimationClip"/>.
/// </summary>
/// <param name="shouldImportAsOriginalWorkflow">Should import as original workflow.</param>
/// <param name="shouldClearAnimationCurves">Should clear animation clip curves.</param>
/// <param name="isCallFormModelJson">Is function call form <see cref="CubismModel3Json"/>.</param>
/// <param name="poseJson">pose3.json asset.</param>
/// <returns>The instantiated clip on success; <see langword="null"/> otherwise.</returns>
/// <remarks>
/// Note this method generates <see cref="AnimationClip.legacy"/> clips when called at runtime.
/// </remarks>
public AnimationClip ToAnimationClip(bool shouldImportAsOriginalWorkflow = false, bool shouldClearAnimationCurves = false,
bool isCallFormModelJson = false, CubismPose3Json poseJson = null)
{
// Check béziers restriction flag.
if (!Meta.AreBeziersRestricted)
{
Debug.LogWarning("Béziers are not restricted and curves might be off. Please export motions from Cubism in restricted mode for perfect match.");
}
// Create animation clip.
var animationClip = new AnimationClip
{
#if UNITY_EDITOR
frameRate = Meta.Fps
#else
frameRate = Meta.Fps,
legacy = true,
wrapMode = (Meta.Loop)
? WrapMode.Loop
: WrapMode.Default
#endif
};
return ToAnimationClip(animationClip, shouldImportAsOriginalWorkflow, shouldClearAnimationCurves, isCallFormModelJson, poseJson);
}
/// <summary>
/// Instantiates an <see cref="AnimationClip"/>.
/// </summary>
/// <param name="animationClip">Previous animation clip.</param>
/// <param name="shouldImportAsOriginalWorkflow">Should import as original workflow.</param>
/// <param name="shouldClearAnimationCurves">Should clear animation clip curves.</param>
/// <param name="isCallFormModelJson">Is function call form <see cref="CubismModel3Json"/>.</param>
/// <param name="poseJson">pose3.json asset.</param>
/// <returns>The instantiated clip on success; <see langword="null"/> otherwise.</returns>
/// <remarks>
/// Note this method generates <see cref="AnimationClip.legacy"/> clips when called at runtime.
/// </remarks>
public AnimationClip ToAnimationClip(AnimationClip animationClip, bool shouldImportAsOriginalWorkflow = false, bool shouldClearAnimationCurves = false
, bool isCallFormModelJson = false, CubismPose3Json poseJson = null)
{
// Clear curves.
if (shouldClearAnimationCurves && (!shouldImportAsOriginalWorkflow || (isCallFormModelJson && shouldImportAsOriginalWorkflow)))
{
animationClip.ClearCurves();
}
// Convert curves.
for (var i = 0; i < Curves.Length; ++i)
{
var curve = Curves[i];
// If should import as original workflow mode, skip add part opacity curve when call not from model3.json.
if (curve.Target == "PartOpacity" && shouldImportAsOriginalWorkflow && !isCallFormModelJson)
{
continue;
}
var relativePath = string.Empty;
var type = default(Type);
var propertyName = string.Empty;
var animationCurve = new AnimationCurve(ConvertCurveSegmentsToKeyframes(curve.Segments));
// Create model binding.
if (curve.Target == "Model")
{
// Bind opacity.
if (curve.Id == "Opacity")
{
relativePath = string.Empty;
propertyName = "Opacity";
type = typeof(CubismRenderController);
}
// Bind eye-blink.
else if (curve.Id == "EyeBlink")
{
relativePath = string.Empty;
propertyName = "EyeOpening";
type = typeof(CubismEyeBlinkController);
}
// Bind lip-sync.
else if (curve.Id == "LipSync")
{
relativePath = string.Empty;
propertyName = "MouthOpening";
type = typeof(CubismMouthController);
}
}
// Create parameter binding.
else if (curve.Target == "Parameter")
{
relativePath = "Parameters/" + curve.Id;
propertyName = "Value";
type = typeof(CubismParameter);
}
// Create part opacity binding.
else if (curve.Target == "PartOpacity")
{
relativePath = "Parts/" + curve.Id;
propertyName = "Opacity";
type = typeof(CubismPart);
// original workflow.
if (shouldImportAsOriginalWorkflow && poseJson != null && poseJson.FadeInTime != 0.0f)
{
animationCurve = ConvertSteppedCurveToLinerCurver(curve, poseJson.FadeInTime);
}
}
#if UNITY_EDITOR
var curveBinding = new EditorCurveBinding
{
path = relativePath,
propertyName = propertyName,
type = type
};
AnimationUtility.SetEditorCurve(animationClip, curveBinding, animationCurve);
#else
animationClip.SetCurve(relativePath, type, propertyName, animationCurve);
#endif
}
#if UNITY_EDITOR
// Apply settings.
var animationClipSettings = new AnimationClipSettings
{
loopTime = Meta.Loop,
stopTime = Meta.Duration
};
AnimationUtility.SetAnimationClipSettings(animationClip, animationClipSettings);
#endif
#if UNITY_EDITOR
// Add animation events from user data.
if (UserData != null)
{
var animationEvents = new List<AnimationEvent>();
for (var i = 0; i < UserData.Length; ++i)
{
var animationEvent = new AnimationEvent
{
time = UserData[i].Time,
stringParameter = UserData[i].Value,
};
animationEvents.Add(animationEvent);
}
if (animationEvents.Count > 0)
{
AnimationUtility.SetAnimationEvents(animationClip, animationEvents.ToArray());
}
}
#endif
return animationClip;
}
#region Segment Parsing
/// <summary>
/// Offset to use for setting of keyframes.
/// </summary>
private const float OffsetGranularity = 0.01f;
/// <summary>
/// Handles parsing of a single segment.
/// </summary>
/// <param name="segments">Curve segments.</param>
/// <param name="result">Buffer to append result to.</param>
/// <param name="position">Offset of segment.</param>
private delegate void SegmentParser(float[] segments, List<Keyframe> result, ref int position);
/// <summary>
/// Available segment parsers.
/// </summary>
// ReSharper disable once InconsistentNaming
private static Dictionary<float, SegmentParser> Parsers = new Dictionary<float, SegmentParser>
{
{0f, ParseLinearSegment},
{1f, ParseBezierSegment},
{2f, ParseSteppedSegment},
{3f, ParseInverseSteppedSegment}
};
/// <summary>
/// Parses a linear segment.
/// </summary>
/// <param name="segments">Curve segments.</param>
/// <param name="result">Buffer to append result to.</param>
/// <param name="position">Offset of segment.</param>
private static void ParseLinearSegment(float[] segments, List<Keyframe> result, ref int position)
{
// Compute slope.
var length = (segments[position + 1] - result[result.Count - 1].time);
var slope = (segments[position + 2] - result[result.Count - 1].value) / length;
// Determine tangents.
var outTangent = slope;
var inTangent = outTangent;
// Create keyframes.
var keyframe = new Keyframe(
result[result.Count - 1].time,
result[result.Count - 1].value,
result[result.Count - 1].inTangent,
outTangent);
result[result.Count - 1] = keyframe;
keyframe = new Keyframe(
segments[position + 1],
segments[position + 2],
inTangent,
0);
result.Add(keyframe);
// Update position.
position += 3;
}
/// <summary>
/// Parses a bezier segment.
/// </summary>
/// <param name="segments">Curve segments.</param>
/// <param name="result">Buffer to append result to.</param>
/// <param name="position">Offset of segment.</param>
private static void ParseBezierSegment(float[] segments, List<Keyframe> result, ref int position)
{
// Compute tangents.
var tangentLength = Mathf.Abs(result[result.Count - 1].time - segments[position + 5]) * 0.333333f;
var outTangent = (segments[position + 2] - result[result.Count - 1].value) / tangentLength;
var inTangent = (segments[position + 6] - segments[position + 4]) / tangentLength;
// Create keyframes.
var keyframe = new Keyframe(
result[result.Count - 1].time,
result[result.Count - 1].value,
result[result.Count - 1].inTangent,
outTangent);
result[result.Count - 1] = keyframe;
keyframe = new Keyframe(
segments[position + 5],
segments[position + 6],
inTangent,
0);
result.Add(keyframe);
// Update position.
position += 7;
}
/// <summary>
/// Parses a stepped segment.
/// </summary>
/// <param name="segments">Curve segments.</param>
/// <param name="result">Buffer to append result to.</param>
/// <param name="position">Offset of segment.</param>
private static void ParseSteppedSegment(float[] segments, List<Keyframe> result, ref int position)
{
// Create keyframe.
result.Add(
new Keyframe(segments[position + 1], segments[position + 2])
{
inTangent = float.PositiveInfinity
});
// Update position.
position += 3;
}
/// <summary>
/// Parses a inverse-stepped segment.
/// </summary>
/// <param name="segments">Curve segments.</param>
/// <param name="result">Buffer to append result to.</param>
/// <param name="position">Offset of segment.</param>
private static void ParseInverseSteppedSegment(float[] segments, List<Keyframe> result, ref int position)
{
// Compute tangents.
var keyframe = result[result.Count - 1];
var tangent = (float)Math.Atan2(
(segments[position + 2] - keyframe.value),
(segments[position + 1] - keyframe.time));
keyframe.outTangent = tangent;
result[result.Count - 1] = keyframe;
result.Add(
new Keyframe(keyframe.time + OffsetGranularity, segments[position + 2])
{
inTangent = tangent,
outTangent = 0
});
result.Add(
new Keyframe(segments[position + 1], segments[position + 2])
{
inTangent = 0
});
// Update position.
position += 3;
}
#endregion
#region Json Object Types
/// <summary>
/// Motion meta info.
/// </summary>
[Serializable]
public struct SerializableMeta
{
/// <summary>
/// Duration in seconds.
/// </summary>
[SerializeField]
public float Duration;
/// <summary>
/// Framerate in seconds.
/// </summary>
[SerializeField]
public float Fps;
/// <summary>
/// True if motion is looping.
/// </summary>
[SerializeField]
public bool Loop;
/// <summary>
/// Number of curves.
/// </summary>
[SerializeField]
public int CurveCount;
/// <summary>
/// Total number of curve segments.
/// </summary>
[SerializeField]
public int TotalSegmentCount;
/// <summary>
/// Total number of curve points.
/// </summary>
[SerializeField]
public int TotalPointCount;
/// <summary>
/// True if beziers are restricted.
/// </summary>
[SerializeField]
public bool AreBeziersRestricted;
/// <summary>
/// Total number of UserData.
/// </summary>
[SerializeField]
public int UserDataCount;
/// <summary>
/// Total size of UserData in bytes.
/// </summary>
[SerializeField]
public int TotalUserDataSize;
/// <summary>
/// [Optional] Time of the Fade-In for easing in seconds.
/// </summary>
[SerializeField]
public float FadeInTime;
/// <summary>
/// [Optional] Time of the Fade-Out for easing in seconds.
/// </summary>
[SerializeField]
public float FadeOutTime;
};
/// <summary>
/// Single motion curve.
/// </summary>
[Serializable]
public struct SerializableCurve
{
/// <summary>
/// Target type.
/// </summary>
[SerializeField]
public string Target;
/// <summary>
/// Id within target.
/// </summary>
[SerializeField]
public string Id;
/// <summary>
/// Flattened curve segments.
/// </summary>
[SerializeField]
public float[] Segments;
/// <summary>
/// [Optional] Time of the overall Fade-In for easing in seconds.
/// </summary>
[SerializeField]
public float FadeInTime;
/// <summary>
/// [Optional] Time of the overall Fade-Out for easing in seconds.
/// </summary>
[SerializeField]
public float FadeOutTime;
};
/// <summary>
/// User data.
/// </summary>
[Serializable]
public struct SerializableUserData
{
/// <summary>
/// Time in seconds.
/// </summary>
[SerializeField]
public float Time;
/// <summary>
/// Content of user data.
/// </summary>
[SerializeField]
public string Value;
}
#endregion
}
}