glTFAnimation.cs
6.31 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
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
using System;
using System.Linq;
using System.Collections.Generic;
using UniJSON;
namespace UniGLTF
{
[Serializable]
public class glTFAnimationTarget : JsonSerializableBase
{
[JsonSchema(Minimum = 0)]
public int node;
[JsonSchema(Required = true, EnumValues = new object[] { "translation", "rotation", "scale", "weights" }, EnumSerializationType = EnumSerializationType.AsString)]
public string path;
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => node);
if (!string.IsNullOrEmpty(path))
{
f.KeyValue(() => path);
}
}
public enum Interpolations
{
LINEAR,
STEP,
CUBICSPLINE
}
public const string PATH_TRANSLATION = "translation";
public const string PATH_EULER_ROTATION = "rotation";
public const string PATH_ROTATION = "rotation";
public const string PATH_SCALE = "scale";
public const string PATH_WEIGHT = "weights";
public const string NOT_IMPLEMENTED = "NotImplemented";
public enum AnimationPropertys
{
Translation,
EulerRotation,
Rotation,
Scale,
Weight,
BlendShape,
NotImplemented
}
public static string GetPathName(AnimationPropertys property)
{
switch (property)
{
case AnimationPropertys.Translation:
return PATH_TRANSLATION;
case AnimationPropertys.EulerRotation:
case AnimationPropertys.Rotation:
return PATH_ROTATION;
case AnimationPropertys.Scale:
return PATH_SCALE;
case AnimationPropertys.BlendShape:
return PATH_WEIGHT;
default: throw new NotImplementedException();
}
}
public static AnimationPropertys GetAnimationProperty(string path)
{
switch (path)
{
case PATH_TRANSLATION:
return AnimationPropertys.Translation;
case PATH_ROTATION:
return AnimationPropertys.Rotation;
case PATH_SCALE:
return AnimationPropertys.Scale;
case PATH_WEIGHT:
return AnimationPropertys.BlendShape;
default: throw new NotImplementedException();
}
}
public static int GetElementCount(AnimationPropertys property)
{
switch (property)
{
case AnimationPropertys.Translation: return 3;
case AnimationPropertys.EulerRotation: return 3;
case AnimationPropertys.Rotation: return 4;
case AnimationPropertys.Scale: return 3;
case AnimationPropertys.BlendShape: return 1;
default: throw new NotImplementedException();
}
}
public static int GetElementCount(string path)
{
return GetElementCount(GetAnimationProperty(path));
}
}
[Serializable]
public class glTFAnimationChannel : JsonSerializableBase
{
[JsonSchema(Required = true, Minimum = 0)]
public int sampler = -1;
[JsonSchema(Required = true)]
public glTFAnimationTarget target;
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => sampler);
f.Key("target"); f.GLTFValue(target);
}
}
[Serializable]
public class glTFAnimationSampler : JsonSerializableBase
{
[JsonSchema(Required = true, Minimum = 0)]
public int input = -1;
[JsonSchema(EnumValues = new object[] { "LINEAR", "STEP", "CUBICSPLINE" }, EnumSerializationType = EnumSerializationType.AsString)]
public string interpolation;
[JsonSchema(Required = true, Minimum = 0)]
public int output = -1;
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
f.KeyValue(() => input);
if (!string.IsNullOrEmpty(interpolation))
{
f.KeyValue(() => interpolation);
}
f.KeyValue(() => output);
}
}
[Serializable]
public class glTFAnimation : JsonSerializableBase
{
public string name = "";
[JsonSchema(Required = true, MinItems = 1)]
public List<glTFAnimationChannel> channels = new List<glTFAnimationChannel>();
[JsonSchema(Required = true, MinItems = 1)]
public List<glTFAnimationSampler> samplers = new List<glTFAnimationSampler>();
// empty schemas
public object extensions;
public object extras;
protected override void SerializeMembers(GLTFJsonFormatter f)
{
if (!string.IsNullOrEmpty(name))
{
f.KeyValue(() => name);
}
f.Key("channels"); f.GLTFValue(channels);
f.Key("samplers"); f.GLTFValue(samplers);
}
public int AddChannelAndGetSampler(int nodeIndex, glTFAnimationTarget.AnimationPropertys property)
{
// find channel
var channel = channels.FirstOrDefault(x => x.target.node == nodeIndex && x.target.path == glTFAnimationTarget.GetPathName(property));
if (channel != null)
{
return channel.sampler;
}
// not found. create new
var samplerIndex = samplers.Count;
var sampler = new glTFAnimationSampler();
samplers.Add(sampler);
channel = new glTFAnimationChannel
{
sampler = samplerIndex,
target = new glTFAnimationTarget
{
node = nodeIndex,
path = glTFAnimationTarget.GetPathName(property),
},
};
channels.Add(channel);
return samplerIndex;
}
}
}