Skip to content

Commit 5a24aec

Browse files
committed
Fix GLTFDocument so it can export CSG Meshes correctly.
1 parent 8bf8f41 commit 5a24aec

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

modules/gltf/gltf_document.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5119,7 +5119,11 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> p_state, MeshIn
51195119
ERR_FAIL_COND_V_MSG(p_mesh_instance->get_mesh().is_null(), -1, "glTF: Tried to export a MeshInstance3D node named " + p_mesh_instance->get_name() + ", but it has no mesh. This node will be exported without a mesh.");
51205120
Ref<Mesh> mesh_resource = p_mesh_instance->get_mesh();
51215121
ERR_FAIL_COND_V_MSG(mesh_resource->get_surface_count() == 0, -1, "glTF: Tried to export a MeshInstance3D node named " + p_mesh_instance->get_name() + ", but its mesh has no surfaces. This node will be exported without a mesh.");
5122-
5122+
TypedArray<Material> instance_materials;
5123+
for (int32_t surface_i = 0; surface_i < mesh_resource->get_surface_count(); surface_i++) {
5124+
Ref<Material> mat = p_mesh_instance->get_active_material(surface_i);
5125+
instance_materials.append(mat);
5126+
}
51235127
Ref<ImporterMesh> current_mesh = _mesh_to_importer_mesh(mesh_resource);
51245128
Vector<float> blend_weights;
51255129
int32_t blend_count = mesh_resource->get_blend_shape_count();
@@ -5130,17 +5134,6 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> p_state, MeshIn
51305134

51315135
Ref<GLTFMesh> gltf_mesh;
51325136
gltf_mesh.instantiate();
5133-
TypedArray<Material> instance_materials;
5134-
for (int32_t surface_i = 0; surface_i < current_mesh->get_surface_count(); surface_i++) {
5135-
Ref<Material> mat = current_mesh->get_surface_material(surface_i);
5136-
if (p_mesh_instance->get_surface_override_material(surface_i).is_valid()) {
5137-
mat = p_mesh_instance->get_surface_override_material(surface_i);
5138-
}
5139-
if (p_mesh_instance->get_material_override().is_valid()) {
5140-
mat = p_mesh_instance->get_material_override();
5141-
}
5142-
instance_materials.append(mat);
5143-
}
51445137
gltf_mesh->set_instance_materials(instance_materials);
51455138
gltf_mesh->set_mesh(current_mesh);
51465139
gltf_mesh->set_blend_weights(blend_weights);
@@ -5309,18 +5302,30 @@ void GLTFDocument::_convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeInd
53095302
Ref<ImporterMesh> mesh;
53105303
mesh.instantiate();
53115304
{
5312-
Ref<Mesh> csg_mesh = csg->get_meshes()[1];
5313-
5305+
Ref<ArrayMesh> csg_mesh = csg->get_meshes()[1];
53145306
for (int32_t surface_i = 0; surface_i < csg_mesh->get_surface_count(); surface_i++) {
53155307
Array array = csg_mesh->surface_get_arrays(surface_i);
5316-
Ref<Material> mat = csg_mesh->surface_get_material(surface_i);
5308+
5309+
Ref<Material> mat;
5310+
5311+
Ref<Material> mat_override = csg->get_material_override();
5312+
if (mat_override.is_valid()) {
5313+
mat = mat_override;
5314+
}
5315+
5316+
Ref<Material> mat_surface_override = csg_mesh->surface_get_material(surface_i);
5317+
if (mat_surface_override.is_valid() && mat.is_null()) {
5318+
mat = mat_surface_override;
5319+
}
5320+
53175321
String mat_name;
53185322
if (mat.is_valid()) {
53195323
mat_name = mat->get_name();
53205324
} else {
53215325
// Assign default material when no material is assigned.
53225326
mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
53235327
}
5328+
53245329
mesh->add_surface(csg_mesh->surface_get_primitive_type(surface_i),
53255330
array, csg_mesh->surface_get_blend_shape_arrays(surface_i), csg_mesh->surface_get_lods(surface_i), mat,
53265331
mat_name, csg_mesh->surface_get_format(surface_i));
@@ -5334,7 +5339,7 @@ void GLTFDocument::_convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeInd
53345339
GLTFMeshIndex mesh_i = p_state->meshes.size();
53355340
p_state->meshes.push_back(gltf_mesh);
53365341
p_gltf_node->mesh = mesh_i;
5337-
p_gltf_node->transform = csg->get_meshes()[0];
5342+
p_gltf_node->transform = csg->get_transform();
53385343
p_gltf_node->set_original_name(csg->get_name());
53395344
p_gltf_node->set_name(_gen_unique_name(p_state, csg->get_name()));
53405345
}

0 commit comments

Comments
 (0)