Skip to content

Commit 60425f6

Browse files
committed
#4314 Fix model suffixes
Usecase: Unable to use the same GLB model for physics
1 parent b7dbe0e commit 60425f6

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

indra/llprimitive/lldaeloader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ void LLDAELoader::processDomModel(LLModel* model, DAE* dae, daeElement* root, do
16821682
{
16831683
materials[model->mMaterialList[i]] = LLImportMaterial();
16841684
}
1685+
// todo: likely a bug here, shouldn't be using suffixed label, see how it gets used in other places.
16851686
mScene[transformation].push_back(LLModelInstance(model, model->mLabel, transformation, materials));
16861687
stretch_extents(model, transformation);
16871688
}
@@ -2414,7 +2415,7 @@ std::string LLDAELoader::getElementLabel(daeElement *element)
24142415
}
24152416

24162417
// static
2417-
size_t LLDAELoader::getSuffixPosition(std::string label)
2418+
size_t LLDAELoader::getSuffixPosition(const std::string &label)
24182419
{
24192420
if ((label.find("_LOD") != -1) || (label.find("_PHYS") != -1))
24202421
{

indra/llprimitive/lldaeloader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class LLDAELoader : public LLModelLoader
9898
bool loadModelsFromDomMesh(domMesh* mesh, std::vector<LLModel*>& models_out, U32 submodel_limit);
9999

100100
static std::string getElementLabel(daeElement *element);
101-
static size_t getSuffixPosition(std::string label);
101+
static size_t getSuffixPosition(const std::string& label);
102102
static std::string getLodlessLabel(daeElement *element);
103103

104104
static std::string preprocessDAE(std::string filename);

indra/newview/gltf/llgltfloader.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ bool LLGLTFLoader::OpenFile(const std::string &filename)
188188

189189
void LLGLTFLoader::addModelToScene(
190190
LLModel* pModel,
191+
const std::string& model_name,
191192
U32 submodel_limit,
192193
const LLMatrix4& transformation,
193194
const LLVolumeParams& volume_params,
@@ -239,7 +240,7 @@ void LLGLTFLoader::addModelToScene(
239240
LLModel* next = new LLModel(volume_params, 0.f);
240241
next->ClearFacesAndMaterials();
241242
next->mSubmodelID = ++submodelID;
242-
next->mLabel = pModel->mLabel + (char)((int)'a' + next->mSubmodelID) + lod_suffix[mLod];
243+
next->mLabel = model_name + (char)((int)'a' + next->mSubmodelID) + lod_suffix[mLod];
243244
next->getVolumeFaces() = remainder;
244245
next->mNormalizedScale = current_model->mNormalizedScale;
245246
next->mNormalizedTranslation = current_model->mNormalizedTranslation;
@@ -289,7 +290,12 @@ void LLGLTFLoader::addModelToScene(
289290
materials[model->mMaterialList[i]] = LLImportMaterial();
290291
}
291292
}
292-
mScene[transformation].push_back(LLModelInstance(model, model->mLabel, transformation, materials));
293+
std::string base_name = model_name;
294+
if (model->mSubmodelID > 0)
295+
{
296+
base_name += (char)((int)'a' + model->mSubmodelID);
297+
}
298+
mScene[transformation].push_back(LLModelInstance(model, base_name, transformation, materials));
293299
stretch_extents(model, transformation);
294300
}
295301
}
@@ -387,15 +393,21 @@ void LLGLTFLoader::processNodeHierarchy(S32 node_idx, std::map<std::string, S32>
387393
const LL::GLTF::Mesh& mesh = mGLTFAsset.mMeshes[node.mMesh];
388394

389395
// Get base mesh name and track usage
390-
std::string base_name = mesh.mName;
396+
std::string base_name = getLodlessLabel(mesh);
391397
if (base_name.empty())
392398
{
393399
base_name = "mesh_" + std::to_string(node.mMesh);
394400
}
395401

396402
S32 instance_count = mesh_name_counts[base_name]++;
397403

398-
if (populateModelFromMesh(pModel, mesh, node, mats, instance_count) &&
404+
// make name unique
405+
if (instance_count > 0)
406+
{
407+
base_name = base_name + "_copy_" + std::to_string(instance_count);
408+
}
409+
410+
if (populateModelFromMesh(pModel, base_name, mesh, node, mats) &&
399411
(LLModel::NO_ERRORS == pModel->getStatus()) &&
400412
validate_model(pModel))
401413
{
@@ -443,7 +455,7 @@ void LLGLTFLoader::processNodeHierarchy(S32 node_idx, std::map<std::string, S32>
443455
mWarningsArray.append(args);
444456
}
445457

446-
addModelToScene(pModel, submodel_limit, transformation, volume_params, mats);
458+
addModelToScene(pModel, base_name, submodel_limit, transformation, volume_params, mats);
447459
mats.clear();
448460
}
449461
else
@@ -529,29 +541,16 @@ bool LLGLTFLoader::addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_
529541
return true;
530542
}
531543

532-
bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh& mesh, const LL::GLTF::Node& nodeno, material_map& mats, S32 instance_count)
544+
bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const std::string& base_name, const LL::GLTF::Mesh& mesh, const LL::GLTF::Node& nodeno, material_map& mats)
533545
{
534546
// Set the requested label for the floater display and uploading
535547
pModel->mRequestedLabel = gDirUtilp->getBaseFileName(mFilename, true);
548+
// Set name and suffix. Suffix is nessesary for model matching logic
549+
// because sometimes higher lod can be used as a lower one, so they
550+
// need unique names not just in scope of one lod, but across lods.
551+
pModel->mLabel = base_name + lod_suffix[mLod];
536552

537-
// Create unique model name
538-
std::string base_name = mesh.mName;
539-
if (base_name.empty())
540-
{
541-
S32 mesh_index = static_cast<S32>(&mesh - &mGLTFAsset.mMeshes[0]);
542-
base_name = "mesh_" + std::to_string(mesh_index);
543-
}
544-
545-
LL_DEBUGS("GLTF_DEBUG") << "Processing model " << base_name << LL_ENDL;
546-
547-
if (instance_count > 0)
548-
{
549-
pModel->mLabel = base_name + "_copy_" + std::to_string(instance_count);
550-
}
551-
else
552-
{
553-
pModel->mLabel = base_name;
554-
}
553+
LL_DEBUGS("GLTF_DEBUG") << "Processing model " << pModel->mLabel << LL_ENDL;
555554

556555
pModel->ClearFacesAndMaterials();
557556

@@ -1723,3 +1722,22 @@ void LLGLTFLoader::notifyUnsupportedExtension(bool unsupported)
17231722
}
17241723
}
17251724

