Skip to content

Commit da9a77c

Browse files
committed
GLTF: Preserve mesh name on export
1 parent 1ce3101 commit da9a77c

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

modules/gltf/gltf_document.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static Ref<ImporterMesh> _mesh_to_importer_mesh(Ref<Mesh> p_mesh) {
135135
mat_name, p_mesh->surface_get_format(surface_i));
136136
}
137137
importer_mesh->merge_meta_from(*p_mesh);
138+
importer_mesh->set_name(p_mesh->get_name());
138139
return importer_mesh;
139140
}
140141

@@ -2840,13 +2841,14 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> p_state) {
28402841
Array meshes;
28412842
for (GLTFMeshIndex gltf_mesh_i = 0; gltf_mesh_i < p_state->meshes.size(); gltf_mesh_i++) {
28422843
print_verbose("glTF: Serializing mesh: " + itos(gltf_mesh_i));
2843-
Ref<ImporterMesh> import_mesh = p_state->meshes.write[gltf_mesh_i]->get_mesh();
2844+
Ref<GLTFMesh> &gltf_mesh = p_state->meshes.write[gltf_mesh_i];
2845+
Ref<ImporterMesh> import_mesh = gltf_mesh->get_mesh();
28442846
if (import_mesh.is_null()) {
28452847
continue;
28462848
}
2847-
Array instance_materials = p_state->meshes.write[gltf_mesh_i]->get_instance_materials();
2849+
Array instance_materials = gltf_mesh->get_instance_materials();
28482850
Array primitives;
2849-
Dictionary gltf_mesh;
2851+
Dictionary mesh_dict;
28502852
Array target_names;
28512853
Array weights;
28522854
for (int morph_i = 0; morph_i < import_mesh->get_blend_shape_count(); morph_i++) {
@@ -3233,27 +3235,31 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> p_state) {
32333235
if (!target_names.is_empty()) {
32343236
Dictionary e;
32353237
e["targetNames"] = target_names;
3236-
gltf_mesh["extras"] = e;
3238+
mesh_dict["extras"] = e;
32373239
}
3238-
_attach_meta_to_extras(import_mesh, gltf_mesh);
3240+
_attach_meta_to_extras(import_mesh, mesh_dict);
32393241

32403242
weights.resize(target_names.size());
32413243
for (int name_i = 0; name_i < target_names.size(); name_i++) {
32423244
real_t weight = 0.0;
3243-
if (name_i < p_state->meshes.write[gltf_mesh_i]->get_blend_weights().size()) {
3244-
weight = p_state->meshes.write[gltf_mesh_i]->get_blend_weights()[name_i];
3245+
if (name_i < gltf_mesh->get_blend_weights().size()) {
3246+
weight = gltf_mesh->get_blend_weights()[name_i];
32453247
}
32463248
weights[name_i] = weight;
32473249
}
32483250
if (weights.size()) {
3249-
gltf_mesh["weights"] = weights;
3251+
mesh_dict["weights"] = weights;
32503252
}
32513253

32523254
ERR_FAIL_COND_V(target_names.size() != weights.size(), FAILED);
32533255

3254-
gltf_mesh["primitives"] = primitives;
3256+
mesh_dict["primitives"] = primitives;
32553257

3256-
meshes.push_back(gltf_mesh);
3258+
if (!gltf_mesh->get_name().is_empty()) {
3259+
mesh_dict["name"] = gltf_mesh->get_name();
3260+
}
3261+
3262+
meshes.push_back(mesh_dict);
32573263
}
32583264

32593265
if (!meshes.size()) {
@@ -4482,19 +4488,19 @@ Error GLTFDocument::_parse_texture_samplers(Ref<GLTFState> p_state) {
44824488
Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
44834489
Array materials;
44844490
for (int32_t i = 0; i < p_state->materials.size(); i++) {
4485-
Dictionary d;
4491+
Dictionary mat_dict;
44864492
Ref<Material> material = p_state->materials[i];
44874493
if (material.is_null()) {
4488-
materials.push_back(d);
4494+
materials.push_back(mat_dict);
44894495
continue;
44904496
}
44914497
if (!material->get_name().is_empty()) {
4492-
d["name"] = _gen_unique_name(p_state, material->get_name());
4498+
mat_dict["name"] = _gen_unique_name(p_state, material->get_name());
44934499
}
44944500

44954501
Ref<BaseMaterial3D> base_material = material;
44964502
if (base_material.is_null()) {
4497-
materials.push_back(d);
4503+
materials.push_back(mat_dict);
44984504
continue;
44994505
}
45004506

@@ -4647,7 +4653,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
46474653
if (has_ao) {
46484654
Dictionary occt;
46494655
occt["index"] = orm_texture_index;
4650-
d["occlusionTexture"] = occt;
4656+
mat_dict["occlusionTexture"] = occt;
46514657
}
46524658
if (has_roughness || has_metalness) {
46534659
mrt["index"] = orm_texture_index;
@@ -4661,7 +4667,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
46614667
}
46624668
}
46634669

4664-
d["pbrMetallicRoughness"] = mr;
4670+
mat_dict["pbrMetallicRoughness"] = mr;
46654671
if (base_material->get_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING) && _image_format != "None") {
46664672
Dictionary nt;
46674673
Ref<ImageTexture> tex;
@@ -4701,14 +4707,14 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
47014707
nt["scale"] = base_material->get_normal_scale();
47024708
if (gltf_texture_index != -1) {
47034709
nt["index"] = gltf_texture_index;
4704-
d["normalTexture"] = nt;
4710+
mat_dict["normalTexture"] = nt;
47054711
}
47064712
}
47074713

47084714
if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION)) {
47094715
const Color c = base_material->get_emission().linear_to_srgb();
47104716
Array arr = { c.r, c.g, c.b };
4711-
d["emissiveFactor"] = arr;
4717+
mat_dict["emissiveFactor"] = arr;
47124718
}
47134719

47144720
if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION) && _image_format != "None") {
@@ -4722,20 +4728,20 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
47224728

47234729
if (gltf_texture_index != -1) {
47244730
et["index"] = gltf_texture_index;
4725-
d["emissiveTexture"] = et;
4731+
mat_dict["emissiveTexture"] = et;
47264732
}
47274733
}
47284734

