Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine_animations.inc

raw
1 {%MainUnit x3dloadinternalspine.pas}
2 {
3 Copyright 2014-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Spine animations. }
18
19 {$ifdef read_interface}
20 TAnimation = class
21 Name: string;
22 BoneTimelines: TBoneTimelineList;
23 SlotTimelines: TSlotTimelineList;
24 DrawOrderTimelines: TDrawOrderTimelineList;
25 DeformTimelines: TDeformTimelineList;
26 WeightedMeshTimelines: TWeightedMeshTimelineList;
27 Node: TTimeSensorNode;
28 NodeUsedAsChild: boolean;
29 constructor Create;
30 destructor Destroy; override;
31 procedure Parse(const Json: TJSONObject;
32 const Bones: TBoneList; const Slots: TSlotList; const CurrentSkin: TSkin;
33 const NormalizeCurvesKeyValues: Boolean;
34 var CurvesRemoved: Cardinal);
35 procedure BuildNodes(const BaseUrl: String; const Container: TX3DRootNode);
36 end;
37
38 TAnimationList = class({$ifdef FPC}specialize{$endif} TObjectList<TAnimation>)
39 procedure Parse(const Json: TJSONObject;
40 const Bones: TBoneList; const Slots: TSlotList; const CurrentSkin: TSkin;
41 const NormalizeCurvesKeyValues: Boolean);
42 procedure BuildNodes(const BaseUrl: String; const Container: TX3DRootNode);
43 { Add all TimeSensors to EXPORTed nodes. }
44 procedure Exported(const RootNode: TX3DRootNode);
45 end;
46 {$endif}
47
48 {$ifdef read_implementation}
49
50 { TAnimation ----------------------------------------------------------------- }
51
52 constructor TAnimation.Create;
53 begin
54 inherited;
55 BoneTimelines := TBoneTimelineList.Create;
56 SlotTimelines := TSlotTimelineList.Create;
57 DrawOrderTimelines := TDrawOrderTimelineList.Create;
58 DeformTimelines := TDeformTimelineList.Create;
59 WeightedMeshTimelines := TWeightedMeshTimelineList.Create;
60 end;
61
62 destructor TAnimation.Destroy;
63 begin
64 if NodeUsedAsChild then
65 Node := nil else
66 FreeIfUnusedAndNil(Node);
67 FreeAndNil(BoneTimelines);
68 FreeAndNil(SlotTimelines);
69 FreeAndNil(DrawOrderTimelines);
70 FreeAndNil(DeformTimelines);
71 FreeAndNil(WeightedMeshTimelines);
72 inherited;
73 end;
74
75 procedure TAnimation.Parse(const Json: TJSONObject;
76 const Bones: TBoneList; const Slots: TSlotList; const CurrentSkin: TSkin;
77 const NormalizeCurvesKeyValues: Boolean;
78 var CurvesRemoved: Cardinal);
79
80 procedure ReadBoneTimelines(const Bone: TBone; const Json: TJSONObject);
81 var
82 BoneTimeline: TBoneTimeline;
83 ChildArray: TJSONArray;
84 begin
85 ChildArray := Json.Find('translate', jtArray) as TJSONArray;
86 if ChildArray <> nil then
87 begin
88 BoneTimeline := TBoneTimelineTranslate.Create;
89 BoneTimeline.Bone := Bone;
90 BoneTimeline.Parse(ChildArray, NormalizeCurvesKeyValues);
91
92 if BoneTimeline.DoingSomething then
93 begin
94 BoneTimelines.Add(BoneTimeline);
95 end else
96 begin
97 Inc(CurvesRemoved);
98 FreeAndNil(BoneTimeline);
99 end;
100 end;
101
102 ChildArray := Json.Find('scale', jtArray) as TJSONArray;
103 if ChildArray <> nil then
104 begin
105 BoneTimeline := TBoneTimelineScale.Create;
106 BoneTimeline.Bone := Bone;
107 BoneTimeline.Parse(ChildArray, NormalizeCurvesKeyValues);
108
109 if BoneTimeline.DoingSomething then
110 begin
111 BoneTimelines.Add(BoneTimeline);
112 end else
113 begin
114 Inc(CurvesRemoved);
115 FreeAndNil(BoneTimeline);
116 end;
117 end;
118
119 ChildArray := Json.Find('rotate', jtArray) as TJSONArray;
120 if ChildArray <> nil then
121 begin
122 BoneTimeline := TBoneTimelineRotate.Create;
123 BoneTimeline.Bone := Bone;
124 BoneTimeline.Parse(ChildArray, NormalizeCurvesKeyValues);
125
126 if BoneTimeline.DoingSomething then
127 begin
128 BoneTimelines.Add(BoneTimeline);
129 end else
130 begin
131 Inc(CurvesRemoved);
132 FreeAndNil(BoneTimeline);
133 end;
134 end;
135 end;
136
137 procedure ReadSlotTimelines(const Slot: TSlot; const Json: TJSONObject);
138 var
139 SlotTimeline: TSlotTimeline;
140 ChildArray: TJSONArray;
141 begin
142 ChildArray := Json.Find('attachment', jtArray) as TJSONArray;
143 if ChildArray <> nil then
144 begin
145 SlotTimeline := TSlotTimelineAttachment.Create;
146 SlotTimeline.Slot := Slot;
147 SlotTimeline.Parse(ChildArray, NormalizeCurvesKeyValues);
148 SlotTimelines.Add(SlotTimeline);
149 end;
150
151 ChildArray := Json.Find('color', jtArray) as TJSONArray;
152 if ChildArray = nil then
153 ChildArray := Json.Find('rgba', jtArray) as TJSONArray; // new name around Spine >= 4.0
154 if ChildArray <> nil then
155 begin
156 SlotTimeline := TSlotTimelineColor.Create;
157 SlotTimeline.Slot := Slot;
158 SlotTimeline.Parse(ChildArray, NormalizeCurvesKeyValues);
159 SlotTimelines.Add(SlotTimeline);
160 end;
161 end;
162
163 procedure ReadDeformTimelines(const SlotName: string;
164 const Json: TJSONObject; const Slots: TSlotList);
165 var
166 I: Integer;
167 DeformTimeline: TDeformTimeline;
168 Attachment: TAttachment;
169 AttachmentName: String;
170 begin
171 for I := 0 to Json.Count - 1 do
172 begin
173 AttachmentName := Json.Names[I];
174 Attachment := CurrentSkin.Attachments.Find(SlotName, AttachmentName, nil, false);
175 if Attachment = nil then
176 begin
177 WritelnWarning('Cannot find attachment "%s" in slot "%s" to animate using deformation', [
178 AttachmentName,
179 SlotName
180 ]);
181 Continue;
182 end;
183 if not (Attachment is TMeshAttachment) then
184 begin
185 WritelnWarning('Attachment "%s" in slot "%s" cannot be animated using deformation, as it is not a mesh attachment, it is %s', [
186 AttachmentName,
187 SlotName,
188 Attachment.ClassName
189 ]);
190 Continue;
191 end;
192 if not (Json.Items[I] is TJSONArray) then
193 begin
194 WritelnWarning('Attachment "%s" in slot "%s" cannot be animated: JSON error, expected array', [
195 AttachmentName,
196 SlotName
197 ]);
198 Continue;
199 end;
200 DeformTimeline := TDeformTimeline.Create;
201 DeformTimeline.Slot := Slots.Find(SlotName);
202 DeformTimeline.Attachment := Attachment as TMeshAttachment;
203 DeformTimeline.Parse(Json.Items[I] as TJSONArray, NormalizeCurvesKeyValues);
204 DeformTimelines.Add(DeformTimeline);
205 end;
206 end;
207
208 { There's no such thing as 'weighted mesh timeline', so we generate weighted
209 mesh timelines by checking to see if a mesh is affect by bones, and that
210 mesh is a weighted mesh or not, then we will calculate vertices affected by
211 those bones }
212 procedure ReadWeightedMeshTimelines;
213 var
214 I, J: Integer;
215 Attachment: TAttachment;
216 WeightedMeshTimeline: TWeightedMeshTimeline;
217 AffectedDeformTimeline: TDeformTimeline;
218 begin
219 for I := 0 to CurrentSkin.Attachments.Count - 1 do
220 begin
221 Attachment := CurrentSkin.Attachments[I];
222 // Make sure this attachment is a weighted mesh
223 if (Attachment is TMeshAttachment) and ((Attachment as TMeshAttachment).Weights.Count > 0) then
224 begin
225 // Backup original bone timeline properties
226 for J := 0 to BoneTimelines.Count - 1 do
227 BoneTimelines[J].Backup;
228 // Search through deform timeline list to find affected one
229 AffectedDeformTimeline := nil;
230 for J := 0 to DeformTimelines.Count - 1 do
231 begin
232 if DeformTimelines[J].Attachment.Name = Attachment.Name then
233 begin
234 AffectedDeformTimeline := DeformTimelines[J];
235 AffectedDeformTimeline.IsHandledByWeightedMeshTimeline := True;
236 // Backup the original Time
237 AffectedDeformTimeline.Backup;
238 Break;
239 end;
240 end;
241 // We generate weightedmesh timeline if the number of bone timeline > 0,
242 // or has AffectedDeformTimeline
243 if (BoneTimelines.Count > 0) or (AffectedDeformTimeline <> nil) then
244 begin
245 WeightedMeshTimeline := TWeightedMeshTimeline.Create;
246 WeightedMeshTimeline.AffectedDeformTimeline := AffectedDeformTimeline;
247 WeightedMeshTimeline.Name := IntToStr(I);
248 WeightedMeshTimeline.Attachment := Attachment as TMeshAttachment;
249 WeightedMeshTimeline.AffectedBoneTimelines := BoneTimelines;
250 WeightedMeshTimeline.Parse;
251 WeightedMeshTimelines.Add(WeightedMeshTimeline);
252 end;
253 // Restore bone timeline properties
254 for J := 0 to BoneTimelines.Count - 1 do
255 BoneTimelines[J].Restore;
256 end;
257 end;
258 end;
259
260 var
261 I: Integer;
262 Bone: TBone;
263 Slot: TSlot;
264 ChildObj: TJSONObject;
265 ChildArray: TJSONArray;
266 begin
267 ChildObj := Json.Find('bones', jtObject) as TJSONObject;
268 if ChildObj <> nil then
269 begin
270 for I := 0 to ChildObj.Count - 1 do
271 if ChildObj.Items[I] is TJSONObject then
272 begin
273 Bone := Bones.Find(ChildObj.Names[I]);
274 ReadBoneTimelines(Bone, TJSONObject(ChildObj.Items[I]));
275 end;
276 end;
277
278 ChildObj := Json.Find('slots', jtObject) as TJSONObject;
279 if ChildObj <> nil then
280 begin
281 for I := 0 to ChildObj.Count - 1 do
282 if ChildObj.Items[I] is TJSONObject then
283 begin
284 Slot := Slots.Find(ChildObj.Names[I]);
285 ReadSlotTimelines(Slot, TJSONObject(ChildObj.Items[I]));
286 end;
287 end;
288
289 ChildArray := Json.Find('draworder', jtArray) as TJSONArray;
290 if ChildArray = nil then
291 ChildArray := Json.Find('drawOrder', jtArray) as TJSONArray; // try different case
292 if ChildArray <> nil then
293 DrawOrderTimelines.Parse(ChildArray, Slots);
294
295 ChildObj := Json.Find('deform', jtObject) as TJSONObject;
296 if ChildObj <> nil then
297 begin
298 ChildObj := ChildObj.Find(CurrentSkin.Name) as TJSONObject;
299 if ChildObj <> nil then
300 for I := 0 to ChildObj.Count - 1 do
301 if ChildObj.Items[I] is TJSONObject then
302 ReadDeformTimelines(ChildObj.Names[I], TJSONObject(ChildObj.Items[I]), Slots);
303 end;
304
305 ReadWeightedMeshTimelines;
306 end;
307
308 procedure TAnimation.BuildNodes(const BaseUrl: String;
309 const Container: TX3DRootNode);
310 var
311 MaxTime: Single;
312 I: Integer;
313 Route: TX3DRoute;
314 begin
315 Node := TTimeSensorNode.Create(DefaultAnimationPrefix + Name, BaseUrl);
316
317 MaxTime := 0;
318 for I := 0 to BoneTimelines.Count - 1 do
319 MaxVar(MaxTime, BoneTimelines[I].MaxTime);
320 for I := 0 to SlotTimelines.Count - 1 do
321 MaxVar(MaxTime, SlotTimelines[I].MaxTime);
322 for I := 0 to DeformTimelines.Count - 1 do
323 MaxVar(MaxTime, DeformTimelines[I].MaxTime);
324 MaxVar(MaxTime, DrawOrderTimelines.MaxTime);
325 { TimeSensor.cycleInterval should be > 0
326 (see testcase spine/test-animation-zero-time for where it's needed).
327 Also, this is passed as MaxAnimationTime to other BuildNodes methods,
328 that also assume that MaxAnimationTime > 0. }
329 if MaxTime <= 0 then
330 MaxTime := 1;
331 Node.CycleInterval := MaxTime;
332
333 NodeUsedAsChild := true;
334 Container.AddChildren(Node);
335
336 for I := 0 to BoneTimelines.Count - 1 do
337 begin
338 BoneTimelines[I].BuildNodes(BaseUrl, MaxTime, Container);
339 { add animation name to bone timeline, to make it unique in case
340 we have many animations }
341 BoneTimelines[I].Node.X3DName := Node.X3DName + '_' + BoneTimelines[I].Node.X3DName;
342
343 Route := TX3DRoute.Create;
344 Route.SetSourceDirectly(Node.EventFraction_changed);
345 Route.SetDestinationDirectly(BoneTimelines[I].Node.EventSet_fraction);
346 Container.AddRoute(Route);
347 end;
348
349 for I := 0 to SlotTimelines.Count - 1 do
350 begin
351 SlotTimelines[I].BuildNodes(BaseUrl, MaxTime, Container);
352 { add animation name to slot timeline, to make it unique in case
353 we have many animations }
354 SlotTimelines[I].Node1.Node.X3DName := Node.X3DName + '_' +
355 SlotTimelines[I].Node1.Node.X3DName;
356 Route := TX3DRoute.Create;
357 Route.SetSourceDirectly(Node.EventFraction_changed);
358 Route.SetDestinationDirectly(SlotTimelines[I].Node1.EventSet_fraction);
359 Container.AddRoute(Route);
360
361 if SlotTimelines[I].HasNode2 then
362 begin
363 SlotTimelines[I].Node2.Node.X3DName := Node.X3DName + '_' +
364 SlotTimelines[I].Node2.Node.X3DName;
365 Route := TX3DRoute.Create;
366 Route.SetSourceDirectly(Node.EventFraction_changed);
367 Route.SetDestinationDirectly(SlotTimelines[I].Node2.EventSet_fraction);
368 Container.AddRoute(Route);
369 end;
370 end;
371
372 for I := 0 to DrawOrderTimelines.Count - 1 do
373 begin
374 DrawOrderTimelines[I].BuildNodes(BaseUrl, DrawOrderTimelines.Time,
375 MaxTime, Container);
376 { add animation name to draworder timeline, to make it unique in case
377 we have many animations }
378 DrawOrderTimelines[I].Node.X3DName := Node.X3DName + '_' +
379 DrawOrderTimelines[I].Node.X3DName;
380 Route := TX3DRoute.Create;
381 Route.SetSourceDirectly(Node.EventFraction_changed);
382 Route.SetDestinationDirectly(DrawOrderTimelines[I].Node.EventSet_fraction);
383 Container.AddRoute(Route);
384 end;
385
386 for I := 0 to DeformTimelines.Count - 1 do
387 begin
388 if DeformTimelines[I].IsHandledByWeightedMeshTimeline then
389 Continue;
390 DeformTimelines[I].BuildNodes(MaxTime, Container);
391 { add animation name to deform timeline, to make it unique in case
392 we have many animations }
393 DeformTimelines[I].Node.X3DName := Node.X3DName + '_' +
394 DeformTimelines[I].Node.X3DName;
395 Route := TX3DRoute.Create;
396 Route.SetSourceDirectly(Node.EventFraction_changed);
397 Route.SetDestinationDirectly(DeformTimelines[I].Node.EventSet_fraction);
398 Container.AddRoute(Route);
399 end;
400
401 for I := 0 to WeightedMeshTimelines.Count - 1 do
402 begin
403 WeightedMeshTimelines[I].BuildNodes(MaxTime, Container);
404 { add animation name to weighted mesh timeline, to make it unique in case
405 we have many animations }
406 WeightedMeshTimelines[I].Node.X3DName := Node.X3DName + '_' +
407 WeightedMeshTimelines[I].Node.X3DName;
408 Route := TX3DRoute.Create;
409 Route.SetSourceDirectly(Node.EventFraction_changed);
410 Route.SetDestinationDirectly(WeightedMeshTimelines[I].Node.EventSet_fraction);
411 Container.AddRoute(Route);
412 end;
413 end;
414
415 { TAnimationList ------------------------------------------------------------- }
416
417 procedure TAnimationList.Parse(const Json: TJSONObject;
418 const Bones: TBoneList; const Slots: TSlotList; const CurrentSkin: TSkin;
419 const NormalizeCurvesKeyValues: Boolean);
420 var
421 I: Integer;
422 Animation: TAnimation;
423 ChildObj: TJSONObject;
424 CurvesRemoved: Cardinal;
425 begin
426 ChildObj := Json.Find('animations', jtObject) as TJSONObject;
427 if ChildObj <> nil then
428 begin
429 { Note that we do not raise error when "animations" element is missing,
430 it just means for us that there are no animations.
431 Not sure whether Spine can actually output such file, though. }
432
433 CurvesRemoved := 0;
434
435 for I := 0 to ChildObj.Count - 1 do
436 if ChildObj.Items[I] is TJSONObject then
437 begin
438 Animation := TAnimation.Create;
439 Add(Animation);
440 Animation.Name := ChildObj.Names[I];
441 Animation.Parse(TJSONObject(ChildObj.Items[I]), Bones, Slots,
442 CurrentSkin, NormalizeCurvesKeyValues, CurvesRemoved);
443 end;
444
445 // WritelnLog('Spine', 'Spine animations curves parsed, empty curves removed: %d',
446 // [CurvesRemoved]);
447 end;
448 end;
449
450 procedure TAnimationList.BuildNodes(const BaseUrl: String;
451 const Container: TX3DRootNode);
452 var
453 I: Integer;
454 begin
455 for I := 0 to Count - 1 do
456 Items[I].BuildNodes(BaseUrl, Container);
457 end;
458
459 procedure TAnimationList.Exported(const RootNode: TX3DRootNode);
460 var
461 I: Integer;
462 begin
463 for I := 0 to Count - 1 do
464 RootNode.ExportNode(Items[I].Node);
465 end;
466
467 {$endif}