Skip to content

Commit 875a418

Browse files
committed
#4080 Import GLTF skin data
1 parent fdeef47 commit 875a418

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

indra/llprimitive/llmodelloader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ void LLModelLoader::run()
150150
{
151151
mWarningsArray.clear();
152152
doLoadModel();
153+
// todo: we are inside of a thread, push this into main thread worker,
154+
// not into doOnIdleOneTime that laks tread safety
153155
doOnIdleOneTime(boost::bind(&LLModelLoader::loadModelCallback,this));
154156
}
155157

indra/newview/gltf/llgltfloader.cpp

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool LLGLTFLoader::parseMeshes()
142142

143143
// Populate the joints from skins first.
144144
// There's not many skins - and you can pretty easily iterate through the nodes from that.
145-
for (auto skin : mGLTFAsset.mSkins)
145+
for (auto& skin : mGLTFAsset.mSkins)
146146
{
147147
populateJointFromSkin(skin);
148148
}
@@ -164,7 +164,7 @@ bool LLGLTFLoader::parseMeshes()
164164
coord_system_rotation.initRotation(90.0f * DEG_TO_RAD, 0.0f, 0.0f);
165165

166166
// Gather bounds from all meshes
167-
for (auto node : mGLTFAsset.mNodes)
167+
for (auto &node : mGLTFAsset.mNodes)
168168
{
169169
auto meshidx = node.mMesh;
170170
if (meshidx >= 0 && meshidx < mGLTFAsset.mMeshes.size())
@@ -238,7 +238,7 @@ bool LLGLTFLoader::parseMeshes()
238238
}
239239

240240
// Second pass: Process each node with the global scale and offset
241-
for (auto node : mGLTFAsset.mNodes)
241+
for (auto &node : mGLTFAsset.mNodes)
242242
{
243243
LLMatrix4 transformation;
244244
material_map mats;
@@ -281,7 +281,7 @@ bool LLGLTFLoader::parseMeshes()
281281
transformation = mesh_scale;
282282
if (transformation.determinant() < 0)
283283
{ // negative scales are not supported
284-
LL_INFOS() << "Negative scale detected, unsupported post-normalization transform. domInstance_geometry: "
284+
LL_INFOS("GLTF") << "Negative scale detected, unsupported post-normalization transform. domInstance_geometry: "
285285
<< pModel->mLabel << LL_ENDL;
286286
LLSD args;
287287
args["Message"] = "NegativeScaleNormTrans";
@@ -292,6 +292,60 @@ bool LLGLTFLoader::parseMeshes()
292292
mScene[transformation].push_back(LLModelInstance(pModel, pModel->mLabel, transformation, mats));
293293
stretch_extents(pModel, transformation);
294294
mTransform = saved_transform;
295+
296+
S32 skin_index = node.mSkin;
297+
if (skin_index >= 0 && mGLTFAsset.mSkins.size() > skin_index)
298+
{
299+
LL::GLTF::Skin& gltf_skin = mGLTFAsset.mSkins[skin_index];
300+
LLMeshSkinInfo& skin_info = pModel->mSkinInfo;
301+
302+
size_t jointCnt = gltf_skin.mJoints.size();
303+
if (gltf_skin.mInverseBindMatrices >= 0 && jointCnt != gltf_skin.mInverseBindMatricesData.size())
304+
{
305+
LL_INFOS("GLTF") << "Bind matrices count mismatch joints count" << LL_ENDL;
306+
LLSD args;
307+
args["Message"] = "InvBindCountMismatch";
308+
mWarningsArray.append(args);
309+
}
310+
311+
for (size_t i = 0; i < jointCnt; ++i)
312+
{
313+
// Process joint name and idnex
314+
S32 joint = gltf_skin.mJoints[i];
315+
LL::GLTF::Node& jointNode = mGLTFAsset.mNodes[joint];
316+
jointNode.makeMatrixValid();
317+
318+
std::string legal_name(jointNode.mName);
319+
if (mJointMap.find(legal_name) != mJointMap.end())
320+
{
321+
legal_name = mJointMap[legal_name];
322+
}
323+
skin_info.mJointNames.push_back(legal_name);
324+
skin_info.mJointNums.push_back(-1);
325+
326+
if (i < gltf_skin.mInverseBindMatricesData.size())
327+
{
328+
// Process bind matrix
329+
LL::GLTF::mat4 gltf_mat = gltf_skin.mInverseBindMatricesData[i];
330+
LLMatrix4 gltf_transform(glm::value_ptr(gltf_mat));
331+
skin_info.mInvBindMatrix.push_back(LLMatrix4a(gltf_transform));
332+
333+
LL_DEBUGS("GLTF") << "mInvBindMatrix name: " << legal_name << " val: " << gltf_transform << LL_ENDL;
334+
335+
// Translate based of mJointList
336+
gltf_transform.setTranslation(mJointList[legal_name].getTranslation());
337+
skin_info.mAlternateBindMatrix.push_back(LLMatrix4a(gltf_transform));
338+
}
339+
}
340+
341+
// "Bind Shape Matrix" is supposed to transform the geometry of the skinned mesh
342+
// into the coordinate space of the joints.
343+
// In GLTF, this matrix is omitted, and it is assumed that this transform is either
344+
// premultiplied with the mesh data, or postmultiplied to the inverse bind matrices.
345+
LLMatrix4 bind_shape;
346+
bind_shape.setIdentity();
347+
skin_info.mBindShapeMatrix.loadu(bind_shape);
348+
}
295349
}
296350
else
297351
{
@@ -560,10 +614,29 @@ void LLGLTFLoader::populateJointFromSkin(const LL::GLTF::Skin& skin)
560614
for (auto joint : skin.mJoints)
561615
{
562616
auto jointNode = mGLTFAsset.mNodes[joint];
617+
618+
std::string legal_name(jointNode.mName);
619+
if (mJointMap.find(legal_name) != mJointMap.end())
620+
{
621+
legal_name = mJointMap[legal_name];
622+
}
623+
else
624+
{
625+
LL_INFOS("GLTF") << "Rigged to unrecognized joint name : "
626+
<< legal_name << LL_ENDL;
627+
LLSD args;
628+
args["Message"] = "UnrecognizedJoint";
629+
args["[NAME]"] = legal_name;
630+
mWarningsArray.append(args);
631+
}
632+
563633
jointNode.makeMatrixValid();
564634

565-
mJointList[jointNode.mName] = LLMatrix4(glm::value_ptr(jointNode.mMatrix));
566-
mJointsFromNode.push_front(jointNode.mName);
635+
LLMatrix4 gltf_transform = LLMatrix4(glm::value_ptr(jointNode.mMatrix));
636+
mJointList[legal_name] = gltf_transform;
637+
mJointsFromNode.push_front(legal_name);
638+
639+
LL_DEBUGS("GLTF") << "mJointList name: " << legal_name << " val: " << gltf_transform << LL_ENDL;
567640
}
568641
}
569642

indra/newview/skins/default/xui/en/floater_model_preview.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<string name="UnrecognizedJoint">Rigged to unrecognized joint name [NAME]</string>
4646
<string name="UnknownJoints">Skinning disabled due to [COUNT] unknown joints</string>
4747
<string name="ModelLoaded">Model [MODEL_NAME] loaded</string>
48+
<string name="InvBindCountMismatch">Bind matrices count mismatch joints count</string>
4849

4950
<string name="IncompleteTC">Texture coordinates data is not complete.</string>
5051
<string name="PositionNaN">Found NaN while loading position data from DAE-Model, invalid model.</string>

0 commit comments

Comments
 (0)