| 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 slots. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
TSlotList = class; |
| 21 |
|
| 22 |
TUnlitMaterialNodeList = {$ifdef FPC}specialize{$endif} TObjectList<TUnlitMaterialNode>; |
| 23 |
|
| 24 |
TColorInterpolatorNodeList = {$ifdef FPC}specialize{$endif} TObjectList<TColorInterpolatorNode>; |
| 25 |
TScalarInterpolatorNodeList = {$ifdef FPC}specialize{$endif} TObjectList<TScalarInterpolatorNode>; |
| 26 |
|
| 27 |
TSlot = class |
| 28 |
public |
| 29 |
const |
| 30 |
DrawOrderZ = 0.01; |
| 31 |
var |
| 32 |
Name: string; |
| 33 |
Bone: TBone; |
| 34 |
Color: TCastleColor; |
| 35 |
{ Possible attachment names on this slot. |
| 36 |
First attachment name (alwats exists, but may be '') |
| 37 |
indicates the setup pose attachment. |
| 38 |
The rest of attachments may be used during animations. |
| 39 |
The order of this list is also the order of Switch @link(Node), |
| 40 |
that represents this slot in X3D. } |
| 41 |
AttachmentNames: TStringList; |
| 42 |
{ Draw order, from 1st (most background) to last (most foreground). |
| 43 |
Initially derived looking at slots order, this is what determines |
| 44 |
drawing order for spine, see |
| 45 |
http://esotericsoftware.com/spine-using-runtimes } |
| 46 |
DrawOrder: Integer; |
| 47 |
Node: TTransformNode; |
| 48 |
SwitchNode: TSwitchNode; |
| 49 |
NodeUsedAsChild: boolean; |
| 50 |
Materials: TUnlitMaterialNodeList; |
| 51 |
constructor Create; |
| 52 |
destructor Destroy; override; |
| 53 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList; |
| 54 |
const DragonBonesFormat: boolean); |
| 55 |
procedure BuildNodes(const BaseUrl: String; |
| 56 |
const AttachmentsPreferred, AttachmentsDefault: TAttachmentList); |
| 57 |
end; |
| 58 |
|
| 59 |
TSlotList = class({$ifdef FPC}specialize{$endif} TObjectList<TSlot>) |
| 60 |
{ Find slot by name. |
| 61 |
@raises ESpineReadError If slot does not exist. } |
| 62 |
function Find(const SlotName: string): TSlot; |
| 63 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList; |
| 64 |
const DragonBonesFormat: boolean); |
| 65 |
procedure BuildNodes(const BaseUrl: String; |
| 66 |
const AttachmentsPreferred, AttachmentsDefault: TAttachmentList); |
| 67 |
procedure Assign(const Source: TSlotList); |
| 68 |
end; |
| 69 |
{$endif} |
| 70 |
|
| 71 |
{$ifdef read_implementation} |
| 72 |
|
| 73 |
{ TSlot ---------------------------------------------------------------------- } |
| 74 |
|
| 75 |
constructor TSlot.Create; |
| 76 |
begin |
| 77 |
inherited; |
| 78 |
AttachmentNames := TStringList.Create; |
| 79 |
Materials := TUnlitMaterialNodeList.Create(false); |
| 80 |
end; |
| 81 |
|
| 82 |
destructor TSlot.Destroy; |
| 83 |
begin |
| 84 |
if NodeUsedAsChild then |
| 85 |
Node := nil |
| 86 |
else |
| 87 |
FreeIfUnusedAndNil(Node); |
| 88 |
FreeAndNil(AttachmentNames); |
| 89 |
FreeAndNil(Materials); |
| 90 |
inherited; |
| 91 |
end; |
| 92 |
|
| 93 |
procedure TSlot.Parse(const Json: TJSONObject; const Bones: TBoneList; |
| 94 |
const DragonBonesFormat: boolean); |
| 95 |
begin |
| 96 |
Name := Json.Get('name', ''); |
| 97 |
if DragonBonesFormat then |
| 98 |
Bone := Bones.Find(Json.Get('parent', '')) |
| 99 |
else |
| 100 |
Bone := Bones.Find(Json.Get('bone', '')); |
| 101 |
AttachmentNames.Add(Json.Get('attachment', '')); |
| 102 |
Color := HexToColor(Json.Get('color', 'ffffffff')); |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TSlot.BuildNodes(const BaseUrl: String; |
| 106 |
const AttachmentsPreferred, AttachmentsDefault: TAttachmentList); |
| 107 |
var |
| 108 |
IsWeightedMeshAttachmentExisted: Boolean {$ifdef FPC}= False{$endif}; |
| 109 |
IsOtherAttachmentExisted: Boolean {$ifdef FPC}= False{$endif}; |
| 110 |
|
| 111 |
{ Node for a single AttachmentName. Never @nil. } |
| 112 |
function AttachmentNode(const AttachmentName: string): TAbstractChildNode; |
| 113 |
var |
| 114 |
A: TAttachment; |
| 115 |
begin |
| 116 |
Result := nil; |
| 117 |
|
| 118 |
{ ignore empty attachment names, as http://esotericsoftware.com/spine-json-format |
| 119 |
says explicitly "Assume no attachment for the setup pose if omitted." } |
| 120 |
if AttachmentName <> '' then |
| 121 |
begin |
| 122 |
if AttachmentsPreferred <> AttachmentsDefault then |
| 123 |
A := AttachmentsPreferred.Find(Name, AttachmentName, AttachmentsDefault, true) else |
| 124 |
A := AttachmentsPreferred.Find(Name, AttachmentName, nil, true); |
| 125 |
if A <> nil then |
| 126 |
begin |
| 127 |
if A.NodeUsedAsChild and |
| 128 |
not ( |
| 129 |
TVector3.PerfectlyEquals(A.Material.FdEmissiveColor.Value, WhiteRGB) or |
| 130 |
(A.Material.FdTransparency.Value <> 1.0) or |
| 131 |
TVector4.PerfectlyEquals(Color, White) |
| 132 |
) then |
| 133 |
begin |
| 134 |
WritelnWarning('Spine', 'Attachment "%s" used by multiple slots with different (not opaque white) colors. This is not supported, instead make the slot colors to be applied during animations.', |
| 135 |
[A.Node.X3DName]); |
| 136 |
end; |
| 137 |
|
| 138 |
{ TODO: This is a hackjob and need a better way to handle this issue. |
| 139 |
Since we "baked" mesh transformation into TCoordinateInterpolatorNode, |
| 140 |
including bone transformation, we need to move this mesh to root bone. } |
| 141 |
if (A is TMeshAttachment) and ((A as TMeshAttachment).Weights.Count > 0) then |
| 142 |
begin |
| 143 |
IsWeightedMeshAttachmentExisted := True; |
| 144 |
while Bone.Parent <> nil do |
| 145 |
begin |
| 146 |
Bone := Bone.Parent; |
| 147 |
end; |
| 148 |
end else |
| 149 |
begin |
| 150 |
IsOtherAttachmentExisted := True; |
| 151 |
end; |
| 152 |
|
| 153 |
{ Transform skinnedmesh into local space. |
| 154 |
Note that the new weighted mesh baked transformations directly into |
| 155 |
TCoordinateInterpolatorNode so this is not necessary for weighted mesh. } |
| 156 |
A.TransformToBoneSpace(Bone); |
| 157 |
A.NodeUsedAsChild := true; |
| 158 |
Result := A.Node; |
| 159 |
|
| 160 |
// We need to copy slot color to the material node inside attachment |
| 161 |
A.Material.FdEmissiveColor.Value := Color.XYZ; |
| 162 |
A.Material.FdTransparency.Value := 1 - Color[3]; |
| 163 |
|
| 164 |
{ Do not put duplicates on Materials list, |
| 165 |
this would cause TSlotTimelineColor.BuildNodes |
| 166 |
to create multiple ROUTEs to the same node. } |
| 167 |
if not Materials.Contains(A.Material) then |
| 168 |
Materials.Add(A.Material); |
| 169 |
end; |
| 170 |
end; |
| 171 |
|
| 172 |
{ the caller needs non-nil result, to have 1 node for every AttachmentName, |
| 173 |
so that Switch.whichChoice can work reliably. |
| 174 |
So create an empty node if needed. } |
| 175 |
if Result = nil then |
| 176 |
Result := TStaticGroupNode.Create('SlotEmptyAttachment_' + Name, BaseUrl); |
| 177 |
end; |
| 178 |
|
| 179 |
var |
| 180 |
I: Integer; |
| 181 |
begin |
| 182 |
{$ifndef FPC} |
| 183 |
// can't initialize local variables in Delphi |
| 184 |
IsWeightedMeshAttachmentExisted := false; |
| 185 |
IsOtherAttachmentExisted := false; |
| 186 |
{$endif} |
| 187 |
|
| 188 |
Node := TTransformNode.Create('SlotTransform_' + Name, BaseUrl); |
| 189 |
Node.FdTranslation.Value := Vector3(0, 0, DrawOrder * DrawOrderZ); |
| 190 |
NodeUsedAsChild := true; |
| 191 |
|
| 192 |
SwitchNode := TSwitchNode.Create('SlotSwitch_' + Name, BaseUrl); |
| 193 |
SwitchNode.FdWhichChoice.Value := 0; |
| 194 |
Node.AddChildren(SwitchNode); |
| 195 |
|
| 196 |
Materials.Clear; |
| 197 |
|
| 198 |
for I := 0 to AttachmentNames.Count - 1 do |
| 199 |
SwitchNode.AddChildren(AttachmentNode(AttachmentNames[I])); |
| 200 |
if IsWeightedMeshAttachmentExisted and IsOtherAttachmentExisted then |
| 201 |
WritelnWarning('Spine', 'Weighted mesh attachment alongside with other types of attachment in the same slot is not supported'); |
| 202 |
Bone.Node.AddChildren(Node); |
| 203 |
end; |
| 204 |
|
| 205 |
{ TSlotList ------------------------------------------------------------------ } |
| 206 |
|
| 207 |
function TSlotList.Find(const SlotName: string): TSlot; |
| 208 |
var |
| 209 |
Index: Integer; |
| 210 |
begin |
| 211 |
for Index := 0 to Count - 1 do |
| 212 |
if Items[Index].Name = SlotName then |
| 213 |
Exit(Items[Index]); |
| 214 |
raise ESpineReadError.CreateFmt('Slot name "%s" not found', [SlotName]); |
| 215 |
end; |
| 216 |
|
| 217 |
procedure TSlotList.Parse(const Json: TJSONObject; const Bones: TBoneList; const DragonBonesFormat: boolean); |
| 218 |
var |
| 219 |
I: Integer; |
| 220 |
Slot: TSlot; |
| 221 |
ChildArray: TJSONArray; |
| 222 |
ArrayName: string; |
| 223 |
begin |
| 224 |
if DragonBonesFormat then |
| 225 |
ArrayName := 'slot' |
| 226 |
else |
| 227 |
ArrayName := 'slots'; |
| 228 |
ChildArray := Json.Find(ArrayName, jtArray) as TJSONArray; |
| 229 |
if ChildArray = nil then |
| 230 |
raise ESpineReadError.CreateFmt('Spine / Dragon Bones JSON skeleton: Missing "%s" array', [ArrayName]); |
| 231 |
|
| 232 |
for I := 0 to ChildArray.Count - 1 do |
| 233 |
if ChildArray[I] is TJSONObject then |
| 234 |
begin |
| 235 |
Slot := TSlot.Create; |
| 236 |
Slot.DrawOrder := Count; |
| 237 |
Add(Slot); |
| 238 |
Slot.Parse(TJSONObject(ChildArray[I]), Bones, DragonBonesFormat); |
| 239 |
end; |
| 240 |
end; |
| 241 |
|
| 242 |
procedure TSlotList.BuildNodes(const BaseUrl: String; |
| 243 |
const AttachmentsPreferred, AttachmentsDefault: TAttachmentList); |
| 244 |
var |
| 245 |
I: Integer; |
| 246 |
begin |
| 247 |
for I := 0 to Count - 1 do |
| 248 |
Items[I].BuildNodes(BaseUrl, AttachmentsPreferred, AttachmentsDefault); |
| 249 |
end; |
| 250 |
|
| 251 |
procedure TSlotList.Assign(const Source: TSlotList); |
| 252 |
begin |
| 253 |
Clear; |
| 254 |
AddRange(Source); |
| 255 |
end; |
| 256 |
|
| 257 |
{$endif} |