| 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 attachments. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
TAttachment = class abstract |
| 21 |
{ SlotName and AttachmentName are map keys, to detect which TAttachment to use. } |
| 22 |
SlotName, AttachmentName: string; |
| 23 |
{ The real attachment name. |
| 24 |
|
| 25 |
For image attachment this refers to texture loader (e.g. atlas) region name. |
| 26 |
This is a unique region name, but multiple TAttachment may refer to the same |
| 27 |
Name (see Escape ship_upgrade.json testcase). |
| 28 |
|
| 29 |
For path this refers to path name. |
| 30 |
Multiple slots may refer to the same path name, |
| 31 |
and it does *not* have to be the same path geometry then. } |
| 32 |
Name: string; |
| 33 |
Node: TTransformNode; |
| 34 |
NodeUsedAsChild: boolean; |
| 35 |
Material: TUnlitMaterialNode; //< initialized together with Node, always inside |
| 36 |
Appearance: TAppearanceNode; //< initialized together with Node, always inside |
| 37 |
destructor Destroy; override; |
| 38 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList); virtual; |
| 39 |
{ Create and parse correct TAttachment descendant. |
| 40 |
May return @nil if none possible. } |
| 41 |
class function CreateAndParse(const Json: TJSONObject; |
| 42 |
const ASlotName, AnAttachmentName: string; const Bones: TBoneList): TAttachment; |
| 43 |
procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); virtual; abstract; |
| 44 |
{ Some attachments (skinnedmesh) can be attached only to a single bone |
| 45 |
at a time, and must be transformed from world to bone space. } |
| 46 |
procedure TransformToBoneSpace(const Bone: TBone); virtual; |
| 47 |
end; |
| 48 |
|
| 49 |
TRegionAttachment = class(TAttachment) |
| 50 |
XY: TVector2; |
| 51 |
Scale: TVector2; |
| 52 |
Rotation: Single; |
| 53 |
Width, Height: Integer; |
| 54 |
Path: string; |
| 55 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList); override; |
| 56 |
procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); override; |
| 57 |
function BuildGeometry(const BaseUrl: String; |
| 58 |
const TexCoord: TQuadTexCoord; const TexRect: TQuadTexRect; |
| 59 |
const TexRotated: boolean): TAbstractGeometryNode; virtual; |
| 60 |
end; |
| 61 |
|
| 62 |
TAbstractMeshAttachment = class(TRegionAttachment) |
| 63 |
public |
| 64 |
Coord: TCoordinateNode; |
| 65 |
UVs: TVector2List; |
| 66 |
Triangles: TInt32List; |
| 67 |
Vertices: TVector2List; |
| 68 |
Edges: TInt32List; //< unused for now |
| 69 |
{ Unused for now. |
| 70 |
Looking as official Spine runtime in spine-csharp and spine-unity, |
| 71 |
it is also unused there. } |
| 72 |
Hull: Integer; |
| 73 |
Invalid: boolean; |
| 74 |
constructor Create; |
| 75 |
destructor Destroy; override; |
| 76 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList); override; |
| 77 |
function BuildGeometry(const BaseUrl: String; |
| 78 |
const TexCoord: TQuadTexCoord; const TexRect: TQuadTexRect; |
| 79 |
const TexRotated: boolean): TAbstractGeometryNode; override; |
| 80 |
end; |
| 81 |
|
| 82 |
TMeshVertexBone = class |
| 83 |
Bone: TBone; |
| 84 |
V: TVector2; //< vertex position in local Bone coordinate system |
| 85 |
Weight: Single; |
| 86 |
end; |
| 87 |
TMeshVertexBoneList = {$ifdef FPC}specialize{$endif} TObjectList<TMeshVertexBone>; |
| 88 |
|
| 89 |
TMeshVertex = class |
| 90 |
Offset: Integer; // Offset relative to where this Vertex is loaded from the list |
| 91 |
Bones: TMeshVertexBoneList; |
| 92 |
constructor Create; |
| 93 |
destructor Destroy; override; |
| 94 |
procedure Parse(const JsonArray: TJSONArray; |
| 95 |
Index: Integer; const BonesCount: Cardinal; const AllBones: TBoneList); |
| 96 |
end; |
| 97 |
|
| 98 |
TMeshVertexList = class({$ifdef FPC}specialize{$endif} TObjectList<TMeshVertex>) |
| 99 |
procedure Parse(const JsonArray: TJSONArray; const Bones: TBoneList); |
| 100 |
end; |
| 101 |
|
| 102 |
TMeshAttachment = class(TAbstractMeshAttachment) |
| 103 |
Weights: TMeshVertexList; |
| 104 |
constructor Create; |
| 105 |
destructor Destroy; override; |
| 106 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList); override; |
| 107 |
end; |
| 108 |
|
| 109 |
TSkinnedMeshAttachment = class(TMeshAttachment) |
| 110 |
procedure TransformToBoneSpace(const Bone: TBone); override; |
| 111 |
end; |
| 112 |
|
| 113 |
TPathAttachment = class(TAttachment) |
| 114 |
Vertices: TVector2List; |
| 115 |
Lengths: TSingleList; |
| 116 |
constructor Create; |
| 117 |
destructor Destroy; override; |
| 118 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList); override; |
| 119 |
procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); override; |
| 120 |
end; |
| 121 |
|
| 122 |
TClippingAttachment = class(TAttachment) |
| 123 |
Vertices: TVector2List; |
| 124 |
constructor Create; |
| 125 |
destructor Destroy; override; |
| 126 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList); override; |
| 127 |
procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); override; |
| 128 |
end; |
| 129 |
|
| 130 |
TAttachmentList = class({$ifdef FPC}specialize{$endif} TObjectList<TAttachment>) |
| 131 |
{ Find by slot+attachment name. |
| 132 |
@raises ESpineReadError If does not exist. } |
| 133 |
function Find(const SlotName, AttachmentName: string; |
| 134 |
const FallbackList: TAttachmentList; |
| 135 |
const NilWhenNotFound: boolean): TAttachment; |
| 136 |
end; |
| 137 |
{$endif} |
| 138 |
|
| 139 |
{$ifdef read_implementation} |
| 140 |
|
| 141 |
{ TAttachment ---------------------------------------------------------------- } |
| 142 |
|
| 143 |
class function TAttachment.CreateAndParse(const Json: TJSONObject; |
| 144 |
const ASlotName, AnAttachmentName: string; const Bones: TBoneList): TAttachment; |
| 145 |
var |
| 146 |
TypeName: string; |
| 147 |
begin |
| 148 |
TypeName := Json.Get('type', 'region'); |
| 149 |
if TypeName = 'mesh' then |
| 150 |
begin |
| 151 |
Result := TMeshAttachment.Create; |
| 152 |
Result.SlotName := ASlotName; |
| 153 |
Result.AttachmentName := AnAttachmentName; |
| 154 |
Result.Parse(Json, Bones); |
| 155 |
end else |
| 156 |
if (TypeName = 'region') or |
| 157 |
(TypeName = 'regionsequence') then |
| 158 |
begin |
| 159 |
if TypeName = 'regionsequence' then |
| 160 |
WritelnWarning('Spine', 'Spine region type "regionsequence" is for now rendered just like normal "region"'); |
| 161 |
Result := TRegionAttachment.Create; |
| 162 |
Result.SlotName := ASlotName; |
| 163 |
Result.AttachmentName := AnAttachmentName; |
| 164 |
Result.Parse(Json, Bones); |
| 165 |
end else |
| 166 |
if TypeName = 'path' then |
| 167 |
begin |
| 168 |
Result := TPathAttachment.Create; |
| 169 |
Result.SlotName := ASlotName; |
| 170 |
Result.AttachmentName := AnAttachmentName; |
| 171 |
Result.Parse(Json, Bones); |
| 172 |
end else |
| 173 |
if TypeName = 'clipping' then |
| 174 |
begin |
| 175 |
Result := TClippingAttachment.Create; |
| 176 |
Result.SlotName := ASlotName; |
| 177 |
Result.AttachmentName := AnAttachmentName; |
| 178 |
Result.Parse(Json, Bones); |
| 179 |
end else |
| 180 |
if TypeName = 'skinnedmesh' then |
| 181 |
begin |
| 182 |
WritelnWarning('Spine', 'Region type skinnedmesh may not be supported in future CGE versions, please export your model again from latest Spine version'); |
| 183 |
Result := TSkinnedMeshAttachment.Create; |
| 184 |
Result.SlotName := ASlotName; |
| 185 |
Result.AttachmentName := AnAttachmentName; |
| 186 |
Result.Parse(Json, Bones); |
| 187 |
end else |
| 188 |
begin |
| 189 |
WritelnWarning('Spine', 'Spine region (attachment) type "%s" is not supported', [TypeName]); |
| 190 |
Result := nil; |
| 191 |
end; |
| 192 |
end; |
| 193 |
|
| 194 |
procedure TAttachment.Parse(const Json: TJSONObject; const Bones: TBoneList); |
| 195 |
begin |
| 196 |
Name := Json.Get('name', AttachmentName); |
| 197 |
end; |
| 198 |
|
| 199 |
destructor TAttachment.Destroy; |
| 200 |
begin |
| 201 |
if NodeUsedAsChild then |
| 202 |
Node := nil else |
| 203 |
FreeIfUnusedAndNil(Node); |
| 204 |
inherited; |
| 205 |
end; |
| 206 |
|
| 207 |
procedure TAttachment.TransformToBoneSpace(const Bone: TBone); |
| 208 |
begin |
| 209 |
end; |
| 210 |
|
| 211 |
{ TRegionAttachment ---------------------------------------------------------- } |
| 212 |
|
| 213 |
procedure TRegionAttachment.Parse(const Json: TJSONObject; const Bones: TBoneList); |
| 214 |
begin |
| 215 |
inherited; |
| 216 |
XY.X := Json.Get('x', 0.0); |
| 217 |
XY.Y := Json.Get('y', 0.0); |
| 218 |
Scale.X := Json.Get('scaleX', 1.0); |
| 219 |
Scale.Y := Json.Get('scaleY', 1.0); |
| 220 |
Rotation := Json.Get('rotation', 0.0); |
| 221 |
Width := Json.Get('width', 0); |
| 222 |
Height := Json.Get('height', 0); |
| 223 |
Path := Json.Get('path', ''); |
| 224 |
end; |
| 225 |
|
| 226 |
procedure TRegionAttachment.BuildNodes(const BaseUrl: String; |
| 227 |
const TextureLoader: TTextureLoader); |
| 228 |
var |
| 229 |
TexRect: TQuadTexRect; |
| 230 |
TexCoord: TQuadTexCoord; |
| 231 |
Shape: TShapeNode; |
| 232 |
AtlasRegionName: string; |
| 233 |
TexRotated: boolean; |
| 234 |
begin |
| 235 |
if Node <> nil then |
| 236 |
WritelnWarning('Spine', 'TRegionAttachment.BuildNodes multiple times on same node'); |
| 237 |
|
| 238 |
Node := TTransformNode.Create( |
| 239 |
Format('Slot_%s_Attachment_%s', [SlotName, Name]), |
| 240 |
BaseUrl); |
| 241 |
Node.Translation := Vector3(XY.X, XY.Y, 0); |
| 242 |
Node.Scale := Vector3(Scale.X, Scale.Y, 1); |
| 243 |
Node.Rotation := Vector4(0, 0, 1, DegToRad(Rotation)); |
| 244 |
|
| 245 |
Shape := TShapeNode.Create('', BaseUrl); |
| 246 |
Node.AddChildren(Shape); |
| 247 |
|
| 248 |
Appearance := TAppearanceNode.Create( |
| 249 |
Format('Appearance_Slot_%s_Attachment_%s', [SlotName, Name]), |
| 250 |
BaseUrl); |
| 251 |
Shape.Appearance := Appearance; |
| 252 |
|
| 253 |
AtlasRegionName := Name; |
| 254 |
if Path <> '' then |
| 255 |
AtlasRegionName := Path; |
| 256 |
Appearance.Texture := TextureLoader.UseNode(AtlasRegionName, |
| 257 |
TexCoord, TexRect, TexRotated); |
| 258 |
|
| 259 |
{ Other alpha values are not really useful for Spine. |
| 260 |
|
| 261 |
Setting AlphaMode explicitly is necessary in case we use TSimpleTextureLoader, |
| 262 |
as then it's easy to have some images with blend, some with opaque, |
| 263 |
and if we render them with different (automatic) alpha modes |
| 264 |
-> it will cause fighting in Z-buffer. This fighting in Z-buffer can be counteracted |
| 265 |
by increasing TSlot.DrawOrderZ, but sometimes it would require really large increase. |
| 266 |
Testcase: private-assets/spine/unholy_images_without_atlas/ |
| 267 |
where even TSlot.DrawOrderZ = 1 is not enough (10 is enough). |
| 268 |
|
| 269 |
So with TSimpleTextureLoader, it's more reliable to use amBlend for all. |
| 270 |
|
| 271 |
For TAtlas, it's just simpler. Why auto-detect, when we just know to use blending |
| 272 |
in all normal cases. } |
| 273 |
Appearance.AlphaMode := amBlend; |
| 274 |
|
| 275 |
if Appearance.Texture <> nil then |
| 276 |
Shape.Geometry := BuildGeometry(BaseUrl, TexCoord, TexRect, TexRotated); |
| 277 |
|
| 278 |
Material := TUnlitMaterialNode.Create( |
| 279 |
Format('Material_Slot_%s_Attachment_%s', [SlotName, Name]), |
| 280 |
BaseUrl); |
| 281 |
Material.EmissiveColor := WhiteRGB; |
| 282 |
Appearance.Material := Material; |
| 283 |
end; |
| 284 |
|
| 285 |
function TRegionAttachment.BuildGeometry(const BaseUrl: String; |
| 286 |
const TexCoord: TQuadTexCoord; const TexRect: TQuadTexRect; |
| 287 |
const TexRotated: boolean): TAbstractGeometryNode; |
| 288 |
var |
| 289 |
Coord: TCoordinateNode; |
| 290 |
TexCoordNode: TTextureCoordinateNode; |
| 291 |
Faces: TIndexedTriangleSetNode; |
| 292 |
Rect: array [0..1] of TVector2; |
| 293 |
begin |
| 294 |
{ We use here TIndexedTriangleSetNode, not TIndexedFaceSetNode, |
| 295 |
to allow DynamicBatching to merge TRegionAttachment with TMeshAttachment |
| 296 |
into one shape. } |
| 297 |
Faces := TIndexedTriangleSetNode.Create('', BaseUrl); |
| 298 |
Result := Faces; |
| 299 |
|
| 300 |
Faces.NormalPerVertex := false; // optimization: do not try to smooth normals, no point |
| 301 |
Faces.Solid := false; |
| 302 |
Faces.SetIndex([0, 1, 2, 0, 2, 3]); // 2 triangles forming a quad |
| 303 |
|
| 304 |
Coord := TCoordinateNode.Create( |
| 305 |
Format('Coordinate_Slot_%s_Attachment_%s', [SlotName, Name]), // name it, as ROUTE may lead to it |
| 306 |
Faces.BaseUrl); |
| 307 |
Rect[0] := Vector2( |
| 308 |
MapRange(TexRect[0].X, 0, 1, -Width / 2, Width / 2), |
| 309 |
MapRange(TexRect[0].Y, 0, 1, -Height / 2, Height / 2)); |
| 310 |
Rect[1] := Vector2( |
| 311 |
MapRange(TexRect[1].X, 0, 1, -Width / 2, Width / 2), |
| 312 |
MapRange(TexRect[1].Y, 0, 1, -Height / 2, Height / 2)); |
| 313 |
Coord.SetPoint([ |
| 314 |
Vector3(Rect[0].X, Rect[0].Y, 0), |
| 315 |
Vector3(Rect[1].X, Rect[0].Y, 0), |
| 316 |
Vector3(Rect[1].X, Rect[1].Y, 0), |
| 317 |
Vector3(Rect[0].X, Rect[1].Y, 0)]); |
| 318 |
Faces.Coord := Coord; |
| 319 |
|
| 320 |
TexCoordNode := TTextureCoordinateNode.Create('', Faces.BaseUrl); |
| 321 |
TexCoordNode.SetPoint([ |
| 322 |
TexCoord[0], |
| 323 |
TexCoord[1], |
| 324 |
TexCoord[2], |
| 325 |
TexCoord[3]]); |
| 326 |
Faces.TexCoord := TexCoordNode; |
| 327 |
end; |
| 328 |
|
| 329 |
{ TAbstractMeshAttachment ---------------------------------------------------- } |
| 330 |
|
| 331 |
constructor TAbstractMeshAttachment.Create; |
| 332 |
begin |
| 333 |
inherited; |
| 334 |
UVs := TVector2List.Create; |
| 335 |
Triangles := TInt32List.Create; |
| 336 |
Vertices := TVector2List.Create; |
| 337 |
Edges := TInt32List.Create; |
| 338 |
end; |
| 339 |
|
| 340 |
destructor TAbstractMeshAttachment.Destroy; |
| 341 |
begin |
| 342 |
FreeAndNil(UVs); |
| 343 |
FreeAndNil(Triangles); |
| 344 |
FreeAndNil(Vertices); |
| 345 |
FreeAndNil(Edges); |
| 346 |
inherited; |
| 347 |
end; |
| 348 |
|
| 349 |
procedure TAbstractMeshAttachment.Parse(const Json: TJSONObject; const Bones: TBoneList); |
| 350 |
begin |
| 351 |
inherited; |
| 352 |
ReadVector2List(Json, 'uvs', UVs); |
| 353 |
|
| 354 |
ReadInt32List(Json, 'triangles', Triangles); |
| 355 |
if Triangles.Count mod 3 <> 0 then |
| 356 |
WritelnWarning('Spine', 'Triangles list ends in the middle of the triangle'); |
| 357 |
if Triangles.Count = 0 then |
| 358 |
WritelnWarning('Spine', 'Triangles empty, mesh is empty'); |
| 359 |
|
| 360 |
ReadInt32List(Json, 'edges', Edges); |
| 361 |
Hull := Json.Get('hull', 0); |
| 362 |
end; |
| 363 |
|
| 364 |
function TAbstractMeshAttachment.BuildGeometry(const BaseUrl: String; |
| 365 |
const TexCoord: TQuadTexCoord; const TexRect: TQuadTexRect; |
| 366 |
const TexRotated: boolean): TAbstractGeometryNode; |
| 367 |
var |
| 368 |
TexCoordNode: TTextureCoordinateNode; |
| 369 |
I: Integer; |
| 370 |
UVRangeX, UVRangeY: TVector2; |
| 371 |
Faces: TIndexedTriangleSetNode; |
| 372 |
begin |
| 373 |
Faces := TIndexedTriangleSetNode.Create('', BaseUrl); |
| 374 |
Result := Faces; |
| 375 |
|
| 376 |
// do not call SetIndex in case Vertices count is known to be invalid |
| 377 |
if Invalid then Exit; |
| 378 |
|
| 379 |
Faces.NormalPerVertex := false; // optimization: do not try to smooth normals, no point |
| 380 |
Faces.Solid := false; |
| 381 |
Faces.SetIndex(Triangles); |
| 382 |
|
| 383 |
Coord := TCoordinateNode.Create( |
| 384 |
Format('Coordinate_Slot_%s_Attachment_%s', [SlotName, Name]), // name it, as ROUTE may lead to it |
| 385 |
Faces.BaseUrl); |
| 386 |
for I := 0 to Vertices.Count - 1 do |
| 387 |
Coord.FdPoint.Items.Add(Vector3(Vertices[I].X, Vertices[I].Y, 0)); |
| 388 |
Faces.Coord := Coord; |
| 389 |
|
| 390 |
{ We do not guarantee correct rendering when model used |
| 391 |
atlas "whitespace compression" by Spine, and used mesh attachment types. |
| 392 |
I see no way to support it sensibly --- we cannot easily squeeze the geometry |
| 393 |
to eliminate the empty space, because in general it means that triangles |
| 394 |
may need to be cut in half, and need to be changed to quads with new vertexes, |
| 395 |
to avoid rendering the atlas part where there should be whitespace. |
| 396 |
So supporting this would be crazy difficult. |
| 397 |
|
| 398 |
I don't see official spine runtimes having any code to deal with it either. |
| 399 |
|
| 400 |
The rendering will be Ok only if the mesh faces used only the texture |
| 401 |
parts within the non-whitespace region (TexRect). Which is true |
| 402 |
in practice in all the examples. } |
| 403 |
|
| 404 |
if ((TexRect[0].X <> 0) or |
| 405 |
(TexRect[1].X <> 0) or |
| 406 |
(TexRect[0].Y <> 1) or |
| 407 |
(TexRect[1].Y <> 1)) and |
| 408 |
SpineVerboseWarnings then |
| 409 |
WritelnLog('Spine', Format('Slot "%s" with attachment "%s" uses "mesh" attachment type, and you exported atlas with "Strip whitespace X" or "Strip whitespace Y" options selected. '+'Rendering artifacts may occur if mesh faces use the texture in the "whitespace" region. To be safe, export with "Strip whitespace X = Off" and "Strip whitespace Y = Off" in "Texture Packer Settings".', |
| 410 |
[SlotName, Name])); |
| 411 |
|
| 412 |
if TexRotated then |
| 413 |
WritelnWarning('Spine ' + UriDisplay(BaseUrl), 'Atlas region used for mesh attachment is rotated. This is not supported, texture will be applied incorrectly. Export with "Rotation" turned off in "Texture Packer Settings".'); |
| 414 |
|
| 415 |
TexCoordNode := TTextureCoordinateNode.Create('', Faces.BaseUrl); |
| 416 |
UVRangeX.X := Min(TexCoord[0].X, TexCoord[2].X); |
| 417 |
UVRangeX.Y := Max(TexCoord[0].X, TexCoord[2].X); |
| 418 |
UVRangeY.X := Min(TexCoord[0].Y, TexCoord[2].Y); |
| 419 |
UVRangeY.Y := Max(TexCoord[0].Y, TexCoord[2].Y); |
| 420 |
for I := 0 to UVs.Count - 1 do |
| 421 |
TexCoordNode.FdPoint.Items.Add(Vector2( |
| 422 |
MapRange(UVs[I].X, 0, 1, UVRangeX.X, UVRangeX.Y), |
| 423 |
MapRange(UVs[I].Y, 1, 0, UVRangeY.X, UVRangeY.Y) |
| 424 |
)); |
| 425 |
Faces.FdTexCoord.Value := TexCoordNode; |
| 426 |
end; |
| 427 |
|
| 428 |
{ TMeshAttachment ------------------------------------------------------------ } |
| 429 |
|
| 430 |
{ TMeshVertex --------------------------------------------------------- } |
| 431 |
|
| 432 |
constructor TMeshVertex.Create; |
| 433 |
begin |
| 434 |
inherited; |
| 435 |
Bones := TMeshVertexBoneList.Create; |
| 436 |
end; |
| 437 |
|
| 438 |
destructor TMeshVertex.Destroy; |
| 439 |
begin |
| 440 |
FreeAndNil(Bones); |
| 441 |
inherited; |
| 442 |
end; |
| 443 |
|
| 444 |
procedure TMeshVertex.Parse(const JsonArray: TJSONArray; |
| 445 |
Index: Integer; const BonesCount: Cardinal; const AllBones: TBoneList); |
| 446 |
var |
| 447 |
I, BoneIndex: Integer; |
| 448 |
B: TMeshVertexBone; |
| 449 |
begin |
| 450 |
for I := 0 to Integer(BonesCount) - 1 do |
| 451 |
begin |
| 452 |
BoneIndex := JsonArray[Index].AsInteger; Inc(Index); |
| 453 |
if not Between(BoneIndex, 0, AllBones.Count - 1) then |
| 454 |
raise Exception.CreateFmt('Invalid bone index in mesh.vertices: %d', [BoneIndex]); |
| 455 |
B := TMeshVertexBone.Create; |
| 456 |
Bones.Add(B); |
| 457 |
B.Bone := AllBones[BoneIndex]; |
| 458 |
B.V.X := JsonArray[Index].AsFloat; Inc(Index); |
| 459 |
B.V.Y := JsonArray[Index].AsFloat; Inc(Index); |
| 460 |
B.Weight := JsonArray[Index].AsFloat; Inc(Index); |
| 461 |
end; |
| 462 |
end; |
| 463 |
|
| 464 |
{ TMeshVertexList ----------------------------------------------------- } |
| 465 |
|
| 466 |
procedure TMeshVertexList.Parse(const JsonArray: TJSONArray; const Bones: TBoneList); |
| 467 |
var |
| 468 |
BonesCount: Cardinal; |
| 469 |
V: TMeshVertex; |
| 470 |
I: Integer; |
| 471 |
Offset: Integer; |
| 472 |
begin |
| 473 |
Offset := 0; |
| 474 |
I := 0; |
| 475 |
while I < JsonArray.Count do |
| 476 |
begin |
| 477 |
BonesCount := Max(0, JsonArray[I].AsInteger); |
| 478 |
if I + BonesCount * 4 < JsonArray.Count then |
| 479 |
begin |
| 480 |
if BonesCount = 0 then |
| 481 |
WritelnWarning('Spine', 'mesh bonesCount is <= 0'); |
| 482 |
V := TMeshVertex.Create; |
| 483 |
V.Parse(JsonArray, I + 1, BonesCount, Bones); |
| 484 |
V.Offset := Offset; |
| 485 |
Offset := Offset + BonesCount * 2; |
| 486 |
Add(V); |
| 487 |
end else |
| 488 |
begin |
| 489 |
WritelnWarning('Spine', Format('mesh weights (called "vertices" in JSON) do not have enough items (last bonesCount = %d, but array does not have enough floats)', |
| 490 |
[BonesCount])); |
| 491 |
Break; |
| 492 |
end; |
| 493 |
|
| 494 |
I := I + 1 + BonesCount * 4; |
| 495 |
end; |
| 496 |
end; |
| 497 |
|
| 498 |
{ TMeshAttachment ----------------------------------------------------- } |
| 499 |
|
| 500 |
constructor TMeshAttachment.Create; |
| 501 |
begin |
| 502 |
inherited; |
| 503 |
Weights := TMeshVertexList.Create; |
| 504 |
end; |
| 505 |
|
| 506 |
destructor TMeshAttachment.Destroy; |
| 507 |
begin |
| 508 |
FreeAndNil(Weights); |
| 509 |
inherited; |
| 510 |
end; |
| 511 |
|
| 512 |
procedure TMeshAttachment.Parse(const Json: TJSONObject; const Bones: TBoneList); |
| 513 |
var |
| 514 |
JsonArray: TJSONArray; |
| 515 |
I, J: Integer; |
| 516 |
VB: TMeshVertexBone; |
| 517 |
V: TVector2; |
| 518 |
begin |
| 519 |
inherited; |
| 520 |
ReadVector2List(Json, 'vertices', Vertices, true); |
| 521 |
|
| 522 |
if Vertices.Count > UVs.Count then |
| 523 |
begin |
| 524 |
Vertices.Clear; |
| 525 |
JsonArray := Json.Find('vertices', jtArray) as TJSONArray; |
| 526 |
if JsonArray <> nil then |
| 527 |
Weights.Parse(JsonArray, Bones); |
| 528 |
{ calculate initial Vertices from Weights. |
| 529 |
Similar to ComputeWorldVertices in spine-runtimes/spine-csharp/src/Attachments/SkinnedMeshAttachment.cs } |
| 530 |
Vertices.Count := Weights.Count; |
| 531 |
for I := 0 to Weights.Count - 1 do |
| 532 |
begin |
| 533 |
V := TVector2.Zero; |
| 534 |
for J := 0 to Weights[I].Bones.Count - 1 do |
| 535 |
begin |
| 536 |
VB := Weights[I].Bones[J]; |
| 537 |
V := V + (VB.Bone.ToWorldSpace(VB.V) * VB.Weight); |
| 538 |
end; |
| 539 |
Vertices[I] := V; |
| 540 |
end; |
| 541 |
end; |
| 542 |
end; |
| 543 |
|
| 544 |
|
| 545 |
{ TSkinnedMeshAttachment --------------------------------------------------- } |
| 546 |
|
| 547 |
procedure TSkinnedMeshAttachment.TransformToBoneSpace(const Bone: TBone); |
| 548 |
var |
| 549 |
List: TVector3List; |
| 550 |
I: Integer; |
| 551 |
begin |
| 552 |
inherited; |
| 553 |
|
| 554 |
List := Coord.FdPoint.Items; |
| 555 |
for I := 0 to List.Count - 1 do |
| 556 |
List.L[I] := Bone.ToLocalSpace(List.L[I]); |
| 557 |
end; |
| 558 |
|
| 559 |
{ TPathAttachment ---------------------------------------------------------- } |
| 560 |
|
| 561 |
constructor TPathAttachment.Create; |
| 562 |
begin |
| 563 |
inherited; |
| 564 |
Vertices := TVector2List.Create; |
| 565 |
Lengths := TSingleList.Create; |
| 566 |
end; |
| 567 |
|
| 568 |
destructor TPathAttachment.Destroy; |
| 569 |
begin |
| 570 |
FreeAndNil(Lengths); |
| 571 |
FreeAndNil(Vertices); |
| 572 |
inherited; |
| 573 |
end; |
| 574 |
|
| 575 |
procedure TPathAttachment.Parse(const Json: TJSONObject; const Bones: TBoneList); |
| 576 |
var |
| 577 |
VertexCount: Integer; |
| 578 |
begin |
| 579 |
inherited; |
| 580 |
VertexCount := Json.Get('vertexCount', 0); |
| 581 |
ReadVector2List(Json, 'vertices', Vertices); |
| 582 |
ReadSingleList(Json, 'lengths', Lengths); |
| 583 |
if Vertices.Count <> VertexCount then |
| 584 |
WritelnWarning('Spine "path" attachment has unexpected "vertices" count: expected %d, has %d', [VertexCount, Vertices.Count]); |
| 585 |
if Lengths.Count <> VertexCount div 3 then |
| 586 |
WritelnWarning('Spine "path" attachment has unexpected "lengths" count: expected %d, has %d', [VertexCount div 3, Lengths.Count]); |
| 587 |
end; |
| 588 |
|
| 589 |
procedure TPathAttachment.BuildNodes(const BaseUrl: String; |
| 590 |
const TextureLoader: TTextureLoader); |
| 591 |
var |
| 592 |
Shape: TShapeNode; |
| 593 |
ControlPoints: TCoordinateNode; |
| 594 |
Curve: TNurbsCurveNode; |
| 595 |
I: Integer; |
| 596 |
begin |
| 597 |
if Node <> nil then |
| 598 |
WritelnWarning('Spine', 'TPathAttachment.BuildNodes multiple times on same node'); |
| 599 |
|
| 600 |
Node := TTransformNode.Create( |
| 601 |
Format('Slot_%s_Attachment_%s', [SlotName, Name]), |
| 602 |
BaseUrl); |
| 603 |
|
| 604 |
Shape := TShapeNode.Create('', BaseUrl); |
| 605 |
Node.AddChildren(Shape); |
| 606 |
|
| 607 |
Material := TUnlitMaterialNode.Create( |
| 608 |
Format('Material_Slot_%s_Attachment_%s', [SlotName, Name]), |
| 609 |
BaseUrl); |
| 610 |
Material.EmissiveColor := WhiteRGB; |
| 611 |
|
| 612 |
Appearance := TAppearanceNode.Create( |
| 613 |
Format('Appearance_Slot_%s_Attachment_%s', [SlotName, Name]), |
| 614 |
BaseUrl); |
| 615 |
Appearance.Material := Material; |
| 616 |
|
| 617 |
Shape.Appearance := Appearance; |
| 618 |
|
| 619 |
Shape.Visible := false; |
| 620 |
|
| 621 |
ControlPoints := TCoordinateNode.Create('', BaseUrl); |
| 622 |
{ we ignore first and last Vertices, they are only handles outside of |
| 623 |
the curve, not taking part in curve calculation } |
| 624 |
for I := 1 to Vertices.Count - 2 do |
| 625 |
ControlPoints.FdPoint.Items.Add(Vector3(Vertices[I], 0)); |
| 626 |
|
| 627 |
Curve := TNurbsCurveNode.Create('', BaseUrl); |
| 628 |
Curve.ControlPoint := ControlPoints; |
| 629 |
Curve.Order := 4; // cubic Bezier |
| 630 |
Curve.Tessellation := ControlPoints.FdPoint.Count * 10; // by default, make quite detailed tesselation |
| 631 |
Curve.PiecewiseBezier; |
| 632 |
Shape.Geometry := Curve; |
| 633 |
end; |
| 634 |
|
| 635 |
{ TClippingAttachment ---------------------------------------------------------- } |
| 636 |
|
| 637 |
constructor TClippingAttachment.Create; |
| 638 |
begin |
| 639 |
inherited; |
| 640 |
Vertices := TVector2List.Create; |
| 641 |
end; |
| 642 |
|
| 643 |
destructor TClippingAttachment.Destroy; |
| 644 |
begin |
| 645 |
FreeAndNil(Vertices); |
| 646 |
// If you didn't call VisualizeClipPolygon in BuildNodes, Appearance node remains unused |
| 647 |
FreeIfUnusedAndNil(Appearance); |
| 648 |
inherited; |
| 649 |
end; |
| 650 |
|
| 651 |
procedure TClippingAttachment.Parse(const Json: TJSONObject; const Bones: TBoneList); |
| 652 |
var |
| 653 |
VertexCount: Integer; |
| 654 |
begin |
| 655 |
inherited; |
| 656 |
VertexCount := Json.Get('vertexCount', 0); |
| 657 |
ReadVector2List(Json, 'vertices', Vertices); |
| 658 |
if Vertices.Count <> VertexCount then |
| 659 |
WritelnWarning('Spine "clipping" attachment has unexpected "vertices" count: expected %d, has %d', [VertexCount, Vertices.Count]); |
| 660 |
end; |
| 661 |
|
| 662 |
procedure TClippingAttachment.BuildNodes(const BaseUrl: String; |
| 663 |
const TextureLoader: TTextureLoader); |
| 664 |
|
| 665 |
procedure VisualizeClipPolygon; |
| 666 |
var |
| 667 |
Coordinates: TCoordinateNode; |
| 668 |
Faces: TIndexedFaceSetNode; |
| 669 |
Shape: TShapeNode; |
| 670 |
I: Integer; |
| 671 |
begin |
| 672 |
Shape := TShapeNode.Create('', BaseUrl); |
| 673 |
Shape.Appearance := Appearance; |
| 674 |
Node.AddChildren(Shape); |
| 675 |
|
| 676 |
Faces := TIndexedFaceSetNode.Create('', BaseUrl); |
| 677 |
Faces.NormalPerVertex := false; // optimization: do not try to smooth normals, no point |
| 678 |
Faces.CreaseAngle := 0; // optimization: do not try to smooth normals, no point |
| 679 |
Faces.Solid := false; |
| 680 |
|
| 681 |
Coordinates := TCoordinateNode.Create('', BaseUrl); |
| 682 |
for I := 0 to Vertices.Count - 1 do |
| 683 |
begin |
| 684 |
Coordinates.FdPoint.Items.Add(Vector3(Vertices[I], 0)); |
| 685 |
Faces.FdCoordIndex.Items.Add(I); |
| 686 |
end; |
| 687 |
|
| 688 |
Faces.Coord := Coordinates; |
| 689 |
Faces.Solid := false; |
| 690 |
Faces.Convex := false; |
| 691 |
Shape.Geometry := Faces; |
| 692 |
end; |
| 693 |
|
| 694 |
procedure AddClipPlane(const Point, NextPoint: TVector2); |
| 695 |
var |
| 696 |
D: Single; |
| 697 |
Plane: TVector4; |
| 698 |
ClipPlane: TClipPlaneNode; |
| 699 |
begin |
| 700 |
D := PointsDistance(Point, NextPoint); |
| 701 |
Plane := Triangle3( |
| 702 |
Vector3(Point, 0), |
| 703 |
Vector3(NextPoint, 0), |
| 704 |
Vector3((Point + NextPoint) / 2, D) |
| 705 |
).NormalizedPlane; |
| 706 |
ClipPlane := TClipPlaneNode.Create('', BaseUrl); |
| 707 |
ClipPlane.Plane := Plane; |
| 708 |
Node.AddChildren(ClipPlane); |
| 709 |
end; |
| 710 |
|
| 711 |
var |
| 712 |
I: Integer; |
| 713 |
begin |
| 714 |
if Node <> nil then |
| 715 |
WritelnWarning('Spine', 'TClippingAttachment.BuildNodes multiple times on same node'); |
| 716 |
|
| 717 |
Node := TTransformNode.Create( |
| 718 |
Format('Slot_%s_Attachment_%s', [SlotName, Name]), |
| 719 |
BaseUrl); |
| 720 |
|
| 721 |
Material := TUnlitMaterialNode.Create( |
| 722 |
Format('Material_Slot_%s_Attachment_%s', [SlotName, Name]), |
| 723 |
BaseUrl); |
| 724 |
Material.EmissiveColor := WhiteRGB; |
| 725 |
|
| 726 |
Appearance := TAppearanceNode.Create( |
| 727 |
Format('Appearance_Slot_%s_Attachment_%s', [SlotName, Name]), |
| 728 |
BaseUrl); |
| 729 |
Appearance.Material := Material; |
| 730 |
|
| 731 |
// debug |
| 732 |
//VisualizeClipPolygon; |
| 733 |
|
| 734 |
for I := 0 to Vertices.Count - 1 do |
| 735 |
AddClipPlane(Vertices[I], Vertices[(I + 1) mod Vertices.Count]); |
| 736 |
|
| 737 |
// TODO: This is cool, but ClipPlanes don't affect anything. |
| 738 |
// To make this approach cool, we would need ClipPlane.global field. |
| 739 |
// Alternatively, we could make a different approach using stencil buffer. |
| 740 |
|
| 741 |
WritelnWarning('Spine', 'Spine "clipping" region type is not yet implemented'); |
| 742 |
end; |
| 743 |
|
| 744 |
{ TAttachmentList ------------------------------------------------------------ } |
| 745 |
|
| 746 |
function TAttachmentList.Find(const SlotName, AttachmentName: string; |
| 747 |
const FallbackList: TAttachmentList; |
| 748 |
const NilWhenNotFound: boolean): TAttachment; |
| 749 |
var |
| 750 |
I: Integer; |
| 751 |
Err: string; |
| 752 |
begin |
| 753 |
for I := 0 to Count - 1 do |
| 754 |
if (Items[I].SlotName = SlotName) and |
| 755 |
(Items[I].AttachmentName = AttachmentName) then |
| 756 |
Exit(Items[I]); |
| 757 |
|
| 758 |
if FallbackList <> nil then |
| 759 |
Result := FallbackList.Find(SlotName, AttachmentName, nil, NilWhenNotFound) else |
| 760 |
begin |
| 761 |
Err := Format('Attachment values for slot "%s" and attachment name "%s" not found', |
| 762 |
[SlotName, AttachmentName]); |
| 763 |
if NilWhenNotFound then |
| 764 |
begin |
| 765 |
WritelnLog('Spine', Err); // this is actually normal, as tested on Venice models, so only log, no warning |
| 766 |
Result := nil; |
| 767 |
end else |
| 768 |
raise ESpineReadError.Create(Err); |
| 769 |
end; |
| 770 |
end; |
| 771 |
{$endif} |