| 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 bone timelines. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
TBoneTimeline = class abstract |
| 21 |
strict private |
| 22 |
FMaxTime: Single; |
| 23 |
protected |
| 24 |
IsBackup: Boolean; |
| 25 |
public |
| 26 |
Bone: TBone; |
| 27 |
Time: TSingleList; |
| 28 |
BackupTime: TSingleList; |
| 29 |
Curve: boolean; |
| 30 |
CurveControlPoints: TVector4List; |
| 31 |
Node: TAbstractInterpolatorNode; |
| 32 |
NodeUsedAsChild: boolean; |
| 33 |
property MaxTime: Single read FMaxTime; |
| 34 |
constructor Create; |
| 35 |
destructor Destroy; override; |
| 36 |
procedure Parse(const Json: TJSONArray; |
| 37 |
const NormalizeCurvesKeyValues: Boolean); virtual; |
| 38 |
procedure ParseSingleValue(const Json: TJSONObject); virtual; abstract; |
| 39 |
procedure BuildNodes(const BaseUrl: String; |
| 40 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); virtual; |
| 41 |
function DoingSomething: boolean; virtual; abstract; |
| 42 |
procedure ParseControlPoints(const Json: TJSONArray; |
| 43 |
const NormalizeCurvesKeyValues: Boolean); virtual; abstract; |
| 44 |
procedure Backup; virtual; |
| 45 |
procedure Restore; virtual; |
| 46 |
end; |
| 47 |
|
| 48 |
TBoneTimelineVector2 = class(TBoneTimeline) |
| 49 |
public |
| 50 |
{ Position or scale values on the timeline. |
| 51 |
This always has the same length as @link(Time) list. } |
| 52 |
Vectors: TVector2List; |
| 53 |
BackupVectors: TVector2List; |
| 54 |
constructor Create; |
| 55 |
destructor Destroy; override; |
| 56 |
procedure Backup; override; |
| 57 |
procedure Restore; override; |
| 58 |
procedure ParseControlPoints(const Json: TJSONArray; |
| 59 |
const NormalizeCurvesKeyValues: Boolean); override; |
| 60 |
end; |
| 61 |
|
| 62 |
TBoneTimelineTranslate = class(TBoneTimelineVector2) |
| 63 |
procedure ParseSingleValue(const Json: TJSONObject); override; |
| 64 |
procedure BuildNodes(const BaseUrl: String; |
| 65 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); override; |
| 66 |
function DoingSomething: boolean; override; |
| 67 |
end; |
| 68 |
|
| 69 |
TBoneTimelineScale = class(TBoneTimelineVector2) |
| 70 |
procedure ParseSingleValue(const Json: TJSONObject); override; |
| 71 |
procedure BuildNodes(const BaseUrl: String; |
| 72 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); override; |
| 73 |
function DoingSomething: boolean; override; |
| 74 |
end; |
| 75 |
|
| 76 |
TBoneTimelineRotate = class(TBoneTimeline) |
| 77 |
{ Angle values on the timeline. |
| 78 |
This always has the same length as @link(Time) list. } |
| 79 |
Angles: TSingleList; |
| 80 |
BackupAngles: TSingleList; |
| 81 |
constructor Create; |
| 82 |
destructor Destroy; override; |
| 83 |
procedure ParseSingleValue(const Json: TJSONObject); override; |
| 84 |
procedure BuildNodes(const BaseUrl: String; |
| 85 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); override; |
| 86 |
function DoingSomething: boolean; override; |
| 87 |
procedure Backup; override; |
| 88 |
procedure Restore; override; |
| 89 |
procedure ParseControlPoints(const Json: TJSONArray; |
| 90 |
const NormalizeCurvesKeyValues: Boolean); override; |
| 91 |
end; |
| 92 |
|
| 93 |
TBoneTimelineList = class({$ifdef FPC}specialize{$endif} TObjectList<TBoneTimeline>) |
| 94 |
end; |
| 95 |
{$endif} |
| 96 |
|
| 97 |
{$ifdef read_implementation} |
| 98 |
|
| 99 |
{ TBoneTimeline -------------------------------------------------------------- } |
| 100 |
|
| 101 |
constructor TBoneTimeline.Create; |
| 102 |
begin |
| 103 |
inherited; |
| 104 |
Time := TSingleList.Create; |
| 105 |
BackupTime := TSingleList.Create; |
| 106 |
CurveControlPoints := TVector4List.Create; |
| 107 |
end; |
| 108 |
|
| 109 |
destructor TBoneTimeline.Destroy; |
| 110 |
begin |
| 111 |
FreeAndNil(Time); |
| 112 |
FreeAndNil(BackupTime); |
| 113 |
FreeAndNil(CurveControlPoints); |
| 114 |
inherited; |
| 115 |
end; |
| 116 |
|
| 117 |
procedure TBoneTimeline.Parse(const Json: TJSONArray; |
| 118 |
const NormalizeCurvesKeyValues: Boolean); |
| 119 |
var |
| 120 |
I: Integer; |
| 121 |
O: TJSONObject; |
| 122 |
NextTime: Single; |
| 123 |
begin |
| 124 |
Curve := false; |
| 125 |
|
| 126 |
for I := 0 to Json.Count - 1 do |
| 127 |
if Json[I] is TJSONObject then |
| 128 |
begin |
| 129 |
O := TJSONObject(Json[I]); |
| 130 |
|
| 131 |
NextTime := O.Get('time', 0.0); |
| 132 |
if (Time.Count <> 0) and (Time.Last > NextTime) then |
| 133 |
raise ESpineReadError.Create('Timeline must have increasing time values'); |
| 134 |
Time.Add(NextTime); |
| 135 |
FMaxTime := NextTime; |
| 136 |
|
| 137 |
// only detect whether we have a curve at this point |
| 138 |
if not Curve then |
| 139 |
Curve := O.Find('curve') is TJSONArray; |
| 140 |
|
| 141 |
ParseSingleValue(O); |
| 142 |
end; |
| 143 |
|
| 144 |
if Curve then |
| 145 |
ParseControlPoints(Json, NormalizeCurvesKeyValues); |
| 146 |
|
| 147 |
//Writeln('got timeline for bone ', Bone.Name, ' with ', Time.Count, ' items'); |
| 148 |
end; |
| 149 |
|
| 150 |
procedure TBoneTimeline.BuildNodes(const BaseUrl: String; |
| 151 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 152 |
var |
| 153 |
I: Integer; |
| 154 |
begin |
| 155 |
{ We assume that descendant already created Node in overridden BuildNodes } |
| 156 |
Node.FdKey.Items.Clear; |
| 157 |
for I := 0 to Time.Count - 1 do |
| 158 |
Node.FdKey.Items.Add(Time[I] / MaxAnimationTime); |
| 159 |
|
| 160 |
NodeUsedAsChild := true; |
| 161 |
Container.AddChildren(Node); |
| 162 |
end; |
| 163 |
|
| 164 |
procedure TBoneTimeline.Backup; |
| 165 |
begin |
| 166 |
end; |
| 167 |
|
| 168 |
procedure TBoneTimeline.Restore; |
| 169 |
begin |
| 170 |
end; |
| 171 |
|
| 172 |
{ TBoneTimelineVector2 ------------------------------------------------------- } |
| 173 |
|
| 174 |
constructor TBoneTimelineVector2.Create; |
| 175 |
begin |
| 176 |
inherited Create; |
| 177 |
Vectors := TVector2List.Create; |
| 178 |
BackupVectors := TVector2List.Create; |
| 179 |
end; |
| 180 |
|
| 181 |
destructor TBoneTimelineVector2.Destroy; |
| 182 |
begin |
| 183 |
FreeAndNil(Vectors); |
| 184 |
FreeAndNil(BackupVectors); |
| 185 |
inherited; |
| 186 |
end; |
| 187 |
|
| 188 |
procedure TBoneTimelineVector2.Backup; |
| 189 |
begin |
| 190 |
if not IsBackup then |
| 191 |
begin |
| 192 |
BackupTime.Clear; |
| 193 |
BackupVectors.Clear; |
| 194 |
BackupTime.AddRange(Time); |
| 195 |
BackupVectors.AddRange(Vectors); |
| 196 |
IsBackup := True; |
| 197 |
end; |
| 198 |
end; |
| 199 |
|
| 200 |
procedure TBoneTimelineVector2.Restore; |
| 201 |
begin |
| 202 |
if IsBackup then |
| 203 |
begin |
| 204 |
Time.Clear; |
| 205 |
Vectors.Clear; |
| 206 |
Time.AddRange(BackupTime); |
| 207 |
Vectors.AddRange(BackupVectors); |
| 208 |
IsBackup := False; |
| 209 |
end; |
| 210 |
end; |
| 211 |
|
| 212 |
procedure TBoneTimelineVector2.ParseControlPoints(const Json: TJSONArray; |
| 213 |
const NormalizeCurvesKeyValues: Boolean); |
| 214 |
var |
| 215 |
I: Integer; |
| 216 |
PreviousTime, NextTime: Single; |
| 217 |
PreviousValue, NextValue: TVector4; |
| 218 |
O: TJSONObject; |
| 219 |
begin |
| 220 |
Assert(Time.Count = Vectors.Count); |
| 221 |
if Json.Count <> Time.Count then |
| 222 |
begin |
| 223 |
WritelnWarning('Invalid Spine JSON: Curve support requires that timeline JSON count is correct'); |
| 224 |
Curve := false; |
| 225 |
Exit; |
| 226 |
end; |
| 227 |
|
| 228 |
for I := 0 to Time.Count - 2 do |
| 229 |
begin |
| 230 |
PreviousTime := Time[I]; |
| 231 |
NextTime := Time[I + 1]; |
| 232 |
PreviousValue := Vector4(Vectors.L[I ].X, Vectors.L[I ].Y, 0, 0); |
| 233 |
NextValue := Vector4(Vectors.L[I + 1].X, Vectors.L[I + 1].Y, 0, 0); |
| 234 |
|
| 235 |
if not (Json[I] is TJSONObject) then |
| 236 |
begin |
| 237 |
WritelnWarning('Invalid Spine JSON: Curve support requires that all JSON timeline chldren are object'); |
| 238 |
Curve := false; |
| 239 |
Exit; |
| 240 |
end; |
| 241 |
|
| 242 |
O := TJSONObject(Json[I]); |
| 243 |
|
| 244 |
CurveControlPoints.Add(ReadCurveControlPoints('bone(2D)', O, NormalizeCurvesKeyValues, 2, |
| 245 |
PreviousTime, NextTime, |
| 246 |
PreviousValue, NextValue)); |
| 247 |
end; |
| 248 |
|
| 249 |
CurveControlPoints.Add(Vector4(0, 0, 1, 1)); // TODO: add last dummy value, because our TCubicBezier* may expect it |
| 250 |
end; |
| 251 |
|
| 252 |
{ TBoneTimelineTranslate ----------------------------------------------------- } |
| 253 |
|
| 254 |
procedure TBoneTimelineTranslate.ParseSingleValue(const Json: TJSONObject); |
| 255 |
begin |
| 256 |
Vectors.Add(Vector2( |
| 257 |
Json.Get('x', 0.0), |
| 258 |
Json.Get('y', 0.0))); |
| 259 |
end; |
| 260 |
|
| 261 |
procedure TBoneTimelineTranslate.BuildNodes(const BaseUrl: String; |
| 262 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 263 |
var |
| 264 |
I: Integer; |
| 265 |
N: TPositionInterpolatorNode; |
| 266 |
NC: TCubicBezierPositionInterpolatorNode; |
| 267 |
EventValueChanged: TX3DEvent; |
| 268 |
Route: TX3DRoute; |
| 269 |
begin |
| 270 |
if Curve then |
| 271 |
begin |
| 272 |
NC := TCubicBezierPositionInterpolatorNode.Create('BoneTimeline_translate_' + Bone.Name); |
| 273 |
for I := 0 to Vectors.Count - 1 do |
| 274 |
begin |
| 275 |
NC.FdKeyValue.Items.Add(Vector3( |
| 276 |
{ add setup pose transform, as bone timeline is relative to setup pose } |
| 277 |
Bone.Node.FdTranslation.Value[0] + Vectors[I][0], |
| 278 |
Bone.Node.FdTranslation.Value[1] + Vectors[I][1], 0)); |
| 279 |
end; |
| 280 |
NC.SetControlPoints(CurveControlPoints); |
| 281 |
NC.OptimizeControlPoints; |
| 282 |
EventValueChanged := NC.EventValue_changed; |
| 283 |
Node := NC; |
| 284 |
//WritelnLog('Spine', 'Using curve for translation on bone ' + Bone.Name); |
| 285 |
end else |
| 286 |
begin |
| 287 |
N := TPositionInterpolatorNode.Create('BoneTimeline_translate_' + Bone.Name); |
| 288 |
for I := 0 to Vectors.Count - 1 do |
| 289 |
N.FdKeyValue.Items.Add(Vector3( |
| 290 |
{ add setup pose transform, as bone timeline is relative to setup pose } |
| 291 |
Bone.Node.FdTranslation.Value[0] + Vectors[I][0], |
| 292 |
Bone.Node.FdTranslation.Value[1] + Vectors[I][1], 0)); |
| 293 |
EventValueChanged := N.EventValue_changed; |
| 294 |
Node := N; |
| 295 |
end; |
| 296 |
|
| 297 |
inherited; |
| 298 |
|
| 299 |
Route := TX3DRoute.Create; |
| 300 |
Route.SetSourceDirectly(EventValueChanged); |
| 301 |
Route.SetDestinationDirectly(Bone.Node.FdTranslation.EventIn); |
| 302 |
Container.AddRoute(Route); |
| 303 |
|
| 304 |
{ When there's only 1 keyframe, Spine does something weird: instead of smooth |
| 305 |
interpolation, animation suddenly jumps from setup pose to given keyframe |
| 306 |
pose at given key time (unless animation is looping and it's the last frame, |
| 307 |
then it's visibly ignored). } |
| 308 |
if SpineVerboseWarnings and (Time.Count = 1) then |
| 309 |
WritelnWarning('Spine', Format(SSpine1KeyFrameWarning, ['bone', Bone.Name, 'translation'])); |
| 310 |
end; |
| 311 |
|
| 312 |
function TBoneTimelineTranslate.DoingSomething: boolean; |
| 313 |
var |
| 314 |
I: Integer; |
| 315 |
begin |
| 316 |
for I := 0 to Vectors.Count - 1 do |
| 317 |
if not Vectors[I].IsPerfectlyZero then |
| 318 |
Exit(true); |
| 319 |
Result := false; |
| 320 |
end; |
| 321 |
|
| 322 |
{ TBoneTimelineScale --------------------------------------------------------- } |
| 323 |
|
| 324 |
procedure TBoneTimelineScale.ParseSingleValue(const Json: TJSONObject); |
| 325 |
begin |
| 326 |
Vectors.Add(Vector2( |
| 327 |
Json.Get('x', 1.0), |
| 328 |
Json.Get('y', 1.0))); |
| 329 |
end; |
| 330 |
|
| 331 |
procedure TBoneTimelineScale.BuildNodes(const BaseUrl: String; |
| 332 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 333 |
|
| 334 |
{ Combine initial node scale and 2D key scale into a final scale. } |
| 335 |
function FinalScale(const InitialScale: TVector3; |
| 336 |
const KeyScale: TVector2): TVector3; |
| 337 |
begin |
| 338 |
{ multiply setup pose transformation (scale) by key scale, |
| 339 |
as bone timeline is relative to setup pose } |
| 340 |
Result.X := InitialScale.X * KeyScale.X; |
| 341 |
Result.Y := InitialScale.Y * KeyScale.Y; |
| 342 |
{ This must be 1.0, not something like (Result.X + Result.Y) / 2, |
| 343 |
since scaling in Z will move the bone in front/back of the other bones. } |
| 344 |
Result.Z := 1.0; |
| 345 |
end; |
| 346 |
|
| 347 |
var |
| 348 |
I: Integer; |
| 349 |
N: TPositionInterpolatorNode; |
| 350 |
NC: TCubicBezierPositionInterpolatorNode; |
| 351 |
EventValueChanged: TX3DEvent; |
| 352 |
Route: TX3DRoute; |
| 353 |
begin |
| 354 |
if Curve then |
| 355 |
begin |
| 356 |
NC := TCubicBezierPositionInterpolatorNode.Create('BoneTimeline_scale_' + Bone.Name); |
| 357 |
for I := 0 to Vectors.Count - 1 do |
| 358 |
begin |
| 359 |
NC.FdKeyValue.Items.Add(FinalScale(Bone.Node.FdScale.Value, Vectors[I])); |
| 360 |
end; |
| 361 |
NC.SetControlPoints(CurveControlPoints); |
| 362 |
NC.OptimizeControlPoints; |
| 363 |
EventValueChanged := NC.EventValue_changed; |
| 364 |
Node := NC; |
| 365 |
//WritelnLog('Spine', 'Using curve for scale on bone ' + Bone.Name); |
| 366 |
end else |
| 367 |
begin |
| 368 |
N := TPositionInterpolatorNode.Create('BoneTimeline_scale_' + Bone.Name); |
| 369 |
for I := 0 to Vectors.Count - 1 do |
| 370 |
N.FdKeyValue.Items.Add(FinalScale(Bone.Node.FdScale.Value, Vectors[I])); |
| 371 |
EventValueChanged := N.EventValue_changed; |
| 372 |
Node := N; |
| 373 |
end; |
| 374 |
inherited; |
| 375 |
|
| 376 |
Route := TX3DRoute.Create; |
| 377 |
Route.SetSourceDirectly(EventValueChanged); |
| 378 |
Route.SetDestinationDirectly(Bone.Node.FdScale.EventIn); |
| 379 |
Container.AddRoute(Route); |
| 380 |
|
| 381 |
{ When there's only 1 keyframe, Spine does something weird: instead of smooth |
| 382 |
interpolation, animation suddenly jumps from setup pose to given keyframe |
| 383 |
pose at given key time (unless animation is looping and it's the last frame, |
| 384 |
then it's visibly ignored). } |
| 385 |
if SpineVerboseWarnings and (Time.Count = 1) then |
| 386 |
WritelnWarning('Spine', Format(SSpine1KeyFrameWarning, ['bone', Bone.Name, 'scale'])); |
| 387 |
end; |
| 388 |
|
| 389 |
function TBoneTimelineScale.DoingSomething: boolean; |
| 390 |
var |
| 391 |
I: Integer; |
| 392 |
begin |
| 393 |
for I := 0 to Vectors.Count - 1 do |
| 394 |
if (Vectors[I].X <> 1) or |
| 395 |
(Vectors[I].Y <> 1) then |
| 396 |
Exit(true); |
| 397 |
Result := false; |
| 398 |
end; |
| 399 |
|
| 400 |
{ TBoneTimelineRotate -------------------------------------------------------- } |
| 401 |
|
| 402 |
constructor TBoneTimelineRotate.Create; |
| 403 |
begin |
| 404 |
inherited; |
| 405 |
Angles := TSingleList.Create; |
| 406 |
BackupAngles := TSingleList.Create; |
| 407 |
end; |
| 408 |
|
| 409 |
destructor TBoneTimelineRotate.Destroy; |
| 410 |
begin |
| 411 |
FreeAndNil(Angles); |
| 412 |
FreeAndNil(BackupAngles); |
| 413 |
inherited; |
| 414 |
end; |
| 415 |
|
| 416 |
procedure TBoneTimelineRotate.ParseSingleValue(const Json: TJSONObject); |
| 417 |
var |
| 418 |
Angle: Single; |
| 419 |
begin |
| 420 |
// around Spine >= 4.0 they changed the name of this to "value" |
| 421 |
if Json.IndexOfName('angle') <> -1 then |
| 422 |
Angle := Json.Get('angle', 0.0) |
| 423 |
else |
| 424 |
Angle := Json.Get('value', 0.0); |
| 425 |
Angles.Add(Angle); |
| 426 |
end; |
| 427 |
|
| 428 |
procedure TBoneTimelineRotate.BuildNodes(const BaseUrl: String; |
| 429 |
const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 430 |
var |
| 431 |
I: Integer; |
| 432 |
N: TOrientationInterpolator2DNode; |
| 433 |
NC: TCubicBezier2DOrientationInterpolatorNode; |
| 434 |
EventValueChanged: TX3DEvent; |
| 435 |
Route: TX3DRoute; |
| 436 |
AngleValue: Single; |
| 437 |
begin |
| 438 |
if Curve then |
| 439 |
begin |
| 440 |
NC := TCubicBezier2DOrientationInterpolatorNode.Create('BoneTimeline_rotate_' + Bone.Name); |
| 441 |
NC.FdAxis.Value := Vector3(0, 0, 1); |
| 442 |
for I := 0 to Angles.Count - 1 do |
| 443 |
begin |
| 444 |
{ add setup pose transform, as bone timeline is relative to setup pose } |
| 445 |
AngleValue := Bone.Node.FdRotation.Value[3] + DegToRad(Angles[I]); |
| 446 |
NC.FdKeyValue.Items.Add(AngleValue); |
| 447 |
NC.FdControlPoints.Items.Add(CurveControlPoints[I]); |
| 448 |
end; |
| 449 |
NC.OptimizeControlPoints; |
| 450 |
EventValueChanged := NC.EventValue_changed; |
| 451 |
Node := NC; |
| 452 |
//WritelnLog('Spine', 'Using curve for rotation on bone ' + Bone.Name); |
| 453 |
end else |
| 454 |
begin |
| 455 |
{ We use TOrientationInterpolator2DNode instead of |
| 456 |
TOrientationInterpolatorNode with constant axis. |
| 457 |
This is not only for optimization (TOrientationInterpolator2DNode |
| 458 |
does trivial lerp using AngleLerp, instead of SLerp on 3D rotations), |
| 459 |
it's also for correctness: TOrientationInterpolatorNode could choose |
| 460 |
to interpolate through a 3D space sometimes, it seems. |
| 461 |
|
| 462 |
Testcase: Unholy exorcist going_on_ladder_up animation hand movement. |
| 463 |
It could do unexpected interpolation using slerp: |
| 464 |
(this was before SLerp was hacked to specially honor case when Rot1Axis equals Rot2Axis). |
| 465 |
|
| 466 |
uses CastleVectors, CastleQuaternions; |
| 467 |
const |
| 468 |
Steps = 10; |
| 469 |
var |
| 470 |
V: TVector4; |
| 471 |
I: Integer; |
| 472 |
begin |
| 473 |
for I := 0 to Steps - 1 do |
| 474 |
begin |
| 475 |
V := SLerp(I / (Steps - 1), |
| 476 |
Vector4(0, 0, 1, -1.6584116220474243), |
| 477 |
Vector4(0, 0, 1, 4.6247735023498535) |
| 478 |
); |
| 479 |
Writeln(V.ToString); |
| 480 |
end; |
| 481 |
end. |
| 482 |
} |
| 483 |
N := TOrientationInterpolator2DNode.Create('BoneTimeline_rotate_' + Bone.Name); |
| 484 |
for I := 0 to Angles.Count - 1 do |
| 485 |
begin |
| 486 |
{ add setup pose transform, as bone timeline is relative to setup pose } |
| 487 |
AngleValue := Bone.Node.FdRotation.Value[3] + DegToRad(Angles[I]); |
| 488 |
N.FdKeyValue.Items.Add(AngleValue); |
| 489 |
end; |
| 490 |
EventValueChanged := N.EventValue_changed; |
| 491 |
Node := N; |
| 492 |
end; |
| 493 |
|
| 494 |
inherited; |
| 495 |
|
| 496 |
Route := TX3DRoute.Create; |
| 497 |
Route.SetSourceDirectly(EventValueChanged); |
| 498 |
Route.SetDestinationDirectly(Bone.Node.FdRotation.EventIn); |
| 499 |
Container.AddRoute(Route); |
| 500 |
|
| 501 |
{ When there's only 1 keyframe, Spine does something weird: instead of smooth |
| 502 |
interpolation, animation suddenly jumps from setup pose to given keyframe |
| 503 |
pose at given key time (unless animation is looping and it's the last frame, |
| 504 |
then it's visibly ignored). } |
| 505 |
if SpineVerboseWarnings and (Time.Count = 1) then |
| 506 |
WritelnWarning('Spine', Format(SSpine1KeyFrameWarning, ['bone', Bone.Name, 'rotation'])); |
| 507 |
end; |
| 508 |
|
| 509 |
function TBoneTimelineRotate.DoingSomething: boolean; |
| 510 |
var |
| 511 |
I: Integer; |
| 512 |
begin |
| 513 |
for I := 0 to Angles.Count - 1 do |
| 514 |
if Angles[I] <> 0 then |
| 515 |
Exit(true); |
| 516 |
Result := false; |
| 517 |
end; |
| 518 |
|
| 519 |
procedure TBoneTimelineRotate.Backup; |
| 520 |
begin |
| 521 |
if not IsBackup then |
| 522 |
begin |
| 523 |
BackupTime.Clear; |
| 524 |
BackupAngles.Clear; |
| 525 |
BackupTime.AddRange(Time); |
| 526 |
BackupAngles.AddRange(Angles); |
| 527 |
IsBackup := True; |
| 528 |
end; |
| 529 |
end; |
| 530 |
|
| 531 |
procedure TBoneTimelineRotate.Restore; |
| 532 |
begin |
| 533 |
if IsBackup then |
| 534 |
begin |
| 535 |
Time.Clear; |
| 536 |
Angles.Clear; |
| 537 |
Time.AddRange(BackupTime); |
| 538 |
Angles.AddRange(BackupAngles); |
| 539 |
IsBackup := False; |
| 540 |
end; |
| 541 |
end; |
| 542 |
|
| 543 |
procedure TBoneTimelineRotate.ParseControlPoints(const Json: TJSONArray; |
| 544 |
const NormalizeCurvesKeyValues: Boolean); |
| 545 |
var |
| 546 |
I: Integer; |
| 547 |
PreviousTime, NextTime: Single; |
| 548 |
PreviousValue, NextValue: TVector4; |
| 549 |
O: TJSONObject; |
| 550 |
begin |
| 551 |
Assert(Time.Count = Angles.Count); |
| 552 |
if Json.Count <> Time.Count then |
| 553 |
begin |
| 554 |
WritelnWarning('Invalid Spine JSON: Curve support requires that timeline JSON count is correct'); |
| 555 |
Curve := false; |
| 556 |
Exit; |
| 557 |
end; |
| 558 |
|
| 559 |
for I := 0 to Time.Count - 2 do |
| 560 |
begin |
| 561 |
PreviousTime := Time[I]; |
| 562 |
NextTime := Time[I + 1]; |
| 563 |
PreviousValue := Vector4(Angles.L[I ], 0, 0, 0); |
| 564 |
NextValue := Vector4(Angles.L[I + 1], 0, 0, 0); |
| 565 |
|
| 566 |
if not (Json[I] is TJSONObject) then |
| 567 |
begin |
| 568 |
WritelnWarning('Invalid Spine JSON: Curve support requires that all JSON timeline chldren are object'); |
| 569 |
Curve := false; |
| 570 |
Exit; |
| 571 |
end; |
| 572 |
|
| 573 |
O := TJSONObject(Json[I]); |
| 574 |
|
| 575 |
CurveControlPoints.Add(ReadCurveControlPoints('bone(rotation)', O, NormalizeCurvesKeyValues, 1, |
| 576 |
PreviousTime, NextTime, |
| 577 |
PreviousValue, NextValue)); |
| 578 |
end; |
| 579 |
|
| 580 |
CurveControlPoints.Add(Vector4(0, 0, 1, 1)); // TODO: add last dummy value, because our TCubicBezier* may expect it |
| 581 |
end; |
| 582 |
|
| 583 |
{$endif} |