forked from KhronosGroup/glTF-Asset-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation_NodeMisc.cs
373 lines (342 loc) · 16.7 KB
/
Animation_NodeMisc.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
using AssetGenerator.Runtime;
using System;
using System.Collections.Generic;
using System.Numerics;
namespace AssetGenerator.ModelGroups
{
internal class Animation_NodeMisc : ModelGroup
{
public override ModelGroupId Id => ModelGroupId.Animation_NodeMisc;
public Animation_NodeMisc(List<string> imageList)
{
var baseColorTexture = new Texture { Source = UseTexture(imageList, "BaseColor_Cube") };
// There are no common properties in this model group that are reported in the readme.
Model CreateModel(Action<List<Property>, List<AnimationChannel>, List<Node>, List<Animation>> setProperties)
{
var properties = new List<Property>();
var cubeMeshPrimitive = MeshPrimitive.CreateCube();
// Apply the common properties to the gltf.
cubeMeshPrimitive.Material = new Runtime.Material
{
PbrMetallicRoughness = new PbrMetallicRoughness
{
BaseColorTexture = new TextureInfo { Texture = baseColorTexture },
},
};
var channels = new List<AnimationChannel>
{
new AnimationChannel()
};
var nodes = new List<Node>
{
new Node(),
};
var animations = new List<Animation>
{
new Animation
{
Channels = channels
}
};
// Apply the properties that are specific to this gltf.
setProperties(properties, channels, nodes, animations);
// Create the gltf object.
foreach (var node in nodes)
{
node.Mesh = new Runtime.Mesh
{
MeshPrimitives = new List<Runtime.MeshPrimitive>
{
cubeMeshPrimitive
}
};
}
GLTF gltf = CreateGLTF(() => new Scene
{
Nodes = nodes
});
gltf.Animations = animations;
return new Model
{
Properties = properties,
GLTF = gltf,
Animated = true,
};
}
void SetTranslationChannelTarget(AnimationChannel channel, Node node)
{
channel.Target = new AnimationChannelTarget
{
Node = node,
Path = AnimationChannelTargetPath.Translation,
};
}
void SetRotationChannelTarget(AnimationChannel channel, Node node)
{
channel.Target = new AnimationChannelTarget
{
Node = node,
Path = AnimationChannelTargetPath.Rotation,
};
}
void SetLinearSamplerForTranslation(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
0.0f,
2.0f,
4.0f,
}),
Output = Data.Create(new[]
{
new Vector3(-0.1f, 0.0f, 0.0f),
new Vector3(0.1f, 0.0f, 0.0f),
new Vector3(-0.1f, 0.0f, 0.0f),
}),
};
}
void SetLinearSamplerForHorizontalRotation(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
0.0f,
1.0f,
2.0f,
3.0f,
4.0f,
}),
Output = Data.Create(new[]
{
Quaternion.CreateFromYawPitchRoll(FloatMath.ToRadians(90.0f), 0.0f, 0.0f),
Quaternion.Identity,
Quaternion.CreateFromYawPitchRoll(FloatMath.ToRadians(-90.0f), 0.0f, 0.0f),
Quaternion.Identity,
Quaternion.CreateFromYawPitchRoll(FloatMath.ToRadians(90.0f), 0.0f, 0.0f),
}),
};
}
void SetLinearSamplerForVerticalRotation(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
0.0f,
1.0f,
2.0f,
3.0f,
4.0f,
}),
Output = Data.Create(new[]
{
Quaternion.CreateFromYawPitchRoll(0.0f, FloatMath.ToRadians(90.0f), 0.0f),
Quaternion.Identity,
Quaternion.CreateFromYawPitchRoll(0.0f, FloatMath.ToRadians(-90.0f), 0.0f),
Quaternion.Identity,
Quaternion.CreateFromYawPitchRoll(0.0f, FloatMath.ToRadians(90.0f), 0.0f),
}),
};
}
void SetLinearSamplerForConstantRotation(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
0.0f,
6.0f,
}),
Output = Data.Create(new[]
{
Quaternion.CreateFromYawPitchRoll(-FloatMath.Pi / 3.0f, 0.0f, 0.0f),
Quaternion.CreateFromYawPitchRoll(-FloatMath.Pi / 3.0f, 0.0f, 0.0f),
}),
};
}
void SetLinearSamplerForTranslationStartsAboveZero(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
2.0f,
4.0f,
6.0f,
}),
Output = Data.Create(new[]
{
new Vector3(0.0f, -0.1f, 0.0f),
new Vector3(0.0f, 0.1f, 0.0f),
new Vector3(0.0f, -0.1f, 0.0f),
}),
};
}
void SetLinearSamplerWithOneKey(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
0.0f,
}),
Output = Data.Create(new[]
{
new Vector3(-0.1f, 0.0f, 0.0f),
}),
};
}
void SetLinearSamplerForRotationThatStartsAboveZero(AnimationChannel channel)
{
channel.Sampler = new AnimationSampler
{
Interpolation = AnimationSamplerInterpolation.Linear,
Input = Data.Create(new[]
{
1.0f,
2.0f,
3.0f,
4.0f,
5.0f,
}),
Output = Data.Create(new[]
{
Quaternion.CreateFromYawPitchRoll(FloatMath.ToRadians(90.0f), 0.0f, 0.0f),
Quaternion.Identity,
Quaternion.CreateFromYawPitchRoll(FloatMath.ToRadians(-90.0f), 0.0f, 0.0f),
Quaternion.Identity,
Quaternion.CreateFromYawPitchRoll(FloatMath.ToRadians(90.0f), 0.0f, 0.0f),
}),
};
}
void CreateMultipleChannelsWithUniqueTargets(List<AnimationChannel> channels, Node node)
{
// The first channel is already added as a common property.
channels.Add(new AnimationChannel());
SetTranslationChannelTarget(channels[0], node);
SetRotationChannelTarget(channels[1], node);
var samplerPropertiesList = new List<Property>();
SetLinearSamplerForTranslation(channels[0]);
SetLinearSamplerForHorizontalRotation(channels[1]);
}
void CreateMultipleChannelsWithDifferentTimes(List<AnimationChannel> channels, Node node)
{
// The first channel is already added as a common property.
channels.Add(new AnimationChannel());
SetTranslationChannelTarget(channels[0], node);
SetRotationChannelTarget(channels[1], node);
SetLinearSamplerForTranslationStartsAboveZero(channels[0]);
SetLinearSamplerForRotationThatStartsAboveZero(channels[1]);
}
void CreateMultipleChannelsForDifferentNodes(List<AnimationChannel> channels, Node node0, Node node1)
{
// The first channel is already added as a common property.
channels.Add(new AnimationChannel());
SetRotationChannelTarget(channels[0], node0);
SetRotationChannelTarget(channels[1], node1);
SetLinearSamplerForHorizontalRotation(channels[0]);
SetLinearSamplerForVerticalRotation(channels[1]);
}
Models = new List<Model>
{
CreateModel((properties, channels, nodes, animations) =>
{
// Multiple channels
CreateMultipleChannelsWithUniqueTargets(channels, nodes[0]);
properties.Add(new Property(PropertyName.Description,
"There are two channels. The first channel targets translation. The second channel targets rotation. The start and end times of both channels are `0.0` and `4.0` respectively."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Curve that doesn't start at zero
SetRotationChannelTarget(channels[0], nodes[0]);
SetLinearSamplerForRotationThatStartsAboveZero(channels[0]);
properties.Add(new Property(PropertyName.Description,
"There is one channel with a non-zero start time. The channel targets rotation. The start time is `1.0`."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Two channels with different start/end times
CreateMultipleChannelsWithDifferentTimes(channels, nodes[0]);
properties.Add(new Property(PropertyName.Description,
"There are two channels with different start and end times. The first channel targets translation with start and end times of `2.0` and `6.0` respectively. The second channel targets rotation with start and end times of `1.0` and `5.0` respectively."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Has only one key
SetTranslationChannelTarget(channels[0], nodes[0]);
SetLinearSamplerWithOneKey(channels[0]);
properties.Add(new Property(PropertyName.Description,
"There is one channel with only one keyframe. The channel targets translation with a value of <code>[-0.1, 0.0, 0.0]</code>."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Creates a second node based on the existing node, and applies a transform to help differentiate them.
nodes.Add(DeepCopy.CloneObject(nodes[0]));
nodes[0].Translation = new Vector3(-0.2f, 0.0f, 0.0f);
nodes[1].Translation = new Vector3(0.2f, 0.0f, 0.0f);
nodes[0].Scale = new Vector3(0.5f, 0.5f, 0.5f);
nodes[1].Scale = new Vector3(0.5f, 0.5f, 0.5f);
// One animation, two channels for two nodes.
CreateMultipleChannelsForDifferentNodes(channels, nodes[0], nodes[1]);
properties.Add(new Property(PropertyName.Description,
"There are two channels with different nodes. The first channel targets the left node and rotation along the X axis. The second channel targets the right node and rotation along the Y axis."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Rotate the model, and then apply the same target animation to it (Animation overrides)
nodes[0].Rotation = Quaternion.CreateFromYawPitchRoll(FloatMath.Pi / 3.0f, 0.0f, 0.0f);
SetRotationChannelTarget(channels[0], nodes[0]);
SetLinearSamplerForConstantRotation(channels[0]);
properties.Add(new Property(PropertyName.Description,
"There is one channel that targets a node. The node has a rotation of <code>[0.0, 0.5, 0.0, 0.866]</code>. The channel overrides the rotation of the node to a different constant value of <code>[0.0, -0.5, 0.0, 0.866]</code>."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Rotate the model, and then apply an translation animation to it (Animation doesn't override rotation)
nodes[0].Rotation = Quaternion.CreateFromYawPitchRoll(FloatMath.Pi / 3.0f, 0.0f, 0.0f);
SetTranslationChannelTarget(channels[0], nodes[0]);
SetLinearSamplerForTranslation(channels[0]);
properties.Add(new Property(PropertyName.Description,
"There is one channel that targets a node. The node has a rotation of <code>[0.0, 0.5, 0.0, 0.866]</code>. The channel targets the translation of the node."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Two animations. One rotates, the other translates. They should not interact or bleed across.
var channel = new AnimationChannel();
SetTranslationChannelTarget(channel, nodes[0]);
SetLinearSamplerForTranslation(channel);
animations.Add(new Animation
{
Channels = new[]
{
channel
}
});
// The first animation is already added as an empty common property.
SetRotationChannelTarget(channels[0], nodes[0]);
SetLinearSamplerForHorizontalRotation(channels[0]);
properties.Add(new Property(PropertyName.Description,
"There are two animations, each with one channel. The first animation's channel targets rotation. The second animation's channel targets translation."));
}),
CreateModel((properties, channels, nodes, animations) =>
{
// Multiple channels, but one omits node (the channel with no target node is ignored per the spec).
CreateMultipleChannelsForDifferentNodes(channels, null, nodes[0]);
properties.Add(new Property(PropertyName.Description,
"There are two channels. The first channel has a rotation along the X axis but does not specify a node. The second channel does target the node and has a rotation along the Y axis."));
}),
};
GenerateUsedPropertiesList();
}
}
}