-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cs
More file actions
375 lines (287 loc) · 9.88 KB
/
Animation.cs
File metadata and controls
375 lines (287 loc) · 9.88 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
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
using System.Numerics;
using BfresLibrary;
namespace tmdl_utility;
public partial class ModelUtility
{
public class Key<T>
{
public float timeStamp;
public T value;
public Key(float time, T val)
{
timeStamp = time;
value = val;
}
public float Time
{
get => timeStamp;
set => timeStamp = value;
}
public override string ToString()
{
return $"({Time}) - {value}";
}
}
public class Bone
{
public List<Bone> children = new();
public int id = -1;
public string name;
public Node node;
public Matrix4x4 offsetMatrix = Matrix4x4.Identity;
public Bone parent;
public Vec3 Position = new();
public Vec4 Rotation = new(0, 0, 0, 1);
public Vec3 Scale = new(1);
public Bone(Node node)
{
name = node.name;
id = node.id;
this.node = node;
Position = node.Position;
Rotation = node.Rotation;
Scale = node.Scale;
node.IsBone = true;
}
public Bone(BfresLibrary.Bone bone)
{
name = bone.Name;
var bNode = new Node(bone.Name);
bNode.Position = new Vec3(bone.Position.X, bone.Position.Y, bone.Position.Z);
if (bone.FlagsRotation != BoneFlagsRotation.EulerXYZ)
{
bNode.Rotation = new Vec4(bone.Rotation.X, bone.Rotation.Y, bone.Rotation.Z, bone.Rotation.W);
}
else
{
var eul = new Vec4(bone.Rotation.X, bone.Rotation.Y, bone.Rotation.Z, bone.Rotation.W);
bNode.Rotation = Vec4.FromEuler(eul.ToEuler());
}
//bNode.Rotation = new Vec4(bone.Rotation.X, bone.Rotation.Y, bone.Rotation.Z, bone.Rotation.W);
bNode.Scale = new Vec3(bone.Scale.X, bone.Scale.Y, bone.Scale.Z);
node = bNode;
Position = node.Position;
Rotation = node.Rotation;
Scale = node.Scale;
node.IsBone = true;
}
public Bone()
{
}
public static Matrix4x4 CalculateTransformMatrix(Bone bone)
{
return Matrix4x4.CreateScale(bone.Scale) *
Matrix4x4.CreateFromQuaternion(bone.Rotation) *
Matrix4x4.CreateTranslation(bone.Scale);
}
public static (Matrix4x4, Matrix4x4) CalculateOffsetMatrix(Bone bone)
{
var mat = Matrix4x4.Identity;
if (bone.parent != null) mat *= CalculateOffsetMatrix(bone.parent).Item1;
mat = Matrix4x4.Multiply(CalculateTransformMatrix(bone), mat);
Matrix4x4.Invert(mat, out var inverse);
return (mat, inverse);
}
public void SetParent(Bone b)
{
if (parent != null)
parent.children.Remove(this);
parent = b;
parent.children.Add(this);
}
public void AddChild(Bone b)
{
b.SetParent(this);
}
public void Write(ModelWriter writer)
{
writer.WriteNonSigString(name);
writer.Write(id);
Position.Write(writer);
Rotation.Write(writer);
Scale.Write(writer);
for (var i = 0; i < 4; i++)
for (var j = 0; j < 4; j++)
writer.Write(offsetMatrix[i, j]);
writer.Write(children.Count);
foreach (var child in children) writer.WriteNonSigString(child.name);
}
public override string ToString()
{
return name;
}
}
public class Skeleton
{
public List<Bone> bones = new();
public string rootName;
public Skeleton(BfresLibrary.Skeleton skl)
{
rootName = skl.Bones[0].Name;
bones = new List<Bone>();
foreach (var bone in skl.BoneList)
{
var nBone = new Bone(bone);
bones.Add(nBone);
}
for (var i = 0; i < skl.BoneList.Count; i++)
{
var bone = skl.BoneList[i];
var nBone = bones[i];
nBone.id = i;
if (bone.ParentIndex > -1) nBone.SetParent(bones[bone.ParentIndex]);
}
}
public Skeleton(Node rootNode)
{
var nodes = rootNode.GetAllChildren();
rootName = rootNode.name;
var rootBone = new Bone(rootNode);
rootBone.id = bones.Count;
bones.Add(rootBone);
foreach (var node in nodes)
{
var b = new Bone(node);
b.id = bones.Count;
bones.Add(b);
}
foreach (var node in nodes)
{
var bone = GetBone(node.name);
if (node.parent != null)
{
var parent = GetBone(node.parent.name);
bone.SetParent(parent);
}
}
}
public void Write(ModelWriter writer)
{
writer.Write(bones.Count);
foreach (var bone in bones) bone.Write(writer);
writer.WriteNonSigString(rootName);
}
public Bone GetBone(string name)
{
foreach (var bone in bones)
if (bone.name == name)
return bone;
return null;
}
public Node ToNode()
{
var rootNode = new Node(bones[0]);
return rootNode;
}
}
public class Animation
{
public int assignedModel = -1;
public float duration;
public string name;
public Dictionary<string, NodeChannel> nodeChannels = new();
public int ticksPerSecond;
public Animation()
{
}
public Animation(Assimp.Animation anim)
{
name = anim.Name;
duration = (float)anim.DurationInTicks;
ticksPerSecond = (int)anim.TicksPerSecond;
foreach (var ch in anim.NodeAnimationChannels)
{
var channel = new NodeChannel();
channel.NodeName = ch.NodeName;
foreach (var pkey in ch.PositionKeys)
channel.Positions.Add(new Key<Vec3>((float)pkey.Time, new Vec3(pkey.Value)));
foreach (var rkey in ch.RotationKeys)
channel.Rotations.Add(new Key<Vec4>((float)rkey.Time,
new Vec4(rkey.Value.X, rkey.Value.Y, rkey.Value.Z, rkey.Value.W)));
foreach (var skey in ch.ScalingKeys)
channel.Scales.Add(new Key<Vec3>((float)skey.Time, new Vec3(skey.Value)));
nodeChannels.Add(channel.NodeName, channel);
}
}
public void ApplySkeleton(Skeleton skeleton)
{
foreach (var (name, channel) in nodeChannels) channel.Bone = skeleton.GetBone(name);
}
public void Write(ModelWriter writer)
{
writer.WriteString("TANM");
writer.WriteNonSigString(name);
writer.Write(duration);
writer.Write(ticksPerSecond);
var channelCount = nodeChannels.Count;
foreach (var keyValuePair in nodeChannels)
if (keyValuePair.Value.Bone == null)
channelCount--;
writer.Write(channelCount);
foreach (var (name, channel) in nodeChannels)
{
if (channel.Bone == null)
continue;
writer.WriteNonSigString(channel.NodeName);
writer.Write(channel.Bone.id);
writer.Write(channel.Positions.Count);
foreach (var pkey in channel.Positions)
{
writer.Write(pkey.timeStamp);
pkey.value.Write(writer);
}
writer.Write(channel.Rotations.Count);
foreach (var pkey in channel.Rotations)
{
writer.Write(pkey.timeStamp);
pkey.value.Write(writer);
}
writer.Write(channel.Scales.Count);
foreach (var pkey in channel.Scales)
{
writer.Write(pkey.timeStamp);
pkey.value.Write(writer);
}
}
}
public class NodeChannel
{
private const float KeyToleration = 0.01f;
public Bone Bone;
public string NodeName;
public List<Key<Vec3>> Positions = new();
public List<Key<Vec4>> Rotations = new();
public List<Key<Vec3>> Scales = new();
public void AddPosition(Key<Vec3> p)
{
foreach (var position in Positions)
if (Math.Abs(position.timeStamp - p.timeStamp) < KeyToleration)
{
position.value += p.value;
return;
}
Positions.Add(p);
}
public void AddRotation(Key<Vec4> r)
{
foreach (var rotation in Rotations)
if (Math.Abs(rotation.timeStamp - r.timeStamp) < KeyToleration)
{
rotation.value *= r.value;
return;
}
Rotations.Add(r);
}
public void AddScale(Key<Vec3> s)
{
foreach (var scale in Scales)
if (Math.Abs(scale.timeStamp - s.timeStamp) < KeyToleration)
{
scale.value += s.value;
return;
}
Scales.Add(s);
}
}
}
}