Skip to content

Commit

Permalink
Fixed flow bone scaling issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuprynowicz committed Aug 27, 2023
1 parent 0590c37 commit 8c87d6f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
16 changes: 9 additions & 7 deletions libraries/animation/src/Flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ void FlowJoint::toHelperJoint(const glm::vec3& initialPosition, float length) {
_length = length;
}

FlowThread::FlowThread(int rootIndex, std::map<int, FlowJoint>* joints) {
FlowThread::FlowThread(int rootIndex, std::map<int, FlowJoint>* joints, float rigScale) {
_rigScale = rigScale;
_jointsPointer = joints;
computeFlowThread(rootIndex);
}
Expand Down Expand Up @@ -365,7 +366,7 @@ void FlowThread::computeRecovery() {
glm::quat parentRotation = parentJoint._parentWorldRotation * parentJoint._initialRotation;
for (size_t i = 1; i < _joints.size(); i++) {
auto joint = _jointsPointer->at(_joints[i]);
_jointsPointer->at(_joints[i])._recoveryPosition = joint._recoveryPosition = parentJoint._recoveryPosition + (parentRotation * (joint._initialTranslation * 0.01f));
_jointsPointer->at(_joints[i])._recoveryPosition = joint._recoveryPosition = parentJoint._recoveryPosition + (parentRotation * (joint._initialTranslation * _rigScale));
parentJoint = joint;
}
};
Expand Down Expand Up @@ -405,7 +406,7 @@ void FlowThread::computeJointRotations() {
auto joint0 = _jointsPointer->at(_joints[0]);
auto joint1 = _jointsPointer->at(_joints[1]);

auto initial_pos1 = pos0 + (joint0._initialRotation * (joint1._initialTranslation * 0.01f));
auto initial_pos1 = pos0 + (joint0._initialRotation * (joint1._initialTranslation * _rigScale));

auto vec0 = initial_pos1 - pos0;
auto vec1 = pos1 - pos0;
Expand All @@ -417,11 +418,11 @@ void FlowThread::computeJointRotations() {
for (size_t i = 1; i < _joints.size() - 1; i++) {
auto nextJoint = _jointsPointer->at(_joints[i + 1]);
for (size_t j = i; j < _joints.size(); j++) {
_rootFramePositions[j] = glm::inverse(joint0._currentRotation) * _rootFramePositions[j] - (joint0._initialTranslation * 0.01f);
_rootFramePositions[j] = glm::inverse(joint0._currentRotation) * _rootFramePositions[j] - (joint0._initialTranslation * _rigScale);
}
pos0 = _rootFramePositions[i];
pos1 = _rootFramePositions[i + 1];
initial_pos1 = pos0 + joint1._initialRotation * (nextJoint._initialTranslation * 0.01f);
initial_pos1 = pos0 + joint1._initialRotation * (nextJoint._initialTranslation * _rigScale);

vec0 = initial_pos1 - pos0;
vec1 = pos1 - pos0;
Expand Down Expand Up @@ -554,8 +555,9 @@ void Flow::calculateConstraints(const std::shared_ptr<AnimSkeleton>& skeleton,
}
}
int extraIndex = -1;
qDebug() << "GetScaleFactorGeometryToUnscaledRig() " << _rig->GetScaleFactorGeometryToUnscaledRig();
for (size_t i = 0; i < roots.size(); i++) {
FlowThread thread = FlowThread(roots[i], &_flowJointData);
FlowThread thread = FlowThread(roots[i], &_flowJointData, _rig->GetScaleFactorGeometryToUnscaledRig());
// add threads with at least 2 joints
if (thread._joints.size() > 0) {
if (thread._joints.size() == 1) {
Expand All @@ -570,7 +572,7 @@ void Flow::calculateConstraints(const std::shared_ptr<AnimSkeleton>& skeleton,
glm::vec3 translation = glm::vec3(0.0f, HELPER_JOINT_LENGTH, 0.0f);
newJoint.setInitialData(jointPosition + translation, 100.0f * translation , Quaternions::IDENTITY, jointPosition);
_flowJointData.insert(std::pair<int, FlowJoint>(extraIndex, newJoint));
FlowThread newThread = FlowThread(jointIndex, &_flowJointData);
FlowThread newThread = FlowThread(jointIndex, &_flowJointData, _rig->GetScaleFactorGeometryToUnscaledRig());
if (newThread._joints.size() > 1) {
_jointThreads.push_back(newThread);
}
Expand Down
9 changes: 7 additions & 2 deletions libraries/animation/src/Flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class FlowThread {
FlowThread() {};
FlowThread& operator=(const FlowThread& otherFlowThread);

FlowThread(int rootIndex, std::map<int, FlowJoint>* joints);
FlowThread(int rootIndex, std::map<int, FlowJoint>* joints, float rigScale);

void resetLength();
void computeFlowThread(int rootIndex);
Expand All @@ -278,14 +278,15 @@ class FlowThread {
std::vector<glm::vec3> _positions;
float _radius{ 0.0f };
float _length{ 0.0f };
float _rigScale { 100.0f };
std::map<int, FlowJoint>* _jointsPointer;
std::vector<glm::vec3> _rootFramePositions;
};

class Flow : public QObject{
Q_OBJECT
public:
Flow() { }
Flow(Rig *rig) : _rig (rig) {};
Flow& operator=(const Flow& otherFlow);
bool getActive() const { return _active; }
void setActive(bool active) { _active = active; }
Expand Down Expand Up @@ -323,6 +324,10 @@ class Flow : public QObject{

float _scale { 1.0f };
float _lastScale{ 1.0f };
// 100.0f was default rig scale when it was hardcoded but it caused issues with most avatars
//float _rigScale{ 100.0f };
// Rig to which flow system belongs, it's used for getting rig scale
Rig *_rig { nullptr };
glm::vec3 _entityPosition;
glm::quat _entityRotation;
std::map<int, FlowJoint> _flowJointData;
Expand Down
2 changes: 1 addition & 1 deletion libraries/animation/src/Rig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static const QString MAIN_STATE_MACHINE_RIGHT_HAND_POSITION("mainStateMachineRig
// - isLeftThumbRaise
// - isLeftIndexPointAndThumbRaise
// - isLeftHandGrasp
Rig::Rig() {
Rig::Rig() : _internalFlow(this), _networkFlow(this) {
// Ensure thread-safe access to the rigRegistry.
std::lock_guard<std::mutex> guard(rigRegistryMutex);

Expand Down
6 changes: 3 additions & 3 deletions libraries/animation/src/Rig.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ class Rig : public QObject {
bool getNetworkGraphActive() const;
void setDirectionalBlending(const QString& targetName, const glm::vec3& blendingTarget, const QString& alphaName, float alpha);

// Get the scale factor to convert distances in the geometry frame into the unscaled rig frame.
float GetScaleFactorGeometryToUnscaledRig() const;

signals:
void onLoadComplete();
void onLoadFailed();
Expand Down Expand Up @@ -290,9 +293,6 @@ class Rig : public QObject {
glm::vec3 deflectHandFromTorso(const glm::vec3& handPosition, const HFMJointShapeInfo& hipsShapeInfo, const HFMJointShapeInfo& spineShapeInfo,
const HFMJointShapeInfo& spine1ShapeInfo, const HFMJointShapeInfo& spine2ShapeInfo) const;

// Get the scale factor to convert distances in the geometry frame into the unscaled rig frame.
float GetScaleFactorGeometryToUnscaledRig() const;

// The ground plane Y position in geometry space.
static constexpr float GEOMETRY_GROUND_Y = 0.0f;

Expand Down

0 comments on commit 8c87d6f

Please sign in to comment.