Pole vectors can be controlled via anim vars.

This commit is contained in:
Anthony J. Thibault 2017-06-13 15:36:05 -07:00
parent 7521d6124e
commit f20c03fa6e
8 changed files with 96 additions and 94 deletions

View file

@ -86,7 +86,9 @@
"typeVar": "rightFootType", "typeVar": "rightFootType",
"weightVar": "rightFootWeight", "weightVar": "rightFootWeight",
"weight": 1.0, "weight": 1.0,
"flexCoefficients": [1, 0.45, 0.45] "flexCoefficients": [1, 0.45, 0.45],
"poleJointNameVar": "rightFootPoleJointName",
"poleVectorVar": "rightFootPoleVector"
}, },
{ {
"jointName": "LeftFoot", "jointName": "LeftFoot",
@ -95,7 +97,9 @@
"typeVar": "leftFootType", "typeVar": "leftFootType",
"weightVar": "leftFootWeight", "weightVar": "leftFootWeight",
"weight": 1.0, "weight": 1.0,
"flexCoefficients": [1, 0.45, 0.45] "flexCoefficients": [1, 0.45, 0.45],
"poleJointNameVar": "leftFootPoleJointName",
"poleVectorVar": "leftFootPoleVector"
}, },
{ {
"jointName": "Spine2", "jointName": "Spine2",

View file

@ -47,12 +47,15 @@ float easeOutExpo(float t) {
} }
AnimInverseKinematics::IKTargetVar::IKTargetVar(const QString& jointNameIn, const QString& positionVarIn, const QString& rotationVarIn, AnimInverseKinematics::IKTargetVar::IKTargetVar(const QString& jointNameIn, const QString& positionVarIn, const QString& rotationVarIn,
const QString& typeVarIn, const QString& weightVarIn, float weightIn, const std::vector<float>& flexCoefficientsIn) : const QString& typeVarIn, const QString& weightVarIn, float weightIn, const std::vector<float>& flexCoefficientsIn,
const QString& poleJointNameVarIn, const QString& poleVectorVarIn) :
jointName(jointNameIn), jointName(jointNameIn),
positionVar(positionVarIn), positionVar(positionVarIn),
rotationVar(rotationVarIn), rotationVar(rotationVarIn),
typeVar(typeVarIn), typeVar(typeVarIn),
weightVar(weightVarIn), weightVar(weightVarIn),
poleJointNameVar(poleJointNameVarIn),
poleVectorVar(poleVectorVarIn),
weight(weightIn), weight(weightIn),
numFlexCoefficients(flexCoefficientsIn.size()), numFlexCoefficients(flexCoefficientsIn.size()),
jointIndex(-1) jointIndex(-1)
@ -69,6 +72,8 @@ AnimInverseKinematics::IKTargetVar::IKTargetVar(const IKTargetVar& orig) :
rotationVar(orig.rotationVar), rotationVar(orig.rotationVar),
typeVar(orig.typeVar), typeVar(orig.typeVar),
weightVar(orig.weightVar), weightVar(orig.weightVar),
poleJointNameVar(orig.poleJointNameVar),
poleVectorVar(orig.poleVectorVar),
weight(orig.weight), weight(orig.weight),
numFlexCoefficients(orig.numFlexCoefficients), numFlexCoefficients(orig.numFlexCoefficients),
jointIndex(orig.jointIndex) jointIndex(orig.jointIndex)
@ -122,8 +127,9 @@ void AnimInverseKinematics::computeAbsolutePoses(AnimPoseVec& absolutePoses) con
} }
void AnimInverseKinematics::setTargetVars(const QString& jointName, const QString& positionVar, const QString& rotationVar, void AnimInverseKinematics::setTargetVars(const QString& jointName, const QString& positionVar, const QString& rotationVar,
const QString& typeVar, const QString& weightVar, float weight, const std::vector<float>& flexCoefficients) { const QString& typeVar, const QString& weightVar, float weight, const std::vector<float>& flexCoefficients,
IKTargetVar targetVar(jointName, positionVar, rotationVar, typeVar, weightVar, weight, flexCoefficients); const QString& poleJointNameVar, const QString& poleVectorVar) {
IKTargetVar targetVar(jointName, positionVar, rotationVar, typeVar, weightVar, weight, flexCoefficients, poleJointNameVar, poleVectorVar);
// if there are dups, last one wins. // if there are dups, last one wins.
bool found = false; bool found = false;
@ -170,63 +176,14 @@ void AnimInverseKinematics::computeTargets(const AnimVariantMap& animVars, std::
target.setIndex(targetVar.jointIndex); target.setIndex(targetVar.jointIndex);
target.setWeight(weight); target.setWeight(weight);
target.setFlexCoefficients(targetVar.numFlexCoefficients, targetVar.flexCoefficients); target.setFlexCoefficients(targetVar.numFlexCoefficients, targetVar.flexCoefficients);
target.setPoleIndex(-1);
/* // AJT: TODO: cache the pole joint index.
// AJT: HACK REMOVE manually set pole vector. QString poleJointName = animVars.lookup(targetVar.poleJointNameVar, QString(""));
if (targetVar.jointName == "RightHand") { int poleJointIndex = _skeleton->nameToJointIndex(poleJointName);
int armJointIndex = _skeleton->nameToJointIndex("RightArm"); target.setPoleIndex(poleJointIndex);
glm::vec3 armPosition = _skeleton->getAbsolutePose(armJointIndex, _relativePoses).trans();
glm::vec3 d = glm::normalize(translation - armPosition);
// project hand y-axis onto d. glm::vec3 poleVector = animVars.lookupRigToGeometryVector(targetVar.poleVectorVar, Vectors::UNIT_Z);
glm::vec3 handY = rotation * Vectors::UNIT_Y; target.setPoleVector(glm::normalize(poleVector));
glm::vec3 handYProj = handY - glm::dot(handY, d) * d;
// project negative pole vector on to d.
glm::vec3 p = glm::normalize(glm::vec3(-1, -3, -3));
glm::vec3 pProj = p - glm::dot(p, d) * d;
// compute a rotation around d that will bring the pProj to to yProj
float theta = acosf(glm::dot(glm::normalize(-pProj), glm::normalize(handYProj)));
glm::vec3 axis = glm::normalize(glm::cross(-pProj, handYProj));
if (glm::dot(axis, d) < 0.0f) {
// as eProjLen and pProjLen become orthognal to d, reduce the amount of rotation.
float magnitude = easeOutExpo(glm::min(glm::length(handYProj), glm::length(pProj)));
glm::quat poleAdjustRot = angleAxis(magnitude * (theta / 4.0f), axis);
target.setPoleVector(poleAdjustRot * p);
} else {
target.setPoleVector(p);
}
target.setPoleIndex(_skeleton->nameToJointIndex(targetVar.jointName));
}
if (targetVar.jointName == "LeftHand") {
target.setPoleVector(glm::normalize(glm::vec3(1, -3, -3)));
target.setPoleIndex(_skeleton->nameToJointIndex(targetVar.jointName));
}
*/
if (targetVar.jointName == "LeftFoot" || targetVar.jointName == "RightFoot") {
// compute the forward direction of the foot.
AnimPose defaultPose = _skeleton->getAbsoluteDefaultPose(targetVar.jointIndex);
glm::vec3 localForward = glm::inverse(defaultPose.rot()) * Vectors::UNIT_Z;
glm::vec3 footForward = rotation * localForward;
// compute the forward direction of the hips.
glm::quat hipsRotation = _skeleton->getAbsolutePose(_hipsIndex, _relativePoses).rot();
glm::vec3 hipsForward = hipsRotation * Vectors::UNIT_Z;
// blend between the hips and the foot.
const float FOOT_TO_HIPS_BLEND_FACTOR = 0.5f;
glm::vec3 poleVector = glm::normalize(lerp(footForward, hipsForward, FOOT_TO_HIPS_BLEND_FACTOR));
// TODO: smooth toward desired pole vector from previous pole vector... to reduce jitter
target.setPoleVector(poleVector);
target.setPoleIndex(_skeleton->nameToJointIndex(targetVar.jointName));
}
targets.push_back(target); targets.push_back(target);

View file

@ -42,7 +42,8 @@ public:
void computeAbsolutePoses(AnimPoseVec& absolutePoses) const; void computeAbsolutePoses(AnimPoseVec& absolutePoses) const;
void setTargetVars(const QString& jointName, const QString& positionVar, const QString& rotationVar, void setTargetVars(const QString& jointName, const QString& positionVar, const QString& rotationVar,
const QString& typeVar, const QString& weightVar, float weight, const std::vector<float>& flexCoefficients); const QString& typeVar, const QString& weightVar, float weight, const std::vector<float>& flexCoefficients,
const QString& poleJointNameVar, const QString& poleVectorVar);
virtual const AnimPoseVec& evaluate(const AnimVariantMap& animVars, const AnimContext& context, float dt, AnimNode::Triggers& triggersOut) override; virtual const AnimPoseVec& evaluate(const AnimVariantMap& animVars, const AnimContext& context, float dt, AnimNode::Triggers& triggersOut) override;
virtual const AnimPoseVec& overlay(const AnimVariantMap& animVars, const AnimContext& context, float dt, Triggers& triggersOut, const AnimPoseVec& underPoses) override; virtual const AnimPoseVec& overlay(const AnimVariantMap& animVars, const AnimContext& context, float dt, Triggers& triggersOut, const AnimPoseVec& underPoses) override;
@ -108,7 +109,8 @@ protected:
enum FlexCoefficients { MAX_FLEX_COEFFICIENTS = 10 }; enum FlexCoefficients { MAX_FLEX_COEFFICIENTS = 10 };
struct IKTargetVar { struct IKTargetVar {
IKTargetVar(const QString& jointNameIn, const QString& positionVarIn, const QString& rotationVarIn, IKTargetVar(const QString& jointNameIn, const QString& positionVarIn, const QString& rotationVarIn,
const QString& typeVarIn, const QString& weightVarIn, float weightIn, const std::vector<float>& flexCoefficientsIn); const QString& typeVarIn, const QString& weightVarIn, float weightIn, const std::vector<float>& flexCoefficientsIn,
const QString& poleJointNameVar, const QString& poleVectorVar);
IKTargetVar(const IKTargetVar& orig); IKTargetVar(const IKTargetVar& orig);
QString jointName; QString jointName;
@ -116,6 +118,8 @@ protected:
QString rotationVar; QString rotationVar;
QString typeVar; QString typeVar;
QString weightVar; QString weightVar;
QString poleJointNameVar;
QString poleVectorVar;
float weight; float weight;
float flexCoefficients[MAX_FLEX_COEFFICIENTS]; float flexCoefficients[MAX_FLEX_COEFFICIENTS];
size_t numFlexCoefficients; size_t numFlexCoefficients;

View file

@ -479,6 +479,8 @@ AnimNode::Pointer loadInverseKinematicsNode(const QJsonObject& jsonObj, const QS
READ_OPTIONAL_STRING(typeVar, targetObj); READ_OPTIONAL_STRING(typeVar, targetObj);
READ_OPTIONAL_STRING(weightVar, targetObj); READ_OPTIONAL_STRING(weightVar, targetObj);
READ_OPTIONAL_FLOAT(weight, targetObj, 1.0f); READ_OPTIONAL_FLOAT(weight, targetObj, 1.0f);
READ_OPTIONAL_STRING(poleJointNameVar, targetObj);
READ_OPTIONAL_STRING(poleVectorVar, targetObj);
auto flexCoefficientsValue = targetObj.value("flexCoefficients"); auto flexCoefficientsValue = targetObj.value("flexCoefficients");
if (!flexCoefficientsValue.isArray()) { if (!flexCoefficientsValue.isArray()) {
@ -491,7 +493,7 @@ AnimNode::Pointer loadInverseKinematicsNode(const QJsonObject& jsonObj, const QS
flexCoefficients.push_back((float)value.toDouble()); flexCoefficients.push_back((float)value.toDouble());
} }
node->setTargetVars(jointName, positionVar, rotationVar, typeVar, weightVar, weight, flexCoefficients); node->setTargetVars(jointName, positionVar, rotationVar, typeVar, weightVar, weight, flexCoefficients, poleJointNameVar, poleVectorVar);
}; };
READ_OPTIONAL_STRING(solutionSource, jsonObj); READ_OPTIONAL_STRING(solutionSource, jsonObj);

View file

@ -479,12 +479,6 @@ bool Rig::getAbsoluteJointPoseInRigFrame(int jointIndex, AnimPose& returnPose) c
} }
} }
bool Rig::getJointCombinedRotation(int jointIndex, glm::quat& result, const glm::quat& rotation) const {
// AJT: TODO: used by attachments
ASSERT(false);
return false;
}
void Rig::calcAnimAlpha(float speed, const std::vector<float>& referenceSpeeds, float* alphaOut) const { void Rig::calcAnimAlpha(float speed, const std::vector<float>& referenceSpeeds, float* alphaOut) const {
ASSERT(referenceSpeeds.size() > 0); ASSERT(referenceSpeeds.size() > 0);
@ -950,6 +944,7 @@ void Rig::updateAnimations(float deltaTime, const glm::mat4& rootTransform, cons
// evaluate the animation // evaluate the animation
AnimNode::Triggers triggersOut; AnimNode::Triggers triggersOut;
_internalPoseSet._relativePoses = _animNode->evaluate(_animVars, context, deltaTime, triggersOut); _internalPoseSet._relativePoses = _animNode->evaluate(_animVars, context, deltaTime, triggersOut);
if ((int)_internalPoseSet._relativePoses.size() != _animSkeleton->getNumJoints()) { if ((int)_internalPoseSet._relativePoses.size() != _animSkeleton->getNumJoints()) {
// animations haven't fully loaded yet. // animations haven't fully loaded yet.
@ -1145,6 +1140,21 @@ void Rig::updateEyeJoint(int index, const glm::vec3& modelTranslation, const glm
} }
} }
glm::vec3 Rig::calculateKneePoleVector(int footJointIndex, const glm::quat& footTargetOrientation, int hipsIndex) const {
AnimPose defaultPose = _animSkeleton->getAbsoluteDefaultPose(footJointIndex);
glm::vec3 localForward = glm::inverse(defaultPose.rot()) * Vectors::UNIT_Z;
glm::vec3 footForward = footTargetOrientation * localForward;
// compute the forward direction of the hips.
glm::quat hipsRotation = _externalPoseSet._absolutePoses[hipsIndex].rot();
glm::vec3 hipsForward = hipsRotation * Vectors::UNIT_Z;
// blend between the hips and the foot.
const float FOOT_TO_HIPS_BLEND_FACTOR = 0.5f;
return glm::normalize(lerp(footForward, hipsForward, FOOT_TO_HIPS_BLEND_FACTOR));
}
void Rig::updateFromHandAndFeetParameters(const HandAndFeetParameters& params, float dt) { void Rig::updateFromHandAndFeetParameters(const HandAndFeetParameters& params, float dt) {
if (_animSkeleton && _animNode) { if (_animSkeleton && _animNode) {
const float HAND_RADIUS = 0.05f; const float HAND_RADIUS = 0.05f;
@ -1255,16 +1265,46 @@ void Rig::updateFromHandAndFeetParameters(const HandAndFeetParameters& params, f
_animVars.set("leftFootPosition", params.leftFootPosition); _animVars.set("leftFootPosition", params.leftFootPosition);
_animVars.set("leftFootRotation", params.leftFootOrientation); _animVars.set("leftFootRotation", params.leftFootOrientation);
_animVars.set("leftFootType", (int)IKTarget::Type::RotationAndPosition); _animVars.set("leftFootType", (int)IKTarget::Type::RotationAndPosition);
int footJointIndex = _animSkeleton->nameToJointIndex("LeftFoot");
glm::vec3 poleVector = calculateKneePoleVector(footJointIndex, params.leftFootOrientation, hipsIndex);
// smooth toward desired pole vector from previous pole vector... to reduce jitter
if (!_prevLeftFootPoleVectorValid) {
_prevLeftFootPoleVectorValid = true;
_prevLeftFootPoleVector = poleVector;
}
const float POLE_VECTOR_BLEND_FACTOR = 0.9f;
_prevLeftFootPoleVector = lerp(poleVector, _prevLeftFootPoleVector, POLE_VECTOR_BLEND_FACTOR);
_animVars.set("leftFootPoleVector", _prevLeftFootPoleVector);
_animVars.set("leftFootPoleJointName", QString("LeftFoot"));
} else { } else {
_animVars.unset("leftFootPosition"); _animVars.unset("leftFootPosition");
_animVars.unset("leftFootRotation"); _animVars.unset("leftFootRotation");
_animVars.set("leftFootType", (int)IKTarget::Type::RotationAndPosition); _animVars.set("leftFootType", (int)IKTarget::Type::RotationAndPosition);
_prevLeftFootPoleVectorValid = false;
} }
if (params.isRightFootEnabled) { if (params.isRightFootEnabled) {
_animVars.set("rightFootPosition", params.rightFootPosition); _animVars.set("rightFootPosition", params.rightFootPosition);
_animVars.set("rightFootRotation", params.rightFootOrientation); _animVars.set("rightFootRotation", params.rightFootOrientation);
_animVars.set("rightFootType", (int)IKTarget::Type::RotationAndPosition); _animVars.set("rightFootType", (int)IKTarget::Type::RotationAndPosition);
int footJointIndex = _animSkeleton->nameToJointIndex("RightFoot");
glm::vec3 poleVector = calculateKneePoleVector(footJointIndex, params.rightFootOrientation, hipsIndex);
// smooth toward desired pole vector from previous pole vector... to reduce jitter
if (!_prevRightFootPoleVectorValid) {
_prevRightFootPoleVectorValid = true;
_prevRightFootPoleVector = poleVector;
}
const float POLE_VECTOR_BLEND_FACTOR = 0.9f;
_prevRightFootPoleVector = lerp(poleVector, _prevRightFootPoleVector, POLE_VECTOR_BLEND_FACTOR);
_animVars.set("rightFootPoleVector", _prevRightFootPoleVector);
_animVars.set("rightFootPoleJointName", QString("RightFoot"));
} else { } else {
_animVars.unset("rightFootPosition"); _animVars.unset("rightFootPosition");
_animVars.unset("rightFootRotation"); _animVars.unset("rightFootRotation");
@ -1455,22 +1495,18 @@ void Rig::computeAvatarBoundingCapsule(
AnimInverseKinematics ikNode("boundingShape"); AnimInverseKinematics ikNode("boundingShape");
ikNode.setSkeleton(_animSkeleton); ikNode.setSkeleton(_animSkeleton);
ikNode.setTargetVars("LeftHand", ikNode.setTargetVars("LeftHand", "leftHandPosition", "leftHandRotation",
"leftHandPosition", "leftHandType", "leftHandWeight", 1.0f, {},
"leftHandRotation", "leftHandPoleJointName", "leftHandPoleVector");
"leftHandType", "leftHandWeight", 1.0f, {}); ikNode.setTargetVars("RightHand", "rightHandPosition", "rightHandRotation",
ikNode.setTargetVars("RightHand", "rightHandType", "rightHandWeight", 1.0f, {},
"rightHandPosition", "rightHandPoleJointName", "rightHandPoleVector");
"rightHandRotation", ikNode.setTargetVars("LeftFoot", "leftFootPosition", "leftFootRotation",
"rightHandType", "rightHandWeight", 1.0f, {}); "leftFootType", "leftFootWeight", 1.0f, {},
ikNode.setTargetVars("LeftFoot", "leftFootPoleJointName", "leftFootPoleVector");
"leftFootPosition", ikNode.setTargetVars("RightFoot", "rightFootPosition", "rightFootRotation",
"leftFootRotation", "rightFootType", "rightFootWeight", 1.0f, {},
"leftFootType", "leftFootWeight", 1.0f, {}); "rightFootPoleJointName", "rightFootPoleVector");
ikNode.setTargetVars("RightFoot",
"rightFootPosition",
"rightFootRotation",
"rightFootType", "rightFootWeight", 1.0f, {});
AnimPose geometryToRig = _modelOffset * _geometryOffset; AnimPose geometryToRig = _modelOffset * _geometryOffset;

View file

@ -153,9 +153,6 @@ public:
bool getAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation) const; bool getAbsoluteJointTranslationInRigFrame(int jointIndex, glm::vec3& translation) const;
bool getAbsoluteJointPoseInRigFrame(int jointIndex, AnimPose& returnPose) const; bool getAbsoluteJointPoseInRigFrame(int jointIndex, AnimPose& returnPose) const;
// legacy
bool getJointCombinedRotation(int jointIndex, glm::quat& result, const glm::quat& rotation) const;
// rig space // rig space
glm::mat4 getJointTransform(int jointIndex) const; glm::mat4 getJointTransform(int jointIndex) const;
@ -252,6 +249,8 @@ protected:
void updateEyeJoint(int index, const glm::vec3& modelTranslation, const glm::quat& modelRotation, const glm::vec3& lookAt, const glm::vec3& saccade); void updateEyeJoint(int index, const glm::vec3& modelTranslation, const glm::quat& modelRotation, const glm::vec3& lookAt, const glm::vec3& saccade);
void calcAnimAlpha(float speed, const std::vector<float>& referenceSpeeds, float* alphaOut) const; void calcAnimAlpha(float speed, const std::vector<float>& referenceSpeeds, float* alphaOut) const;
glm::vec3 Rig::calculateKneePoleVector(int footJointIndex, const glm::quat& footTargetOrientation, int hipsIndex) const;
AnimPose _modelOffset; // model to rig space AnimPose _modelOffset; // model to rig space
AnimPose _geometryOffset; // geometry to model space (includes unit offset & fst offsets) AnimPose _geometryOffset; // geometry to model space (includes unit offset & fst offsets)
AnimPose _invGeometryOffset; AnimPose _invGeometryOffset;
@ -347,7 +346,6 @@ protected:
bool _enableDebugDrawIKConstraints { false }; bool _enableDebugDrawIKConstraints { false };
bool _enableDebugDrawIKChains { false }; bool _enableDebugDrawIKChains { false };
private:
QMap<int, StateHandler> _stateHandlers; QMap<int, StateHandler> _stateHandlers;
int _nextStateHandlerId { 0 }; int _nextStateHandlerId { 0 };
QMutex _stateMutex; QMutex _stateMutex;
@ -358,6 +356,12 @@ private:
float _rightHandRelaxDuration { 0.0f }; float _rightHandRelaxDuration { 0.0f };
AnimPose _lastLeftHandControlledPose; AnimPose _lastLeftHandControlledPose;
AnimPose _lastRightHandControlledPose; AnimPose _lastRightHandControlledPose;
glm::vec3 _prevRightFootPoleVector = { Vectors::UNIT_Z };
bool _prevRightFootPoleVectorValid = { false };
glm::vec3 _prevLeftFootPoleVector = { Vectors::UNIT_Z };
bool _prevLeftFootPoleVectorValid = { false };
}; };
#endif /* defined(__hifi__Rig__) */ #endif /* defined(__hifi__Rig__) */

View file

@ -866,10 +866,6 @@ bool Model::getRelativeDefaultJointTranslation(int jointIndex, glm::vec3& transl
return _rig.getRelativeDefaultJointTranslation(jointIndex, translationOut); return _rig.getRelativeDefaultJointTranslation(jointIndex, translationOut);
} }
bool Model::getJointCombinedRotation(int jointIndex, glm::quat& rotation) const {
return _rig.getJointCombinedRotation(jointIndex, rotation, _rotation);
}
QStringList Model::getJointNames() const { QStringList Model::getJointNames() const {
if (QThread::currentThread() != thread()) { if (QThread::currentThread() != thread()) {
QStringList result; QStringList result;

View file

@ -176,7 +176,6 @@ public:
int getJointStateCount() const { return (int)_rig.getJointStateCount(); } int getJointStateCount() const { return (int)_rig.getJointStateCount(); }
bool getJointPositionInWorldFrame(int jointIndex, glm::vec3& position) const; bool getJointPositionInWorldFrame(int jointIndex, glm::vec3& position) const;
bool getJointRotationInWorldFrame(int jointIndex, glm::quat& rotation) const; bool getJointRotationInWorldFrame(int jointIndex, glm::quat& rotation) const;
bool getJointCombinedRotation(int jointIndex, glm::quat& rotation) const;
/// \param jointIndex index of joint in model structure /// \param jointIndex index of joint in model structure
/// \param rotation[out] rotation of joint in model-frame /// \param rotation[out] rotation of joint in model-frame