Debian Codegraph @

src/scene/x3d/x3dnodes_standard_interpolation.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-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 {$ifdef read_interface}
18 { Interpolation mode, for @link(TAbstractInterpolatorNode.Interpolation). }
19 TInterpolation = (
20 { The value changes linearly from previous to next.
21 Some interpolators have a specific algorithm for "linear change",
22 for example colors are interpolated in HSV (LerpRgbInHsv),
23 rotations are interpolated using Slerp. }
24 inLinear,
25
26 { The value changes immediately from previous to next,
27 at the time of next key.
28 Before the time of 1st key, the value is equal to the 1st value.
29
30 E.g. key = [ 0.1, 0.2 ], keyValue = [ 1 2 ] =>
31 value is 1 when time is [0, 0.2),
32 value is 2 when time is [0.2, ...].
33
34 This is consistent with glTF "step" interpolation,
35 see https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#appendix-c-interpolation . }
36 inStep
37 );
38
39 { Basis for all nodes interpolating (animating) values. }
40 TAbstractInterpolatorNode = class(TAbstractChildNode)
41 strict protected
42 function KeyRange(const Fraction: Single; out T: Single): Integer;
43
44 {$I auto_generated_node_helpers/x3dnodes_x3dinterpolatornode.inc}
45 end;
46
47 { Common base class for all implemented interpolator nodes. }
48 {$ifdef FPC}generic{$endif}
49 TAbstractCommonInterpolatorNode<
50 TOutputField: TX3DField> = class(TAbstractInterpolatorNode)
51 strict private
52 FEventValue_changed: TX3DEvent;
53 FTempFields: array of TOutputField;
54 FTempFieldsUsed: TListSize;
55 { Check is Index < KeyValueCount.
56 Makes warning when returning false. }
57 function CheckKeyValue(const Index: TListSize): Boolean;
58 procedure EventSet_FractionReceive(const Event: TX3DEvent; const Value: TX3DField;
59 const Time: TX3DTime);
60 strict protected
61 { Values count determines allowed indexes for InterpolatorLerp, InterpolatorSet. }
62 function KeyValueCount: TListSize; virtual; abstract;
63 { Set Target.Value to Lerp(A, KeyValue[Index1], KeyValue[Index2]).
64 Index1 and Index2 are for sure valid (between 0 and KeyValueCount-1). }
65 procedure InterpolatorLerp(const Target: TOutputField;
66 const A: Single; const Index1, Index2: Integer); virtual; abstract;
67 { Set Target.Value to KeyValue[Index].
68 Index is for sure valid (between 0 and KeyValueCount-1). }
69 procedure InterpolatorSet(const Target: TOutputField; const Index: Integer); virtual; abstract;
70 { Create TOutputField instance, to send it by EventValue_Changed. }
71 function CreateOutputField: TOutputField; virtual; abstract;
72 public
73 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
74 destructor Destroy; override;
75 end;
76
77 { Interpolate (animate) a single color. }
78 TColorInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
79 TSFVec3f>)
80 strict protected
81 function KeyValueCount: TListSize; override;
82 procedure InterpolatorLerp(const Target: TSFVec3f; const A: Single;
83 const Index1, Index2: Integer); override;
84 procedure InterpolatorSet(const Target: TSFVec3f; const Index: Integer); override;
85 function CreateOutputField: TSFVec3f; override;
86
87 {$I auto_generated_node_helpers/x3dnodes_colorinterpolator.inc}
88 end;
89
90 { Interpolate (animate) a set of colors,
91 for example to animate a set of @link(TBackgroundNode)
92 or @link(TColorNode) colors. }
93 TColorSetInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
94 TMFColor>)
95 strict private
96 function FrameValues: TListSize;
97 strict protected
98 function KeyValueCount: TListSize; override;
99 procedure InterpolatorLerp(const Target: TMFColor; const A: Single;
100 const Index1, Index2: Integer); override;
101 procedure InterpolatorSet(const Target: TMFColor; const Index: Integer); override;
102 function CreateOutputField: TMFColor; override;
103
104 {$I auto_generated_node_helpers/x3dnodes_colorsetinterpolator.inc}
105 end;
106
107 { Interpolate (animate) a set of 3D positions,
108 for example to animate @link(TCoordinateNode coordinates) of a mesh. }
109 TCoordinateInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
110 TMFVec3f>)
111 strict private
112 function FrameValues: TListSize;
113 strict protected
114 function KeyValueCount: TListSize; override;
115 procedure InterpolatorLerp(const Target: TMFVec3f; const A: Single;
116 const Index1, Index2: Integer); override;
117 procedure InterpolatorSet(const Target: TMFVec3f; const Index: Integer); override;
118 function CreateOutputField: TMFVec3f; override;
119
120 {$I auto_generated_node_helpers/x3dnodes_coordinateinterpolator.inc}
121 end;
122
123 { Interpolate (animate) a set of 2D positions,
124 for example to animate 2D texture coordinates. }
125 TCoordinateInterpolator2DNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
126 TMFVec2f>)
127 strict private
128 function FrameValues: TListSize;
129 strict protected
130 function KeyValueCount: TListSize; override;
131 procedure InterpolatorLerp(const Target: TMFVec2f; const A: Single;
132 const Index1, Index2: Integer); override;
133 procedure InterpolatorSet(const Target: TMFVec2f; const Index: Integer); override;
134 function CreateOutputField: TMFVec2f; override;
135
136 {$I auto_generated_node_helpers/x3dnodes_coordinateinterpolator2d.inc}
137 end;
138
139 { Support controlled gradual transitions by modifying
140 TimeSensor node fractions.
141 @bold(Not implemented yet.) }
142 TEaseInEaseOutNode = class(TAbstractChildNode)
143 {$I auto_generated_node_helpers/x3dnodes_easeineaseout.inc}
144 end;
145
146 { Interpolate (animate) a set of 3D directions, for example normal vectors. }
147 TNormalInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
148 TMFVec3f>)
149 strict private
150 function FrameValues: TListSize;
151 strict protected
152 function KeyValueCount: TListSize; override;
153 procedure InterpolatorLerp(const Target: TMFVec3f; const A: Single;
154 const Index1, Index2: Integer); override;
155 procedure InterpolatorSet(const Target: TMFVec3f; const Index: Integer); override;
156 function CreateOutputField: TMFVec3f; override;
157
158 {$I auto_generated_node_helpers/x3dnodes_normalinterpolator.inc}
159 end;
160
161 { Interpolate (animate) an orientation,
162 for example to animate @link(TTransformNode.Rotation). }
163 TOrientationInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
164 TSFRotation>)
165 strict protected
166 function KeyValueCount: TListSize; override;
167 procedure InterpolatorLerp(const Target: TSFRotation; const A: Single;
168 const Index1, Index2: Integer); override;
169 procedure InterpolatorSet(const Target: TSFRotation; const Index: Integer); override;
170 function CreateOutputField: TSFRotation; override;
171 public
172 { Return rotation (as axis+angle) at given time (key).
173 Makes a warning and returns zero if FdKey is empty. }
174 function Interpolate(const TimeKey: Single): TVector4;
175
176 {$I auto_generated_node_helpers/x3dnodes_orientationinterpolator.inc}
177 end;
178
179 { Interpolate (animate) a 3D position,
180 for example to animate @link(TTransformNode.Translation). }
181 TPositionInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
182 TSFVec3f>)
183 strict protected
184 function KeyValueCount: TListSize; override;
185 procedure InterpolatorLerp(const Target: TSFVec3f; const A: Single;
186 const Index1, Index2: Integer); override;
187 procedure InterpolatorSet(const Target: TSFVec3f; const Index: Integer); override;
188 function CreateOutputField: TSFVec3f; override;
189 public
190 { Return position at given time (key).
191 Makes a warning and returns zero if FdKey is empty. }
192 function Interpolate(const TimeKey: Single): TVector3;
193
194 {$I auto_generated_node_helpers/x3dnodes_positioninterpolator.inc}
195 end;
196
197 { Interpolate (animate) a 2D position. }
198 TPositionInterpolator2DNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
199 TSFVec2f>)
200 strict protected
201 function KeyValueCount: TListSize; override;
202 procedure InterpolatorLerp(const Target: TSFVec2f; const A: Single;
203 const Index1, Index2: Integer); override;
204 procedure InterpolatorSet(const Target: TSFVec2f; const Index: Integer); override;
205 function CreateOutputField: TSFVec2f; override;
206
207 {$I auto_generated_node_helpers/x3dnodes_positioninterpolator2d.inc}
208 end;
209
210 { Interpolate (animate) a single floating-point value,
211 for example to animate @link(TMaterialNode.Transparency). }
212 TScalarInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
213 TSFFloat>)
214 strict protected
215 function KeyValueCount: TListSize; override;
216 procedure InterpolatorLerp(const Target: TSFFloat; const A: Single;
217 const Index1, Index2: Integer); override;
218 procedure InterpolatorSet(const Target: TSFFloat; const Index: Integer); override;
219 function CreateOutputField: TSFFloat; override;
220
221 {$I auto_generated_node_helpers/x3dnodes_scalarinterpolator.inc}
222 end;
223
224 { Non-linearly interpolate (animate) a 3D position.
225 @bold(Not implemented yet.) }
226 TSplinePositionInterpolatorNode = class(TAbstractInterpolatorNode)
227 {$I auto_generated_node_helpers/x3dnodes_splinepositioninterpolator.inc}
228 end;
229
230 { Non-linearly interpolate (animate) a 2D position.
231 @bold(Not implemented yet.) }
232 TSplinePositionInterpolator2DNode = class(TAbstractInterpolatorNode)
233 {$I auto_generated_node_helpers/x3dnodes_splinepositioninterpolator2d.inc}
234 end;
235
236 { Non-linearly interpolate (animate) a single floating-point value.
237 @bold(Not implemented yet.) }
238 TSplineScalarInterpolatorNode = class(TAbstractInterpolatorNode)
239 {$I auto_generated_node_helpers/x3dnodes_splinescalarinterpolator.inc}
240 end;
241
242 { Non-linearly interpolate (animate) a rotation.
243 @bold(Not implemented yet.) }
244 TSquadOrientationInterpolatorNode = class(TAbstractInterpolatorNode)
245 {$I auto_generated_node_helpers/x3dnodes_squadorientationinterpolator.inc}
246 end;
247
248 { Interpolate (animate) a set of floats, for example to animate
249 @link(TElevationGridNode) heights. }
250 TVectorInterpolatorNode = class({$ifdef FPC}specialize{$endif} TAbstractCommonInterpolatorNode<
251 TMFFloat>)
252 strict private
253 function FrameValues: TListSize;
254 strict protected
255 function KeyValueCount: TListSize; override;
256 procedure InterpolatorLerp(const Target: TMFFloat; const A: Single;
257 const Index1, Index2: Integer); override;
258 procedure InterpolatorSet(const Target: TMFFloat; const Index: Integer); override;
259 function CreateOutputField: TMFFloat; override;
260
261 {$I auto_generated_node_helpers/x3dnodes_vectorinterpolator.inc}
262 end;
263
264 {$endif read_interface}
265
266 {$ifdef read_implementation}
267
268 const
269 InterpolationNames: array [TInterpolation] of String = (
270 'LINEAR',
271 'STEP'
272 );
273
274 { TAbstractInterpolatorNode -------------------------------------------------- }
275
276 function TAbstractInterpolatorNode.KeyRange(
277 const Fraction: Single; out T: Single): Integer;
278 begin
279 Result := X3DNodes.KeyRange(FdKey.Items, Fraction, T);
280 end;
281
282 { TAbstractCommonInterpolatorNode -------------------------------------------- }
283
284 constructor TAbstractCommonInterpolatorNode
285 {$ifndef FPC}<TOutputField>{$endif} .
286 Create(const AX3DName, ABaseUrl: String);
287 begin
288 inherited;
289 FEventValue_changed := AnyEvent('value_changed', true);
290 EventSet_Fraction.AddNotification(
291 {$ifdef FPC}@{$endif} EventSet_FractionReceive);
292 end;
293
294 destructor TAbstractCommonInterpolatorNode
295 {$ifndef FPC}<TOutputField>{$endif} .
296 Destroy;
297 var
298 I: TListSize;
299 begin
300 for I := 0 to High(FTempFields) do
301 FreeAndNil(FTempFields[I]);
302 inherited;
303 end;
304
305 function TAbstractCommonInterpolatorNode
306 {$ifndef FPC}<TOutputField>{$endif} .
307 CheckKeyValue(const Index: TListSize): Boolean;
308 begin
309 Result := Index < KeyValueCount;
310 if not Result then
311 WritelnWarning('Not enough key values for interpolator "%s"', [NiceName]);
312 end;
313
314 procedure TAbstractCommonInterpolatorNode
315 {$ifndef FPC}<TOutputField>{$endif} .
316 EventSet_FractionReceive(const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
317 var
318 T: Single;
319 KeyCount, Range: Integer;
320 TempFieldIndex: TListSize;
321 OutputField: TOutputField;
322 begin
323 if not FEventValue_Changed.SendNeeded then Exit;
324
325 KeyCount := FdKey.Items.Count;
326 if KeyCount = 0 then
327 begin
328 { Interpolator nodes containing no keys in the key field
329 shall not produce any events.
330
331 Moreover, in case of array interpolators (like TColorInterpolatorNode)
332 the "div KeyCount" used to calculate FrameValues would cause errors. }
333 Exit;
334 end;
335
336 { FTempFields serves as a pool of ready field instances created by CreateOutputField.
337 It is expanded when necessary. }
338 TempFieldIndex := FTempFieldsUsed;
339 Inc(FTempFieldsUsed);
340 try
341 if FTempFieldsUsed > Length(FTempFields) then
342 begin
343 SetLength(FTempFields, FTempFieldsUsed);
344 {$ifndef FPC} Assert(FTempFields[TempFieldIndex] = nil); {$endif}
345 FTempFields[TempFieldIndex] := CreateOutputField;
346 end;
347 OutputField := FTempFields[TempFieldIndex];
348 {$ifndef FPC} Assert(OutputField <> nil); {$endif}
349
350 Assert(Value is TSFFloat, 'EventSet_FractionReceive can only process SFFloat input events');
351 Assert(not IsNan((Value as TSFFloat).Value), 'EventSet_FractionReceive cannot handle fraction = NaN');
352 Range := KeyRange((Value as TSFFloat).Value, T);
353 if Range = 0 then
354 begin
355 if not CheckKeyValue(0) then Exit;
356 InterpolatorSet(OutputField, 0);
357 end else
358 if Range = KeyCount then
359 begin
360 if not CheckKeyValue(KeyCount - 1) then Exit;
361 InterpolatorSet(OutputField, KeyCount - 1);
362 end else
363 begin
364 case Interpolation of
365 inLinear:
366 begin
367 if not CheckKeyValue(Range) then Exit;
368 InterpolatorLerp(OutputField, T, Range - 1, Range);
369 end;
370 inStep:
371 begin
372 if not CheckKeyValue(Range - 1) then Exit;
373 InterpolatorSet(OutputField, Range - 1);
374 end;
375 {$ifndef COMPILER_CASE_ANALYSIS}
376 else raise EInternalError.Create('TAbstractCommonInterpolatorNode:Interpolation?');
377 {$endif}
378 end;
379 end;
380
381 OutputField.InternalPartialSend := Value.InternalPartialSend;
382 FEventValue_Changed.Send(OutputField, Time);
383 finally
384 Dec(FTempFieldsUsed);
385 end;
386 end;
387
388 { TColorInterpolatorNode ----------------------------------------------------- }
389
390 function TColorInterpolatorNode.KeyValueCount: TListSize;
391 begin
392 Result := FdKeyValue.Items.Count;
393 end;
394
395 procedure TColorInterpolatorNode.InterpolatorLerp(const Target: TSFVec3f;
396 const A: Single; const Index1, Index2: Integer);
397 begin
398 Target.Value := LerpRgbInHsv(A,
399 FdKeyValue.Items.L[Index1],
400 FdKeyValue.Items.L[Index2]);
401 end;
402
403 procedure TColorInterpolatorNode.InterpolatorSet(const Target: TSFVec3f; const Index: Integer);
404 begin
405 Target.Value := FdKeyValue.Items.L[Index];
406 end;
407
408 function TColorInterpolatorNode.CreateOutputField: TSFVec3f;
409 begin
410 // We pass Self as ParentNode to
411 // TKeyValue.CreateUndefined.
412 // This is not required, ParentNode could be @nil and everything
413 // will work Ok, but setting ParentNode allows InstantReality Logger node
414 // to display "sending node" information, useful for debugging.
415
416 Result := TSFColor.CreateUndefined(Self, false, '');
417 end;
418
419 { TColorSetInterpolatorNode -------------------------------------------------- }
420
421 function TColorSetInterpolatorNode.FrameValues: TListSize;
422 begin
423 Result := FdKeyValue.Count div FdKey.Count;
424 end;
425
426 function TColorSetInterpolatorNode.KeyValueCount: TListSize;
427 begin
428 Result := FdKeyValue.Items.Count div FrameValues;
429 end;
430
431 procedure TColorSetInterpolatorNode.InterpolatorLerp(const Target: TMFColor; const A: Single;
432 const Index1, Index2: Integer);
433 var
434 FV: TListSize;
435 begin
436 FV := FrameValues;
437 Target.Items.AssignLerpRgbInHsv(A, FdKeyValue.Items, FdKeyValue.Items,
438 Index1 * FV, Index2 * FV, FrameValues);
439 end;
440
441 procedure TColorSetInterpolatorNode.InterpolatorSet(const Target: TMFColor; const Index: Integer);
442 var
443 FV: TListSize;
444 begin
445 FV := FrameValues;
446 Target.Items.Clear;
447 Target.Items.AddSubRange(FdKeyValue.Items, Index * FV, FV);
448 end;
449
450 function TColorSetInterpolatorNode.CreateOutputField: TMFColor;
451 begin
452 Result := TMFColor.CreateUndefined(Self, false, '');
453 end;
454
455 { TCoordinateInterpolatorNode ---------------------------------------------------- }
456
457 function TCoordinateInterpolatorNode.FrameValues: TListSize;
458 begin
459 Result := FdKeyValue.Count div FdKey.Count;
460 end;
461
462 function TCoordinateInterpolatorNode.KeyValueCount: TListSize;
463 begin
464 Result := FdKeyValue.Items.Count div FrameValues;
465 end;
466
467 procedure TCoordinateInterpolatorNode.InterpolatorLerp(const Target: TMFVec3f; const A: Single;
468 const Index1, Index2: Integer);
469 var
470 FV: TListSize;
471 begin
472 FV := FrameValues;
473 Target.Items.AssignLerpRange(A, FdKeyValue.Items, FdKeyValue.Items,
474 Index1 * FV, Index2 * FV, FrameValues);
475 end;
476
477 procedure TCoordinateInterpolatorNode.InterpolatorSet(const Target: TMFVec3f; const Index: Integer);
478 var
479 FV: TListSize;
480 begin
481 FV := FrameValues;
482 Target.Items.Clear;
483 Target.Items.AddSubRange(FdKeyValue.Items, Index * FV, FV);
484 end;
485
486 function TCoordinateInterpolatorNode.CreateOutputField: TMFVec3f;
487 begin
488 Result := TMFVec3f.CreateUndefined(Self, false, '');
489 end;
490
491 { TCoordinateInterpolator2DNode ---------------------------------------------------- }
492
493 function TCoordinateInterpolator2DNode.FrameValues: TListSize;
494 begin
495 Result := FdKeyValue.Count div FdKey.Count;
496 end;
497
498 function TCoordinateInterpolator2DNode.KeyValueCount: TListSize;
499 begin
500 Result := FdKeyValue.Items.Count div FrameValues;
501 end;
502
503 procedure TCoordinateInterpolator2DNode.InterpolatorLerp(const Target: TMFVec2f; const A: Single;
504 const Index1, Index2: Integer);
505 var
506 FV: TListSize;
507 begin
508 FV := FrameValues;
509 Target.Items.AssignLerpRange(A, FdKeyValue.Items, FdKeyValue.Items,
510 Index1 * FV, Index2 * FV, FrameValues);
511 end;
512
513 procedure TCoordinateInterpolator2DNode.InterpolatorSet(const Target: TMFVec2f; const Index: Integer);
514 var
515 FV: TListSize;
516 begin
517 FV := FrameValues;
518 Target.Items.Clear;
519 Target.Items.AddSubRange(FdKeyValue.Items, Index * FV, FV);
520 end;
521
522 function TCoordinateInterpolator2DNode.CreateOutputField: TMFVec2f;
523 begin
524 Result := TMFVec2f.CreateUndefined(Self, false, '');
525 end;
526
527 { TNormalInterpolatorNode -------------------------------------------------- }
528
529 function TNormalInterpolatorNode.FrameValues: TListSize;
530 begin
531 Result := FdKeyValue.Count div FdKey.Count;
532 end;
533
534 function TNormalInterpolatorNode.KeyValueCount: TListSize;
535 begin
536 Result := FdKeyValue.Items.Count div FrameValues;
537 end;
538
539 procedure TNormalInterpolatorNode.InterpolatorLerp(const Target: TMFVec3f; const A: Single;
540 const Index1, Index2: Integer);
541 var
542 FV: TListSize;
543 begin
544 FV := FrameValues;
545 Target.Items.AssignLerpNormalize(A, FdKeyValue.Items, FdKeyValue.Items,
546 Index1 * FV, Index2 * FV, FrameValues);
547 end;
548
549 procedure TNormalInterpolatorNode.InterpolatorSet(const Target: TMFVec3f; const Index: Integer);
550 var
551 FV: TListSize;
552 begin
553 FV := FrameValues;
554 Target.Items.Clear;
555 Target.Items.AddSubRange(FdKeyValue.Items, Index * FV, FV);
556 end;
557
558 function TNormalInterpolatorNode.CreateOutputField: TMFVec3f;
559 begin
560 Result := TMFVec3f.CreateUndefined(Self, false, '');
561 end;
562
563 { TOrientationInterpolatorNode -------------------------------------------------- }
564
565 function TOrientationInterpolatorNode.KeyValueCount: TListSize;
566 begin
567 Result := FdKeyValue.Items.Count;
568 end;
569
570 procedure TOrientationInterpolatorNode.InterpolatorLerp(const Target: TSFRotation;
571 const A: Single; const Index1, Index2: Integer);
572 var
573 Q1, Q2, QResult: TQuaternion;
574 begin
575 { In my tests, NLerp works as good as SLerp here.
576 Although NLerp doesn't guarantee constant velocity, and should be
577 faster --- neither of these qualities seem to be noticeable
578 in practice. }
579
580 if KeyValueQuaternions then
581 begin
582 // interpolate on quaternions, assuming our inputs are unit quaternions
583 Q1.Data.Vector4 := FdKeyValue.Items.L[Index1];
584 Q2.Data.Vector4 := FdKeyValue.Items.L[Index2];
585 QResult := SLerp(A, Q1, Q2);
586 Target.Value := QResult.ToAxisAngle;
587 end else
588 begin
589 Target.Value := SLerp(A,
590 FdKeyValue.Items.L[Index1],
591 FdKeyValue.Items.L[Index2]);
592 end;
593 end;
594
595 procedure TOrientationInterpolatorNode.InterpolatorSet(const Target: TSFRotation; const Index: Integer);
596 begin
597 if KeyValueQuaternions then
598 Target.Value := Quaternion(FdKeyValue.Items.L[Index]).ToAxisAngle
599 else
600 Target.Value := FdKeyValue.Items.L[Index];
601 end;
602
603 function TOrientationInterpolatorNode.CreateOutputField: TSFRotation;
604 begin
605 Result := TSFRotation.CreateUndefined(Self, false, '');
606 end;
607
608 function TOrientationInterpolatorNode.Interpolate(const TimeKey: Single): TVector4;
609 var
610 T: Single;
611 KeyValue: TVector4List;
612 Range: Integer;
613 begin
614 if FdKey.Items.Count = 0 then
615 begin
616 WritelnWarning('TOrientationInterpolatorNode has no keys, cannot interpolate, returning rotation = zero');
617 Exit(TVector4.Zero);
618 end;
619
620 Range := KeyRange(TimeKey, T);
621 KeyValue := FdKeyValue.Items;
622
623 if KeyValueQuaternions then
624 begin
625 if Range = 0 then
626 Result := Quaternion(KeyValue[0]).ToAxisAngle
627 else
628 if Range = KeyValue.Count then
629 Result := Quaternion(KeyValue[KeyValue.Count - 1]).ToAxisAngle
630 else
631 // SLerp, like TOrientationInterpolatorNode.InterpolatorLerp
632 Result := SLerp(T, Quaternion(KeyValue[Range - 1]), Quaternion(KeyValue[Range])).ToAxisAngle;
633 end else
634 begin
635 if Range = 0 then
636 Result := KeyValue[0]
637 else
638 if Range = KeyValue.Count then
639 Result := KeyValue[KeyValue.Count - 1]
640 else
641 // SLerp, like TOrientationInterpolatorNode.InterpolatorLerp
642 Result := SLerp(T, KeyValue[Range - 1], KeyValue[Range]);
643 end;
644 end;
645
646 { TPositionInterpolatorNode -------------------------------------------------- }
647
648 function TPositionInterpolatorNode.KeyValueCount: TListSize;
649 begin
650 Result := FdKeyValue.Items.Count;
651 end;
652
653 procedure TPositionInterpolatorNode.InterpolatorLerp(const Target: TSFVec3f;
654 const A: Single; const Index1, Index2: Integer);
655 begin
656 Target.Value := Lerp(A,
657 FdKeyValue.Items.L[Index1],
658 FdKeyValue.Items.L[Index2]);
659 end;
660
661 procedure TPositionInterpolatorNode.InterpolatorSet(const Target: TSFVec3f; const Index: Integer);
662 begin
663 Target.Value := FdKeyValue.Items.L[Index];
664 end;
665
666 function TPositionInterpolatorNode.CreateOutputField: TSFVec3f;
667 begin
668 Result := TSFVec3f.CreateUndefined(Self, false, '');
669 end;
670
671 function TPositionInterpolatorNode.Interpolate(const TimeKey: Single): TVector3;
672 var
673 T: Single;
674 KeyValue: TVector3List;
675 Range: Integer;
676 begin
677 if FdKey.Items.Count = 0 then
678 begin
679 WritelnWarning('TPositionInterpolatorNode has no keys, cannot interpolate, returning position = zero');
680 Exit(TVector3.Zero);
681 end;
682
683 Range := KeyRange(TimeKey, T);
684 KeyValue := FdKeyValue.Items;
685
686 if Range = 0 then
687 Result := KeyValue[0]
688 else
689 if Range = KeyValue.Count then
690 Result := KeyValue[KeyValue.Count - 1]
691 else
692 Result := TVector3.Lerp(T, KeyValue[Range - 1], KeyValue[Range]);
693 end;
694
695 { TPositionInterpolator2DNode -------------------------------------------------- }
696
697 function TPositionInterpolator2DNode.KeyValueCount: TListSize;
698 begin
699 Result := FdKeyValue.Items.Count;
700 end;
701
702 procedure TPositionInterpolator2DNode.InterpolatorLerp(const Target: TSFVec2f;
703 const A: Single; const Index1, Index2: Integer);
704 begin
705 Target.Value := Lerp(A,
706 FdKeyValue.Items.L[Index1],
707 FdKeyValue.Items.L[Index2]);
708 end;
709
710 procedure TPositionInterpolator2DNode.InterpolatorSet(const Target: TSFVec2f; const Index: Integer);
711 begin
712 Target.Value := FdKeyValue.Items.L[Index];
713 end;
714
715 function TPositionInterpolator2DNode.CreateOutputField: TSFVec2f;
716 begin
717 Result := TSFVec2f.CreateUndefined(Self, false, '');
718 end;
719
720 { TScalarInterpolatorNode -------------------------------------------------- }
721
722 function TScalarInterpolatorNode.KeyValueCount: TListSize;
723 begin
724 Result := FdKeyValue.Items.Count;
725 end;
726
727 procedure TScalarInterpolatorNode.InterpolatorLerp(const Target: TSFFloat;
728 const A: Single; const Index1, Index2: Integer);
729 begin
730 Target.Value := Lerp(A,
731 FdKeyValue.Items.L[Index1],
732 FdKeyValue.Items.L[Index2]);
733 end;
734
735 procedure TScalarInterpolatorNode.InterpolatorSet(const Target: TSFFloat; const Index: Integer);
736 begin
737 Target.Value := FdKeyValue.Items.L[Index];
738 end;
739
740 function TScalarInterpolatorNode.CreateOutputField: TSFFloat;
741 begin
742 Result := TSFFloat.CreateUndefined(Self, false, '');
743 end;
744
745 { TVectorInterpolatorNode ---------------------------------------------------- }
746
747 function TVectorInterpolatorNode.FrameValues: TListSize;
748 begin
749 Result := FdKeyValue.Count div FdKey.Count;
750 end;
751
752 function TVectorInterpolatorNode.KeyValueCount: TListSize;
753 begin
754 Result := FdKeyValue.Items.Count div FrameValues;
755 end;
756
757 procedure TVectorInterpolatorNode.InterpolatorLerp(const Target: TMFFloat; const A: Single;
758 const Index1, Index2: Integer);
759 var
760 FV: TListSize;
761 begin
762 FV := FrameValues;
763 Target.Items.AssignLerpRange(A, FdKeyValue.Items, FdKeyValue.Items,
764 Index1 * FV, Index2 * FV, FrameValues);
765 end;
766
767 procedure TVectorInterpolatorNode.InterpolatorSet(const Target: TMFFloat; const Index: Integer);
768 var
769 FV: TListSize;
770 begin
771 FV := FrameValues;
772 Target.Items.Clear;
773 Target.Items.AddSubRange(FdKeyValue.Items, Index * FV, FV);
774 end;
775
776 function TVectorInterpolatorNode.CreateOutputField: TMFFloat;
777 begin
778 Result := TMFFloat.CreateUndefined(Self, false, '');
779 end;
780
781 { registration --------------------------------------------------------------- }
782
783 procedure RegisterInterpolationNodes;
784 begin
785 NodesManager.RegisterNodeClasses([
786 TColorInterpolatorNode,
787 TColorSetInterpolatorNode,
788 TCoordinateInterpolatorNode,
789 TCoordinateInterpolator2DNode,
790 TEaseInEaseOutNode,
791 TNormalInterpolatorNode,
792 TOrientationInterpolatorNode,
793 TPositionInterpolatorNode,
794 TPositionInterpolator2DNode,
795 TScalarInterpolatorNode,
796 TSplinePositionInterpolatorNode,
797 TSplinePositionInterpolator2DNode,
798 TSplineScalarInterpolatorNode,
799 TSquadOrientationInterpolatorNode,
800 TVectorInterpolatorNode
801 ]);
802 end;
803
804 {$endif read_implementation}