Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine.pas

raw
1 {
2 Copyright 2014-2021 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Spine 2D animations loader. }
17 unit X3DLoadInternalSpine;
18
19 {$I castleconf.inc}
20
21 interface
22
23 var
24 { Turn this on to see some additional warnings when loading Spine models.
25 These warnings are sometimes too verbose (often the models will work fine,
26 and these warning can be ignored), so they are disabled by default. }
27 SpineVerboseWarnings: boolean = false;
28
29 { Do not use textures and atlases referenced in the Spine model,
30 do not set their URLs and do not try to load them in any way. }
31 SpineIgnoreTextures: boolean = false;
32
33 implementation
34
35 uses SysUtils, Classes,
36 Generics.Collections, FpJson, JSONParser, JSONScanner, Math,
37 X3DNodes, X3DLoad, CastleVectors, CastleCurves, CastleUtils, CastleLog,
38 CastleUriUtils, CastleDownload,
39 CastleStringUtils, CastleClassUtils, CastleColors, X3DLoadInternalUtils,
40 CastleTriangles, CastleRenderOptions,
41 X3DFields;
42
43 type
44 ESpineReadError = class(Exception);
45
46 {$I x3dloadinternalspine_url.inc}
47 {$I x3dloadinternalspine_textureloader.inc}
48 {$I x3dloadinternalspine_simpletextureloader.inc}
49 {$I x3dloadinternalspine_atlas.inc}
50
51 { JSON skeleton -------------------------------------------------------------- }
52
53 type
54 { forward declarations }
55 TBoneList = class;
56 TAttachmentList = class;
57
58 {$define read_interface}
59 {$I x3dloadinternalspine_animutils.inc}
60 {$I x3dloadinternalspine_json.inc}
61 {$I x3dloadinternalspine_bones.inc}
62 {$I x3dloadinternalspine_slots.inc}
63 {$I x3dloadinternalspine_attachments.inc}
64 {$I x3dloadinternalspine_skins.inc}
65 {$I x3dloadinternalspine_bonetimelines.inc}
66 {$I x3dloadinternalspine_slottimelines.inc}
67 {$I x3dloadinternalspine_drawordertimelines.inc}
68 {$I x3dloadinternalspine_deformtimelines.inc}
69 {$I x3dloadinternalspine_weightedmeshtimelines.inc}
70 {$I x3dloadinternalspine_animations.inc}
71 {$I x3dloadinternalspine_skeleton.inc}
72 {$undef read_interface}
73
74 {$define read_implementation}
75 {$I x3dloadinternalspine_animutils.inc}
76 {$I x3dloadinternalspine_json.inc}
77 {$I x3dloadinternalspine_bones.inc}
78 {$I x3dloadinternalspine_slots.inc}
79 {$I x3dloadinternalspine_attachments.inc}
80 {$I x3dloadinternalspine_skins.inc}
81 {$I x3dloadinternalspine_bonetimelines.inc}
82 {$I x3dloadinternalspine_slottimelines.inc}
83 {$I x3dloadinternalspine_drawordertimelines.inc}
84 {$I x3dloadinternalspine_deformtimelines.inc}
85 {$I x3dloadinternalspine_weightedmeshtimelines.inc}
86 {$I x3dloadinternalspine_animations.inc}
87 {$I x3dloadinternalspine_skeleton.inc}
88 {$undef read_implementation}
89
90 { Main loading function ------------------------------------------------------ }
91
92 function LoadSpine(const Stream: TStream; const BaseUrl: String): TX3DRootNode;
93
94 function CreateTextureLoader(const CustomAtlasName: String): TTextureLoader;
95
96 function FindAtlas(const InitialAtlasUrl: String; out AtlasUrl: String): boolean;
97 var
98 NoExtension: string;
99 begin
100 AtlasUrl := InitialAtlasUrl;
101 if URIFileExists(AtlasUrl) then Exit(true);
102
103 NoExtension := ChangeURIExt(BaseUrl, '');
104
105 // try with "_tex", used by Dragon Bones
106 AtlasUrl := NoExtension + '_tex.atlas';
107 if URIFileExists(AtlasUrl) then Exit(true);
108
109 // try to remove -pro and -ess suffixes, Spine runtimes on
110 // https://github.com/EsotericSoftware/spine-runtimes
111 // (sometimes) do it too
112 if IsSuffix('-pro', NoExtension, true) then
113 begin
114 AtlasUrl := SuffixRemove('-pro', NoExtension, true) + '.atlas';
115 if URIFileExists(AtlasUrl) then Exit(true);
116 end;
117
118 if IsSuffix('-ess', NoExtension, true) then
119 begin
120 AtlasUrl := SuffixRemove('-ess', NoExtension, true) + '.atlas';
121 if URIFileExists(AtlasUrl) then Exit(true);
122 end;
123
124 Exit(false);
125 end;
126
127 var
128 StandardAtlasUrl, AtlasUrl: String;
129 Atlas: TAtlas;
130 begin
131 if SpineIgnoreTextures then
132 Exit(TSimpleTextureLoader.Create(BaseUrl));
133
134 // calculate StandardAtlasUrl
135 if CustomAtlasName <> '' then
136 StandardAtlasUrl := ExtractURIPath(BaseUrl) + CustomAtlasName + '.atlas'
137 else
138 StandardAtlasUrl := ChangeURIExt(BaseUrl, '.atlas');
139
140 if FindAtlas(StandardAtlasUrl, AtlasUrl) then
141 begin
142 Atlas := TAtlas.Create;
143 try
144 Atlas.Parse(AtlasUrl);
145 Atlas.BuildNodes(BaseUrl);
146 Result := Atlas;
147 except FreeAndNil(Atlas); raise end;
148 end else
149 begin
150 WritelnLog('Spine', 'Atlas not found under URL "' + StandardAtlasUrl + '" (and some alternative URLs we tried), will load images without atlas, using "images/xxx.png" filenames');
151 Result := TSimpleTextureLoader.Create(BaseUrl);
152 end;
153 end;
154
155 var
156 Json: TJSONData;
157 P: TJSONParser;
158 Skeleton: TSkeleton;
159 SkinName, CustomAtlasName: string;
160 TextureLoader: TTextureLoader;
161 begin
162 { strip additional info (in the URL anchor, i.e. '#xxx' suffix) }
163 URIExtractInfo(BaseUrl, SkinName, CustomAtlasName);
164
165 TextureLoader := CreateTextureLoader(CustomAtlasName);
166 try
167 { Should we add joUTF8?
168 - Long time ago, it failed to work on
169 tests/data/escape_from_the_universe_boss/boss.json
170 with FPC 3.1.1-r36683 [2017/07/08] for Linux x86_64.
171 - TODO: Needs retest.
172 It may have been some error on our side?
173 Later note reported joUTF8 is OK with FPC 3.0.2. }
174
175 P := TJSONParser.Create(Stream, [joComments]);
176 try
177 Json := P.Parse;
178 try
179 Result := TX3DRootNode.Create('', BaseUrl);
180 try
181 try
182 if Assigned(Json) then
183 begin
184 Skeleton := TSkeleton.Create;
185 try
186 Skeleton.Parse(BaseUrl, Json, SkinName);
187 Skeleton.BuildNodes(BaseUrl, TextureLoader, Result);
188 if Skeleton.DefaultSkin = nil then Exit;
189 Skeleton.Animations.Exported(Result);
190 finally FreeAndNil(Skeleton) end;
191 end;
192 except
193 on E: ESpineReadError do
194 begin
195 E.Message := E.Message + ' (inside ' + UriDisplay(BaseUrl) + ')';
196 raise;
197 end;
198 end;
199 except FreeAndNil(Result); raise end;
200 finally FreeAndNil(Json) end;
201 finally FreeAndNil(P) end;
202 finally FreeAndNil(TextureLoader) end;
203 end;
204
205 var
206 ModelFormat: TModelFormat;
207 initialization
208 ModelFormat := TModelFormat.Create;
209 ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadSpine;
210 ModelFormat.MimeTypes.Add('application/json');
211 ModelFormat.FileFilterName := 'Spine animation (*.json)';
212 ModelFormat.Extensions.Add('.json');
213 RegisterModelFormat(ModelFormat);
214 end.