@@ -345,18 +345,61 @@ convert_anim_node(AnimBundleNode *node, const WorkingNodePath &node_path,
345
345
*/
346
346
void EggSaver::
347
347
convert_character_bundle (PartGroup *bundleNode, EggGroupNode *egg_parent, CharacterJointMap *joint_map) {
348
+ convert_character_bundle (bundleNode, egg_parent, joint_map, nullptr );
349
+ }
350
+
351
+ /* *
352
+ * Converts the indicated Character Bundle to the corresponding Egg joints
353
+ * structure.
354
+ */
355
+ void EggSaver::
356
+ convert_character_bundle (PartGroup *bundleNode, EggGroupNode *egg_parent,
357
+ CharacterJointMap *joint_map, const CharacterJoint *parent_joint) {
348
358
int num_children = bundleNode->get_num_children ();
349
359
360
+ const CharacterJoint *character_joint = nullptr ;
361
+
350
362
EggGroupNode *joint_group = egg_parent;
351
363
if (bundleNode->is_of_type (CharacterJoint::get_class_type ())) {
352
- CharacterJoint * character_joint = DCAST (CharacterJoint, bundleNode);
364
+ character_joint = DCAST (CharacterJoint, bundleNode);
353
365
354
- LMatrix4 transformf;
355
- character_joint->get_transform (transformf);
356
- LMatrix4d transformd (LCAST (double , transformf));
357
366
EggGroup *joint = new EggGroup (bundleNode->get_name ());
358
- joint->add_matrix4 (transformd);
359
367
joint->set_group_type (EggGroup::GT_joint);
368
+
369
+ // The default_value originally passed to the CharacterJoint is what is used
370
+ // for skinning. However, the _default_value can be changed after joint
371
+ // construction (such as via a <DefaultPose>), so we can't just pull the
372
+ // current _default_value.
373
+ //
374
+ // We have to instead work back from the _initial_net_transform_inverse,
375
+ // which is computed at construction time from the original default_value:
376
+ //
377
+ // _net_transform = default_value * parent_joint->_net_transform;
378
+ // _initial_net_transform_inverse = invert(_net_transform);
379
+ //
380
+ // So we should be able to reconstruct the original default_value like so:
381
+ //
382
+ // default_value = invert(_initial_net_transform_inverse)
383
+ // * parent_joint->_initial_net_transform_inverse;
384
+ //
385
+ LMatrix4d net_transform = invert (LCAST (double , character_joint->_initial_net_transform_inverse ));
386
+ if (parent_joint != nullptr ) {
387
+ if (parent_joint->_initial_net_transform_inverse != character_joint->_initial_net_transform_inverse ) {
388
+ LMatrix4d parent_inverse = LCAST (double , parent_joint->_initial_net_transform_inverse );
389
+ joint->add_matrix4 (net_transform * parent_inverse);
390
+ }
391
+ } else if (!net_transform.is_identity ()) {
392
+ joint->add_matrix4 (net_transform);
393
+ }
394
+
395
+ // The joint's _default_value, if different, goes into a <DefaultPose>.
396
+ LMatrix4d default_pose = LCAST (double , character_joint->_default_value );
397
+ if (default_pose != joint->get_transform3d ()) {
398
+ EggTransform transform;
399
+ transform.add_matrix4 (LCAST (double , default_pose));
400
+ joint->set_default_pose (transform);
401
+ }
402
+
360
403
joint_group = joint;
361
404
egg_parent->add_child (joint_group);
362
405
if (joint_map != nullptr ) {
@@ -373,7 +416,7 @@ convert_character_bundle(PartGroup *bundleNode, EggGroupNode *egg_parent, Charac
373
416
374
417
for (int i = 0 ; i < num_children ; i++) {
375
418
PartGroup *partGroup= bundleNode->get_child (i);
376
- convert_character_bundle (partGroup, joint_group, joint_map);
419
+ convert_character_bundle (partGroup, joint_group, joint_map, character_joint );
377
420
}
378
421
379
422
}
0 commit comments