1725+
size_t LLGLTFLoader::getSuffixPosition(const std::string &label)
1726+
{
1727+
if ((label.find("_LOD") != -1) || (label.find("_PHYS") != -1))
1728+
{
1729+
return label.rfind('_');
1730+
}
1731+
return -1;
1732+
}
1733+
1734+
std::string LLGLTFLoader::getLodlessLabel(const LL::GLTF::Mesh& mesh)
1735+
{
1736+
size_t ext_pos = getSuffixPosition(mesh.mName);
1737+
if (ext_pos != -1)
1738+
{
1739+
return mesh.mName.substr(0, ext_pos);
1740+
}
1741+
return mesh.mName;
1742+
}
1743+

indra/newview/gltf/llgltfloader.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ class LLGLTFLoader : public LLModelLoader
136136
void computeCombinedNodeTransform(const LL::GLTF::Asset& asset, S32 node_index, glm::mat4& combined_transform) const;
137137
void processNodeHierarchy(S32 node_idx, std::map<std::string, S32>& mesh_name_counts, U32 submodel_limit, const LLVolumeParams& volume_params);
138138
bool addJointToModelSkin(LLMeshSkinInfo& skin_info, S32 gltf_skin_idx, size_t gltf_joint_idx);
139-
bool populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh &mesh, const LL::GLTF::Node &node, material_map& mats, S32 instance_count);
139+
bool populateModelFromMesh(LLModel* pModel, const std::string& base_name, const LL::GLTF::Mesh &mesh, const LL::GLTF::Node &node, material_map& mats);
140140
void populateJointsFromSkin(S32 skin_idx);
141141
void populateJointGroups();
142-
void addModelToScene(LLModel* pModel, U32 submodel_limit, const LLMatrix4& transformation, const LLVolumeParams& volume_params, const material_map& mats);
142+
void addModelToScene(LLModel* pModel, const std::string& model_name, U32 submodel_limit, const LLMatrix4& transformation, const LLVolumeParams& volume_params, const material_map& mats);
143143
void buildJointGroup(LLJointData& viewer_data, const std::string& parent_group);
144144
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;
145145
glm::mat4 buildGltfRestMatrix(S32 joint_node_index, const LL::GLTF::Skin& gltf_skin) const;
@@ -153,6 +153,9 @@ class LLGLTFLoader : public LLModelLoader
153153

154154
void notifyUnsupportedExtension(bool unsupported);
155155

156+
static size_t getSuffixPosition(const std::string& label);
157+
static std::string getLodlessLabel(const LL::GLTF::Mesh& mesh);
158+
156159
// bool mPreprocessGLTF;
157160

158161
/* Below inherited from dae loader - unknown if/how useful here
@@ -190,10 +193,6 @@ class LLGLTFLoader : public LLModelLoader
190193
//
191194
bool loadModelsFromGltfMesh(gltfMesh *mesh, std::vector<LLModel *> &models_out, U32 submodel_limit);
192195
193-
static std::string getElementLabel(gltfElement *element);
194-
static size_t getSuffixPosition(std::string label);
195-
static std::string getLodlessLabel(gltfElement *element);
196-
197196
static std::string preprocessGLTF(std::string filename);
198197
*/
199198

0 commit comments

Comments
 (0)