-
Notifications
You must be signed in to change notification settings - Fork 7
/
tanimation.cxs
876 lines (614 loc) · 20.4 KB
/
tanimation.cxs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
Import minib3d
Import minib3d.monkeybuffer
''
'' NOTES:
'' -- does not transform normals. TODO?
Class TVertexAnim Extends FloatBuffer
Field offset:Int
Function Create:TVertexAnim(i:Int=0)
Local b:TVertexAnim = New TVertexAnim
b.buf = FloatBuffer.Create(i*SIZE+1).buf
b.offset=0
Return b
End
End
Class TAnimation
Private
Global quat:Quaternion
Global new_mat:Matrix = New Matrix
Public
'' testing
Global testi:Int=0
Function AnimateMesh(ent1:TEntity,framef:Float,start_frame:Int,end_frame:Int)
Local temp_vec:Vector = New Vector
Local quat:Quaternion = New Quaternion
Local mesh:TMesh = TMesh(ent1)
If mesh<>Null
If mesh.anim=0 Or mesh.Hidden() = True Then Return ' mesh contains no anim data
mesh.anim_render=True
' cap framef values
If framef>end_frame Then framef=end_frame
If framef<start_frame Then framef=start_frame
For Local bent:TBone=Eachin mesh.bones
If Not bent Continue
' position
temp_vec = bent.keys.GetPosition(framef, start_frame, end_frame)
' store current keyframe for use with transtions
bent.kx=temp_vec.x
bent.ky=temp_vec.y
bent.kz=temp_vec.z
' rotation
quat = bent.keys.GetQuaternion(framef, start_frame, end_frame)
' store current keyframe for use with transtions
bent.kqw=quat.w
bent.kqx=quat.x
bent.kqy=quat.y
bent.kqz=quat.z
TBone(bent).Transform( temp_vec, quat, False)
Next
' update children when all bones are placed
TBone.UpdateBoneChildren(mesh )
' --- vertex deform ---
VertexDeform(mesh)
Endif
End
' AnimateMesh2, used to animate transitions between animations, very similar to AnimateMesh except it
' interpolates between current animation pose (via saved keyframe) and first keyframe of new animation.
' framef:Float interpolates between 0 and 1
Function AnimateMesh2(ent1:TEntity,framef:Float,start_frame,end_frame)
Local temp_vec:Vector = New Vector
Local mesh:TMesh = TMesh(ent1)
If mesh<>Null
If mesh.anim=0 Or mesh.Hidden() = True Then Return ' mesh contains no anim data
mesh.anim_render=True
'Local frame=framef ' float to int
Local quat:Quaternion = New Quaternion
For Local bent:TBone=Eachin mesh.bones
Local i=0
Local ii=0
Local fd1:Float=framef ' fd1 always between 0 and 1 for this function
Local fd2:Float=1.0-fd1 ' fd1+fd2 always equals 0 for this function
Local found=False
Local no_keys=False
Local w1:Float
' get current keyframe
Local x1:Float=TBone(bent).kx
Local y1:Float=TBone(bent).ky
Local z1:Float=TBone(bent).kz
Local w2:Float
Local x2:Float
Local y2:Float
Local z2:Float
Local flag=0
' position
' forwards
'i=frame
i=start_frame-1
Repeat
i=i+1
If i>end_frame Then i=start_frame;ii=ii+1
flag=TBone(bent).keys.flags[i]&1
If flag
x2=TBone(bent).keys.px[i]
y2=TBone(bent).keys.py[i]
z2=TBone(bent).keys.pz[i]
'fd2=i-framef
found=True
Endif
Until found=True Or ii>=2
If found=False Then no_keys=True
found=False
ii=0
Local px3:Float=0
Local py3:Float=0
Local pz3:Float=0
If no_keys=True ' no keyframes
px3=TBone(bent).n_px
py3=TBone(bent).n_py
pz3=TBone(bent).n_pz
Else
If fd1+fd2=0.0 ' one keyframe
' if only one keyframe, fd1+fd2 will equal 0 resulting in division error and garbage positional values (which can affect children)
' so we check for this, and if true then positional values equals x1,y1,z1 (same as x2,y2,z2)
px3=x1
py3=y1
pz3=z1
Else ' more than one keyframe
Local fd_inv:Float = 1.0/(fd1+fd2)
px3=(((x2-x1)*fd_inv)*fd1)+x1
py3=(((y2-y1)*fd_inv)*fd1)+y1
pz3=(((z2-z1)*fd_inv)*fd1)+z1
Endif
Endif
no_keys=False
' get current keyframe
w1=TBone(bent).kqw
x1=TBone(bent).kqx
y1=TBone(bent).kqy
z1=TBone(bent).kqz
' rotation
' forwards
'i=frame
i=start_frame-1
Repeat
i=i+1
If i>end_frame Then i=start_frame;ii=ii+1
flag=TBone(bent).keys.flags[i]&4
If flag
w2=TBone(bent).keys.qw[i]
x2=TBone(bent).keys.qx[i]
y2=TBone(bent).keys.qy[i]
z2=TBone(bent).keys.qz[i]
'fd2=i-framef
found=True
Endif
Until found=True Or ii>=2
If found=False Then no_keys=True
found=False
ii=0
' interpolate keys
Local w3:Float=0
Local x3:Float=0
Local y3:Float=0
Local z3:Float=0
If no_keys=True ' no keyframes
w3=TBone(bent).n_qw
x3=TBone(bent).n_qx
y3=TBone(bent).n_qy
z3=TBone(bent).n_qz
Else
If fd1+fd2=0.0 ' one keyframe
' if only one keyframe, fd1+fd2 will equal 0 resulting in division error and garbage rotational values (which can affect children)
' so we check for this, and if true then rotational values equals w1,x1,y1,z1 (same as w2,x2,y2,z2)
w3=w1
x3=x1
y3=y1
z3=z1
Else ' more than one keyframe
Local t:Float=(1.0/(fd1+fd2))*fd1
quat = Quaternion.Slerp(x1,y1,z1,w1,x2,y2,z2,w2,t) ' interpolate between prev and next rotations
Endif
Endif
no_keys=False
temp_vec.Update(px3, py3, pz3)
TBone(bent).Transform( temp_vec, quat)
Next
' --- vertex deform ---
VertexDeform(mesh)
Endif
End
'' AnimateVertex()
''
'' -- does not handle frame interpolation for fractional frames, since VBO based
Function AnimateVertex(ent:TEntity, frame:Float, start_frame:Int, end_frame:Int )
''each key, interpolate all vert_anims
Local vx:Float, vy:Float ,vz:Float, has_animation:Bool=False
Local mesh:TMesh = TMesh(ent)
If Not ent Then Return
If frame>end_frame Then frame=end_frame
If frame<start_frame Then frame=start_frame
' cycle through all surfs
Local anim_surf:TSurface
For Local surf:TSurface=Eachin mesh.surf_list
anim_surf = mesh.GetAnimSurface(surf)
If Not anim_surf Then Continue
has_animation = True
anim_surf.vbo_dyn = True
'Local vanim:Int=0, pack:Int=0, pack_id:Int=0, get_next_pack:Int=1
'Local va_id:Int=0
''point to new buffer
If anim_surf.vert_anim[frame]
'If DONT_USE_VERT_POINTER = False
'anim_surf.anim_frame = frame
mesh.anim_surf_frame[surf.surf_id] = frame '' move frame tracking to mesh, to reuse vertex surfaces
anim_surf.reset_vbo = anim_surf.reset_vbo|1
'Else
'' --- per vertex memory copy, slow, used for XNA
'For Local vid:Int=0 To anim_surf.no_verts-1
'Local vv:Int = vid*3
'anim_surf.vert_data.PokeVertCoords(vid, anim_surf.vert_anim[frame].Peek(vv), anim_surf.vert_anim[frame].Peek(vv+1), anim_surf.vert_anim[frame].Peek(vv+2))
'Next
''update vbo
'anim_surf.reset_vbo = anim_surf.reset_vbo|1
'Endif
Endif
Next
If has_animation Then mesh.anim_render=True
End
Function VertexDeform:Void(ent:TMesh)
'Local ovx:Float,ovy:Float,ovz:Float ' original vertex positions
Local ov:Vector = New Vector ' original vertex positions
Local x:Float=0,y:Float=0,z:Float=0
Local bone:TBone
Local weight:Float
' cycle through all surfs
For Local surf:TSurface=Eachin ent.surf_list
Local anim_surf:TSurface = ent.GetAnimSurface(surf)
If Not anim_surf Then Continue
' mesh shape will be changed, update reset_vbo flag (1=vertices move)
anim_surf.reset_vbo = anim_surf.reset_vbo|1
anim_surf.vbo_dyn = True
Local vid:Int
Local vid3:Int
For Local vid:Int=0 To anim_surf.no_verts-1
vid3=vid*3
' BONE 1
Local tweight:Float=0.0
If anim_surf.vert_bone1_no[vid]<>0
bone=ent.bones[anim_surf.vert_bone1_no[vid]-1]
weight=anim_surf.vert_weight1[vid]
tweight += weight
If weight > 0.0
' get original vertex position
'Local j:Int = vid3*4
'ovx=surf.vert_data.VertexX(vid) 'surf.vert_coords.buf.PeekFloat(j+0) 'VertexX(vid)
'ovy=surf.vert_data.VertexY(vid) 'surf.vert_coords.buf.PeekFloat(j+4) 'VertexY(vid)
'ovz=surf.vert_data.VertexZ(vid) 'surf.vert_coords.buf.PeekFloat(j+8) 'VertexZ(vid)
surf.vert_data.GetVertCoords(ov, vid)
' transform vertex position with transform mat
x= ( bone.tform_mat.grid[0][0]*ov.x + bone.tform_mat.grid[1][0]*ov.y + bone.tform_mat.grid[2][0]*ov.z + bone.tform_mat.grid[3][0] ) * weight '+ (1.0-weight)*ovx
y= ( bone.tform_mat.grid[0][1]*ov.x + bone.tform_mat.grid[1][1]*ov.y + bone.tform_mat.grid[2][1]*ov.z + bone.tform_mat.grid[3][1] ) * weight '+ (1.0-weight)*ovy
z= ( bone.tform_mat.grid[0][2]*ov.x + bone.tform_mat.grid[1][2]*ov.y + bone.tform_mat.grid[2][2]*ov.z + bone.tform_mat.grid[3][2] ) * weight '+ (1.0-weight)*ovz
Endif
' BONE 2
If anim_surf.vert_bone2_no[vid]<>0 And anim_surf.vert_weight2[vid]>0.0
bone=ent.bones[anim_surf.vert_bone2_no[vid]-1]
weight=anim_surf.vert_weight2[vid]
tweight += weight
' transform vertex position with transform mat
x+= ( bone.tform_mat.grid[0][0]*ov.x + bone.tform_mat.grid[1][0]*ov.y + bone.tform_mat.grid[2][0]*ov.z + bone.tform_mat.grid[3][0] ) * weight '+ (1.0-weight2-weight)*ovx
y+= ( bone.tform_mat.grid[0][1]*ov.x + bone.tform_mat.grid[1][1]*ov.y + bone.tform_mat.grid[2][1]*ov.z + bone.tform_mat.grid[3][1] ) * weight '+ (1.0-weight2-weight)*ovy
z+= ( bone.tform_mat.grid[0][2]*ov.x + bone.tform_mat.grid[1][2]*ov.y + bone.tform_mat.grid[2][2]*ov.z + bone.tform_mat.grid[3][2] ) * weight '+ (1.0-weight2-weight)*ovz
' BONE 3
If anim_surf.vert_bone3_no[vid]<>0 And anim_surf.vert_weight3[vid]>0.0
bone=ent.bones[anim_surf.vert_bone3_no[vid]-1]
weight=anim_surf.vert_weight3[vid]
tweight +=weight
' transform vertex position with transform mat
x+= ( bone.tform_mat.grid[0][0]*ov.x + bone.tform_mat.grid[1][0]*ov.y + bone.tform_mat.grid[2][0]*ov.z + bone.tform_mat.grid[3][0] ) * weight '+ (1.0-weight3-weight2-weight)*ovx
y+= ( bone.tform_mat.grid[0][1]*ov.x + bone.tform_mat.grid[1][1]*ov.y + bone.tform_mat.grid[2][1]*ov.z + bone.tform_mat.grid[3][1] ) * weight '+ (1.0-weight3-weight2-weight)*ovy
z+= ( bone.tform_mat.grid[0][2]*ov.x + bone.tform_mat.grid[1][2]*ov.y + bone.tform_mat.grid[2][2]*ov.z + bone.tform_mat.grid[3][2] ) * weight '+ (1.0-weight3-weight2-weight)*ovz
' BONE 4
If anim_surf.vert_bone4_no[vid]<>0 And anim_surf.vert_weight4[vid]>0.0
bone=ent.bones[anim_surf.vert_bone4_no[vid]-1]
weight=anim_surf.vert_weight4[vid]
tweight +=weight
' transform vertex position with transform mat
x+= ( bone.tform_mat.grid[0][0]*ov.x + bone.tform_mat.grid[1][0]*ov.y + bone.tform_mat.grid[2][0]*ov.z + bone.tform_mat.grid[3][0] ) * weight '+ (1.0-weight4-weight3-weight2-weight)*ovx
y+= ( bone.tform_mat.grid[0][1]*ov.x + bone.tform_mat.grid[1][1]*ov.y + bone.tform_mat.grid[2][1]*ov.z + bone.tform_mat.grid[3][1] ) * weight '+ (1.0-weight4-weight3-weight2-weight)*ovy
z+= ( bone.tform_mat.grid[0][2]*ov.x + bone.tform_mat.grid[1][2]*ov.y + bone.tform_mat.grid[2][2]*ov.z + bone.tform_mat.grid[3][2] ) * weight '+ (1.0-weight4-weight3-weight2-weight)*ovz
Endif
Endif
Endif
#rem
'' if we need this, it can be added. try to use stock functions: TAnimation.NormaliseWeights()
If tweight <1.0
x+= (1.0-tweight)*ovx '*bone.n_sx
y+= (1.0-tweight)*ovy '*bone.n_sy
z+= (1.0-tweight)*ovz '*bone.n_sz
Endif
#End
' update vertex position
'anim_surf.VertexCoords(vid,x,y,z)
anim_surf.vert_data.PokeVertCoords(vid,x,y,z)
'Local j:= vid3*4
'anim_surf.vert_coords.buf.PokeFloat(j+0,x)
'anim_surf.vert_coords.buf.PokeFloat(j+4,y)
'anim_surf.vert_coords.buf.PokeFloat(j+8,z)
Endif
Next
Next
End
' this function will normalise weights if their sum doesn't equal 1.0 (unused)
Function NormaliseWeights(mesh:TMesh)
Local anim_surf:TSurface
' cycle through all surfs
For Local surf:TSurface=Eachin mesh.surf_list
anim_surf = mesh.anim_surf[surf.surf_id]
If Not anim_surf Then Continue
For Local vid=0 Until anim_surf.no_verts
' normalise weights
Local w1:Float=anim_surf.vert_weight1[vid]
Local w2:Float=anim_surf.vert_weight2[vid]
Local w3:Float=anim_surf.vert_weight3[vid]
Local w4:Float=anim_surf.vert_weight4[vid]
Local wt:Float=w1+w2+w3+w4
' normalise weights if sum of them <> 1.0
If wt<0.99 Or wt>1.01
Local wm:Float
If wt<>0.0
wm=1.0/wt
w1=w1*wm
w2=w2*wm
w3=w3*wm
w4=w4*wm
Else
wm=1.0
Endif
Endif
If w1 < 0.001 Then w1 = 0.0
If w1 > 0.999 Then w1 = 1.0
If w2 < 0.001 Then w2 = 0.0
If w2 > 0.999 Then w2 = 1.0
If w3 < 0.001 Then w3 = 0.0
If w3 > 0.999 Then w3 = 1.0
If w4 < 0.001 Then w4 = 0.0
If w4 > 0.999 Then w4 = 1.0
anim_surf.vert_weight1[vid]=w1
anim_surf.vert_weight2[vid]=w2
anim_surf.vert_weight3[vid]=w3
anim_surf.vert_weight4[vid]=w4
Next
Next
End
'' LimitBoneWeights()
'' --will reduce the influence of bones per vertex to n bones. n=4 has no effect
'' --for mobile performance, use during initialization
'' -- think about: adding a variable to mesh, that can allow immediate bone limits for LOD
Function LimitBoneWeights(mesh:TMesh, no_bones:Int=1)
If Not mesh Or no_bones > 3 Then Return
Local anim_surf:TSurface
For Local surf:TSurface=Eachin mesh.surf_list
anim_surf = mesh.anim_surf[surf.surf_id]
If Not anim_surf Then Continue
For Local vid:Int =0 Until anim_surf.no_verts
If no_bones < 4
anim_surf.vert_bone4_no[vid]=0
Endif
If no_bones < 3
anim_surf.vert_bone3_no[vid]=0
Endif
If no_bones < 2
anim_surf.vert_bone2_no[vid]=0
Endif
Next
Next
End
'' BoneToVertexAnim()
''--converts bone animation to vertex animation
''-- can get memory hungry
Function BoneToVertexAnim:Void(mesh:TMesh)
''find all keys
'' each bone could have x number of keys
Local maxkeys:Int=0
For Local bone:TBone=Eachin mesh.bones
If bone.keys.frames > maxkeys Then maxkeys = bone.keys.frames
Next
Local no_verts:Int=0
Local surf:TSurface
For surf = Eachin mesh.surf_list
no_verts += surf.no_verts
'surf.vert_anim = surf.vert_anim.Resize(maxkeys+1)
Local anim_surf:TSurface = mesh.GetAnimSurface(surf)
If Not anim_surf Then Continue
anim_surf.vert_anim = New TVertexAnim[maxkeys+1]
Next
Local qx:Float, qy:Float, qz:Float, qw:Float, rot:Float[4]
Local bPos:Vector, bQuat:Quaternion
Local basemesh:Float[no_verts*3]
''maxkeys is inclusive i guess (not maxkeys-1)
For Local i:Int =0 To maxkeys
For Local bone:TBone=Eachin mesh.bones
bPos= bone.keys.GetPosition(i,0,bone.keys.frames)
bQuat= bone.keys.GetQuaternion(i,0,bone.keys.frames)
bone.Transform(bPos, bQuat)
Next
''deform & store
''mesh.anim_surf[] holds new vert info
VertexDeform(mesh)
Local vx:Float, vy:Float, vz:Float
Local sid:Int
Local ov:Vector = New Vector
For surf = Eachin mesh.surf_list
Local pack:Int=0, pack_id:Int=0
Local no_anim_verts:Int=0
Local org_surf:TSurface = mesh.GetAnimSurface(surf)
If Not org_surf Then Continue
org_surf.reset_vbo=-1
'org_surf.vert_anim[i] = New TVertexAnim ''new set of anim keys per surface
org_surf.vert_anim[i] = TVertexAnim.Create(surf.no_verts*3)
sid = surf.surf_id
For Local j:Int =0 To surf.no_verts-1
Local j3:Int = j*3
org_surf.vert_data.GetVertCoords(ov, j)
org_surf.vert_anim[i].PokeVertCoords(j,ov.x, ov.y, ov.z)
Next ''all verts
Next 'all surfs
Next 'all keys
''clear bones & bone keys
For Local e:TEntity = Eachin TBone.GetBaseBones(mesh) 'mesh.child_list
If e Then e.FreeEntity()
Next
mesh.bones=mesh.bones.Resize(0)
''set new anim=2 number for vert animation
mesh.ActivateVertexAnim()
End
End
Class TAnimationKeys
Field frames:Int
Field flags:Int[1]
Field px:Float[1]
Field py:Float[1]
Field pz:Float[1]
Field sx:Float[1]
Field sy:Float[1]
Field sz:Float[1]
Field qw:Float[1]
Field qx:Float[1]
Field qy:Float[1]
Field qz:Float[1]
Method New()
End Method
Method Delete()
End Method
Method Copy:TAnimationKeys()
Local keys:TAnimationKeys=New TAnimationKeys
keys.frames=frames
keys.flags=flags[..]
keys.px=px[..]
keys.py=py[..]
keys.pz=pz[..]
keys.sx=sx[..]
keys.sy=sy[..]
keys.sz=sz[..]
keys.qw=qw[..]
keys.qx=qx[..]
keys.qy=qy[..]
keys.qz=qz[..]
Return keys
End Method
'' loop is reserved for future
Method GetPosition:Vector(frame:Float, f_start:Int, f_end:Int, loop:Bool = True)
Local i:Int=0
Local ii:Int=0
Local fd1:Float=0 ' anim time since last key
Local fd2:Float=0 ' anim time until next key
Local found:Bool=False
Local no_keys:Bool=False
Local w1:Float
Local x1:Float
Local y1:Float
Local z1:Float
Local w2:Float
Local x2:Float
Local y2:Float
Local z2:Float
Local flag:Int=0
' position
' backwards
i=Int(frame)+1
Repeat
i=i-1
flag=flags[i]&1 'pos
If flag
x1=px[i]
y1=py[i]
z1=pz[i]
fd1=frame-i
found=True
Endif
If i<=f_start Then i=f_end+1;ii=ii+1
Until found=True Or ii>=2
If found=False Then no_keys=True
found=False
ii=0
' forwards
i=Int(frame)
Repeat
i=i+1
If i>f_end Then i=f_start ;ii=ii+1
flag=flags[i]&1
If flag
x2=px[i]
y2=py[i]
z2=pz[i]
fd2=i-frame
found=True
Endif
Until found=True Or ii>=2
If found=False Then no_keys=True
found=False
ii=0
Local px3:Float=0
Local py3:Float=0
Local pz3:Float=0
If no_keys=True ' no keyframes
px3=0
py3=0
pz3=0
Else
If fd1+fd2=0.0 ' one keyframe
' if only one keyframe, fd1+fd2 will equal 0 resulting in division error and garbage positional values (which can affect children)
' so we check for this, and if true then positional values equals x1,y1,z1 (same as x2,y2,z2)
px3=x1
py3=y1
pz3=z1
Else ' more than one keyframe
Local fd_inv:Float = 1.0/(fd1+fd2)
px3=(((x2-x1)*fd_inv)*fd1)+x1
py3=(((y2-y1)*fd_inv)*fd1)+y1
pz3=(((z2-z1)*fd_inv)*fd1)+z1
Endif
Endif
no_keys=False
Return New Vector(px3,py3,pz3)
End
'' loop is reserved for future
Method GetQuaternion:Quaternion(frame:Float, f_start:Int, f_end:Int, loop:Bool = True)
Local i:Int=0
Local ii:Int=0
Local fd1:Float=0 ' anim time since last key
Local fd2:Float=0 ' anim time until next key
Local found:Bool=False
Local no_keys:Bool=False
Local w1:Float
Local x1:Float
Local y1:Float
Local z1:Float
Local w2:Float
Local x2:Float
Local y2:Float
Local z2:Float
Local flag:Int =0
Local quat:Quaternion = New Quaternion
i=Int(frame)+1
Repeat
i=i-1
flag=flags[i]&4
If flag
w1=qw[i]
x1=qx[i]
y1=qy[i]
z1=qz[i]
fd1=frame-i
found=True
Endif
If i<=f_start Then i=f_end+1;ii=ii+1
Until found=True Or ii>=2
If found=False Then no_keys=True
found=False
ii=0
' forwards
i=Int(frame)
Repeat
i=i+1
If i>f_end Then i=f_start;ii=ii+1
flag=flags[i]&4
If flag
w2=qw[i]
x2=qx[i]
y2=qy[i]
z2=qz[i]
fd2=i-frame
found=True
Endif
Until found=True Or ii>=2
If found=False Then no_keys=True
found=False
ii=0
' interpolate keys
Local w3:Float=0
Local x3:Float=0
Local y3:Float=0
Local z3:Float=0
If no_keys=True ' no keyframes
quat.w=1.0
quat.x=0
quat.y=0
quat.z=0
Else
If fd1+fd2=0.0 ' one keyframe
' if only one keyframe, fd1+fd2 will equal 0 resulting in division error and garbage rotational values (which can affect children)
' so we check for this, and if true then rotational values equals w1,x1,y1,z1 (same as w2,x2,y2,z2)
quat.w=w1
quat.x=x1
quat.y=y1
quat.z=z1
Else ' more than one keyframe
Local t:Float=(1.0/(fd1+fd2))*fd1
quat = Quaternion.Slerp(x1,y1,z1,w1,x2,y2,z2,w2,t) ' interpolate between prev and next rotations
Endif
Endif
Return quat
End
End