-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathImportModelCommand.Animation.cs
More file actions
322 lines (277 loc) · 16.8 KB
/
ImportModelCommand.Animation.cs
File metadata and controls
322 lines (277 loc) · 16.8 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
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Stride.Core.BuildEngine;
using Stride.Core.Collections;
using Stride.Core.Extensions;
using Stride.Core.Mathematics;
using Stride.Core.Serialization.Contents;
using Stride.Updater;
using Stride.Animations;
using Stride.Rendering;
namespace Stride.Assets.Models
{
public partial class ImportModelCommand
{
public AnimationRepeatMode AnimationRepeatMode { get; set; }
public bool AnimationRootMotion { get; set; }
public TimeSpan StartFrame { get; set; } = TimeSpan.Zero;
public TimeSpan EndFrame { get; set; } = AnimationAsset.LongestTimeSpan;
public bool ImportCustomAttributes { get; set; }
public int AnimationStack { get; set; }
private unsafe object ExportAnimation(ICommandContext commandContext, ContentManager contentManager, bool failOnEmptyAnimation)
{
// Read from model file
var modelSkeleton = LoadSkeleton(commandContext, contentManager); // we get model skeleton to compare it to real skeleton we need to map to
AdjustSkeleton(modelSkeleton);
TimeSpan duration;
var animationClips = LoadAnimation(commandContext, contentManager, out duration);
// Fix the animation frames
double startFrameSeconds = StartFrame.TotalSeconds;
double endFrameSeconds = EndFrame.TotalSeconds;
var startTime = CompressedTimeSpan.FromSeconds(-startFrameSeconds);
foreach (var clip in animationClips)
{
foreach (var animationCurve in clip.Value.Curves)
{
animationCurve.ShiftKeys(startTime);
}
}
var durationTimeSpan = TimeSpan.FromSeconds((endFrameSeconds - startFrameSeconds));
if (duration > durationTimeSpan)
duration = durationTimeSpan;
// Incase of no mapping or only root mapping use source skeleton
var animationClip = new AnimationClip { Duration = duration };
var skeleton = string.IsNullOrWhiteSpace(SkeletonUrl)?null:contentManager.Load<Skeleton>(SkeletonUrl);
var skeletonMapping = new SkeletonMapping(skeleton, modelSkeleton);
if (animationClips.Count > 0)
{
AnimationClip rootMotionAnimationClip = null;
// If root motion is explicitely enabled, or if there is no skeleton, try to find root node and apply animation directly on TransformComponent
if ((AnimationRootMotion || skeleton == null || skeletonMapping.MapCount < 2) && modelSkeleton.Nodes.Length >= 1)
{
// No skeleton, map root node only
// TODO: For now, it seems to be located on node 1 in FBX files. Need to check if always the case, and what happens with Assimp
var rootNode0 = modelSkeleton.Nodes.Length >= 1 ? modelSkeleton.Nodes[0].Name : null;
var rootNode1 = modelSkeleton.Nodes.Length >= 2 ? modelSkeleton.Nodes[1].Name : null;
if ((rootNode0 != null && animationClips.TryGetValue(rootNode0, out rootMotionAnimationClip))
|| (rootNode1 != null && animationClips.TryGetValue(rootNode1, out rootMotionAnimationClip)))
{
foreach (var channel in rootMotionAnimationClip.Channels)
{
var curve = rootMotionAnimationClip.Curves[channel.Value.CurveIndex];
// Root motion
var channelName = channel.Key;
if (channelName.StartsWith("Transform.", StringComparison.Ordinal))
{
animationClip.AddCurve($"[TransformComponent.Key]." + channelName.Replace("Transform.", string.Empty), curve);
}
// Also apply Camera curves
// TODO: Add some other curves?
if (channelName.StartsWith("Camera.", StringComparison.Ordinal))
{
animationClip.AddCurve($"[CameraComponent.Key]." + channelName.Replace("Camera.", string.Empty), curve);
}
}
}
}
// Load asset reference skeleton
if (SkeletonUrl != null)
{
// Process missing nodes
foreach (var nodeAnimationClipEntry in animationClips)
{
var nodeName = nodeAnimationClipEntry.Key;
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
nodeName = nodeName.Replace(c, '_');
}
var nodeAnimationClip = nodeAnimationClipEntry.Value;
var nodeIndex = modelSkeleton.Nodes.IndexOf(x => x.Name == nodeName.ToString());
// Node doesn't exist in skeleton? skip it
if (nodeIndex == -1 || skeletonMapping.SourceToSource[nodeIndex] != nodeIndex)
continue;
// Skip root motion node (if any)
if (nodeAnimationClip == rootMotionAnimationClip)
continue;
// Find parent node
var parentNodeIndex = modelSkeleton.Nodes[nodeIndex].ParentIndex;
if (parentNodeIndex != -1 && skeletonMapping.SourceToSource[parentNodeIndex] != parentNodeIndex)
{
// Some nodes were removed, we need to concat the anim curves
var currentNodeIndex = nodeIndex;
var nodesToMerge = new List<Tuple<ModelNodeDefinition, AnimationBlender, AnimationClipEvaluator>>();
while (currentNodeIndex != -1 && currentNodeIndex != skeletonMapping.SourceToSource[parentNodeIndex])
{
AnimationClip animationClipToMerge;
AnimationClipEvaluator animationClipEvaluator = null;
AnimationBlender animationBlender = null;
if(GetAnimationKeyVirtualKey(modelSkeleton.Nodes[currentNodeIndex].Name, animationClips, out animationClipToMerge))
{
animationBlender = new AnimationBlender();
animationClipEvaluator = animationBlender.CreateEvaluator(animationClipToMerge);
}
nodesToMerge.Add(Tuple.Create(modelSkeleton.Nodes[currentNodeIndex], animationBlender, animationClipEvaluator));
currentNodeIndex = modelSkeleton.Nodes[currentNodeIndex].ParentIndex;
}
// Put them in proper parent to children order
nodesToMerge.Reverse();
// Find all key times
// TODO: We should detect discontinuities and keep them
var animationKeysSet = new HashSet<CompressedTimeSpan>();
foreach (var node in nodesToMerge)
{
if (node.Item3 != null)
foreach (var curve in node.Item3.Clip.Curves)
{
foreach (CompressedTimeSpan time in curve.Keys)
{
animationKeysSet.Add(time);
}
}
}
// Sort key times
var animationKeys = animationKeysSet.ToList();
animationKeys.Sort();
var animationOperations = new List<AnimationOperation>();
var combinedAnimationClip = new AnimationClip();
var translationCurve = new AnimationCurve<Vector3>();
var rotationCurve = new AnimationCurve<Quaternion>();
var scaleCurve = new AnimationCurve<Vector3>();
// Evaluate at every key frame
foreach (var animationKey in animationKeys)
{
var matrix = Matrix.Identity;
// Evaluate node
foreach (var node in nodesToMerge)
{
// Needs to be an array in order for it to be modified by the UpdateEngine, otherwise it would get passed by value
var modelNodeDefinitions = new ModelNodeDefinition[1] { node.Item1 };
if (node.Item2 != null && node.Item3 != null)
{
// Compute
AnimationClipResult animationClipResult = null;
animationOperations.Clear();
animationOperations.Add(AnimationOperation.NewPush(node.Item3, animationKey));
node.Item2.Compute(animationOperations, ref animationClipResult);
var updateMemberInfos = new List<UpdateMemberInfo>();
foreach (var channel in animationClipResult.Channels)
{
if (channel.IsUserCustomProperty)
continue;
updateMemberInfos.Add(new UpdateMemberInfo { Name = "[0]." + channel.PropertyName, DataOffset = channel.Offset });
}
// TODO: Cache this
var compiledUpdate = UpdateEngine.Compile(typeof(ModelNodeDefinition[]), updateMemberInfos);
fixed (byte* data = animationClipResult.Data)
{
UpdateEngine.Run(modelNodeDefinitions, compiledUpdate, (IntPtr)data, null);
}
}
Matrix localMatrix;
var transformTRS = modelNodeDefinitions[0].Transform;
Matrix.Transformation(ref transformTRS.Scale, ref transformTRS.Rotation, ref transformTRS.Position,
out localMatrix);
matrix = Matrix.Multiply(localMatrix, matrix);
}
// Done evaluating, let's decompose matrix
TransformTRS transform;
matrix.Decompose(out transform.Scale, out transform.Rotation, out transform.Position);
// Create a key
translationCurve.KeyFrames.Add(new KeyFrameData<Vector3>(animationKey, transform.Position));
rotationCurve.KeyFrames.Add(new KeyFrameData<Quaternion>(animationKey, transform.Rotation));
scaleCurve.KeyFrames.Add(new KeyFrameData<Vector3>(animationKey, transform.Scale));
}
combinedAnimationClip.AddCurve($"{nameof(ModelNodeTransformation.Transform)}.{nameof(TransformTRS.Position)}", translationCurve);
combinedAnimationClip.AddCurve($"{nameof(ModelNodeTransformation.Transform)}.{nameof(TransformTRS.Rotation)}", rotationCurve);
combinedAnimationClip.AddCurve($"{nameof(ModelNodeTransformation.Transform)}.{nameof(TransformTRS.Scale)}", scaleCurve);
nodeAnimationClip = combinedAnimationClip;
}
var transformStart = $"{nameof(ModelNodeTransformation.Transform)}.";
var transformPosition = $"{nameof(ModelNodeTransformation.Transform)}.{nameof(TransformTRS.Position)}";
foreach (var channel in nodeAnimationClip.Channels)
{
var curve = nodeAnimationClip.Curves[channel.Value.CurveIndex];
// TODO: Root motion
var channelName = channel.Key;
if (channelName.StartsWith(transformStart, StringComparison.Ordinal))
{
if (channelName == transformPosition)
{
// Translate node with parent 0 using PivotPosition
var keyFrames = ((AnimationCurve<Vector3>)curve).KeyFrames;
var keyFramesSpan = CollectionsMarshal.AsSpan(keyFrames);
for (int i = 0; i < keyFramesSpan.Length; ++i)
{
if (parentNodeIndex == 0)
keyFramesSpan[i].Value -= PivotPosition;
keyFramesSpan[i].Value *= ScaleImport;
}
}
animationClip.AddCurve($"[ModelComponent.Key].Skeleton.NodeTransformations[{skeletonMapping.SourceToTarget[nodeIndex]}]." + channelName, curve);
}
}
}
}
if (ImportCustomAttributes)
{
// Add clips clips animating other properties than node transformations
foreach (var nodeAnimationClipPair in animationClips)
{
var nodeName = nodeAnimationClipPair.Key;
var nodeAnimationClip = nodeAnimationClipPair.Value;
foreach (var channel in nodeAnimationClip.Channels)
{
var channelName = channel.Key;
var channelValue = channel.Value;
if (channelValue.IsUserCustomProperty)
animationClip.AddCurve(nodeName + "_" + channelName, nodeAnimationClip.Curves[channel.Value.CurveIndex], true);
}
}
}
}
if (animationClip.Channels.Count == 0)
{
var logString = $"File {SourcePath} doesn't have any animation information.";
if (failOnEmptyAnimation)
{
commandContext.Logger.Error(logString);
return null;
}
commandContext.Logger.Info(logString);
}
else
{
if (animationClip.Duration.Ticks == 0)
{
commandContext.Logger.Verbose($"File {SourcePath} has a 0 tick long animation.");
}
// Optimize and set common parameters
animationClip.RepeatMode = AnimationRepeatMode;
animationClip.Optimize();
}
return animationClip;
}
public bool GetAnimationKeyVirtualKey(string vKey, Dictionary<string, AnimationClip> animationClips, out AnimationClip clip)
{
bool isFound = false;
AnimationClip outClip = null;
animationClips.ForEach(c =>
{
string _lineItem = c.Key;
System.IO.Path.GetInvalidFileNameChars().ForEach(x => { _lineItem = _lineItem.Replace(x, '_'); });
if (_lineItem == vKey)
{
outClip = c.Value;
isFound = true;
return;
}
});
clip = outClip;
return isFound;
}
}
}