| 1 |
{%MainUnit x3dloadinternalspine.pas} |
| 2 |
{ |
| 3 |
Copyright 2020 Trung Le (kagamma). |
| 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 weighted mesh timelines. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
TWeightedMeshVertexList = class({$ifdef FPC}specialize{$endif} TObjectList<TSingleList>) |
| 21 |
end; |
| 22 |
|
| 23 |
TWeightedMeshTimeline = class |
| 24 |
Name: String; |
| 25 |
Attachment: TMeshAttachment; |
| 26 |
AffectedBoneTimelines: TBoneTimelineList; |
| 27 |
AffectedDeformTimeline: TDeformTimeline; |
| 28 |
Vertices: TWeightedMeshVertexList; |
| 29 |
Time: TSingleList; |
| 30 |
Node: TCoordinateInterpolatorNode; |
| 31 |
constructor Create; |
| 32 |
destructor Destroy; override; |
| 33 |
procedure Parse; |
| 34 |
procedure BuildNodes(const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 35 |
end; |
| 36 |
|
| 37 |
TWeightedMeshTimelineList = class({$ifdef FPC}specialize{$endif} TObjectList<TWeightedMeshTimeline>) |
| 38 |
end; |
| 39 |
{$endif} |
| 40 |
|
| 41 |
{$ifdef read_implementation} |
| 42 |
|
| 43 |
constructor TWeightedMeshTimeline.Create; |
| 44 |
begin |
| 45 |
inherited; |
| 46 |
Time := TSingleList.Create; |
| 47 |
Vertices := TWeightedMeshVertexList.Create; |
| 48 |
end; |
| 49 |
|
| 50 |
destructor TWeightedMeshTimeline.Destroy; |
| 51 |
begin |
| 52 |
FreeAndNil(Vertices); |
| 53 |
FreeAndNil(Time); |
| 54 |
inherited; |
| 55 |
end; |
| 56 |
|
| 57 |
{ We now performing 'parse': |
| 58 |
- Find all possible timeframes by combining timeframes from every bone/deform timelines |
| 59 |
- Perform interpolation for those bone/deform timelines that doesnt have necesssary timeframes |
| 60 |
- Perform vertex calculation |
| 61 |
} |
| 62 |
procedure TWeightedMeshTimeline.Parse; |
| 63 |
{ Example: |
| 64 |
- Time1: [0, 0.2, 0.65, 1] |
| 65 |
- Time2: [0, 0.3, 0.65, 0.7. 1] |
| 66 |
* Add additional steps to make the animation more accurate, default is 0.15 |
| 67 |
Since we are doing animation by using CoordinateInterpolator, in many |
| 68 |
situations, like rotation or non-linear interpolation types, the result is |
| 69 |
not accurate. By adding more steps inbetween we we can achieve a more accurate |
| 70 |
result, as a cost of longer loading time and more memory usage |
| 71 |
- Result: [0, 0.15, 0.2, 0.3, 0.45, 0.6, 0.65, 0.7, 0.75, 0.8, 0.9, 1] |
| 72 |
} |
| 73 |
procedure CombineTimeframes; |
| 74 |
var |
| 75 |
I, J: Integer; |
| 76 |
Value: Single; |
| 77 |
IsRotationRequired: Boolean; |
| 78 |
IsCurved: Boolean; |
| 79 |
IsTimeframeTooLow: Boolean; |
| 80 |
begin |
| 81 |
IsRotationRequired := False; |
| 82 |
IsCurved := False; |
| 83 |
// For bone timelines |
| 84 |
for I := 0 to AffectedBoneTimelines.Count - 1 do |
| 85 |
begin |
| 86 |
if AffectedBoneTimelines[I] is TBoneTimelineRotate then |
| 87 |
IsRotationRequired := True; |
| 88 |
if AffectedBoneTimelines[I].Curve then |
| 89 |
IsCurved := True; |
| 90 |
for J := 0 to AffectedBoneTimelines[I].Time.Count - 1 do |
| 91 |
begin |
| 92 |
Value := AffectedBoneTimelines[I].Time[J]; |
| 93 |
if Time.IndexOf(Value) < 0 then |
| 94 |
begin |
| 95 |
Time.Add(Value); |
| 96 |
end; |
| 97 |
end; |
| 98 |
end; |
| 99 |
// For deform timeline |
| 100 |
if AffectedDeformTimeline <> nil then |
| 101 |
begin |
| 102 |
if AffectedDeformTimeline.Curve then |
| 103 |
IsCurved := True; |
| 104 |
for I := 0 to AffectedDeformTimeline.Time.Count - 1 do |
| 105 |
begin |
| 106 |
Value := AffectedDeformTimeline.Time[I]; |
| 107 |
if Time.IndexOf(Value) < 0 then |
| 108 |
begin |
| 109 |
Time.Add(Value); |
| 110 |
end; |
| 111 |
end; |
| 112 |
end; |
| 113 |
Time.Sort; |
| 114 |
// Make the animation more accurate by adding more steps inbetween |
| 115 |
// Default step is 0.15 |
| 116 |
if IsCurved or IsRotationRequired or (AffectedBoneTimelines.Count > 0) then |
| 117 |
begin |
| 118 |
Value := 0; |
| 119 |
while Value < Time[Time.Count - 1] do |
| 120 |
begin |
| 121 |
IsTimeframeTooLow := False; |
| 122 |
for I := 0 to Time.Count - 1 do |
| 123 |
begin |
| 124 |
if Abs(Time[I] - Value) < 0.15 then |
| 125 |
begin |
| 126 |
IsTimeframeTooLow := True; |
| 127 |
Break; |
| 128 |
end; |
| 129 |
end; |
| 130 |
if not IsTimeframeTooLow then |
| 131 |
Time.Insert(0, Value); |
| 132 |
Value := Value + 0.15; |
| 133 |
end; |
| 134 |
Time.Sort; |
| 135 |
end; |
| 136 |
end; |
| 137 |
|
| 138 |
procedure InterpolateTimeframes; |
| 139 |
var |
| 140 |
I, J, K: Integer; |
| 141 |
P, T1, T2, OldT1, OldT2: Integer; |
| 142 |
BoneTimeline: TBoneTimeline; |
| 143 |
BoneTimelineTranslate: TBoneTimelineTranslate absolute BoneTimeline; |
| 144 |
BoneTimelineScale: TBoneTimelineScale absolute BoneTimeline; |
| 145 |
BoneTimelineRotate: TBoneTimelineRotate absolute BoneTimeline; |
| 146 |
Timeframe, Timeframe1, Timeframe2: Single; |
| 147 |
IsExists: Boolean; |
| 148 |
VertexList, VertexList1, VertexList2: TSingleList; |
| 149 |
T: Single; |
| 150 |
ControlPoints: TCubicBezier2DPoints; |
| 151 |
CachedBezierCurve: array [0..19] of TVector2; |
| 152 |
begin |
| 153 |
ControlPoints[0] := Vector2(0, 0); |
| 154 |
ControlPoints[3] := Vector2(1, 1); |
| 155 |
OldT1 := 0; |
| 156 |
OldT2 := 0; |
| 157 |
// For bone timeline |
| 158 |
for I := 0 to AffectedBoneTimelines.Count - 1 do |
| 159 |
begin |
| 160 |
BoneTimeline := AffectedBoneTimelines[I]; |
| 161 |
for J := 0 to Time.Count - 1 do |
| 162 |
begin |
| 163 |
Timeframe := Time[J]; |
| 164 |
// Check to see if this timeframe exists in bone timeline |
| 165 |
IsExists := False; |
| 166 |
for K := 0 to BoneTimeline.Time.Count - 1 do |
| 167 |
begin |
| 168 |
if Timeframe = BoneTimeline.Time[K] then |
| 169 |
begin |
| 170 |
IsExists := True; |
| 171 |
Break; |
| 172 |
end; |
| 173 |
end; |
| 174 |
// If timeframe does not exist, we insert it into bone timeline and then |
| 175 |
// generate new values for this timeframe by interpolating 2 values between it |
| 176 |
if not IsExists then |
| 177 |
begin |
| 178 |
BoneTimeline.Time.Add(Timeframe); |
| 179 |
BoneTimeline.Time.Sort; |
| 180 |
P := BoneTimeline.Time.IndexOf(Timeframe); |
| 181 |
// TODO: If there's missing timeframe at the start of the animation, |
| 182 |
// we take the value from the next timeframe. This is not correct |
| 183 |
// as default behavior is to take the setup value and then make a |
| 184 |
// "sudden jump" to next frame |
| 185 |
if P = 0 then |
| 186 |
begin |
| 187 |
BoneTimeline.BackupTime.Insert(0, Timeframe); |
| 188 |
if BoneTimeline is TBoneTimelineTranslate then |
| 189 |
begin |
| 190 |
BoneTimelineTranslate.Vectors.Insert(P, BoneTimelineTranslate.Vectors[P]); |
| 191 |
// Add missing value to backup values as safeguard |
| 192 |
BoneTimelineTranslate.BackupVectors.Insert(P, BoneTimelineTranslate.Vectors[P]); |
| 193 |
end else |
| 194 |
if BoneTimeline is TBoneTimelineRotate then |
| 195 |
begin |
| 196 |
BoneTimelineRotate.Angles.Insert(P, BoneTimelineRotate.Angles[P]); |
| 197 |
// Add missing value to backup values as safeguard |
| 198 |
BoneTimelineRotate.BackupAngles.Insert(P, BoneTimelineRotate.BackupAngles[P]); |
| 199 |
end else |
| 200 |
if BoneTimeline is TBoneTimelineScale then |
| 201 |
begin |
| 202 |
BoneTimelineScale.Vectors.Insert(P, BoneTimelineScale.Vectors[P]); |
| 203 |
// Add missing value to backup values as safeguard |
| 204 |
BoneTimelineScale.BackupVectors.Insert(P, BoneTimelineScale.BackupVectors[P]); |
| 205 |
end; |
| 206 |
BoneTimeline.CurveControlPoints.Insert(P, Vector4(0, 0, 1, 1)); |
| 207 |
end else |
| 208 |
if P = BoneTimeline.Time.Count - 1 then |
| 209 |
begin |
| 210 |
BoneTimeline.BackupTime.Add(Timeframe); |
| 211 |
if BoneTimeline is TBoneTimelineTranslate then |
| 212 |
begin |
| 213 |
BoneTimelineTranslate.Vectors.Add(BoneTimelineTranslate.Vectors[BoneTimelineTranslate.Vectors.Count - 1]); |
| 214 |
// Add missing value to backup values as safeguard |
| 215 |
BoneTimelineTranslate.BackupVectors.Add(BoneTimelineTranslate.BackupVectors[BoneTimelineTranslate.BackupVectors.Count - 1]); |
| 216 |
end else |
| 217 |
if BoneTimeline is TBoneTimelineRotate then |
| 218 |
begin |
| 219 |
BoneTimelineRotate.Angles.Add(BoneTimelineRotate.Angles[BoneTimelineRotate.Angles.Count - 1]); |
| 220 |
// Add missing value to backup values as safeguard |
| 221 |
BoneTimelineRotate.BackupAngles.Add(BoneTimelineRotate.BackupAngles[BoneTimelineRotate.BackupAngles.Count - 1]); |
| 222 |
end else |
| 223 |
if BoneTimeline is TBoneTimelineScale then |
| 224 |
begin |
| 225 |
BoneTimelineScale.Vectors.Add(BoneTimelineScale.Vectors[BoneTimelineScale.Vectors.Count - 1]); |
| 226 |
// Add missing value to backup values as safeguard |
| 227 |
BoneTimelineScale.BackupVectors.Add(BoneTimelineScale.BackupVectors[BoneTimelineScale.BackupVectors.Count - 1]); |
| 228 |
end; |
| 229 |
// Add missing value to backup values as safeguard |
| 230 |
BoneTimeline.CurveControlPoints.Add(Vector4(0, 0, 1, 1)); |
| 231 |
end else |
| 232 |
begin |
| 233 |
// If this timeline has control points, then we need to search timeframe in original |
| 234 |
// time instead for accurary result |
| 235 |
if BoneTimeline.Curve then |
| 236 |
begin |
| 237 |
T1 := 0; |
| 238 |
T2 := 0; |
| 239 |
for K := BoneTimeline.BackupTime.Count - 2 downto 0 do |
| 240 |
begin |
| 241 |
if (Timeframe > BoneTimeline.BackupTime[K]) then |
| 242 |
begin |
| 243 |
T1 := K; |
| 244 |
T2 := K + 1; |
| 245 |
Break; |
| 246 |
end; |
| 247 |
end; |
| 248 |
// This is a different timeframe compare to previous one? |
| 249 |
// Then we need to generate a bezier curve in order to use it |
| 250 |
// later for finding step value |
| 251 |
if (T1 <> OldT1) or (T2 <> OldT2) then |
| 252 |
begin; |
| 253 |
ControlPoints[1] := Vector2(BoneTimeline.CurveControlPoints[T1][0], BoneTimeline.CurveControlPoints[T1][1]); |
| 254 |
ControlPoints[2] := Vector2(BoneTimeline.CurveControlPoints[T1][2], BoneTimeline.CurveControlPoints[T1][3]); |
| 255 |
for K := 0 to High(CachedBezierCurve) do |
| 256 |
begin |
| 257 |
CachedBezierCurve[K] := CubicBezier2D(K / High(CachedBezierCurve), ControlPoints); |
| 258 |
end; |
| 259 |
OldT1 := T1; |
| 260 |
OldT2 := T2; |
| 261 |
end; |
| 262 |
Timeframe1 := BoneTimeline.BackupTime[T1]; |
| 263 |
Timeframe2 := BoneTimeline.BackupTime[T2]; |
| 264 |
T := (Timeframe - Timeframe1) / (Timeframe2 - Timeframe1); |
| 265 |
for K := 1 to High(CachedBezierCurve) do |
| 266 |
begin |
| 267 |
if CachedBezierCurve[K].X > T then |
| 268 |
begin |
| 269 |
T := Lerp( |
| 270 |
(T - CachedBezierCurve[K - 1].X) / (CachedBezierCurve[K].X - CachedBezierCurve[K - 1].X), |
| 271 |
CachedBezierCurve[K - 1].Y, CachedBezierCurve[K].Y); |
| 272 |
Break; |
| 273 |
end; |
| 274 |
end; |
| 275 |
if BoneTimeline is TBoneTimelineTranslate then |
| 276 |
begin |
| 277 |
BoneTimelineTranslate.Vectors.Insert(P, |
| 278 |
Lerp(T, BoneTimelineTranslate.BackupVectors.L[T1], BoneTimelineTranslate.BackupVectors.L[T2])); |
| 279 |
end else |
| 280 |
if BoneTimeline is TBoneTimelineRotate then |
| 281 |
begin |
| 282 |
BoneTimelineRotate.Angles.Insert(P, |
| 283 |
Lerp(T, BoneTimelineRotate.BackupAngles.L[T1], BoneTimelineRotate.BackupAngles.L[T2])); |
| 284 |
end else |
| 285 |
if BoneTimeline is TBoneTimelineScale then |
| 286 |
begin |
| 287 |
BoneTimelineScale.Vectors.Insert(P, |
| 288 |
Lerp(T, BoneTimelineScale.BackupVectors.L[T1], BoneTimelineScale.BackupVectors.L[T2])); |
| 289 |
end; |
| 290 |
end else |
| 291 |
begin |
| 292 |
T1 := P - 1; |
| 293 |
T2 := P + 1; |
| 294 |
Timeframe1 := BoneTimeline.Time[T1]; |
| 295 |
Timeframe2 := BoneTimeline.Time[T2]; |
| 296 |
T := (Timeframe - Timeframe1) / (Timeframe2 - Timeframe1); |
| 297 |
if BoneTimeline is TBoneTimelineTranslate then |
| 298 |
begin |
| 299 |
BoneTimelineTranslate.Vectors.Insert(P, |
| 300 |
Lerp(T, BoneTimelineTranslate.Vectors.L[T1], BoneTimelineTranslate.Vectors.L[P])); |
| 301 |
end else |
| 302 |
if BoneTimeline is TBoneTimelineRotate then |
| 303 |
begin |
| 304 |
BoneTimelineRotate.Angles.Insert(P, |
| 305 |
Lerp(T, BoneTimelineRotate.Angles.L[T1], BoneTimelineRotate.Angles.L[P])); |
| 306 |
end else |
| 307 |
if BoneTimeline is TBoneTimelineScale then |
| 308 |
begin |
| 309 |
BoneTimelineScale.Vectors.Insert(P, |
| 310 |
Lerp(T, BoneTimelineScale.Vectors.L[T1], BoneTimelineScale.Vectors.L[P])); |
| 311 |
end; |
| 312 |
end; |
| 313 |
end; |
| 314 |
end; |
| 315 |
end; |
| 316 |
end; |
| 317 |
// For deform timeline |
| 318 |
if AffectedDeformTimeline <> nil then |
| 319 |
begin |
| 320 |
for J := 0 to Time.Count - 1 do |
| 321 |
begin |
| 322 |
Timeframe := Time[J]; |
| 323 |
// Check to see if this timeframe exists in deform timeline |
| 324 |
IsExists := False; |
| 325 |
for K := 0 to AffectedDeformTimeline.Time.Count - 1 do |
| 326 |
begin |
| 327 |
if Timeframe = AffectedDeformTimeline.Time[K] then |
| 328 |
begin |
| 329 |
IsExists := True; |
| 330 |
Break; |
| 331 |
end; |
| 332 |
end; |
| 333 |
// If timeframe does not exist, we insert it into deform timeline and then |
| 334 |
// generate new values for this timeframe by interpolating 2 values between it |
| 335 |
if not IsExists then |
| 336 |
begin |
| 337 |
AffectedDeformTimeline.Time.Add(Timeframe); |
| 338 |
AffectedDeformTimeline.Time.Sort; |
| 339 |
P := AffectedDeformTimeline.Time.IndexOf(Timeframe); |
| 340 |
VertexList := TSingleList.Create; |
| 341 |
// TODO: If there's missing timeframe at the start of the animation, |
| 342 |
// we take the value from the next timeframe. This is not correct |
| 343 |
// as default behavior is to take the setup value and then make a |
| 344 |
// "sudden jump" to next frame |
| 345 |
if P = 0 then |
| 346 |
begin |
| 347 |
AffectedDeformTimeline.BackupTime.Insert(0, Timeframe); |
| 348 |
VertexList.AddRange(AffectedDeformTimeline.Vertices[P]); |
| 349 |
AffectedDeformTimeline.Vertices.Insert(P, VertexList); |
| 350 |
// Add missing value to backup values as safeguard |
| 351 |
AffectedDeformTimeline.BackupVertices.Insert(P, VertexList); |
| 352 |
AffectedDeformTimeline.CurveControlPoints.Insert(P, Vector4(0, 0, 1, 1)); |
| 353 |
end else |
| 354 |
if P = AffectedDeformTimeline.Time.Count - 1 then |
| 355 |
begin |
| 356 |
AffectedDeformTimeline.BackupTime.Add(Timeframe); |
| 357 |
VertexList.AddRange(AffectedDeformTimeline.Vertices[AffectedDeformTimeline.Vertices.Count - 1]); |
| 358 |
AffectedDeformTimeline.Vertices.Add(VertexList); |
| 359 |
// Add missing value to backup values as safeguard |
| 360 |
AffectedDeformTimeline.BackupVertices.Add(VertexList); |
| 361 |
AffectedDeformTimeline.CurveControlPoints.Add(Vector4(0, 0, 1, 1)); |
| 362 |
end else |
| 363 |
begin |
| 364 |
// If this timeline has control points, then we need to search timeframe in original |
| 365 |
// time instead for accurary result |
| 366 |
if AffectedDeformTimeline.Curve then |
| 367 |
begin |
| 368 |
T1 := 0; |
| 369 |
T2 := 0; |
| 370 |
for K := AffectedDeformTimeline.BackupTime.Count - 2 downto 0 do |
| 371 |
begin |
| 372 |
if (Timeframe > AffectedDeformTimeline.BackupTime[K]) then |
| 373 |
begin |
| 374 |
T1 := K; |
| 375 |
T2 := K + 1; |
| 376 |
Break; |
| 377 |
end; |
| 378 |
end; |
| 379 |
// This is a different timeframe compare to previous one? |
| 380 |
// Then we need to generate a bezier curve in order to use it |
| 381 |
// later for finding step value |
| 382 |
if (T1 <> OldT1) or (T2 <> OldT2) then |
| 383 |
begin; |
| 384 |
ControlPoints[1] := Vector2(AffectedDeformTimeline.CurveControlPoints[T1][0], AffectedDeformTimeline.CurveControlPoints[T1][1]); |
| 385 |
ControlPoints[2] := Vector2(AffectedDeformTimeline.CurveControlPoints[T1][2], AffectedDeformTimeline.CurveControlPoints[T1][3]); |
| 386 |
for K := 0 to High(CachedBezierCurve) do |
| 387 |
begin |
| 388 |
CachedBezierCurve[K] := CubicBezier2D(K / High(CachedBezierCurve), ControlPoints); |
| 389 |
end; |
| 390 |
OldT1 := T1; |
| 391 |
OldT2 := T2; |
| 392 |
end; |
| 393 |
Timeframe1 := AffectedDeformTimeline.BackupTime[T1]; |
| 394 |
Timeframe2 := AffectedDeformTimeline.BackupTime[T2]; |
| 395 |
VertexList1 := AffectedDeformTimeline.BackupVertices[T1]; |
| 396 |
VertexList2 := AffectedDeformTimeline.BackupVertices[T2]; |
| 397 |
VertexList.Count := VertexList1.Count; |
| 398 |
T := (Timeframe - Timeframe1) / (Timeframe2 - Timeframe1); |
| 399 |
for K := 1 to High(CachedBezierCurve) do |
| 400 |
begin |
| 401 |
if CachedBezierCurve[K].X > T then |
| 402 |
begin |
| 403 |
T := Lerp( |
| 404 |
(T - CachedBezierCurve[K - 1].X) / (CachedBezierCurve[K].X - CachedBezierCurve[K - 1].X), |
| 405 |
CachedBezierCurve[K - 1].Y, CachedBezierCurve[K].Y); |
| 406 |
Break; |
| 407 |
end; |
| 408 |
end; |
| 409 |
end else |
| 410 |
begin |
| 411 |
T1 := P - 1; |
| 412 |
T2 := P + 1; |
| 413 |
Timeframe1 := AffectedDeformTimeline.Time[T1]; |
| 414 |
Timeframe2 := AffectedDeformTimeline.Time[T2]; |
| 415 |
VertexList1 := AffectedDeformTimeline.Vertices[T1]; |
| 416 |
VertexList2 := AffectedDeformTimeline.Vertices[P]; |
| 417 |
VertexList.Count := VertexList1.Count; |
| 418 |
T := (Timeframe - Timeframe1) / (Timeframe2 - Timeframe1); |
| 419 |
end; |
| 420 |
for K := 0 to VertexList1.Count - 1 do |
| 421 |
begin |
| 422 |
VertexList[K] := Lerp(T, VertexList1[K], VertexList2[K]); |
| 423 |
end; |
| 424 |
AffectedDeformTimeline.Vertices.Insert(P, VertexList); |
| 425 |
end; |
| 426 |
end; |
| 427 |
end; |
| 428 |
end; |
| 429 |
end; |
| 430 |
|
| 431 |
procedure CalculateVertices; |
| 432 |
var |
| 433 |
I, J, K, L: Integer; |
| 434 |
Offset: Integer; |
| 435 |
VB: TMeshVertexBone; |
| 436 |
V, V2: TVector2; |
| 437 |
DeformVertexList, |
| 438 |
VertexList: TSingleList; |
| 439 |
BoneParent: TBone; |
| 440 |
Bones: TBoneList; |
| 441 |
begin |
| 442 |
Bones := TBoneList.Create; |
| 443 |
Bones.OwnsObjects := False; |
| 444 |
try |
| 445 |
for I := 0 to Time.Count - 1 do |
| 446 |
begin |
| 447 |
VertexList := TSingleList.Create; |
| 448 |
VertexList.Count := Attachment.Weights.Count * 2; |
| 449 |
for J := 0 to Attachment.Weights.Count - 1 do |
| 450 |
begin |
| 451 |
V := TVector2.Zero; |
| 452 |
for K := 0 to Attachment.Weights[J].Bones.Count - 1 do |
| 453 |
begin |
| 454 |
VB := Attachment.Weights[J].Bones[K]; |
| 455 |
Offset := Attachment.Weights[J].Offset; |
| 456 |
// |
| 457 |
Bones.Clear; |
| 458 |
BoneParent := VB.Bone; |
| 459 |
// Recalculate bone structure based on timeline values |
| 460 |
while BoneParent <> nil do |
| 461 |
begin |
| 462 |
BoneParent.Backup; |
| 463 |
Bones.Add(BoneParent); |
| 464 |
for L := 0 to AffectedBoneTimelines.Count - 1 do |
| 465 |
begin |
| 466 |
if AffectedBoneTimelines[L].Bone.Name = BoneParent.Name then |
| 467 |
begin |
| 468 |
if AffectedBoneTimelines[L] is TBoneTimelineTranslate then |
| 469 |
BoneParent.XY := BoneParent.XY + (AffectedBoneTimelines[L] as TBoneTimelineTranslate).Vectors[I] |
| 470 |
else |
| 471 |
if AffectedBoneTimelines[L] is TBoneTimelineRotate then |
| 472 |
BoneParent.Rotation := BoneParent.Rotation + (AffectedBoneTimelines[L] as TBoneTimelineRotate).Angles[I] |
| 473 |
else |
| 474 |
if AffectedBoneTimelines[L] is TBoneTimelineScale then |
| 475 |
BoneParent.Scale := BoneParent.Scale * (AffectedBoneTimelines[L] as TBoneTimelineScale).Vectors[I]; |
| 476 |
end; |
| 477 |
end; |
| 478 |
BoneParent := BoneParent.Parent; |
| 479 |
end; |
| 480 |
for L := Bones.Count - 1 downto 0 do |
| 481 |
begin |
| 482 |
Bones[L].UpdateWorldTransform; |
| 483 |
end; |
| 484 |
// |
| 485 |
if AffectedDeformTimeline <> nil then |
| 486 |
begin |
| 487 |
DeformVertexList := AffectedDeformTimeline.Vertices[I]; |
| 488 |
V2.X := DeformVertexList[Offset + K * 2]; |
| 489 |
V2.Y := DeformVertexList[Offset + K * 2 + 1]; |
| 490 |
V := V + (VB.Bone.ToWorldSpace(V2) * VB.Weight); |
| 491 |
end |
| 492 |
else |
| 493 |
V := V + (VB.Bone.ToWorldSpace(VB.V) * VB.Weight); |
| 494 |
// Restore bone structure |
| 495 |
for L := Bones.Count - 1 downto 0 do |
| 496 |
begin |
| 497 |
Bones[L].Restore; |
| 498 |
Bones[L].UpdateWorldTransform; |
| 499 |
end; |
| 500 |
end; |
| 501 |
VertexList[J * 2 ] := V.X; |
| 502 |
VertexList[J * 2 + 1] := V.Y; |
| 503 |
end; |
| 504 |
Vertices.Add(VertexList); |
| 505 |
end; |
| 506 |
finally |
| 507 |
FreeAndNil(Bones); |
| 508 |
end; |
| 509 |
end; |
| 510 |
|
| 511 |
begin |
| 512 |
CombineTimeframes; |
| 513 |
InterpolateTimeframes; |
| 514 |
CalculateVertices; |
| 515 |
end; |
| 516 |
|
| 517 |
procedure TWeightedMeshTimeline.BuildNodes(const MaxAnimationTime: Single; const Container: TX3DRootNode); |
| 518 |
var |
| 519 |
I, J: Integer; |
| 520 |
Route: TX3DRoute; |
| 521 |
VertexList: TSingleList; |
| 522 |
begin |
| 523 |
Node := TCoordinateInterpolatorNode.Create('WeightedMeshTimeline_' + Name); |
| 524 |
for I := 0 to Time.Count - 1 do |
| 525 |
begin |
| 526 |
Node.FdKey.Items.Add(Time[I] / MaxAnimationTime); |
| 527 |
VertexList := Vertices.Items[I]; |
| 528 |
Assert(not Odd(VertexList.Count)); // VertexList is a list of 2D coordinates |
| 529 |
for J := 0 to VertexList.Count div 2 - 1 do |
| 530 |
Node.FdKeyValue.Items.Add(Vector3( |
| 531 |
VertexList.Items[2 * J], |
| 532 |
VertexList.Items[2 * J + 1], |
| 533 |
0 |
| 534 |
)); |
| 535 |
end; |
| 536 |
Container.AddChildren(Node); |
| 537 |
|
| 538 |
Route := TX3DRoute.Create; |
| 539 |
Route.SetSourceDirectly(Node.EventValue_changed); |
| 540 |
Route.SetDestinationDirectly(Attachment.Coord.FdPoint.EventIn); |
| 541 |
Container.AddRoute(Route); |
| 542 |
end; |
| 543 |
|
| 544 |
{$endif} |