#4148 Fix collision bones

meow-7.2.2
Andrey Kleshchev 2025-06-24 17:07:42 +03:00 committed by Andrey Kleshchev
parent 781d9fa481
commit c404b93759
6 changed files with 116 additions and 12 deletions

View File

@ -1683,7 +1683,10 @@ void LLAvatarSkeletonInfo::getJointMatricesAndHierarhy(
{
data.mName = bone_info->mName;
data.mJointMatrix = bone_info->getJointMatrix();
data.mScale = glm::vec3(bone_info->mScale[0], bone_info->mScale[1], bone_info->mScale[2]);
data.mRotation = bone_info->mRot;
data.mRestMatrix = parent_mat * data.mJointMatrix;
data.mIsJoint = bone_info->mIsJoint;
for (LLAvatarBoneInfo* child_info : bone_info->mChildren)
{
LLJointData& child_data = data.mChildren.emplace_back();

View File

@ -36,9 +36,30 @@ public:
std::string mName;
glm::mat4 mJointMatrix;
glm::mat4 mRestMatrix;
glm::vec3 mScale;
LLVector3 mRotation;
typedef std::vector<LLJointData> bones_t;
bones_t mChildren;
bool mIsJoint; // if not, collision_volume
enum SupportCategory
{
SUPPORT_BASE,
SUPPORT_EXTENDED
};
SupportCategory mSupport;
void setSupport(const std::string& support)
{
if (support == "extended")
{
mSupport = SUPPORT_EXTENDED;
}
else
{
mSupport = SUPPORT_BASE;
}
}
};
#endif //LL_LLJOINTDATA_H

View File

@ -468,6 +468,58 @@ bool LLModelLoader::isRigSuitableForJointPositionUpload( const std::vector<std::
return true;
}
void LLModelLoader::dumpDebugData()
{
std::string log_file = mFilename + "_importer.txt";
LLStringUtil::toLower(log_file);
llofstream file;
file.open(log_file.c_str());
if (!file)
{
LL_WARNS() << "dumpDebugData failed to open file " << log_file << LL_ENDL;
return;
}
file << "Importing: " << mFilename << "\n";
std::map<std::string, LLMatrix4a> inv_bind;
std::map<std::string, LLMatrix4a> alt_bind;
for (LLPointer<LLModel>& mdl : mModelList)
{
file << "Model name: " << mdl->mLabel << "\n";
const LLMeshSkinInfo& skin_info = mdl->mSkinInfo;
file << "Shape Bind matrix: " << skin_info.mBindShapeMatrix << "\n";
file << "Skin Weights count: " << (S32)mdl->mSkinWeights.size() << "\n";
// some objects might have individual bind matrices,
// but for now it isn't accounted for
size_t joint_count = skin_info.mJointNames.size();
for (size_t i = 0; i< joint_count;i++)
{
const std::string& joint = skin_info.mJointNames[i];
if (skin_info.mInvBindMatrix.size() > i)
{
inv_bind[joint] = skin_info.mInvBindMatrix[i];
}
if (skin_info.mAlternateBindMatrix.size() > i)
{
alt_bind[joint] = skin_info.mAlternateBindMatrix[i];
}
}
}
file << "Inv Bind matrices.\n";
for (auto& bind : inv_bind)
{
file << "Joint: " << bind.first << " Matrix: " << bind.second << "\n";
}
file << "Alt Bind matrices.\n";
for (auto& bind : alt_bind)
{
file << "Joint: " << bind.first << " Matrix: " << bind.second << "\n";
}
}
//called in the main thread
void LLModelLoader::loadTextures()

View File

@ -198,6 +198,7 @@ public:
const LLSD logOut() const { return mWarningsArray; }
void clearLog() { mWarningsArray.clear(); }
void dumpDebugData();
protected:

View File

@ -1122,7 +1122,7 @@ void LLGLTFLoader::populateJointFromSkin(S32 skin_idx)
glm::mat4 ident(1.0);
for (auto &viewer_data : mViewerJointData)
{
buildOverrideMatrix(viewer_data, joints_data, names_to_nodes, ident);
buildOverrideMatrix(viewer_data, joints_data, names_to_nodes, ident, ident);
}
for (S32 i = 0; i < joint_count; i++)
@ -1299,7 +1299,7 @@ S32 LLGLTFLoader::findParentNode(S32 node) const
return -1;
}
void LLGLTFLoader::buildOverrideMatrix(LLJointData& viewer_data, joints_data_map_t &gltf_nodes, joints_name_to_node_map_t &names_to_nodes, glm::mat4& parent_rest) const
void LLGLTFLoader::buildOverrideMatrix(LLJointData& viewer_data, joints_data_map_t &gltf_nodes, joints_name_to_node_map_t &names_to_nodes, glm::mat4& parent_rest, glm::mat4& parent_support_rest) const
{
glm::mat4 new_lefover(1.f);
glm::mat4 rest(1.f);
@ -1309,26 +1309,42 @@ void LLGLTFLoader::buildOverrideMatrix(LLJointData& viewer_data, joints_data_map
S32 gltf_node_idx = found_node->second;
JointNodeData& node = gltf_nodes[gltf_node_idx];
node.mIsOverrideValid = true;
node.mViewerRestMatrix = viewer_data.mRestMatrix;
glm::mat4 gltf_joint_rest_pose = coord_system_rotation * node.mGltfRestMatrix;
if (mApplyXYRotation)
{
gltf_joint_rest_pose = coord_system_rotationxy * gltf_joint_rest_pose;
}
node.mOverrideMatrix = glm::inverse(parent_rest) * gltf_joint_rest_pose;
glm::vec3 override;
glm::mat4 translated_joint;
// Example:
// Viewer has pelvis->spine1->spine2->torso.
// gltf example model has pelvis->torso
// By doing glm::inverse(transalted_rest_spine2) * gltf_rest_torso
// We get what torso would have looked like if gltf had a spine2
if (viewer_data.mIsJoint)
{
translated_joint = glm::inverse(parent_rest) * gltf_joint_rest_pose;
}
else
{
translated_joint = glm::inverse(parent_support_rest) * gltf_joint_rest_pose;
}
glm::vec3 translation_override;
glm::vec3 skew;
glm::vec3 scale;
glm::vec4 perspective;
glm::quat rotation;
glm::decompose(node.mOverrideMatrix, scale, rotation, override, skew, perspective);
glm::vec3 translate;
glm::decompose(viewer_data.mJointMatrix, scale, rotation, translate, skew, perspective);
glm::mat4 viewer_joint = glm::recompose(scale, rotation, override, skew, perspective);
glm::decompose(translated_joint, scale, rotation, translation_override, skew, perspective);
node.mOverrideMatrix = viewer_joint;
rest = parent_rest * node.mOverrideMatrix;
node.mOverrideMatrix = glm::recompose(glm::vec3(1, 1, 1), glm::identity<glm::quat>(), translation_override, glm::vec3(0, 0, 0), glm::vec4(0, 0, 0, 1));
glm::mat4 override_joint = node.mOverrideMatrix;
override_joint = glm::scale(override_joint, viewer_data.mScale);
rest = parent_rest * override_joint;
node.mOverrideRestMatrix = rest;
}
else
@ -1336,9 +1352,20 @@ void LLGLTFLoader::buildOverrideMatrix(LLJointData& viewer_data, joints_data_map
// No override for this joint
rest = parent_rest * viewer_data.mJointMatrix;
}
glm::mat4 support_rest(1.f);
if (viewer_data.mSupport == LLJointData::SUPPORT_BASE)
{
support_rest = rest;
}
else
{
support_rest = parent_support_rest;
}
for (LLJointData& child_data : viewer_data.mChildren)
{
buildOverrideMatrix(child_data, gltf_nodes, names_to_nodes, rest);
buildOverrideMatrix(child_data, gltf_nodes, names_to_nodes, rest, support_rest);
}
}

View File

@ -129,7 +129,7 @@ private:
S32 findValidRootJointNode(S32 source_joint_node, const LL::GLTF::Skin& gltf_skin) const;
S32 findGLTFRootJointNode(const LL::GLTF::Skin& gltf_skin) const; // if there are multiple roots, gltf stores them under one commor joint
S32 findParentNode(S32 node) const;
void buildOverrideMatrix(LLJointData& data, joints_data_map_t &gltf_nodes, joints_name_to_node_map_t &names_to_nodes, glm::mat4& parent_rest) const;
void buildOverrideMatrix(LLJointData& data, joints_data_map_t &gltf_nodes, joints_name_to_node_map_t &names_to_nodes, glm::mat4& parent_rest, glm::mat4& support_rest) const;
glm::mat4 buildGltfRestMatrix(S32 joint_node_index, const LL::GLTF::Skin& gltf_skin) const;
glm::mat4 buildGltfRestMatrix(S32 joint_node_index, const joints_data_map_t& joint_data) const;
glm::mat4 computeGltfToViewerSkeletonTransform(const joints_data_map_t& joints_data_map, S32 gltf_node_index, const std::string& joint_name) const;