Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine_bones.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 bones. }
18
19 {$ifdef read_interface}
20 TBone = class
21 strict private
22 { World-space bone transformation, necessary for skinnedmesh calculations.
23 WorldMatrix includes rotation and scale, from WorldRotation and WorldScale.
24 @groupBegin }
25 WorldXY: TVector2;
26 WorldScale: TVector2;
27 WorldRotation: Single;
28 WorldMatrix, WorldMatrixInverse: TMatrix2;
29 { @groupEnd }
30 BackupXY: TVector2;
31 BackupScale: TVector2;
32 BackupRotation: Single;
33 IsBackup: Boolean;
34 public
35 Name: string;
36 Length: Single;
37 XY: TVector2;
38 Scale: TVector2;
39 Rotation: Single;
40 InheritScale, InheritRotation: boolean;
41 Parent: TBone;
42 Node: TTransformNode;
43 NodeUsedAsChild: boolean;
44 destructor Destroy; override;
45 procedure Parse(const Json: TJSONObject;
46 const PossibleParents: TBoneList; const ExpectedParent, DragonBonesFormat: boolean);
47 procedure BuildNodes(const BaseUrl: String);
48 function ToWorldSpace(const V: TVector2): TVector2;
49 function ToLocalSpace(V: TVector2): TVector2; overload;
50 function ToLocalSpace(const V: TVector3): TVector3; overload;
51 { Calculate world-space (well, at least in the space of whole JSON file)
52 bone parameters.
53 Similar to UpdateWorldTransform in spine-runtimes/spine-csharp/src/Bone.cs }
54 procedure UpdateWorldTransform;
55 { Backup bone's position, rotation and scale values }
56 procedure Backup;
57 { Restore bone's position, rotation and scale values }
58 procedure Restore;
59 end;
60
61 TBoneList = class({$ifdef FPC}specialize{$endif} TObjectList<TBone>)
62 { Find bone by name.
63 @raises ESpineReadError If bone does not exist. }
64 function Find(const BoneName: string): TBone;
65 { Find bone by name.
66 @return nil if bone does not exist. }
67 function FindByName(const BoneName: string): TBone;
68 procedure Parse(const Json: TJSONObject; var Root: TBone;
69 const DragonBonesFormat: boolean);
70 procedure BuildNodes(const BaseUrl: String);
71 procedure Assign(const Source: TBoneList);
72 end;
73 {$endif}
74
75 {$ifdef read_implementation}
76
77 { TBone ---------------------------------------------------------------------- }
78
79 procedure TBone.Parse(const Json: TJSONObject;
80 const PossibleParents: TBoneList; const ExpectedParent, DragonBonesFormat: boolean);
81 var
82 ParentName: string;
83 Transform: TJSONObject;
84 begin
85 Name := Json.Get('name', '');
86 Length := Json.Get('length', 0.0);
87 if DragonBonesFormat then
88 begin
89 Transform := Json.Find('transform', jtObject) as TJSONObject;
90 if Transform = nil then
91 raise ESpineReadError.CreateFmt('Missing "transform" of Dragon Bones bone "%s"', [Name]);
92 XY.X := Transform.Get('x', 0.0);
93 XY.Y := Transform.Get('y', 0.0);
94 Scale.X := Transform.Get('scX', 1.0);
95 Scale.Y := Transform.Get('scY', 1.0);
96 Rotation := Json.Get('rotation', 0.0); // TODO: is this the correct name in Dragon Bones?
97 InheritScale := Json.Get('inheritScale', true); // TODO: is this the correct name in Dragon Bones?
98 InheritRotation := Json.Get('inheritRotation', true); // TODO: is this the correct name in Dragon Bones?
99 end else
100 begin
101 XY.X := Json.Get('x', 0.0);
102 XY.Y := Json.Get('y', 0.0);
103 Scale.X := Json.Get('scaleX', 1.0);
104 Scale.Y := Json.Get('scaleY', 1.0);
105 Rotation := Json.Get('rotation', 0.0);
106 InheritScale := Json.Get('inheritScale', true);
107 InheritRotation := Json.Get('inheritRotation', true);
108 end;
109
110 // The support seems to be OK for Dragon Bones,
111 // that uses this always, so don't warn by default anymore.
112 if SpineVerboseWarnings and
113 ((not InheritScale) or (not InheritRotation)) then
114 WritelnWarning('Spine', 'bone inheritRotation=false or inheritScale=false is only partially supported');
115
116 ParentName := Json.Get('parent', '');
117 if ParentName <> '' then
118 Parent := PossibleParents.Find(ParentName);
119
120 if ExpectedParent then
121 begin
122 if Parent = nil then
123 raise ESpineReadError.CreateFmt('Parent for bone "%s" expected, but not specified', [Name]);
124 end else
125 begin
126 if Parent <> nil then
127 raise ESpineReadError.CreateFmt('Parent for bone "%s" not expected, but specified', [Name]);
128 end;
129
130 UpdateWorldTransform;
131 end;
132
133 destructor TBone.Destroy;
134 begin
135 if NodeUsedAsChild then
136 Node := nil else
137 FreeIfUnusedAndNil(Node);
138 inherited;
139 end;
140
141 procedure TBone.BuildNodes(const BaseUrl: String);
142 begin
143 Node := TTransformNode.Create('Bone_' + Name, BaseUrl);
144 Node.FdTranslation.Value := Vector3(XY.X, XY.Y, 0);
145 Node.FdScale.Value := Vector3(Scale.X, Scale.Y, 1);
146 Node.FdRotation.Value := Vector4(0, 0, 1, DegToRad(Rotation));
147 if Parent <> nil then
148 begin
149 NodeUsedAsChild := true;
150 Parent.Node.AddChildren(Node);
151 end;
152 end;
153
154 procedure TBone.UpdateWorldTransform;
155 var
156 C, S: Single;
157 begin
158 WorldXY := XY;
159 WorldScale := Scale;
160 WorldRotation := Rotation;
161
162 if Parent <> nil then
163 begin
164 WorldXY := Vector2(
165 WorldXY.X * Parent.WorldMatrix[0, 0] + WorldXY.Y * Parent.WorldMatrix[0, 1],
166 WorldXY.X * Parent.WorldMatrix[1, 0] + WorldXY.Y * Parent.WorldMatrix[1, 1]);
167 WorldXY := WorldXY + Parent.WorldXY;
168 if InheritScale then
169 WorldScale := WorldScale * Parent.WorldScale;
170 if InheritRotation then
171 WorldRotation := WorldRotation + Parent.WorldRotation;
172 end;
173
174 SinCos(DegToRad(WorldRotation), S, C);
175 WorldMatrix[0, 0] := C * WorldScale.X;
176 WorldMatrix[0, 1] := -S * WorldScale.Y;
177 WorldMatrix[1, 0] := S * WorldScale.X;
178 WorldMatrix[1, 1] := C * WorldScale.Y;
179
180 { inverse of 2D rotation matrix is it's transposition. Also, invert scale }
181 WorldMatrixInverse[0, 0] := C / WorldScale.X;
182 WorldMatrixInverse[1, 0] := -S / WorldScale.Y;
183 WorldMatrixInverse[0, 1] := S / WorldScale.X;
184 WorldMatrixInverse[1, 1] := C / WorldScale.Y;
185 end;
186
187 function TBone.ToWorldSpace(const V: TVector2): TVector2;
188 begin
189 Result := Vector2(
190 V.X * WorldMatrix[0, 0] + V.Y * WorldMatrix[0, 1],
191 V.X * WorldMatrix[1, 0] + V.Y * WorldMatrix[1, 1]);
192 Result := Result + WorldXY;
193 end;
194
195 function TBone.ToLocalSpace(V: TVector2): TVector2;
196 begin
197 V := V - WorldXY;
198 Result := Vector2(
199 V.X * WorldMatrixInverse[0, 0] + V.Y * WorldMatrixInverse[0, 1],
200 V.X * WorldMatrixInverse[1, 0] + V.Y * WorldMatrixInverse[1, 1]);
201 end;
202
203 function TBone.ToLocalSpace(const V: TVector3): TVector3;
204 var
205 Result2: TVector2 absolute Result;
206 begin
207 Result := V;
208 Result2 := ToLocalSpace(Result2);
209 end;
210
211 procedure TBone.Backup;
212 begin
213 if not IsBackup then
214 begin
215 BackupXY := XY;
216 BackupRotation := Rotation;
217 BackupScale := Scale;
218 IsBackup := True;
219 end;
220 end;
221
222 procedure TBone.Restore;
223 begin
224 if IsBackup then
225 begin
226 XY := BackupXY;
227 Rotation := BackupRotation;
228 Scale := BackupScale;
229 IsBackup := False;
230 end;
231 end;
232
233 { TBoneList ------------------------------------------------------------------ }
234
235 function TBoneList.Find(const BoneName: string): TBone;
236 var
237 I: Integer;
238 begin
239 for I := 0 to Count - 1 do
240 if Items[I].Name = BoneName then
241 Exit(Items[I]);
242 raise ESpineReadError.CreateFmt('Bone name "%s" not found', [BoneName]);
243 end;
244
245 function TBoneList.FindByName(const BoneName: string): TBone;
246 var
247 I: Integer;
248 begin
249 for I := 0 to Count - 1 do
250 if Items[I].Name = BoneName then
251 Exit(Items[I]);
252 Exit(nil);
253 end;
254
255 procedure TBoneList.Parse(const Json: TJSONObject; var Root: TBone; const DragonBonesFormat: boolean);
256 var
257 I: Integer;
258 Bone: TBone;
259 ChildArray: TJSONArray;
260 ArrayName: string;
261 begin
262 if DragonBonesFormat then
263 ArrayName := 'bone'
264 else
265 ArrayName := 'bones';
266 ChildArray := Json.Find(ArrayName, jtArray) as TJSONArray;
267 if ChildArray = nil then
268 raise ESpineReadError.CreateFmt('Spine / Dragon Bones JSON skeleton: Missing "%s" array', [ArrayName]);
269
270 for I := 0 to ChildArray.Count - 1 do
271 if ChildArray[I] is TJSONObject then
272 begin
273 Bone := TBone.Create;
274 Add(Bone);
275 if Root = nil then
276 begin
277 { reading Root bone. It should have no parent. }
278 Bone.Parse(TJSONObject(ChildArray[I]), Self, false, DragonBonesFormat);
279 Root := Bone;
280 end else
281 { reading child bone. It must have a parent. }
282 Bone.Parse(TJSONObject(ChildArray[I]), Self, true, DragonBonesFormat);
283 end;
284 end;
285
286 procedure TBoneList.BuildNodes(const BaseUrl: String);
287 var
288 I: Integer;
289 begin
290 for I := 0 to Count - 1 do
291 Items[I].BuildNodes(BaseUrl);
292 end;
293
294 procedure TBoneList.Assign(const Source: TBoneList);
295 begin
296 Clear;
297 AddRange(Source);
298 end;
299
300 {$endif}