Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine_drawordertimelines.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 draworder timeline.
18
19 Although similar to slot timeline, it is specified differently in JSON file,
20 and also must be processed a little differently to X3D
21 (needs duplicate keys/values to pretent that it's discrete),
22 so it is implemented independently of TSlotTimeline class. }
23
24 {$ifdef read_interface}
25 TDrawOrderTimeline = class
26 public
27 Slot: TSlot;
28 DrawOrder: TSingleList;
29 Node: TPositionInterpolatorNode;
30 NodeUsedAsChild: boolean;
31 constructor Create;
32 destructor Destroy; override;
33 procedure BuildNodes(const BaseUrl: String;
34 const Time: TSingleList; const MaxAnimationTime: Single;
35 const Container: TX3DRootNode);
36 end;
37
38 TDrawOrderTimelineList = class({$ifdef FPC}specialize{$endif} TObjectList<TDrawOrderTimeline>)
39 strict private
40 FMaxTime: Single;
41 public
42 Time: TSingleList;
43 constructor Create;
44 destructor Destroy; override;
45 property MaxTime: Single read FMaxTime;
46 { Find timeline by a slot. @nil if not exists. }
47 function FindSlot(const Slot: TSlot): TDrawOrderTimeline;
48 procedure Parse(const Json: TJSONArray; const Slots: TSlotList);
49 end;
50 {$endif}
51
52 {$ifdef read_implementation}
53
54 { TDrawOrderTimeline --------------------------------------------------------- }
55
56 constructor TDrawOrderTimeline.Create;
57 begin
58 inherited;
59 DrawOrder := TSingleList.Create;
60 end;
61
62 destructor TDrawOrderTimeline.Destroy;
63 begin
64 FreeAndNil(DrawOrder);
65 inherited;
66 end;
67
68 procedure TDrawOrderTimeline.BuildNodes(const BaseUrl: String;
69 const Time: TSingleList; const MaxAnimationTime: Single;
70 const Container: TX3DRootNode);
71 var
72 I: Integer;
73 Route: TX3DRoute;
74 begin
75 Node := TPositionInterpolatorNode.Create('DrawOrderTimeline_' + Slot.Name);
76
77 for I := 0 to Time.Count - 1 do
78 begin
79 { Write the previous value at the same time. This forces "stepped"
80 interpolation instead of linear for draw order. }
81 if Time[I] <> 0 then
82 begin
83 Node.FdKey.Items.Add(Time[I] / MaxAnimationTime);
84 if I = 0 then
85 Node.FdKeyValue.Items.Add(Slot.Node.FdTranslation.Value) else
86 Node.FdKeyValue.Items.Add(Node.FdKeyValue.Items.Last);
87 end;
88
89 Node.FdKey.Items.Add(Time[I] / MaxAnimationTime);
90 Node.FdKeyValue.Items.Add(Vector3(0, 0, DrawOrder[I] * TSlot.DrawOrderZ));
91 end;
92
93 NodeUsedAsChild := true;
94 Container.AddChildren(Node);
95
96 { Note: in other anims, we checked Time.Count > 1 now.
97 For this, it seems Ok for Time.Count = 1 ? }
98 Route := TX3DRoute.Create;
99 Route.SetSourceDirectly(Node.EventValue_changed);
100 Route.SetDestinationDirectly(Slot.Node.FdTranslation.EventIn);
101 Container.AddRoute(Route);
102 end;
103
104 { TDrawOrderTimelineList ----------------------------------------------------- }
105
106 constructor TDrawOrderTimelineList.Create;
107 begin
108 { do not call just "inherited", it would call TObject.Create?
109 TODO: investigate and report FPC bug if yes. }
110 inherited Create(true);
111 Time := TSingleList.Create;
112 end;
113
114 destructor TDrawOrderTimelineList.Destroy;
115 begin
116 FreeAndNil(Time);
117 inherited;
118 end;
119
120 type
121 TSlotsByDrawOrder = {$ifdef FPC}specialize{$endif} TDictionary<Integer, TSlot>;
122 TSlotsToFloatDrawOrder = {$ifdef FPC}specialize{$endif} TDictionary<TSlot, Single>;
123
124 procedure TDrawOrderTimelineList.Parse(const Json: TJSONArray; const Slots: TSlotList);
125
126 procedure AddTimelines;
127 var
128 I, J: Integer;
129 JsonOffsets: TJSONArray;
130 JsonOffset, O: TJSONObject;
131 SlotName: string;
132 Slot: TSlot;
133 Timeline: TDrawOrderTimeline;
134 begin
135 for I := 0 to Json.Count - 1 do
136 if Json[I] is TJSONObject then
137 begin
138 O := TJSONObject(Json[I]);
139
140 JsonOffsets := O.Find('offsets', jtArray) as TJSONArray;
141 if JsonOffsets <> nil then
142 for J := 0 to JsonOffsets.Count - 1 do
143 if JsonOffsets[J] is TJSONObject then
144 begin
145 JsonOffset := TJSONObject(JsonOffsets[J]);
146 SlotName := JsonOffset.Get('slot', '');
147 Slot := Slots.Find(SlotName);
148 if FindSlot(Slot) = nil then
149 begin
150 Timeline := TDrawOrderTimeline.Create;
151 Timeline.Slot := Slot;
152 Add(Timeline);
153 end;
154 end;
155 end;
156 end;
157
158 { For a subset of slots, we have an integer draw order where they should be.
159 Now we want to calculate for them float draw order where they should be,
160 squeezing them between ranges of unchanged slots.
161
162 This way we avoid animating draw order of unchanged slots (no need
163 to shift them, just because other slots order changed),
164 by utilizing the fact that our draw order is in fact float.
165
166 @param SlotsByDrawOrder Input to our algorithm.
167 @param SlotsToFloatDrawOrder Output of our algorithm.
168 }
169 procedure SqueezeSlotDrawOrder(
170 const SlotsByDrawOrder: TSlotsByDrawOrder;
171 const SlotsToFloatDrawOrder: TSlotsToFloatDrawOrder);
172 var
173 I, J, LastUnchangedDrawOrder, FollowingUnchangedDrawOrder,
174 FirstChanged, LastChanged, NextUnchangedSlot: Integer;
175 SlotsOrdered, SlotsUnchanged: TSlotList;
176 SlotByDrawOrderPair: {$ifdef FPC}TSlotsByDrawOrder.TDictionaryPair{$else}TPair<Integer, TSlot>{$endif};
177 begin
178 SlotsOrdered := nil;
179 SlotsUnchanged := nil;
180 try
181 SlotsOrdered := TSlotList.Create(false);
182 SlotsUnchanged := TSlotList.Create(false);
183
184 { SlotsUnchanged is kept in draw order order, it contains only slots
185 where draw order remains unchanged }
186 SlotsUnchanged.Assign(Slots);
187 { SlotsOrdered contains only slots where draw order changes or nil values,
188 it is indexed by setup pose draw order. }
189 SlotsOrdered.Count := Slots.Count;
190
191 for SlotByDrawOrderPair in SlotsByDrawOrder do
192 begin
193 SlotsOrdered[SlotByDrawOrderPair.Key] := SlotByDrawOrderPair.Value;
194 SlotsUnchanged.Remove(SlotByDrawOrderPair.Value);
195 end;
196
197 I := 0;
198 LastUnchangedDrawOrder := -1;
199 NextUnchangedSlot := 0;
200 while I < SlotsOrdered.Count do
201 begin
202 { look for next block of changed slots }
203 while (I < SlotsOrdered.Count) and (SlotsOrdered[I] = nil) do
204 begin
205 { skip over next unchanged slot }
206 LastUnchangedDrawOrder := SlotsUnchanged[NextUnchangedSlot].DrawOrder;
207 Inc(NextUnchangedSlot);
208 Inc(I);
209 end;
210 FirstChanged := I;
211 while (I < SlotsOrdered.Count) and (SlotsOrdered[I] <> nil) do
212 Inc(I);
213 if NextUnchangedSlot >= SlotsUnchanged.Count then
214 FollowingUnchangedDrawOrder := Slots.Count else
215 FollowingUnchangedDrawOrder := SlotsUnchanged[NextUnchangedSlot].DrawOrder;
216 LastChanged := I - 1;
217 for J := FirstChanged to LastChanged do
218 SlotsToFloatDrawOrder.AddOrSetValue(SlotsOrdered[J],
219 MapRange(J, FirstChanged - 1, LastChanged + 1, LastUnchangedDrawOrder, FollowingUnchangedDrawOrder)
220 );
221 end;
222 finally
223 FreeAndNil(SlotsOrdered);
224 FreeAndNil(SlotsUnchanged);
225 end;
226 end;
227
228 procedure ReadTimelines;
229 var
230 NextTime: Single;
231 I, J, DrawOrder: Integer;
232 JsonOffsets: TJSONArray;
233 JsonOffset, O: TJSONObject;
234 SlotsByDrawOrder: TSlotsByDrawOrder;
235 SlotsToFloatDrawOrder: TSlotsToFloatDrawOrder;
236 Timeline: TDrawOrderTimeline;
237 SlotName: string;
238 Slot: TSlot;
239 SlotsToFloatDrawOrderPair: {$ifdef FPC}TSlotsToFloatDrawOrder.TDictionaryPair{$else}TPair<TSlot, Single>{$endif};
240 begin
241 SlotsByDrawOrder := nil;
242 SlotsToFloatDrawOrder := nil;
243 try
244 SlotsByDrawOrder := TSlotsByDrawOrder.Create;
245 SlotsToFloatDrawOrder := TSlotsToFloatDrawOrder.Create;
246
247 for I := 0 to Json.Count - 1 do
248 if Json[I] is TJSONObject then
249 begin
250 O := TJSONObject(Json[I]);
251
252 NextTime := O.Get('time', 0.0);
253 if (Time.Count <> 0) and (Time.Last > NextTime) then
254 raise ESpineReadError.Create('Timeline must have increasing time values');
255 Time.Add(NextTime);
256 FMaxTime := NextTime;
257
258 SlotsByDrawOrder.Clear;
259 SlotsToFloatDrawOrder.Clear;
260
261 JsonOffsets := O.Find('offsets', jtArray) as TJSONArray;
262 if JsonOffsets <> nil then
263 begin
264 for J := 0 to JsonOffsets.Count - 1 do
265 begin
266 { cast checking is it TJSONObject }
267 JsonOffset := JsonOffsets[J] as TJSONObject;
268 SlotName := JsonOffset.Get('slot', '');
269 Slot := Slots.Find(SlotName);
270 DrawOrder := Slot.DrawOrder + JsonOffset.Get('offset', 0);
271 if (DrawOrder < 0) or
272 (DrawOrder >= Slots.Count) then
273 begin
274 WritelnWarning('Spine', Format('Animated draworder offset incorrect, goes outside draw order range: slot "%s" with draw order %d',
275 [Slot.Name, DrawOrder]));
276 Continue;
277 end;
278 if SlotsByDrawOrder.ContainsKey(DrawOrder) then
279 WritelnWarning('Spine', Format('Animated draworder offset incorrect, multiple slots try to take the same draw order: "%s" and "%s"',
280 [SlotsByDrawOrder[DrawOrder].Name, Slot.Name]));
281
282 { in case of warning above, just overwrite previous slot
283 that landed on the same draw order }
284 SlotsByDrawOrder.AddOrSetValue(DrawOrder, Slot);
285 end;
286
287 SqueezeSlotDrawOrder(SlotsByDrawOrder, SlotsToFloatDrawOrder);
288
289 for SlotsToFloatDrawOrderPair in SlotsToFloatDrawOrder do
290 begin
291 Slot := SlotsToFloatDrawOrderPair.Key;
292 { We do not store just offset, we store calculated DrawOrder
293 in a timeline. This way timeline can be exported without
294 the knowledge of other draw order timelines. }
295 Timeline := FindSlot(Slot);
296 Assert(Timeline <> nil, 'We failed to add slot timeline in AddTimelines');
297 if Timeline.DrawOrder.Count <> Time.Count - 1 then
298 WritelnWarning('Spine', 'draworder timeline count incorrect, it seems that the same slot name was specified multiple times for an offset during the same time for slot: ' +
299 Slot.Name);
300 Timeline.DrawOrder.Add(SlotsToFloatDrawOrderPair.Value);
301 end;
302 end;
303
304 { add offset = 0 for slots not specified here }
305 for J := 0 to Count - 1 do
306 begin
307 Timeline := Items[J];
308 if not SlotsToFloatDrawOrder.ContainsKey(Timeline.Slot) then
309 Timeline.DrawOrder.Add(Timeline.Slot.DrawOrder);
310 end;
311 end;
312 finally
313 FreeAndNil(SlotsByDrawOrder);
314 FreeAndNil(SlotsToFloatDrawOrder);
315 end;
316 end;
317
318 begin
319 { 2 passes: first add timelines, then set them }
320 AddTimelines;
321 ReadTimelines;
322 end;
323
324 function TDrawOrderTimelineList.FindSlot(const Slot: TSlot): TDrawOrderTimeline;
325 var
326 I: Integer;
327 begin
328 for I := 0 to Count - 1 do
329 if Items[I].Slot = Slot then
330 Exit(Items[I]);
331 Result := nil;
332 end;
333
334 {$endif}