Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine_deformtimelines.inc

raw
1 {%MainUnit x3dloadinternalspine.pas}
2 {
3 Copyright 2019 Trung Le (kagamma).
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 deform timelines. }
18
19 {$ifdef read_interface}
20 TDeformVertexList = class({$ifdef FPC}specialize{$endif} TObjectList<TSingleList>)
21 end;
22
23 TDeformTimeline = class
24 strict private
25 FMaxTime: Single;
26 IsBackup: Boolean;
27 public
28 { If this is true, then we will ignore this deform during building node }
29 IsHandledByWeightedMeshTimeline: Boolean;
30 Slot: TSlot;
31 Attachment: TMeshAttachment;
32 Time: TSingleList;
33 BackupTime: TSingleList;
34 Vertices: TDeformVertexList;
35 BackupVertices: TDeformVertexList;
36 Curve: Boolean;
37 CurveControlPoints: TVector4List;
38 Node: TAbstractInterpolatorNode;
39 NodeUsedAsChild: Boolean;
40 constructor Create;
41 destructor Destroy; override;
42 procedure Parse(const Json: TJSONArray;
43 const NormalizeCurvesKeyValues: Boolean); virtual;
44 procedure BuildNodes(const MaxAnimationTime: Single; const Container: TX3DRootNode);
45 property MaxTime: Single read FMaxTime;
46 procedure Backup;
47 procedure Restore;
48 procedure ParseControlPoints(const Json: TJSONArray;
49 const NormalizeCurvesKeyValues: Boolean);
50 end;
51
52 TDeformTimelineList = class({$ifdef FPC}specialize{$endif} TObjectList<TDeformTimeline>)
53 end;
54 {$endif}
55
56 {$ifdef read_implementation}
57
58 constructor TDeformTimeline.Create;
59 begin
60 inherited;
61 Time := TSingleList.Create;
62 BackupTime := TSingleList.Create;
63 Vertices := TDeformVertexList.Create(True);
64 BackupVertices := TDeformVertexList.Create(False);
65 CurveControlPoints := TVector4List.Create;
66 Curve := False;
67 end;
68
69 destructor TDeformTimeline.Destroy;
70 begin
71 FreeAndNil(Time);
72 FreeAndNil(BackupTime);
73 FreeAndNil(Vertices);
74 FreeAndNil(BackupVertices);
75 FreeAndNil(CurveControlPoints);
76 inherited;
77 end;
78
79 procedure TDeformTimeline.Parse(const Json: TJSONArray;
80 const NormalizeCurvesKeyValues: Boolean);
81 var
82 I, J, K: Integer;
83 Obj: TJSONObject;
84 VertexJsonArray: TJSONArray;
85 VertexList: TSingleList;
86 Offset: Integer;
87 VB: TMeshVertexBone;
88 begin
89 Curve := false;
90
91 for I := 0 to Json.Count - 1 do
92 begin
93 Obj := Json.Items[I] as TJSONObject;
94 Time.Add(Obj.Get('time', 0.0));
95 Offset := Obj.Get('offset', 0);
96
97 // only detect whether we have a curve at this point
98 if not Curve then
99 Curve := Obj.Find('curve') is TJSONArray;
100
101 VertexJsonArray := Obj.Get('vertices', TJSONArray(nil));
102 VertexList := TSingleList.Create;
103
104 { Layout of VertexList:
105
106 - static vertices (until Offset)
107 - animated vertices (from VertexJsonArray)
108 - static vertices (until the end)
109
110 Note that Offset and VertexJsonArray.Count may be odd
111 (see https://github.com/castle-engine/demo-models/tree/master/spine/free_form_deformation
112 for demo).
113 }
114 if Attachment.Weights.Count = 0 then
115 // For mesh without weights
116 begin
117 VertexList.Count := Attachment.Vertices.Count * 2;
118 for J := 0 to Attachment.Vertices.Count - 1 do
119 begin
120 VertexList[J * 2 ] := Attachment.Vertices[J].X;
121 VertexList[J * 2 + 1] := Attachment.Vertices[J].Y;
122 end;
123 end else
124 // Things are a bit different for weighted mesh
125 begin
126 VertexList.Count := Attachment.Weights[Attachment.Weights.Count - 1].Offset
127 + Attachment.Weights[Attachment.Weights.Count - 1].Bones.Count * 2;
128 for J := 0 to Attachment.Weights.Count - 1 do
129 begin
130 for K := 0 to Attachment.Weights[J].Bones.Count - 1 do
131 begin
132 VB := Attachment.Weights[J].Bones[K];
133 VertexList[Attachment.Weights[J].Offset + K * 2 ] := VB.V.X;
134 VertexList[Attachment.Weights[J].Offset + K * 2 + 1] := VB.V.Y;
135 end;
136 end;
137 end;
138
139 if VertexJsonArray <> nil then
140 for J := 0 to VertexJsonArray.Count - 1 do
141 VertexList[Offset + J] :=
142 VertexList[Offset + J] + VertexJsonArray.Items[J].AsFloat;
143
144 Vertices.Add(VertexList);
145 end;
146 FMaxTime := Time.Items[Time.Count - 1];
147
148 if Curve then
149 ParseControlPoints(Json, NormalizeCurvesKeyValues);
150 end;
151
152 procedure TDeformTimeline.BuildNodes(const MaxAnimationTime: Single; const Container: TX3DRootNode);
153 var
154 I, J: Integer;
155 Route: TX3DRoute;
156 VertexList: TSingleList;
157 EventValueChanged: TX3DEvent;
158 N: TCoordinateInterpolatorNode;
159 NC: TCubicBezierCoordinateInterpolatorNode;
160 begin
161 if Curve then
162 begin
163 NC := TCubicBezierCoordinateInterpolatorNode.Create('DeformTimeline_' + Slot.Name);
164 for I := 0 to Time.Count - 1 do
165 begin
166 NC.FdKey.Items.Add(Time[I] / MaxAnimationTime);
167 VertexList := Vertices.Items[I];
168 Assert(not Odd(VertexList.Count)); // VertexList is a list of 2D coordinates
169 for J := 0 to VertexList.Count div 2 - 1 do
170 NC.FdKeyValue.Items.Add(Vector3(
171 VertexList.Items[2 * J],
172 VertexList.Items[2 * J + 1],
173 0
174 ));
175 end;
176 NC.SetControlPoints(CurveControlPoints);
177 NC.OptimizeControlPoints;
178 EventValueChanged := NC.EventValue_changed;
179 Node := NC;
180 end else
181 begin
182 N := TCoordinateInterpolatorNode.Create('DeformTimeline_' + Slot.Name);
183 for I := 0 to Time.Count - 1 do
184 begin
185 N.FdKey.Items.Add(Time[I] / MaxAnimationTime);
186 VertexList := Vertices.Items[I];
187 Assert(not Odd(VertexList.Count)); // VertexList is a list of 2D coordinates
188 for J := 0 to VertexList.Count div 2 - 1 do
189 N.FdKeyValue.Items.Add(Vector3(
190 VertexList.Items[2 * J],
191 VertexList.Items[2 * J + 1],
192 0
193 ));
194 end;
195 EventValueChanged := N.EventValue_changed;
196 Node := N;
197 end;
198
199 NodeUsedAsChild := true;
200 Container.AddChildren(Node);
201
202 Route := TX3DRoute.Create;
203 Route.SetSourceDirectly(EventValueChanged);
204 Route.SetDestinationDirectly(Attachment.Coord.FdPoint.EventIn);
205 Container.AddRoute(Route);
206 end;
207
208 procedure TDeformTimeline.Backup;
209 begin
210 if not IsBackup then
211 begin
212 BackupTime.Clear;
213 BackupVertices.Clear;
214 BackupTime.AddRange(Time);
215 BackupVertices.AddRange(Vertices);
216 IsBackup := True;
217 end;
218 end;
219
220 procedure TDeformTimeline.Restore;
221 begin
222 if IsBackup then
223 begin
224 Time.Clear;
225 Vertices.Clear;
226 Time.AddRange(BackupTime);
227 Vertices.AddRange(BackupVertices);
228 IsBackup := False;
229 end;
230 end;
231
232 procedure TDeformTimeline.ParseControlPoints(const Json: TJSONArray;
233 const NormalizeCurvesKeyValues: Boolean);
234 var
235 I: Integer;
236 PreviousTime, NextTime: Single;
237 O: TJSONObject;
238 begin
239 Assert(Time.Count = Vertices.Count);
240 if Json.Count <> Time.Count then
241 begin
242 WritelnWarning('Invalid Spine JSON: Curve support requires that timeline JSON count is correct');
243 Curve := false;
244 Exit;
245 end;
246 for I := 0 to Time.Count - 2 do
247 begin
248 PreviousTime := Time[I];
249 NextTime := Time[I + 1];
250 if not (Json[I] is TJSONObject) then
251 begin
252 WritelnWarning('Invalid Spine JSON: Curve support requires that all JSON timeline chldren are object');
253 Curve := false;
254 Exit;
255 end;
256 O := TJSONObject(Json[I]);
257 CurveControlPoints.Add(ReadCurveControlPointsTimeOnly(O, NormalizeCurvesKeyValues, PreviousTime, NextTime));
258 end;
259 CurveControlPoints.Add(Vector4(0, 0, 1, 1)); // TODO: add last dummy value, because our TCubicBezier* may expect it
260 end;
261
262 {$endif}