-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAvatarGraphics.cpp
More file actions
1676 lines (1324 loc) · 79.4 KB
/
Copy pathAvatarGraphics.cpp
File metadata and controls
1676 lines (1324 loc) · 79.4 KB
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=====================================================================
AvatarGraphics.cpp
------------------
Copyright Glare Technologies Limited 2021 -
=====================================================================*/
#include "AvatarGraphics.h"
#include "AnimationManager.h"
#include "AvatarGroundingUtils.h"
#include "PhysicsWorld.h"
#include "ParticleManager.h"
#include "../shared/GestureSettings.h"
#include "opengl/OpenGLEngine.h"
#include "opengl/MeshPrimitiveBuilding.h"
#include "opengl/OpenGLMeshRenderData.h"
#include "graphics/FormatDecoderGLTF.h"
#include "../dll/include/IndigoMesh.h"
#include <utils/ConPrint.h>
#include <utils/StringUtils.h>
#include <utils/FileInStream.h>
#include <utils/FileOutStream.h>
#include <utils/FileUtils.h>
#include <utils/PlatformUtils.h>
#include <Jolt/Physics/PhysicsSystem.h>
#include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
#include <Jolt/Physics/Body/BodyCreationSettings.h>
AvatarGraphics::AvatarGraphics(Avatar* avatar_)
: loaded_lod_level(-1),
avatar(avatar_),
our_avatar(false)
{
last_pos.set(0, 0, 0);
last_selected_ob_target_pos.set(0, 0, 0);
last_cam_rotation.set(0,0,0);
avatar_rotation.set(0,0,0);
cur_sideweays_lean = 0;
cur_forwards_lean = 0;
last_vel = Vec3d(0.0);
gesture_neck_quat = Quatf::identity();
gesture_head_quat = Quatf::identity();
cur_eye_target_os = Vec4f(0,0,1,1);
next_eye_target_os = Vec4f(0,0,1,1);
eye_start_transition_time = -2;
eye_end_transition_time = -1;
saccade_gap = 0.5;
last_cam_rotation_time = 0;
avatar_eye_height_above_ground = AvatarGrounding::kDefaultAvatarEyeHeightM;
cur_head_rot_z = 0;
//cur_head_rot_quat = Quatf::identity();
turn_anim_end_time = -1;
turning = false;
}
AvatarGraphics::~AvatarGraphics()
{
}
// rotation is (roll, pitch, heading)
static const Matrix4f rotationMatrix(const Vec3f& rotation)
{
// Just rotate avatar according to heading, for pitch we will move head up/down.
return Matrix4f::rotationAroundZAxis(rotation.z);
}
static const Matrix4f rotateThenTranslateMatrix(const Vec3d& translation, const Vec3f& rotation)
{
Matrix4f m = Matrix4f::rotationAroundZAxis(rotation.z);
m.setColumn(3, Vec4f((float)translation.x, (float)translation.y, (float)translation.z, 1.f));
return m;
}
static float mod2PiDiff(float x)
{
const float diff = Maths::floatMod(x, Maths::get2Pi<float>());
if(diff > Maths::pi<float>())
return diff - Maths::get2Pi<float>();
else
return diff;
}
inline static void setProceduralRotation(js::Vector<GLObjectAnimNodeData, 16>& anim_node_data, const int node_index, const Quatf& rot)
{
if(node_index >= 0 && node_index < (int)anim_node_data.size())
{
anim_node_data[node_index].procedural_rot_mask = 0xFFFFFFFF;
anim_node_data[node_index].procedural_rot = rot;
}
}
inline static void clearProceduralRotation(js::Vector<GLObjectAnimNodeData, 16>& anim_node_data, const int node_index)
{
if(node_index >= 0 && node_index < (int)anim_node_data.size())
anim_node_data[node_index].procedural_rot_mask = 0;
}
// NOTE: from player physics
static const float SPHERE_RAD = 0.3f;
static const float CYLINDER_HEIGHT = 1.3f; // Chosen so the capsule top is about the same height as the head of xbot.glb. Can test this by jumping into an overhead ledge :)
static const float EYE_HEIGHT = 1.67f;
static const float cCharacterHeightStanding = CYLINDER_HEIGHT;
static const float cCharacterRadiusStanding = SPHERE_RAD;
void AvatarGraphics::setOverallTransform(OpenGLEngine& engine, PhysicsWorld& physics_world, ParticleManager& particle_manager, const Vec3d& pos, const Vec3f& cam_rotation,
bool use_xyplane_speed_rel_ground_override, float xyplane_speed_rel_ground_override,
const Matrix4f& pre_ob_to_world_matrix, uint32 anim_state, double cur_time, double dt, const PoseConstraint& pose_constraint, AnimEvents& anim_events_out)
{
if(dt == 0.0) // May happen in web client, avoid dividing by zero below.
return;
if(false && !debug_avatar_basis_ob)
{
debug_avatar_basis_ob = engine.allocateObject();
debug_avatar_basis_ob->mesh_data = MeshPrimitiveBuilding::make3DBasisArrowMesh(*engine.vert_buf_allocator); // Base will be at origin, tip will lie at (1, 0, 0)
debug_avatar_basis_ob->materials.resize(3);
debug_avatar_basis_ob->materials[0].albedo_linear_rgb = Colour3f(0.9f, 0.1f, 0.1f);
debug_avatar_basis_ob->materials[1].albedo_linear_rgb = Colour3f(0.1f, 0.9f, 0.1f);
debug_avatar_basis_ob->materials[2].albedo_linear_rgb = Colour3f(0.1f, 0.1f, 0.9f);
debug_avatar_basis_ob->ob_to_world_matrix = Matrix4f::translationMatrix(1000, 0, 0);
engine.addObject(debug_avatar_basis_ob);
}
if(skinned_gl_ob && skinned_gl_ob->mesh_data)
{
// If this is not our avatar, create a physics object for the avatar so we can do mouse picking of it.
if(!our_avatar)
{
if(!physics_ob)
{
JPH::Ref<JPH::Shape> standing_shape = new JPH::CapsuleShape(/*inHalfHeightOfCylinder=*/0.5f * cCharacterHeightStanding, /*inRadius=*/cCharacterRadiusStanding);
PhysicsShape shape;
shape.jolt_shape = standing_shape;
physics_ob = new PhysicsObject(/*collidable=*/false, shape, /*userdata=*/(void*)avatar, /*userdata_type=*/3);
physics_ob->pos = pos.toVec4fPoint();
physics_ob->rot = Quatf::xAxisRot(Maths::pi_2<float>()); // Rotate from the cylinder extending in y direction to extending in z direction.
physics_ob->scale = Vec3f(1.f);
physics_ob->motion_type = PhysicsObject::MotionType_semi_static; // Not dynamic or kinematic but moves occasionally.
physics_world.addObject(physics_ob);
}
// We will translate the position of the capsule down from eye position to the centre of the avatar.
// TODO: rotate based on current head bone and hip bone relative position?
const Vec4f last_eye_pos = getLastHeadPosition(); // Use animated head position so it works for animations with root motion.
physics_world.setNewPosition(*physics_ob, last_eye_pos - Vec4f(0, 0, avatar_eye_height_above_ground / 2.f, 0));
}
const AnimationData& anim_data = skinned_gl_ob->mesh_data->animation_data;
js::Vector<GLObjectAnimNodeData, 16>& anim_node_data = skinned_gl_ob->anim_node_data;
if(cur_time >= skinned_gl_ob->transition_end_time && skinned_gl_ob->next_anim_i != -1)
{
skinned_gl_ob->current_anim_i = skinned_gl_ob->next_anim_i;
skinned_gl_ob->next_anim_i = -1;
//conPrint("Finished transitioning to anim " + skinned_gl_ob->mesh_data->animation_data.animations[skinned_gl_ob->current_anim_i]->name);
}
int new_anim_i = 0;
const Vec4f forwards_vec = rotationMatrix(avatar_rotation) * Vec4f(1,0,0,0);
const Vec4f right_vec = rotationMatrix(avatar_rotation) * Vec4f(0,1,0,0);
//const Vec4f up_vec = rotationMatrix(avatar_rotation) * Vec4f(0,0,1,0);
const bool on_ground = (anim_state & ANIM_STATE_IN_AIR) == 0;
const Vec3d dpos = pos - last_pos;
const Vec3d vel = dpos / dt;
const double speed = vel.length();
// Only consider speed in x-y plane when deciding whether to play walk/run anim etc..
// This is because the stair-climbing code may make jumps in the z coordinate which means a very high z velocity.
double xyplane_speed = Vec3d(vel.x, vel.y, 0).length();
if(use_xyplane_speed_rel_ground_override)
xyplane_speed = xyplane_speed_rel_ground_override; // Use overriding value (from local physics sim if this is our avatar)
// Set xyplane_speed to zero if the controller of the avatar is not trying to move it, and it is on the ground.
// This prevents spurious walk movements when riding platforms in some circumstances (when player velocity does not equal ground velocity for some reason).
// When flying we want to show walk/run anims when coming to a halt against the ground though.
if(on_ground && BitUtils::isBitSet(anim_state, ANIM_STATE_MOVE_IMPULSE_ZERO) && !BitUtils::isBitSet(anim_state, ANIM_STATE_FLYING))
xyplane_speed = 0;
const Vec3d old_vel = last_vel;
const Vec3d accel = (vel - old_vel) / dt;
float unclamped_sideways_accel = dot(right_vec, accel.toVec4fVector());
float unclamped_forwards_accel = dot(forwards_vec, accel.toVec4fVector());
// Don't lean if stationary with respect to ground.
if(on_ground && xyplane_speed < 0.1)
{
unclamped_sideways_accel = 0;
unclamped_forwards_accel = 0;
}
const Vec3f drot = cam_rotation - last_cam_rotation;
//const Vec3f rot_vel = drot / (float)dt;
if(drot.length() > 0.01)
last_cam_rotation_time = cur_time;
Matrix4f lean_matrix = Matrix4f::identity();
// float turn_forwards_nudge = 0;
double new_anim_transition_duration = 0.3; // Duration of the blend period, if we have a new animation to transition to.
const Vec4f up_os(0,1,0,0);
if(pose_constraint.sitting)
{
new_anim_i = idle_anim_i;
const Vec4f seat_right_ws = pose_constraint.seat_to_world * Vec4f(1,0,0,0);
avatar_rotation.z = std::atan2(seat_right_ws[1], seat_right_ws[0]) + Maths::pi_2<float>();
avatar_rotation.y = Maths::pi_2<float>(); // pitch angle
Vec4f hips_pos_os(0,0,0,1);
if(hips_node_i >= 0 && hips_node_i < (int)skinned_gl_ob->anim_node_data.size())
{
hips_pos_os = (Matrix4f::rotationAroundZAxis(Maths::pi<float>()) * pre_ob_to_world_matrix * skinned_gl_ob->anim_node_data[hips_node_i].last_pre_proc_to_object) * Vec4f(0,0,0,1);
}
// pre_ob_to_world_matrix will rotate avatars from y-up and z-forwards to z-up and -y forwards. Rotate around z axis to change to +y-forwards
skinned_gl_ob->ob_to_world_matrix = pose_constraint.seat_to_world * /*move hips to seat position=*/Matrix4f::translationMatrix(-hips_pos_os) * Matrix4f::rotationAroundZAxis(Maths::pi<float>()) * pre_ob_to_world_matrix;
Matrix4f world_to_ob_matrix;
skinned_gl_ob->ob_to_world_matrix.getInverseForAffine3Matrix(world_to_ob_matrix);
if(hips_node_i >= 0 && hips_node_i < (int)skinned_gl_ob->anim_node_data.size()) // Upper torso
{
skinned_gl_ob->anim_node_data[hips_node_i].procedural_transform = Matrix4f::rotationAroundXAxis(-pose_constraint.upper_body_rot_angle); // Apply recline rotation to upper body (lean back)
}
if( left_foot_node_i >= 0 && left_foot_node_i < (int)skinned_gl_ob->anim_node_data.size() &&
right_foot_node_i >= 0 && right_foot_node_i < (int)skinned_gl_ob->anim_node_data.size())
{}
// Set upper leg rotation
if( left_up_leg_node_i >= 0 && left_up_leg_node_i < (int)skinned_gl_ob->anim_node_data.size() &&
right_up_leg_node_i >= 0 && right_up_leg_node_i < (int)skinned_gl_ob->anim_node_data.size())
{
{
const Matrix4f last_bone_to_object_space = skinned_gl_ob->anim_node_data[left_up_leg_node_i].last_pre_proc_to_object; // last bone to object space (y-up) transformation.
const Quatf bone_to_object_space_rot = Quatf::fromMatrix(last_bone_to_object_space);
const Quatf desired_rot_os = /*rotate around thigh bone to move lower leg outwards=*/Quatf::fromAxisAndAngle(Vec4f(0,0,1,0), pose_constraint.upper_leg_rot_around_thigh_bone_angle) *
/*rotate legs outwards=*/Quatf::fromAxisAndAngle(Vec4f(0,1,0,0), pose_constraint.upper_leg_apart_angle) *
Quatf::fromAxisAndAngle(Vec4f(1,0,0,0), /*to rotate to down=*/Maths::pi<float>() - pose_constraint.upper_leg_rot_angle) * Quatf::fromAxisAndAngle(Vec4f(0,1,0,0), Maths::pi<float>());
// Note that node_transform = last_pre_proc_to_object * ob->anim_node_data[node_i].procedural_transform, so we want to undo the bone-to-object-space rotation last (so it should be on left)
skinned_gl_ob->anim_node_data[left_up_leg_node_i ].procedural_transform = (bone_to_object_space_rot.conjugate() * desired_rot_os).toMatrix();
}
{
const Matrix4f last_bone_to_object_space = skinned_gl_ob->anim_node_data[right_up_leg_node_i].last_pre_proc_to_object; // last bone to object space (y-up) transformation.
const Quatf bone_to_object_space_rot = normalise(Quatf::fromMatrix(last_bone_to_object_space));
const Quatf desired_rot_os = /*rotate around thigh bone to move lower leg outwards=*/Quatf::fromAxisAndAngle(Vec4f(0,0,1,0), -pose_constraint.upper_leg_rot_around_thigh_bone_angle) *
/*rotate legs outwards=*/Quatf::fromAxisAndAngle(Vec4f(0,1,0,0), -pose_constraint.upper_leg_apart_angle) *
Quatf::fromAxisAndAngle(Vec4f(1,0,0,0), /*to rotate to down=*/Maths::pi<float>() - pose_constraint.upper_leg_rot_angle) * Quatf::fromAxisAndAngle(Vec4f(0,1,0,0), Maths::pi<float>());
skinned_gl_ob->anim_node_data[right_up_leg_node_i].procedural_transform = (bone_to_object_space_rot.conjugate() * desired_rot_os).toMatrix();
}
}
if( left_knee_node_i >= 0 && left_knee_node_i < (int)skinned_gl_ob->anim_node_data.size() &&
right_knee_node_i >= 0 && right_knee_node_i < (int)skinned_gl_ob->anim_node_data.size())
{
// Bend lower leg at knee
skinned_gl_ob->anim_node_data[left_knee_node_i ].procedural_transform = Matrix4f::rotationAroundZAxis(-pose_constraint.rotate_foot_out_angle) * /*move lower leg out=*/Matrix4f::rotationAroundYAxis( pose_constraint.lower_leg_apart_angle) * Matrix4f::rotationAroundXAxis(pose_constraint.lower_leg_rot_angle);
skinned_gl_ob->anim_node_data[right_knee_node_i].procedural_transform = Matrix4f::rotationAroundZAxis( pose_constraint.rotate_foot_out_angle) * /*move lower leg out=*/Matrix4f::rotationAroundYAxis(-pose_constraint.lower_leg_apart_angle) * Matrix4f::rotationAroundXAxis(pose_constraint.lower_leg_rot_angle);
}
// Inverse kinematics for left arm grab
const bool do_left_arm_IK_grab = isFinite(pose_constraint.left_hand_hold_point_ws[0]);
if(do_left_arm_IK_grab &&
left_shoulder_node_i >= 0 && left_shoulder_node_i < (int)anim_node_data.size() &&
left_arm_node_i >= 0 && left_arm_node_i < (int)anim_node_data.size() &&
left_forearm_node_i >= 0 && left_forearm_node_i < (int)anim_node_data.size() &&
left_hand_node_i >= 0 && left_hand_node_i < (int)anim_node_data.size())
{
const Vec4f last_shoulder_pos_os = anim_node_data[left_arm_node_i ].node_hierarchical_to_object.getColumn(3);
const Vec4f last_elbow_pos_os = anim_node_data[left_forearm_node_i].node_hierarchical_to_object.getColumn(3);
// Compute distance from shoulder to hand hold position
const Vec4f last_shoulder_pos_ws = skinned_gl_ob->ob_to_world_matrix * last_shoulder_pos_os;
const Vec4f last_elbow_pos_ws = skinned_gl_ob->ob_to_world_matrix * last_elbow_pos_os;
// DEBUG: Set transform of debug basis arrows.
//debug_avatar_basis_ob->ob_to_world_matrix = Matrix4f::translationMatrix(last_elbow_pos_ws) * Matrix4f::uniformScaleMatrix(1.2f);
/*debug_avatar_basis_ob->ob_to_world_matrix = skinned_gl_ob->ob_to_world_matrix * skinned_gl_ob->anim_node_data[left_forearm_node_i].node_hierarchical_to_object;
engine.updateObjectTransformData(*debug_avatar_basis_ob);*/
// Make the target wrist position a few cm out from the handlebar, in the direction of the shoulder.
const Vec4f sideways_out_dir_ws = normalise(crossProduct(last_shoulder_pos_ws - pose_constraint.left_hand_hold_point_ws, Vec4f(0,0,1,0)));
const Vec4f target_wrist_pos_ws = pose_constraint.left_hand_hold_point_ws + normalise(last_shoulder_pos_ws -
pose_constraint.left_hand_hold_point_ws) * 0.08 -
Vec4f(0,0,0.05, 0) + sideways_out_dir_ws * 0.02f;
const float hold_shoulder_len = target_wrist_pos_ws.getDist(last_shoulder_pos_ws);
const Vec4f hold_pos_os = world_to_ob_matrix * target_wrist_pos_ws;
/*
From OpenGLEngine:
const Matrix4f last_pre_proc_to_object = (node_data.parent_index == -1) ? TRS : (node_matrices[node_data.parent_index] * node_data.retarget_adjustment * TRS); // Transform without procedural_transform applied
const Matrix4f node_transform = last_pre_proc_to_object * ob->anim_node_data[node_i].procedural_transform;
so
hand_T = lower_arm_T * hand_retarget_T * hand_TRS * hand_procedural_T
lower_arm_T = upper_arm_T * lower_arm_retarget_T * lower_arm_TRS * lower_arm_procedural_T
upper_arm_T = shoulder_T * upper_arm_retarget_T * upper_arm_TRS * upper_arm_procedural_T
hand_T = [upper_arm_T * lower_arm_retarget_T * lower_arm_TRS * lower_arm_procedural_T] * hand_retarget_T * hand_TRS * hand_procedural_T
wrist_pos = hand_T * (0,0,0)
*/
// https://en.wikipedia.org/wiki/Law_of_cosines
const float a = upper_left_arm_len;
const float b = lower_left_arm_len;
const float c = hold_shoulder_len;
const float cos_gamma = (a*a + b*b - c*c) / (2*a*b);
const float gamma = (cos_gamma > -1.f && cos_gamma < 1.f) ? std::acos(cos_gamma) : Maths::pi<float>();
anim_node_data[left_forearm_node_i ].procedural_rot_mask = 0xFFFFFFFF;
anim_node_data[left_forearm_node_i ].procedural_rot = Quatf::xAxisRot(Maths::pi<float>() + gamma);
anim_node_data[left_forearm_node_i ].procedural_transform = Matrix4f::identity();
// upper-arm to object space transformation matrix, without procedural IK rotation.
const Matrix4f upper_arm_T =
anim_node_data[left_shoulder_node_i].node_hierarchical_to_object * // shoulder_T
anim_data.nodes[left_arm_node_i].retarget_adjustment * // upper_arm_retarget_T
Matrix4f::translationMatrix(anim_data.nodes[left_arm_node_i].trans); // upper_arm_TRS [no rotation]
// no procedural transform
// Compute object-space to upper-arm space transformation matrix, will be used later.
Matrix4f ob_to_upper_arm_T;
upper_arm_T.getInverseForAffine3Matrix(ob_to_upper_arm_T);
const Vec4f unrotated_wrist_pos_os =
upper_arm_T *
(anim_data.nodes[left_forearm_node_i].retarget_adjustment *
(Matrix4f::translationMatrix(anim_data.nodes[left_forearm_node_i].trans) * (anim_node_data[left_forearm_node_i ].procedural_rot.toMatrix() * // Lower arm TRS
(anim_data.nodes[left_hand_node_i].retarget_adjustment *
(Matrix4f::translationMatrix(anim_data.nodes[left_hand_node_i].trans)/* * skinned_gl_ob->anim_node_data[left_hand_node_i ].procedural_rot.toMatrix()*/ * // hand TRS
Vec4f(0,0,0,1))))));
const Vec4f unrotated_wrist_pos_ws = skinned_gl_ob->ob_to_world_matrix * unrotated_wrist_pos_os;
// Rotate arm around shoulder so that the shoulder-wrist vector is aligned with shoulder-hold.
Vec4f rotate_axis_os = crossProduct(unrotated_wrist_pos_os - last_shoulder_pos_os, hold_pos_os - last_shoulder_pos_os);
if(rotate_axis_os.length() > 0.001f)
{
rotate_axis_os = normalise(rotate_axis_os);
const Vec4f rotate_axis_arm_space = ob_to_upper_arm_T * rotate_axis_os;
const float rotate_angle = acos(dot(normalise(unrotated_wrist_pos_os - last_shoulder_pos_os), normalise(hold_pos_os - last_shoulder_pos_os)));
anim_node_data[left_arm_node_i].procedural_rot_mask = 0xFFFFFFFF; // Don't apply animation rotation, apply procedural_rot instead
anim_node_data[left_arm_node_i].procedural_rot = Quatf::fromAxisAndAngle(rotate_axis_arm_space, rotate_angle);
anim_node_data[left_arm_node_i ].procedural_transform = Matrix4f::identity();
}
}
else // Else not doing inverse kinematics for left arm:
{
if(left_arm_node_i >= 0 && left_arm_node_i < (int)anim_node_data.size())
{
const Matrix4f last_left_arm_bone_to_object_space = anim_node_data[left_arm_node_i].last_pre_proc_to_object; // last left-arm bone to object space (y-up) transformation.
const Quatf bone_to_object_space_rot = Quatf::fromMatrix(last_left_arm_bone_to_object_space);
const Quatf desired_rot_os = /*rot out=*/Quatf::yAxisRot(pose_constraint.arm_out_angle) * /*rot down=*/Quatf::xAxisRot(pose_constraint.arm_down_angle) *
Quatf::zAxisRot(-pose_constraint.upper_arm_shoulder_lift_angle);
// Note that node_transform = last_pre_proc_to_object * ob->anim_node_data[node_i].procedural_transform, so we want to undo the bone-to-object-space rotation last (so it should be on left)
anim_node_data[left_arm_node_i ].procedural_transform = (bone_to_object_space_rot.conjugate() * desired_rot_os).toMatrix();
}
// Bend lower arms (at elbow)
if(left_forearm_node_i >= 0 && left_forearm_node_i < (int)anim_node_data.size())
{
anim_node_data[left_forearm_node_i ].procedural_transform = Matrix4f::rotationAroundXAxis(-pose_constraint.lower_arm_up_angle);
}
}
// Inverse kinematics for right arm grab. Code very similar to left arm code above.
const bool do_right_arm_IK_grab = isFinite(pose_constraint.right_hand_hold_point_ws[0]);
if(do_right_arm_IK_grab &&
right_shoulder_node_i >= 0 && right_shoulder_node_i < (int)anim_node_data.size() &&
right_arm_node_i >= 0 && right_arm_node_i < (int)anim_node_data.size() &&
right_forearm_node_i >= 0 && right_forearm_node_i < (int)anim_node_data.size() &&
right_hand_node_i >= 0 && right_hand_node_i < (int)anim_node_data.size())
{
const Vec4f last_shoulder_pos_os = anim_node_data[right_arm_node_i ].node_hierarchical_to_object.getColumn(3);
const Vec4f last_elbow_pos_os = anim_node_data[right_forearm_node_i].node_hierarchical_to_object.getColumn(3);
const Vec4f last_shoulder_pos_ws = skinned_gl_ob->ob_to_world_matrix * last_shoulder_pos_os;
const Vec4f last_elbow_pos_ws = skinned_gl_ob->ob_to_world_matrix * last_elbow_pos_os;
// Make the target wrist position a few cm out from the handlebar, in the direction of the shoulder.
const Vec4f sideways_out_dir_ws = normalise(crossProduct(last_shoulder_pos_ws - pose_constraint.right_hand_hold_point_ws, Vec4f(0,0,1,0)));
const Vec4f target_wrist_pos_ws = pose_constraint.right_hand_hold_point_ws + normalise(last_shoulder_pos_ws -
pose_constraint.right_hand_hold_point_ws) * 0.08 -
Vec4f(0,0,0.05, 0) + sideways_out_dir_ws * 0.02f;
const float hold_shoulder_len = target_wrist_pos_ws.getDist(last_shoulder_pos_ws);
const Vec4f hold_pos_os = world_to_ob_matrix * target_wrist_pos_ws;
// https://en.wikipedia.org/wiki/Law_of_cosines
const float a = upper_right_arm_len;
const float b = lower_right_arm_len;
const float c = hold_shoulder_len;
const float cos_gamma = (a*a + b*b - c*c) / (2*a*b);
const float gamma = (cos_gamma > -1.f && cos_gamma < 1.f) ? std::acos(cos_gamma) : Maths::pi<float>();
anim_node_data[right_forearm_node_i ].procedural_rot_mask = 0xFFFFFFFF;
anim_node_data[right_forearm_node_i ].procedural_rot = Quatf::xAxisRot(Maths::pi<float>() + gamma);
anim_node_data[right_forearm_node_i ].procedural_transform = Matrix4f::identity();
// upper-arm to object space transformation matrix, without procedural IK rotation.
Matrix4f upper_arm_T =
anim_node_data[right_shoulder_node_i].node_hierarchical_to_object * // shoulder_T
anim_data.nodes[right_arm_node_i].retarget_adjustment * // upper_arm_retarget_T
Matrix4f::translationMatrix(anim_data.nodes[right_arm_node_i].trans); // upper_arm_TRS [no rotation]
// no procedural transform
Matrix4f ob_to_upper_arm_T;
upper_arm_T.getInverseForAffine3Matrix(ob_to_upper_arm_T);
const Vec4f unrotated_wrist_pos_os =
upper_arm_T *
(anim_data.nodes[right_forearm_node_i].retarget_adjustment *
(Matrix4f::translationMatrix(anim_data.nodes[right_forearm_node_i].trans) * (anim_node_data[right_forearm_node_i ].procedural_rot.toMatrix() * // Lower arm TRS
(anim_data.nodes[right_hand_node_i].retarget_adjustment *
(Matrix4f::translationMatrix(anim_data.nodes[right_hand_node_i].trans) * // hand TRS
Vec4f(0,0,0,1))))));
const Vec4f unrotated_wrist_pos_ws = skinned_gl_ob->ob_to_world_matrix * unrotated_wrist_pos_os;
// DEBUG VIS POSITION
//debug_avatar_basis_ob->ob_to_world_matrix = Matrix4f::translationMatrix(pose_constraint.right_hand_hold_point_ws) * Matrix4f::uniformScaleMatrix(0.5f);
//engine.updateObjectTransformData(*debug_avatar_basis_ob);
// Rotate arm around shoulder so that the shoulder-wrist vector is aligned with shoulder-hold.
Vec4f rotate_axis_os = crossProduct(unrotated_wrist_pos_os - last_shoulder_pos_os, hold_pos_os - last_shoulder_pos_os);
if(rotate_axis_os.length() > 0.001f)
{
rotate_axis_os = normalise(rotate_axis_os);
const Vec4f rotate_axis_arm_space = ob_to_upper_arm_T * rotate_axis_os;
const float rotate_angle = acos(dot(normalise(unrotated_wrist_pos_os - last_shoulder_pos_os), normalise(hold_pos_os - last_shoulder_pos_os)));
anim_node_data[right_arm_node_i].procedural_rot_mask = 0xFFFFFFFF; // Don't apply animation rotation, apply procedural_rot instead
anim_node_data[right_arm_node_i].procedural_rot = Quatf::fromAxisAndAngle(rotate_axis_arm_space, rotate_angle);
anim_node_data[right_arm_node_i].procedural_transform = Matrix4f::identity();
}
}
else // Else not doing IK for right arm:
{
if(right_arm_node_i >= 0 && right_arm_node_i < (int)anim_node_data.size())
{
const Matrix4f last_right_arm_bone_to_object_space = anim_node_data[right_arm_node_i].last_pre_proc_to_object; // last right-arm bone to object space (y-up) transformation.
const Quatf bone_to_object_space_rot = Quatf::fromMatrix(last_right_arm_bone_to_object_space);
const Quatf desired_rot_os = /*rot out=*/Quatf::yAxisRot(-pose_constraint.arm_out_angle) * /*rot down=*/Quatf::xAxisRot(pose_constraint.arm_down_angle) *
Quatf::zAxisRot(pose_constraint.upper_arm_shoulder_lift_angle);
anim_node_data[right_arm_node_i ].procedural_transform = (bone_to_object_space_rot.conjugate() * desired_rot_os).toMatrix();
}
// Bend lower arms (at elbow)
if(right_forearm_node_i >= 0 && right_forearm_node_i < (int)anim_node_data.size())
{
anim_node_data[right_forearm_node_i].procedural_transform = Matrix4f::rotationAroundXAxis(-pose_constraint.lower_arm_up_angle);
}
}
//------------------- Set grabbing pose for hands and fingers if doing IK grabbing -----------------------
// NOTE: z-axis is down for both hands
if(do_left_arm_IK_grab)
setProceduralRotation(anim_node_data, left_hand_node_i,
Quatf::xAxisRot(-0.6f) * // rotate hand up around wrist
Quatf::yAxisRot(-0.2f) * // rotate hand around lower arm bone.
Quatf::zAxisRot(-0.5f) // Rotate hand outwards roughly around up vector at wrist
);
if(do_right_arm_IK_grab)
setProceduralRotation(anim_node_data, right_hand_node_i,
Quatf::xAxisRot(-0.6f) * // rotate hand up around wrist
Quatf::yAxisRot(-0.2f) * // rotate hand around lower arm bone.
Quatf::zAxisRot(0.5f) // Rotate hand outwards roughly around up vector at wrist
);
// DEBUG VIS Node transform
//if(debug_avatar_basis_ob && left_hand_node_i >= 0 && left_hand_node_i < skinned_gl_ob->anim_node_data.size())
//{
// debug_avatar_basis_ob->ob_to_world_matrix = skinned_gl_ob->ob_to_world_matrix * skinned_gl_ob->anim_node_data[left_hand_node_i].node_hierarchical_to_object * Matrix4f::uniformScaleMatrix(0.5f);
// engine.updateObjectTransformData(*debug_avatar_basis_ob);
//}
if(do_left_arm_IK_grab)
{
setProceduralRotation(anim_node_data, LeftHandThumb1_i, Quatf::zAxisRot(0.5) * Quatf::xAxisRot(0.9));
setProceduralRotation(anim_node_data, LeftHandThumb2_i, Quatf::yAxisRot(0.0) * Quatf::zAxisRot(-0.2));
setProceduralRotation(anim_node_data, LeftHandThumb3_i, Quatf::xAxisRot(0.9) * Quatf::zAxisRot(-1.1));
}
if(do_right_arm_IK_grab)
{
setProceduralRotation(anim_node_data, RightHandThumb1_i, Quatf::zAxisRot(-0.5) * Quatf::xAxisRot(0.9));
setProceduralRotation(anim_node_data, RightHandThumb2_i, Quatf::yAxisRot(0.0) * Quatf::zAxisRot(0.2));
setProceduralRotation(anim_node_data, RightHandThumb3_i, Quatf::xAxisRot(0.9) * Quatf::zAxisRot(1.1));
}
const float joint_1_rot = 1.0f;
const float joint_2_rot = 0.7f;
const float joint_3_rot = 1.2f;
if(do_left_arm_IK_grab)
{
setProceduralRotation(anim_node_data, LeftHandIndex1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, LeftHandIndex2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, LeftHandIndex3_i, Quatf::xAxisRot(joint_3_rot));
setProceduralRotation(anim_node_data, LeftHandMiddle1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, LeftHandMiddle2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, LeftHandMiddle3_i, Quatf::xAxisRot(joint_3_rot));
setProceduralRotation(anim_node_data, LeftHandRing1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, LeftHandRing2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, LeftHandRing3_i, Quatf::xAxisRot(joint_3_rot));
setProceduralRotation(anim_node_data, LeftHandPinky1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, LeftHandPinky2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, LeftHandPinky3_i, Quatf::xAxisRot(joint_3_rot));
}
if(do_right_arm_IK_grab)
{
setProceduralRotation(anim_node_data, RightHandIndex1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, RightHandIndex2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, RightHandIndex3_i, Quatf::xAxisRot(joint_3_rot));
setProceduralRotation(anim_node_data, RightHandMiddle1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, RightHandMiddle2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, RightHandMiddle3_i, Quatf::xAxisRot(joint_3_rot));
setProceduralRotation(anim_node_data, RightHandRing1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, RightHandRing2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, RightHandRing3_i, Quatf::xAxisRot(joint_3_rot));
setProceduralRotation(anim_node_data, RightHandPinky1_i, Quatf::xAxisRot(joint_1_rot));
setProceduralRotation(anim_node_data, RightHandPinky2_i, Quatf::xAxisRot(joint_2_rot));
setProceduralRotation(anim_node_data, RightHandPinky3_i, Quatf::xAxisRot(joint_3_rot));
}
}
else // Else if not sitting:
{
// Reset any procedural_transforms we set to the identity matrix.
if(hips_node_i >= 0 && hips_node_i < (int)anim_node_data.size()) // Upper torso
{
anim_node_data[hips_node_i].procedural_transform = Matrix4f::identity();
}
if( left_up_leg_node_i >= 0 && left_up_leg_node_i < (int)anim_node_data.size() &&
right_up_leg_node_i >= 0 && right_up_leg_node_i < (int)anim_node_data.size())
{
anim_node_data[left_up_leg_node_i].procedural_transform = Matrix4f::identity();
anim_node_data[right_up_leg_node_i].procedural_transform = Matrix4f::identity();
}
if( left_knee_node_i >= 0 && left_knee_node_i < (int)anim_node_data.size() &&
right_knee_node_i >= 0 && right_knee_node_i < (int)anim_node_data.size())
{
anim_node_data[left_knee_node_i].procedural_transform = Matrix4f::identity();
anim_node_data[right_knee_node_i].procedural_transform = Matrix4f::identity();
}
if( left_arm_node_i >= 0 && left_arm_node_i < (int)anim_node_data.size() &&
right_arm_node_i >= 0 && right_arm_node_i < (int)anim_node_data.size())
{
anim_node_data[left_arm_node_i ].procedural_transform = Matrix4f::identity();
anim_node_data[right_arm_node_i ].procedural_transform = Matrix4f::identity();
}
if( left_forearm_node_i >= 0 && left_forearm_node_i < (int)anim_node_data.size() &&
right_forearm_node_i >= 0 && right_forearm_node_i < (int)anim_node_data.size())
{
anim_node_data[left_forearm_node_i].procedural_transform = Matrix4f::identity();
anim_node_data[right_forearm_node_i].procedural_transform = Matrix4f::identity();
}
clearProceduralRotation(anim_node_data, left_arm_node_i);
clearProceduralRotation(anim_node_data, left_forearm_node_i);
clearProceduralRotation(anim_node_data, right_arm_node_i);
clearProceduralRotation(anim_node_data, right_forearm_node_i);
clearProceduralRotation(anim_node_data, left_hand_node_i);
clearProceduralRotation(anim_node_data, right_hand_node_i);
clearProceduralRotation(anim_node_data, LeftHandThumb1_i);
clearProceduralRotation(anim_node_data, LeftHandThumb2_i);
clearProceduralRotation(anim_node_data, LeftHandThumb3_i);
clearProceduralRotation(anim_node_data, LeftHandIndex1_i);
clearProceduralRotation(anim_node_data, LeftHandIndex2_i);
clearProceduralRotation(anim_node_data, LeftHandIndex3_i);
clearProceduralRotation(anim_node_data, LeftHandMiddle1_i);
clearProceduralRotation(anim_node_data, LeftHandMiddle2_i);
clearProceduralRotation(anim_node_data, LeftHandMiddle3_i);
clearProceduralRotation(anim_node_data, LeftHandRing1_i);
clearProceduralRotation(anim_node_data, LeftHandRing2_i);
clearProceduralRotation(anim_node_data, LeftHandRing3_i);
clearProceduralRotation(anim_node_data, LeftHandPinky1_i);
clearProceduralRotation(anim_node_data, LeftHandPinky2_i);
clearProceduralRotation(anim_node_data, LeftHandPinky3_i);
clearProceduralRotation(anim_node_data, RightHandThumb1_i);
clearProceduralRotation(anim_node_data, RightHandThumb2_i);
clearProceduralRotation(anim_node_data, RightHandThumb3_i);
clearProceduralRotation(anim_node_data, RightHandIndex1_i);
clearProceduralRotation(anim_node_data, RightHandIndex2_i);
clearProceduralRotation(anim_node_data, RightHandIndex3_i);
clearProceduralRotation(anim_node_data, RightHandMiddle1_i);
clearProceduralRotation(anim_node_data, RightHandMiddle2_i);
clearProceduralRotation(anim_node_data, RightHandMiddle3_i);
clearProceduralRotation(anim_node_data, RightHandRing1_i);
clearProceduralRotation(anim_node_data, RightHandRing2_i);
clearProceduralRotation(anim_node_data, RightHandRing3_i);
clearProceduralRotation(anim_node_data, RightHandPinky1_i);
clearProceduralRotation(anim_node_data, RightHandPinky2_i);
clearProceduralRotation(anim_node_data, RightHandPinky3_i);
if(on_ground) // if on ground:
{
const float max_accel_mag = 10.2f;
float clamped_sideways_accel = myClamp(unclamped_sideways_accel, -max_accel_mag, max_accel_mag);
float clamped_forwards_accel = myClamp(unclamped_forwards_accel, -max_accel_mag, max_accel_mag);
const float blend_frac = 0.03f;
cur_sideweays_lean = cur_sideweays_lean * (1 - blend_frac) + clamped_sideways_accel * blend_frac;
cur_forwards_lean = cur_forwards_lean * (1 - blend_frac) + clamped_forwards_accel * blend_frac;
// We had some problems with these values becoming NaN due to dt being zero in the webclient.
// Just reset to zero if they end up NaN
if(!isFinite(cur_sideweays_lean))
cur_sideweays_lean = 0;
if(!isFinite(cur_forwards_lean))
cur_forwards_lean = 0;
//const float forwards_vel = dot(forwards_vec, vel.toVec4fVector());
const bool moving_forwards = dot(forwards_vec, normalise(dpos.toVec4fVector())) > -0.1f;
//if(speed > 0.1 && (forwards_vel < -0.1f || forwards_vel > 0.1f))
lean_matrix = Matrix4f::rotationAroundXAxis(cur_sideweays_lean * -0.02f) * Matrix4f::rotationAroundYAxis(cur_forwards_lean * -0.02f); // NOTE: this forwards lean rotation dir is probably in the wrong direction, but is not visible anyway.
if(xyplane_speed > 6)
{
// Blend rotation of avatar towards camera rotation
// We consider yaw angle (rotation around up axis) mod 2 pi, and rotate the closest way around the circle.
const float rot_blend_frac = (float)(10 * dt);
Vec3f rot_diff = cam_rotation - this->avatar_rotation;
rot_diff.z = mod2PiDiff(rot_diff.z);
this->avatar_rotation = this->avatar_rotation + rot_diff * rot_blend_frac;
if(moving_forwards)
new_anim_i = running_anim_i;
else
new_anim_i = running_backwards_anim_i;
new_anim_transition_duration = 0.1;
}
else if(xyplane_speed > 0.1)
{
// Blend rotation of avatar towards camera rotation
const float rot_blend_frac = (float)(10 * dt);
Vec3f rot_diff = cam_rotation - this->avatar_rotation;
rot_diff.z = mod2PiDiff(rot_diff.z);
this->avatar_rotation = this->avatar_rotation + rot_diff * rot_blend_frac;
if(moving_forwards)
new_anim_i = walking_anim_i;
else
new_anim_i = walking_backwards_anim_i;
new_anim_transition_duration = 0.2;
}
else // else if (nearly) stationary:
{
const double turn_anim_duration = 57.0 / 60;// Left and right turn anims are 57 frames at 60fps
if(cur_time < turn_anim_end_time) // If we are currently performing a turn left/right anim:
{
// Slowly rotate in the turn direction.
const float turn_sign = turning_left ? 1.f : -1.f;
if(turning_left)
new_anim_i = turn_left_anim_i;
else
new_anim_i = turn_right_anim_i;
{
const double turn_anim_start_time = turn_anim_end_time - turn_anim_duration;
const float frac = (float)((cur_time - turn_anim_start_time) / turn_anim_duration);
this->avatar_rotation = avatar_rotation_at_turn_start + Vec3f(0, 0, turn_sign * ::degreeToRad(118.f) * frac);
const double time_remaining = turn_anim_end_time - cur_time;
if(time_remaining < 0.3)
{
// Start blending into idle pose anim
new_anim_i = idle_anim_i;
}
}
}
else
{
if(turning && (skinned_gl_ob->current_anim_i == turn_left_anim_i || skinned_gl_ob->current_anim_i == turn_right_anim_i)) // If we were performing a turn, and we have finished the animation:
{
//conPrint("Finished the turn anim");
turning = false;
}
if(cur_time < gesture_anim.play_end_time) // if we are playing a gesture:
{
new_anim_i = gesture_anim.anim_i; // Continue current gesture
const double time_remaining = gesture_anim.play_end_time - cur_time;
//printVar(time_remaining);
if(time_remaining < 0.3)
{
// conPrint("Starting blend to idle pose anim");
// Start blending into idle pose anim
new_anim_i = idle_anim_i;
}
}
else // Else if we have finished playing any gesture:
{
//if(next_gesture_anim.anim_i >= 0) // If we have a next gesture:
//{
// gesture_anim = next_gesture_anim;
// next_gesture_anim.anim_i = -1;
// //gesture_anim_i = next_gesture_anim_i;
// //next_gesture_anim_i = -1;
// new_anim_i = gesture_anim.anim_i;
//}
//else
new_anim_i = idle_anim_i;
}
const float yaw_diff = mod2PiDiff(cam_rotation.z - avatar_rotation.z);
// conPrint("cam_rotation.z: " + doubleToStringNSigFigs(cam_rotation.z, 3) + ", avatar_rotation.z: " + doubleToStringNSigFigs(avatar_rotation.z, 3));
if(std::fabs(yaw_diff) > ::degreeToRad(118.f))
{
avatar_rotation_at_turn_start = avatar_rotation;
if(yaw_diff > 0)
{
new_anim_i = turn_left_anim_i;
// conPrint("Starting left turn anim");
turning_left = true;
}
else
{
new_anim_i = turn_right_anim_i;
// conPrint("Starting right turn anim");
turning_left = false;
}
turn_anim_end_time = cur_time + turn_anim_duration;
skinned_gl_ob->use_time_offset = -cur_time; // Set anim time offset so that we are at the beginning of the animation. NOTE: bit of a hack, messes with the blended anim also.
turning = true;
new_anim_transition_duration = 0.3;
}
}
}
}
else // else if not on ground:
{
this->avatar_rotation = cam_rotation;
const bool flying = BitUtils::isBitSet(anim_state, ANIM_STATE_FLYING);
const bool moving_forwards = dot(vel.toVec4fVector(), forwards_vec) > speed * 0.4f;
const float max_accel_mag = 40.f;
float clamped_sideways_accel = myClamp(unclamped_sideways_accel, -max_accel_mag, max_accel_mag);
float clamped_forwards_accel = myClamp(unclamped_forwards_accel, -max_accel_mag, max_accel_mag);
const float blend_frac = 0.03f;
if(!flying) // If jumping:
{
clamped_forwards_accel = 0;
new_anim_transition_duration = 0.15; // Make the transition to jumping faster than the default.
}
cur_sideweays_lean = cur_sideweays_lean * (1 - blend_frac) + clamped_sideways_accel * blend_frac;
cur_forwards_lean = cur_forwards_lean * (1 - blend_frac) + clamped_forwards_accel * blend_frac;
// flying
if(speed > 10 && moving_forwards && flying)
new_anim_i = flying_anim_i;
else
new_anim_i = floating_anim_i;
lean_matrix = Matrix4f::rotationAroundXAxis(cur_sideweays_lean * -0.02f) * Matrix4f::rotationAroundYAxis(cur_forwards_lean * 0.01f);
} // end if not on ground
// Adjust position of avatar upwards if needed, so that the feet and hips are above 'ground' level. ('Ground' level is 1.67 m below the camera position)
// Fixes issues like meebits being partially below ground in some poses.
float lowest_bone_z_os = 0;
if(hips_node_i >= 0 && hips_node_i < (int)skinned_gl_ob->anim_node_data.size())
{
const Vec4f hips_pos_os = (Matrix4f::rotationAroundZAxis(Maths::pi<float>()) * pre_ob_to_world_matrix * skinned_gl_ob->anim_node_data[hips_node_i].last_pre_proc_to_object) * Vec4f(0,0,0,1);
lowest_bone_z_os = myMin(lowest_bone_z_os, hips_pos_os[2]);
}
if(left_foot_node_i >= 0 && left_foot_node_i < (int)skinned_gl_ob->anim_node_data.size())
{
const Vec4f left_foot_pos_os = (Matrix4f::rotationAroundZAxis(Maths::pi<float>()) * pre_ob_to_world_matrix * skinned_gl_ob->anim_node_data[left_foot_node_i].last_pre_proc_to_object) * Vec4f(0,0,0,1);
lowest_bone_z_os = myMin(lowest_bone_z_os, left_foot_pos_os[2]);
}
const float lowest_node_height_above_ground = lowest_bone_z_os + avatar_eye_height_above_ground - 0.03f;
float vertical_adjustment = 0;
if(lowest_node_height_above_ground < 0)
vertical_adjustment = -lowest_node_height_above_ground;
assert(isFinite(vertical_adjustment));
assert(isFinite(avatar_rotation.x));
assert(isFinite(avatar_rotation.y));
assert(isFinite(avatar_rotation.z));
// pre_ob_to_world_matrix will rotate avatars from y-up and z-forwards to z-up and -y forwards. Rotate around z axis to change to +x-forwards
skinned_gl_ob->ob_to_world_matrix = /*Matrix4f::translationMatrix(forwards_vec * turn_forwards_nudge) * */rotateThenTranslateMatrix(pos, avatar_rotation) * lean_matrix * Matrix4f::translationMatrix(0,0,vertical_adjustment) *
Matrix4f::rotationAroundZAxis(Maths::pi_2<float>()) * pre_ob_to_world_matrix;
} // end if not sitting
engine.updateObjectTransformData(*skinned_gl_ob);
// See if we need to start a transition to a new animation
if(new_anim_i != skinned_gl_ob->current_anim_i)
{
// If we are currently transitioning, don't change next
if(cur_time >= skinned_gl_ob->transition_start_time && cur_time < skinned_gl_ob->transition_end_time)
{
// conPrint("Currently transitioning, not changing next state.");
}
else
{
// conPrint("Started transitioning to anim " + skinned_gl_ob->mesh_data->animation_data.animations[new_anim_i]->name + " transition duration: " + doubleToStringNSigFigs(new_anim_transition_duration, 3));
skinned_gl_ob->next_anim_i = new_anim_i;
skinned_gl_ob->transition_start_time = cur_time;
skinned_gl_ob->transition_end_time = cur_time + new_anim_transition_duration;
}
}
last_vel = vel;
// Check node indices are in-bounds
if( head_node_i >= 0 && head_node_i < (int)skinned_gl_ob->anim_node_data.size() &&
neck_node_i >= 0 && neck_node_i < (int)skinned_gl_ob->anim_node_data.size())
{
const bool is_VRM_model = skinned_gl_ob->mesh_data->animation_data.vrm_data.nonNull(); // VRM models tend to have anime eyes which are bigger and can move less without loooking weird.
// TODO: use the metadata limits from the VRM file.
const float MAX_EYE_YAW_MAG = is_VRM_model ? 0.25f : 0.4f; // relative to head
float MAX_EYE_PITCH_MAG_UP = is_VRM_model ? 0.1f : 0.15f; // relative to head
float MAX_EYE_PITCH_MAG_DOWN = is_VRM_model ? 0.15f : 0.4f; // relative to head
const float MAX_HEAD_YAW_MAG = 0.8f;
// Get total yaw and pitch differences between cam rotation and avatar rotation
const float total_yaw_amount = mod2PiDiff(cam_rotation.z - avatar_rotation.z);
const float total_pitch_amount = cam_rotation.y - Maths::pi_2<float>();
//------------------- Do head look-at procedural movement -----------------------
// Get clamped desired/target head yaw.
const float target_head_yaw_amount = myClamp(total_yaw_amount, -MAX_HEAD_YAW_MAG, MAX_HEAD_YAW_MAG);
const float target_head_rot_z = avatar_rotation.z + target_head_yaw_amount; // Get target head rotation given the clamped target yaw
// Blend current head rot z towards target_head_rot_z
const float head_rot_frac = myMin(0.2f, (float)(10 * dt));
this->cur_head_rot_z = this->cur_head_rot_z * (1 - head_rot_frac) + target_head_rot_z * head_rot_frac;
// Update gesture_neck_quat, gesture_head_quat if we are doing a gesture, otherwise leave the last values as is.
const bool doing_gesture_with_animated_head = (cur_time < gesture_anim.play_end_time) && gesture_anim.animated_head;
if(doing_gesture_with_animated_head)
{
gesture_neck_quat = skinned_gl_ob->anim_node_data[neck_node_i].last_rot;
gesture_head_quat = skinned_gl_ob->anim_node_data[head_node_i].last_rot;
}
//const Quatf target_head_rot_quat = Quatf::fromAxisAndAngle(Vec3f(0, 1, 0), target_head_rot_z);
//this->cur_head_rot_quat = Quatf::nlerp(this->cur_head_rot_quat, target_head_rot_quat, 1 - head_rot_frac);
const float unclamped_head_yaw_amount = this->cur_head_rot_z - avatar_rotation.z; // Get actual blended yaw of the head.
const float head_yaw_amount = myClamp(unclamped_head_yaw_amount, -MAX_HEAD_YAW_MAG, MAX_HEAD_YAW_MAG);
const float MAX_HEAD_PITCH_MAG = 0.8f;
const float head_pitch_amount = myClamp(total_pitch_amount, -MAX_HEAD_PITCH_MAG, MAX_HEAD_PITCH_MAG);
// We will interpolate between the gesture head rotation (if the head is being animated), and the procedural lookat rotation.
const float transition_frac = (float)Maths::smoothStep<double>(skinned_gl_ob->transition_start_time, skinned_gl_ob->transition_end_time, cur_time);
float lookat_frac;
if(doing_gesture_with_animated_head)
{
if(new_anim_i == gesture_anim.anim_i) // If we are transitioning *to* the gesture:
lookat_frac = 1 - transition_frac;
else
lookat_frac = transition_frac; // Else we are transitioning out of the gesture.
}
else
lookat_frac = 1;
/*
Lets say we have the head to model transform T:
We want to set T to some target value (T_target) and solve for a procedural head transform that will give T_target.
T = hip_T * neck_T * head_T = body_T * head_T
T_target = body_T * head_T * proc_head
(body_T * head_T)^-1 T_target = proc_head
*/
Matrix4f inverse_last_pre_proc_head_to_world;
skinned_gl_ob->anim_node_data[head_node_i].last_pre_proc_to_object.getInverseForAffine3Matrix(inverse_last_pre_proc_head_to_world);
inverse_last_pre_proc_head_to_world.setColumn(3, Vec4f(0,0,0,1));
// Get rotation quatf from this matrix:
const Quatf inverse_last_pre_proc_head_to_world_rot = Quatf::fromMatrix(inverse_last_pre_proc_head_to_world);
const Quatf last_head_rot = skinned_gl_ob->anim_node_data[head_node_i].last_rot;
const Quatf head_gesture_rot = (last_head_rot.conjugate() * gesture_head_quat); // Procedural rotation that leaves the head in the last rotation (relative to neck) from the gesture
const Quatf head_lookat_rot = inverse_last_pre_proc_head_to_world_rot * Quatf::fromAxisAndAngle(Vec3f(0,1,0), head_yaw_amount) * Quatf::fromAxisAndAngle(Vec3f(1,0,0), head_pitch_amount);
const Quatf head_rot = Quatf::nlerp(head_gesture_rot, head_lookat_rot, lookat_frac);
skinned_gl_ob->anim_node_data[head_node_i].procedural_transform = head_rot.toMatrix();
const float NECK_FACTOR = 0.5f; // relative to amount of head rotation and translation
//const float pitch_move_forwards_factor = head_pitch_amount * 0.0f * NECK_FACTOR;
const float neck_yaw_amount = head_yaw_amount * NECK_FACTOR;
// Keep neutral XR head pose aligned with the horizon instead of baking in a constant downward neck tilt.
const float neck_pitch_amount = head_pitch_amount * NECK_FACTOR;
//Matrix4f neck_rot = Matrix4f::translationMatrix(0, 0, pitch_move_forwards_factor /*- fabs(yaw_amount) * 0.04 * NECK_FACTOR*/) *
// Matrix4f::rotationAroundZAxis(-0.2f * head_yaw_amount * NECK_FACTOR) * Matrix4f::rotationAroundYAxis(head_yaw_amount * NECK_FACTOR) * Matrix4f::rotationAroundXAxis(head_pitch_amount * NECK_FACTOR);
Matrix4f inverse_last_pre_proc_neck_to_world;
skinned_gl_ob->anim_node_data[neck_node_i].last_pre_proc_to_object.getInverseForAffine3Matrix(inverse_last_pre_proc_neck_to_world);
inverse_last_pre_proc_neck_to_world.setColumn(3, Vec4f(0,0,0,1));
const Quatf inverse_last_pre_proc_neck_to_world_rot = Quatf::fromMatrix(inverse_last_pre_proc_neck_to_world);