| 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 slot timelines. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
{ Node with key field and set_fraction event. Like interpolators, |
| 21 |
but not only (e.g. IntegerSequencer). } |
| 22 |
TKeyedNode = record |
| 23 |
EventSet_Fraction: TSFFloatEvent; |
| 24 |
Key: TMFFloat; |
| 25 |
Node: TAbstractChildNode; |
| 26 |
UsedAsChild: boolean; |
| 27 |
end; |
| 28 |
|
| 29 |
TSlotTimeline = class |
| 30 |
strict private |
| 31 |
FMaxTime: Single; |
| 32 |
public |
| 33 |
Slot: TSlot; |
| 34 |
Time: TSingleList; |
| 35 |
Node1, Node2: TKeyedNode; |
| 36 |
HasNode2: boolean; |
| 37 |
Curve: boolean; |
| 38 |
CurveControlPoints: TVector4List; |
| 39 |
property MaxTime: Single read FMaxTime; |
| 40 |
constructor Create; |
| 41 |
destructor Destroy; override; |
| 42 |
procedure Parse(const Json: TJSONArray; |
| 43 |
const NormalizeCurvesKeyValues: Boolean); |
| 44 |
procedure ParseSingleValue(const Json: TJSONObject); virtual; abstract; |
| 45 |
procedure BuildNodes(const BaseUrl: String; |
| 46 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); virtual; |
| 47 |
procedure ParseControlPoints(const Json: TJSONArray; |
| 48 |
const NormalizeCurvesKeyValues: Boolean); virtual; abstract; |
| 49 |
end; |
| 50 |
|
| 51 |
TSlotTimelineAttachment = class(TSlotTimeline) |
| 52 |
Attachments: TStringList; |
| 53 |
constructor Create; |
| 54 |
destructor Destroy; override; |
| 55 |
procedure ParseSingleValue(const Json: TJSONObject); override; |
| 56 |
procedure BuildNodes(const BaseUrl: String; |
| 57 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); override; |
| 58 |
procedure ParseControlPoints(const Json: TJSONArray; |
| 59 |
const NormalizeCurvesKeyValues: Boolean); override; |
| 60 |
end; |
| 61 |
|
| 62 |
TSlotTimelineColor = class(TSlotTimeline) |
| 63 |
Colors: TVector4List; |
| 64 |
constructor Create; |
| 65 |
destructor Destroy; override; |
| 66 |
procedure ParseSingleValue(const Json: TJSONObject); override; |
| 67 |
procedure BuildNodes(const BaseUrl: String; |
| 68 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); override; |
| 69 |
procedure ParseControlPoints(const Json: TJSONArray; |
| 70 |
const NormalizeCurvesKeyValues: Boolean); override; |
| 71 |
end; |
| 72 |
|
| 73 |
TSlotTimelineList = class({$ifdef FPC}specialize{$endif} TObjectList<TSlotTimeline>) |
| 74 |
end; |
| 75 |
{$endif} |
| 76 |
|
| 77 |
{$ifdef read_implementation} |
| 78 |
|
| 79 |
{ TSlotTimeline -------------------------------------------------------------- } |
| 80 |
|
| 81 |
constructor TSlotTimeline.Create; |
| 82 |
begin |
| 83 |
inherited; |
| 84 |
Time := TSingleList.Create; |
| 85 |
CurveControlPoints := TVector4List.Create; |
| 86 |
end; |
| 87 |
|
| 88 |
destructor TSlotTimeline.Destroy; |
| 89 |
begin |
| 90 |
FreeAndNil(Time); |
| 91 |
FreeAndNil(CurveControlPoints); |
| 92 |
inherited; |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TSlotTimeline.Parse(const Json: TJSONArray; |
| 96 |
const NormalizeCurvesKeyValues: Boolean); |
| 97 |
var |
| 98 |
I: Integer; |
| 99 |
O: TJSONObject; |
| 100 |
NextTime: Single; |
| 101 |
begin |
| 102 |
Curve := false; |
| 103 |
|
| 104 |
for I := 0 to Json.Count - 1 do |
| 105 |
if Json[I] is TJSONObject then |
| 106 |
begin |
| 107 |
O := TJSONObject(Json[I]); |
| 108 |
|
| 109 |
NextTime := O.Get('time', 0.0); |
| 110 |
if (Time.Count <> 0) and (Time.Last > NextTime) then |
| 111 |
raise ESpineReadError.Create('Timeline must have increasing time values'); |
| 112 |
Time.Add(NextTime); |
| 113 |
FMaxTime := NextTime; |
| 114 |
|
| 115 |
// only detect whether we have a curve at this point |
| 116 |
if not Curve then |
| 117 |
Curve := O.Find('curve') is TJSONArray; |
| 118 |
|
| 119 |
ParseSingleValue(O); |
| 120 |
end; |
| 121 |
|
| 122 |
if Curve then |
| 123 |
ParseControlPoints(Json, NormalizeCurvesKeyValues); |
| 124 |
end; |
| 125 |
|
| 126 |
procedure TSlotTimeline.BuildNodes(const BaseUrl: String; |
| 127 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 128 |
|
| 129 |
procedure FillKeys(var Node: TKeyedNode); |
| 130 |
var |
| 131 |
I: Integer; |
| 132 |
begin |
| 133 |
Node.Key.Items.Clear; |
| 134 |
for I := 0 to Time.Count - 1 do |
| 135 |
Node.Key.Items.Add(Time[I] / MaxAnimationTime); |
| 136 |
Node.UsedAsChild := true; |
| 137 |
Container.AddChildren(Node.Node); |
| 138 |
end; |
| 139 |
|
| 140 |
begin |
| 141 |
{ We assume that descendant already created Node1/2 in overridden BuildNodes } |
| 142 |
FillKeys(Node1); |
| 143 |
if HasNode2 then |
| 144 |
FillKeys(Node2); |
| 145 |
end; |
| 146 |
|
| 147 |
{ TSlotTimelineAttachment ------------------------------------------------------- } |
| 148 |
|
| 149 |
constructor TSlotTimelineAttachment.Create; |
| 150 |
begin |
| 151 |
inherited Create; |
| 152 |
Attachments := TStringList.Create; |
| 153 |
end; |
| 154 |
|
| 155 |
destructor TSlotTimelineAttachment.Destroy; |
| 156 |
begin |
| 157 |
FreeAndNil(Attachments); |
| 158 |
inherited; |
| 159 |
end; |
| 160 |
|
| 161 |
procedure TSlotTimelineAttachment.ParseSingleValue(const Json: TJSONObject); |
| 162 |
var |
| 163 |
AttachmentName: string; |
| 164 |
begin |
| 165 |
AttachmentName := Json.Get('name', ''); |
| 166 |
Slot.AttachmentNames.Add(AttachmentName); |
| 167 |
Attachments.Add(AttachmentName); |
| 168 |
end; |
| 169 |
|
| 170 |
procedure TSlotTimelineAttachment.BuildNodes(const BaseUrl: String; |
| 171 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 172 |
var |
| 173 |
I: Integer; |
| 174 |
N: TIntegerSequencerNode; |
| 175 |
EventValueChanged: TX3DEvent; |
| 176 |
Route: TX3DRoute; |
| 177 |
begin |
| 178 |
N := TIntegerSequencerNode.Create('SlotTimeline_attachment_' + Slot.Name); |
| 179 |
N.ForceContinuousValue_Changed := true; |
| 180 |
for I := 0 to Attachments.Count - 1 do |
| 181 |
N.FdKeyValue.Items.Add(Slot.AttachmentNames.IndexOf(Attachments[I])); |
| 182 |
EventValueChanged := N.EventValue_changed; |
| 183 |
Node1.EventSet_Fraction := N.EventSet_Fraction; |
| 184 |
Node1.Key := N.FdKey; |
| 185 |
Node1.Node := N; |
| 186 |
|
| 187 |
inherited; |
| 188 |
|
| 189 |
Route := TX3DRoute.Create; |
| 190 |
Route.SetSourceDirectly(EventValueChanged); |
| 191 |
Route.SetDestinationDirectly(Slot.SwitchNode.FdWhichChoice.EventIn); |
| 192 |
Container.AddRoute(Route); |
| 193 |
|
| 194 |
{ When there's only 1 keyframe, Spine does something weird: instead of smooth |
| 195 |
interpolation, animation suddenly jumps from setup pose to given keyframe |
| 196 |
pose at given key time (unless animation is looping and it's the last frame, |
| 197 |
then it's visibly ignored). } |
| 198 |
if SpineVerboseWarnings and (Time.Count = 1) then |
| 199 |
WritelnWarning('Spine', Format(SSpine1KeyFrameWarning, ['slot', Slot.Name, 'attachment'])); |
| 200 |
end; |
| 201 |
|
| 202 |
procedure TSlotTimelineAttachment.ParseControlPoints(const Json: TJSONArray; |
| 203 |
const NormalizeCurvesKeyValues: Boolean); |
| 204 |
begin |
| 205 |
WritelnWarning('Invalid Spine JSON: Curve interpolation for slot attachments animation is not possible'); |
| 206 |
end; |
| 207 |
|
| 208 |
{ TSlotTimelineColor --------------------------------------------------------- } |
| 209 |
|
| 210 |
constructor TSlotTimelineColor.Create; |
| 211 |
begin |
| 212 |
inherited Create; |
| 213 |
Colors := TVector4List.Create; |
| 214 |
end; |
| 215 |
|
| 216 |
destructor TSlotTimelineColor.Destroy; |
| 217 |
begin |
| 218 |
FreeAndNil(Colors); |
| 219 |
inherited; |
| 220 |
end; |
| 221 |
|
| 222 |
procedure TSlotTimelineColor.ParseSingleValue(const Json: TJSONObject); |
| 223 |
begin |
| 224 |
Colors.Add(HexToColor(Json.Get('color', 'ffffffff'))); |
| 225 |
end; |
| 226 |
|
| 227 |
procedure TSlotTimelineColor.ParseControlPoints(const Json: TJSONArray; |
| 228 |
const NormalizeCurvesKeyValues: Boolean); |
| 229 |
var |
| 230 |
I: Integer; |
| 231 |
PreviousTime, NextTime: Single; |
| 232 |
PreviousValue, NextValue: TVector4; |
| 233 |
O: TJSONObject; |
| 234 |
begin |
| 235 |
Assert(Time.Count = Colors.Count); |
| 236 |
if Json.Count <> Time.Count then |
| 237 |
begin |
| 238 |
WritelnWarning('Invalid Spine JSON: Curve support requires that timeline JSON count is correct'); |
| 239 |
Curve := false; |
| 240 |
Exit; |
| 241 |
end; |
| 242 |
|
| 243 |
for I := 0 to Time.Count - 2 do |
| 244 |
begin |
| 245 |
PreviousTime := Time[I]; |
| 246 |
NextTime := Time[I + 1]; |
| 247 |
PreviousValue := Colors.L[I ]; |
| 248 |
NextValue := Colors.L[I + 1]; |
| 249 |
|
| 250 |
if not (Json[I] is TJSONObject) then |
| 251 |
begin |
| 252 |
WritelnWarning('Invalid Spine JSON: Curve support requires that all JSON timeline chldren are object'); |
| 253 |
Curve := false; |
| 254 |
Exit; |
| 255 |
end; |
| 256 |
|
| 257 |
O := TJSONObject(Json[I]); |
| 258 |
|
| 259 |
CurveControlPoints.Add(ReadCurveControlPoints('slot(color)', O, NormalizeCurvesKeyValues, 4, |
| 260 |
PreviousTime, NextTime, |
| 261 |
PreviousValue, NextValue)); |
| 262 |
end; |
| 263 |
|
| 264 |
CurveControlPoints.Add(Vector4(0, 0, 1, 1)); // TODO: add last dummy value, because our TCubicBezier* may expect it |
| 265 |
end; |
| 266 |
|
| 267 |
procedure TSlotTimelineColor.BuildNodes(const BaseUrl: String; |
| 268 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 269 |
var |
| 270 |
I: Integer; |
| 271 |
N: TColorInterpolatorNode; |
| 272 |
NA: TScalarInterpolatorNode; |
| 273 |
EventValueChanged1, EventValueChanged2: TX3DEvent; |
| 274 |
Route: TX3DRoute; |
| 275 |
begin |
| 276 |
(* TODO: colors on curve: |
| 277 |
if Curve then |
| 278 |
begin |
| 279 |
NC := TCubicBezierColorInterpolatorNode.Create('SlotTimeline_color_' + Slot.Name); |
| 280 |
for I := 0 to Colors.Count - 1 do |
| 281 |
begin |
| 282 |
NC.FdKeyValue.Items.Add(Colors[I]); |
| 283 |
NC.FdControlPoints.Items.Add(CurveControlPoints[I]); |
| 284 |
end; |
| 285 |
NC.OptimizeControlPoints; |
| 286 |
EventValueChanged := NC.EventValue_changed; |
| 287 |
Node := NC; |
| 288 |
end else |
| 289 |
*) |
| 290 |
begin |
| 291 |
N := TColorInterpolatorNode.Create('SlotTimeline_color_' + Slot.Name); |
| 292 |
for I := 0 to Colors.Count - 1 do |
| 293 |
N.FdKeyValue.Items.Add(Colors[I].XYZ); |
| 294 |
EventValueChanged1 := N.EventValue_changed; |
| 295 |
Node1.EventSet_Fraction := N.EventSet_Fraction; |
| 296 |
Node1.Node := N; |
| 297 |
Node1.Key := N.FdKey; |
| 298 |
|
| 299 |
NA := TScalarInterpolatorNode.Create('SlotTimeline_alpha_' + Slot.Name); |
| 300 |
for I := 0 to Colors.Count - 1 do |
| 301 |
NA.FdKeyValue.Items.Add(1 - Colors[I][3]); |
| 302 |
EventValueChanged2 := NA.EventValue_changed; |
| 303 |
Node2.EventSet_Fraction := NA.EventSet_Fraction; |
| 304 |
Node2.Node := NA; |
| 305 |
Node2.Key := NA.FdKey; |
| 306 |
HasNode2 := true; |
| 307 |
end; |
| 308 |
|
| 309 |
inherited; |
| 310 |
|
| 311 |
for I := 0 to Slot.Materials.Count - 1 do |
| 312 |
begin |
| 313 |
Route := TX3DRoute.Create; |
| 314 |
Route.SetSourceDirectly(EventValueChanged1); |
| 315 |
Route.SetDestinationDirectly(Slot.Materials[I].FdEmissiveColor); |
| 316 |
Container.AddRoute(Route); |
| 317 |
|
| 318 |
Route := TX3DRoute.Create; |
| 319 |
Route.SetSourceDirectly(EventValueChanged2); |
| 320 |
Route.SetDestinationDirectly(Slot.Materials[I].FdTransparency); |
| 321 |
Container.AddRoute(Route); |
| 322 |
end; |
| 323 |
|
| 324 |
{ When there's only 1 keyframe, Spine does something weird: instead of smooth |
| 325 |
interpolation, animation suddenly jumps from setup pose to given keyframe |
| 326 |
pose at given key time (unless animation is looping and it's the last frame, |
| 327 |
then it's visibly ignored). } |
| 328 |
if SpineVerboseWarnings and (Time.Count = 1) then |
| 329 |
WritelnWarning('Spine', Format(SSpine1KeyFrameWarning, ['slot', Slot.Name, 'color'])); |
| 330 |
end; |
| 331 |
|
| 332 |
{$endif} |