AnimationCurveData.cs
4.39 KB
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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
class AnimationCurveData
{
#if UNITY_EDITOR
public AnimationUtility.TangentMode TangentMode { get; private set; }
public glTFAnimationTarget.AnimationPropertys AnimationProperty { get; private set; }
public int SamplerIndex { get; private set; }
public int ElementCount { get; private set; }
public readonly List<AnimationKeyframeData> Keyframes = new List<AnimationKeyframeData>();
public AnimationCurveData(AnimationUtility.TangentMode tangentMode, glTFAnimationTarget.AnimationPropertys property, int samplerIndex, int elementCount)
{
TangentMode = tangentMode;
AnimationProperty = property;
SamplerIndex = samplerIndex;
ElementCount = elementCount;
}
public string GetInterpolation()
{
switch (TangentMode)
{
case AnimationUtility.TangentMode.Linear:
return glTFAnimationTarget.Interpolations.LINEAR.ToString();
case AnimationUtility.TangentMode.Constant:
return glTFAnimationTarget.Interpolations.STEP.ToString();
default:
return glTFAnimationTarget.Interpolations.LINEAR.ToString();
}
}
/// <summary>
/// キーフレームのデータを入力する
/// </summary>
/// <param name="time"></param>
/// <param name="value"></param>
/// <param name="valueOffset"></param>
public void SetKeyframeData(float time, float value, int valueOffset)
{
var existKeyframe = Keyframes.FirstOrDefault(x => x.Time == time);
if (existKeyframe != null)
{
existKeyframe.SetValue(value, valueOffset);
}
else
{
var newKeyframe = GetKeyframeData(AnimationProperty, ElementCount);
newKeyframe.Time = time;
newKeyframe.SetValue(value, valueOffset);
Keyframes.Add(newKeyframe);
}
}
/// <summary>
/// キー情報がなかった要素に対して直前のキーの値を入力する
/// </summary>
public void RecountEmptyKeyframe()
{
if (Keyframes.Count == 0)
{
return;
}
Keyframes.Sort((x, y) => (x.Time < y.Time) ? -1 : 1);
for (int i = 1; i < Keyframes.Count; i++)
{
var current = Keyframes[i];
var last = Keyframes[i - 1];
for (int j = 0; j < current.EnterValues.Length; j++)
{
if (!current.EnterValues[j])
{
Keyframes[i].SetValue(last.Values[j], j);
}
}
}
}
/// <summary>
/// アニメーションプロパティに対応したキーフレームを挿入する
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
private static AnimationKeyframeData GetKeyframeData(glTFAnimationTarget.AnimationPropertys property, int elementCount)
{
switch (property)
{
case glTFAnimationTarget.AnimationPropertys.Translation:
return new AnimationKeyframeData(elementCount, (values) =>
{
var temp = new Vector3(values[0], values[1], values[2]);
return temp.ReverseZ().ToArray();
});
case glTFAnimationTarget.AnimationPropertys.Rotation:
return new AnimationKeyframeData(elementCount, (values) =>
{
var temp = new Quaternion(values[0], values[1], values[2], values[3]);
return temp.ReverseZ().ToArray();
});
case glTFAnimationTarget.AnimationPropertys.Scale:
return new AnimationKeyframeData(elementCount, null);
case glTFAnimationTarget.AnimationPropertys.BlendShape:
return new AnimationKeyframeData(elementCount, null);
default:
return null;
}
}
#endif
}
}