47294735
const bool ds = base_material->get_cull_mode() == BaseMaterial3D::CULL_DISABLED;
47304736
if (ds) {
4731-
d["doubleSided"] = ds;
4737+
mat_dict["doubleSided"] = ds;
47324738
}
47334739

47344740
if (base_material->get_transparency() == BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR) {
4735-
d["alphaMode"] = "MASK";
4736-
d["alphaCutoff"] = base_material->get_alpha_scissor_threshold();
4741+
mat_dict["alphaMode"] = "MASK";
4742+
mat_dict["alphaCutoff"] = base_material->get_alpha_scissor_threshold();
47374743
} else if (base_material->get_transparency() != BaseMaterial3D::TRANSPARENCY_DISABLED) {
4738-
d["alphaMode"] = "BLEND";
4744+
mat_dict["alphaMode"] = "BLEND";
47394745
}
47404746

47414747
Dictionary extensions;
@@ -4750,10 +4756,10 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
47504756
extensions["KHR_materials_emissive_strength"] = mat_emissive_strength;
47514757
p_state->add_used_extension("KHR_materials_emissive_strength");
47524758
}
4753-
d["extensions"] = extensions;
4759+
mat_dict["extensions"] = extensions;
47544760

4755-
_attach_meta_to_extras(material, d);
4756-
materials.push_back(d);
4761+
_attach_meta_to_extras(material, mat_dict);
4762+
materials.push_back(mat_dict);
47574763
}
47584764
if (!materials.size()) {
47594765
return OK;
@@ -5858,6 +5864,10 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> p_state, MeshIn
58585864

58595865
Ref<GLTFMesh> gltf_mesh;
58605866
gltf_mesh.instantiate();
5867+
if (!mesh_resource->get_name().is_empty()) {
5868+
gltf_mesh->set_original_name(mesh_resource->get_name());
5869+
gltf_mesh->set_name(_gen_unique_name(p_state, mesh_resource->get_name()));
5870+
}
58615871
gltf_mesh->set_instance_materials(instance_materials);
58625872
gltf_mesh->set_mesh(current_mesh);
58635873
gltf_mesh->set_blend_weights(blend_weights);

0 commit comments

Comments
 (0)