| 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 |
{ Animation utilities. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
{$endif} |
| 21 |
|
| 22 |
{$ifdef read_implementation} |
| 23 |
|
| 24 |
const |
| 25 |
SSpine1KeyFrameWarning = 'Timeline on %s "%s" (animation of %s) has only 1 keyframe. We support it in a consistent way (1 keyframe means that the state is constant), but Spine shows such timeline inconsistently. To avoid confusion, set at least 2 keyframes on every timeline.'; |
| 26 |
// SSpineMultipleTimelinesWarning = 'Multiple timelines affect %s "%s" animation of %s'; |
| 27 |
|
| 28 |
{ Safer version than CastleUtils.MapRangeTo01 that doesn't account for SourceBegin=SourceEnd case. } |
| 29 |
function MapRangeTo01(const SourceVal, SourceBegin, SourceEnd: Single): Single; |
| 30 |
begin |
| 31 |
if SameValue(SourceEnd, SourceBegin) then |
| 32 |
Result := 0 // whatever result is valid, just avoid returning NaN |
| 33 |
else |
| 34 |
Result := (SourceVal - SourceBegin) / (SourceEnd - SourceBegin); |
| 35 |
end; |
| 36 |
|
| 37 |
{ Read Bezier curve from Spine file. |
| 38 |
|
| 39 |
Spine docs that curve count should be 4, |
| 40 |
http://esotericsoftware.com/spine-json-format |
| 41 |
"""A Bézier curve has 4 elements which define the control points: cx1, cy1, cx2, cy2. The X axis is from 0 to 1 and represents the percent of time between the two keyframes. The Y axis is from 0 to 1 and represents the percent of the difference between the keyframe's values.""" |
| 42 |
But in reality it is now a **multiple of 4** (see below). |
| 43 |
|
| 44 |
@param(NormalizeKeyValues |
| 45 |
|
| 46 |
Since around Spine 4.0, Spine writes actual times in file (not times normalized to 0..1 range |
| 47 |
between previous and next frames) and actual values in file (not values normalized to 0..1 range |
| 48 |
between previous and next values). |
| 49 |
|
| 50 |
For now, we counteract this, by normalizing them back into 0..1 range, |
| 51 |
as that's what our TCubicBezierPositionInterpolatorNode and friends expect. |
| 52 |
TODO: In the future we should allow TCubicBezierPositionInterpolatorNode to use unnormalized |
| 53 |
values. |
| 54 |
|
| 55 |
The PreviousTime, NextTime, PreviousValue, NextValue are used for this normalization. |
| 56 |
) |
| 57 |
|
| 58 |
@param(VectorComponents |
| 59 |
|
| 60 |
Since around Spine 4.0, Spine write separate curve values for each vector component. |
| 61 |
E.g. to animate 2D translation, Spine actually writes 2 curves. |
| 62 |
To animate RGBA animation, Spine actually writes 4 curves. |
| 63 |
|
| 64 |
This is not documented yet in Spine docs. |
| 65 |
|
| 66 |
TODO: We do not support it yet, instead we check that keys are equal, |
| 67 |
and we average the values (percentage of values, actually; |
| 68 |
NormalizeKeyValues must be true for this). |
| 69 |
For a full support, we'd have to extend our interpolators to account for this. |
| 70 |
|
| 71 |
We only use VectorComponents from PreviousValue and NextValue. |
| 72 |
E.g. when VectorComponents = 2, we only look at PreviousValue.X, PreviousValue.Y, |
| 73 |
and we ignore PreviousValue.Z, PreviousValue.W. |
| 74 |
) |
| 75 |
} |
| 76 |
function ReadCurveControlPoints(const OwnerName: String; const Json: TJSONObject; |
| 77 |
const NormalizeKeyValues: Boolean; |
| 78 |
const VectorComponents: Cardinal; |
| 79 |
const PreviousTime, NextTime: Single; |
| 80 |
const PreviousValue, NextValue: TVector4): TVector4; |
| 81 |
|
| 82 |
function NormalizeTime(const T: Single): Single; |
| 83 |
begin |
| 84 |
if NormalizeKeyValues then |
| 85 |
Result := MapRangeTo01(T, PreviousTime, NextTime) |
| 86 |
else |
| 87 |
Result := T; |
| 88 |
end; |
| 89 |
|
| 90 |
function NormalizeValue(const V: Single; const Component: Cardinal): Single; |
| 91 |
begin |
| 92 |
if NormalizeKeyValues then |
| 93 |
Result := MapRangeTo01(V, PreviousValue[Component], NextValue[Component]) |
| 94 |
else |
| 95 |
Result := V; |
| 96 |
end; |
| 97 |
|
| 98 |
var |
| 99 |
CurveJson: TJSONData; |
| 100 |
CurveJsonArray: TJSONArray; |
| 101 |
C, J: Integer; |
| 102 |
NewControlPoints: TVector4; |
| 103 |
begin |
| 104 |
Result := Vector4(0, 0, 1, 1); // default value, in case curve changes from "curve" to default linear interpolation |
| 105 |
CurveJson := Json.Find('curve'); |
| 106 |
if CurveJson <> nil then |
| 107 |
begin |
| 108 |
if CurveJson is TJSONArray then |
| 109 |
begin |
| 110 |
CurveJsonArray := TJSONArray(CurveJson); |
| 111 |
C := TJSONArray(CurveJson).Count; |
| 112 |
if C = 4 then |
| 113 |
begin |
| 114 |
Result.X := NormalizeTime (CurveJsonArray.Floats[0]); |
| 115 |
Result.Y := NormalizeValue(CurveJsonArray.Floats[1], 0); |
| 116 |
Result.Z := NormalizeTime (CurveJsonArray.Floats[2]); |
| 117 |
Result.W := NormalizeValue(CurveJsonArray.Floats[3], 0); |
| 118 |
end else |
| 119 |
if C = 4 * VectorComponents then |
| 120 |
begin |
| 121 |
Result.X := NormalizeTime (CurveJsonArray.Floats[0]); |
| 122 |
Result.Y := NormalizeValue(CurveJsonArray.Floats[1], 0); |
| 123 |
Result.Z := NormalizeTime (CurveJsonArray.Floats[2]); |
| 124 |
Result.W := NormalizeValue(CurveJsonArray.Floats[3], 0); |
| 125 |
for J := 1 to VectorComponents - 1 do |
| 126 |
begin |
| 127 |
NewControlPoints.X := NormalizeTime (CurveJsonArray.Floats[J * 4 + 0]); |
| 128 |
NewControlPoints.Y := NormalizeValue(CurveJsonArray.Floats[J * 4 + 1], J); |
| 129 |
NewControlPoints.Z := NormalizeTime (CurveJsonArray.Floats[J * 4 + 2]); |
| 130 |
NewControlPoints.W := NormalizeValue(CurveJsonArray.Floats[J * 4 + 3], J); |
| 131 |
if (not SameValue(Result.X, NewControlPoints.X)) or |
| 132 |
(not SameValue(Result.Z, NewControlPoints.Z)) then |
| 133 |
begin |
| 134 |
WritelnWarning('Spine', 'Curve interpolation of "%s" has different keys for %d components', [ |
| 135 |
OwnerName, |
| 136 |
VectorComponents |
| 137 |
]); |
| 138 |
Exit; |
| 139 |
end; |
| 140 |
{ // Avoid this warning, too spamming. |
| 141 |
if (not SameValue(Result.Y, NewControlPoints.Y)) or |
| 142 |
(not SameValue(Result.W, NewControlPoints.W)) then |
| 143 |
WritelnWarning('Spine', 'Curve interpolation of "%s" has different values for %d components. Averaging.', [ |
| 144 |
OwnerName, |
| 145 |
VectorComponents |
| 146 |
]); |
| 147 |
} |
| 148 |
Result.Y := Result.Y + NewControlPoints.Y; |
| 149 |
Result.W := Result.W + NewControlPoints.Y; |
| 150 |
end; |
| 151 |
Result.Y := Result.Y / VectorComponents; |
| 152 |
Result.W := Result.W / VectorComponents; |
| 153 |
end else |
| 154 |
begin |
| 155 |
WritelnWarning('Spine', 'Curve interpolation of "%s" is an array with %d elements (expected 4 or %d)', [ |
| 156 |
OwnerName, |
| 157 |
C, |
| 158 |
4 * VectorComponents |
| 159 |
]); |
| 160 |
end; |
| 161 |
end; |
| 162 |
|
| 163 |
{ For now, silently ignore warning that we don't handle curve type |
| 164 |
'stepped'. Spine optimizes it's curves by using 'stepped' where |
| 165 |
previous and next values are equal, so this is common, and we would |
| 166 |
flood the warnings console for many models because of this (while in fact |
| 167 |
models are handled Ok since 'stepped' is only for optimization). } |
| 168 |
{ else |
| 169 |
if CurveJson.AsString <> 'linear' then |
| 170 |
WritelnWarning('Spine', 'Found "' + CurveJson.AsString + '" interpolation type on slot timeline of slot ' + Slot.Name + ', we do not support this interpolation type'); |
| 171 |
} |
| 172 |
end; |
| 173 |
end; |
| 174 |
|
| 175 |
{ Used by deform. In Spine 4, only time is required to normalize back to 0..1 range, |
| 176 |
while value is still in 0..1 range } |
| 177 |
function ReadCurveControlPointsTimeOnly(const Json: TJSONObject; |
| 178 |
const NormalizeKeyValues: Boolean; |
| 179 |
const PreviousTime, NextTime: Single): TVector4; |
| 180 |
|
| 181 |
function NormalizeTime(const T: Single): Single; |
| 182 |
begin |
| 183 |
if NormalizeKeyValues then |
| 184 |
Result := MapRangeTo01(T, PreviousTime, NextTime) |
| 185 |
else |
| 186 |
Result := T; |
| 187 |
end; |
| 188 |
|
| 189 |
var |
| 190 |
CurveJson: TJSONData; |
| 191 |
CurveJsonArray: TJSONArray; |
| 192 |
begin |
| 193 |
Result := Vector4(0, 0, 1, 1); // default value, in case curve changes from "curve" to default linear interpolation |
| 194 |
CurveJson := Json.Find('curve'); |
| 195 |
if CurveJson <> nil then |
| 196 |
begin |
| 197 |
if CurveJson is TJSONArray then |
| 198 |
begin |
| 199 |
CurveJsonArray := TJSONArray(CurveJson); |
| 200 |
Result.X := NormalizeTime (CurveJsonArray.Floats[0]); |
| 201 |
Result.Y := CurveJsonArray.Floats[1]; |
| 202 |
Result.Z := NormalizeTime (CurveJsonArray.Floats[2]); |
| 203 |
Result.W := CurveJsonArray.Floats[3]; |
| 204 |
end; |
| 205 |
end; |
| 206 |
end; |
| 207 |
|
| 208 |
{$endif} |