GLTF: Preserve mesh name on export

This commit is contained in:
Aaron Franke 2025-08-08 03:15:16 -07:00
parent 1ce3101fbc
commit da9a77ce42
No known key found for this signature in database
GPG key ID: 40A1750B977E56BF

View file

@ -135,6 +135,7 @@ static Ref<ImporterMesh> _mesh_to_importer_mesh(Ref<Mesh> p_mesh) {
mat_name, p_mesh->surface_get_format(surface_i)); mat_name, p_mesh->surface_get_format(surface_i));
} }
importer_mesh->merge_meta_from(*p_mesh); importer_mesh->merge_meta_from(*p_mesh);
importer_mesh->set_name(p_mesh->get_name());
return importer_mesh; return importer_mesh;
} }
@ -2840,13 +2841,14 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> p_state) {
Array meshes; Array meshes;
for (GLTFMeshIndex gltf_mesh_i = 0; gltf_mesh_i < p_state->meshes.size(); gltf_mesh_i++) { for (GLTFMeshIndex gltf_mesh_i = 0; gltf_mesh_i < p_state->meshes.size(); gltf_mesh_i++) {
print_verbose("glTF: Serializing mesh: " + itos(gltf_mesh_i)); print_verbose("glTF: Serializing mesh: " + itos(gltf_mesh_i));
Ref<ImporterMesh> import_mesh = p_state->meshes.write[gltf_mesh_i]->get_mesh(); Ref<GLTFMesh> &gltf_mesh = p_state->meshes.write[gltf_mesh_i];
Ref<ImporterMesh> import_mesh = gltf_mesh->get_mesh();
if (import_mesh.is_null()) { if (import_mesh.is_null()) {
continue; continue;
} }
Array instance_materials = p_state->meshes.write[gltf_mesh_i]->get_instance_materials(); Array instance_materials = gltf_mesh->get_instance_materials();
Array primitives; Array primitives;
Dictionary gltf_mesh; Dictionary mesh_dict;
Array target_names; Array target_names;
Array weights; Array weights;
for (int morph_i = 0; morph_i < import_mesh->get_blend_shape_count(); morph_i++) { 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) {
if (!target_names.is_empty()) { if (!target_names.is_empty()) {
Dictionary e; Dictionary e;
e["targetNames"] = target_names; e["targetNames"] = target_names;
gltf_mesh["extras"] = e; mesh_dict["extras"] = e;
} }
_attach_meta_to_extras(import_mesh, gltf_mesh); _attach_meta_to_extras(import_mesh, mesh_dict);
weights.resize(target_names.size()); weights.resize(target_names.size());
for (int name_i = 0; name_i < target_names.size(); name_i++) { for (int name_i = 0; name_i < target_names.size(); name_i++) {
real_t weight = 0.0; real_t weight = 0.0;
if (name_i < p_state->meshes.write[gltf_mesh_i]->get_blend_weights().size()) { if (name_i < gltf_mesh->get_blend_weights().size()) {
weight = p_state->meshes.write[gltf_mesh_i]->get_blend_weights()[name_i]; weight = gltf_mesh->get_blend_weights()[name_i];
} }
weights[name_i] = weight; weights[name_i] = weight;
} }
if (weights.size()) { if (weights.size()) {
gltf_mesh["weights"] = weights; mesh_dict["weights"] = weights;
} }
ERR_FAIL_COND_V(target_names.size() != weights.size(), FAILED); ERR_FAIL_COND_V(target_names.size() != weights.size(), FAILED);
gltf_mesh["primitives"] = primitives; mesh_dict["primitives"] = primitives;
meshes.push_back(gltf_mesh); if (!gltf_mesh->get_name().is_empty()) {
mesh_dict["name"] = gltf_mesh->get_name();
}
meshes.push_back(mesh_dict);
} }
if (!meshes.size()) { if (!meshes.size()) {
@ -4482,19 +4488,19 @@ Error GLTFDocument::_parse_texture_samplers(Ref<GLTFState> p_state) {
Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) { Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
Array materials; Array materials;
for (int32_t i = 0; i < p_state->materials.size(); i++) { for (int32_t i = 0; i < p_state->materials.size(); i++) {
Dictionary d; Dictionary mat_dict;
Ref<Material> material = p_state->materials[i]; Ref<Material> material = p_state->materials[i];
if (material.is_null()) { if (material.is_null()) {
materials.push_back(d); materials.push_back(mat_dict);
continue; continue;
} }
if (!material->get_name().is_empty()) { if (!material->get_name().is_empty()) {
d["name"] = _gen_unique_name(p_state, material->get_name()); mat_dict["name"] = _gen_unique_name(p_state, material->get_name());
} }
Ref<BaseMaterial3D> base_material = material; Ref<BaseMaterial3D> base_material = material;
if (base_material.is_null()) { if (base_material.is_null()) {
materials.push_back(d); materials.push_back(mat_dict);
continue; continue;
} }
@ -4647,7 +4653,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
if (has_ao) { if (has_ao) {
Dictionary occt; Dictionary occt;
occt["index"] = orm_texture_index; occt["index"] = orm_texture_index;
d["occlusionTexture"] = occt; mat_dict["occlusionTexture"] = occt;
} }
if (has_roughness || has_metalness) { if (has_roughness || has_metalness) {
mrt["index"] = orm_texture_index; mrt["index"] = orm_texture_index;
@ -4661,7 +4667,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
} }
} }
d["pbrMetallicRoughness"] = mr; mat_dict["pbrMetallicRoughness"] = mr;
if (base_material->get_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING) && _image_format != "None") { if (base_material->get_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING) && _image_format != "None") {
Dictionary nt; Dictionary nt;
Ref<ImageTexture> tex; Ref<ImageTexture> tex;
@ -4701,14 +4707,14 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
nt["scale"] = base_material->get_normal_scale(); nt["scale"] = base_material->get_normal_scale();
if (gltf_texture_index != -1) { if (gltf_texture_index != -1) {
nt["index"] = gltf_texture_index; nt["index"] = gltf_texture_index;
d["normalTexture"] = nt; mat_dict["normalTexture"] = nt;
} }
} }
if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION)) { if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION)) {
const Color c = base_material->get_emission().linear_to_srgb(); const Color c = base_material->get_emission().linear_to_srgb();
Array arr = { c.r, c.g, c.b }; Array arr = { c.r, c.g, c.b };
d["emissiveFactor"] = arr; mat_dict["emissiveFactor"] = arr;
} }
if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION) && _image_format != "None") { if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION) && _image_format != "None") {
@ -4722,20 +4728,20 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
if (gltf_texture_index != -1) { if (gltf_texture_index != -1) {
et["index"] = gltf_texture_index; et["index"] = gltf_texture_index;
d["emissiveTexture"] = et; mat_dict["emissiveTexture"] = et;
} }
} }
const bool ds = base_material->get_cull_mode() == BaseMaterial3D::CULL_DISABLED; const bool ds = base_material->get_cull_mode() == BaseMaterial3D::CULL_DISABLED;
if (ds) { if (ds) {
d["doubleSided"] = ds; mat_dict["doubleSided"] = ds;
} }
if (base_material->get_transparency() == BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR) { if (base_material->get_transparency() == BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR) {
d["alphaMode"] = "MASK"; mat_dict["alphaMode"] = "MASK";
d["alphaCutoff"] = base_material->get_alpha_scissor_threshold(); mat_dict["alphaCutoff"] = base_material->get_alpha_scissor_threshold();
} else if (base_material->get_transparency() != BaseMaterial3D::TRANSPARENCY_DISABLED) { } else if (base_material->get_transparency() != BaseMaterial3D::TRANSPARENCY_DISABLED) {
d["alphaMode"] = "BLEND"; mat_dict["alphaMode"] = "BLEND";
} }
Dictionary extensions; Dictionary extensions;
@ -4750,10 +4756,10 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
extensions["KHR_materials_emissive_strength"] = mat_emissive_strength; extensions["KHR_materials_emissive_strength"] = mat_emissive_strength;
p_state->add_used_extension("KHR_materials_emissive_strength"); p_state->add_used_extension("KHR_materials_emissive_strength");
} }
d["extensions"] = extensions; mat_dict["extensions"] = extensions;
_attach_meta_to_extras(material, d); _attach_meta_to_extras(material, mat_dict);
materials.push_back(d); materials.push_back(mat_dict);
} }
if (!materials.size()) { if (!materials.size()) {
return OK; return OK;
@ -5858,6 +5864,10 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> p_state, MeshIn
Ref<GLTFMesh> gltf_mesh; Ref<GLTFMesh> gltf_mesh;
gltf_mesh.instantiate(); gltf_mesh.instantiate();
if (!mesh_resource->get_name().is_empty()) {
gltf_mesh->set_original_name(mesh_resource->get_name());
gltf_mesh->set_name(_gen_unique_name(p_state, mesh_resource->get_name()));
}
gltf_mesh->set_instance_materials(instance_materials); gltf_mesh->set_instance_materials(instance_materials);
gltf_mesh->set_mesh(current_mesh); gltf_mesh->set_mesh(current_mesh);
gltf_mesh->set_blend_weights(blend_weights); gltf_mesh->set_blend_weights(blend_weights);