| 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 |
{ JSON helpers. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
{$endif} |
| 21 |
|
| 22 |
{$ifdef read_implementation} |
| 23 |
|
| 24 |
{ Read from Spine JSON a TVector2List. |
| 25 |
@param(IgnoreListUnexpectedEnd When @true, we ignore (not even a warning) |
| 26 |
when the list ends unexpectedly in the middle of the vector. |
| 27 |
This is known to happen in case of weighted mesh exported by Spine.) |
| 28 |
} |
| 29 |
procedure ReadVector2List(const Json: TJSONObject; const Name: string; |
| 30 |
const List: TVector2List; const IgnoreListUnexpectedEnd: Boolean = false); |
| 31 |
var |
| 32 |
JsonArray: TJSONArray; |
| 33 |
I: Integer; |
| 34 |
Vec2: TVector2; |
| 35 |
Vec2Index: Integer; |
| 36 |
begin |
| 37 |
JsonArray := Json.Find(Name, jtArray) as TJSONArray; |
| 38 |
if JsonArray <> nil then |
| 39 |
begin |
| 40 |
Vec2Index := 0; |
| 41 |
for I := 0 to JsonArray.Count - 1 do |
| 42 |
if JsonArray[I] is TJSONNumber then |
| 43 |
begin |
| 44 |
Vec2.Data[Vec2Index] := JsonArray[I].AsFloat; |
| 45 |
Inc(Vec2Index); |
| 46 |
if Vec2Index = 2 then |
| 47 |
begin |
| 48 |
List.Add(Vec2); |
| 49 |
Vec2Index := 0; |
| 50 |
end; |
| 51 |
end; |
| 52 |
if (Vec2Index <> 0) and (not IgnoreListUnexpectedEnd) then |
| 53 |
WritelnWarning('Spine', 'Vector2 list (like uvs or vertices) ends in the middle of the vector.'); |
| 54 |
end; |
| 55 |
end; |
| 56 |
|
| 57 |
procedure ReadInt32List(const Json: TJSONObject; const Name: string; |
| 58 |
const List: TInt32List); |
| 59 |
var |
| 60 |
JsonArray: TJSONArray; |
| 61 |
I: Integer; |
| 62 |
begin |
| 63 |
JsonArray := Json.Find(Name, jtArray) as TJSONArray; |
| 64 |
if JsonArray <> nil then |
| 65 |
for I := 0 to JsonArray.Count - 1 do |
| 66 |
begin |
| 67 |
{ On FPC 2.6.2, we have here TJSONInt64Number. |
| 68 |
On FPC 3.0.0, we have here TJSONIntegerNumber. |
| 69 |
(Did not test this on FPC 2.6.4 -- just do "Writeln(JsonArray[I].ClassName);" and open any Spine JSON with mesh.) |
| 70 |
So don't check "if JsonArray[I] is TJSONIntegerNumber then" or such, |
| 71 |
better just use [I].AsInteger (or Integers[I]). |
| 72 |
|
| 73 |
(Testcase: when this goes wrong, Spine mesh (e.g. hero wings |
| 74 |
in escape_universe) doesn't show, |
| 75 |
because IndexedTriangleSet remains unset.) } |
| 76 |
List.Add(JsonArray[I].AsInteger); |
| 77 |
end; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure ReadSingleList(const Json: TJSONObject; const Name: string; |
| 81 |
const List: TSingleList); |
| 82 |
var |
| 83 |
JsonArray: TJSONArray; |
| 84 |
I: Integer; |
| 85 |
begin |
| 86 |
JsonArray := Json.Find(Name, jtArray) as TJSONArray; |
| 87 |
if JsonArray <> nil then |
| 88 |
for I := 0 to JsonArray.Count - 1 do |
| 89 |
List.Add(JsonArray[I].AsFloat); |
| 90 |
end; |
| 91 |
{$endif} |
| 92 |
|