From 92f4c334927ea4c45027ad6401716909c0f2d3a8 Mon Sep 17 00:00:00 2001 From: Fazil Babu Date: Fri, 11 Feb 2022 00:01:25 +0530 Subject: [PATCH] CSG Meshes can be exported as glTF --- modules/gltf/gltf_document.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index a3dcfddc7f8..f555c8912d5 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -5322,14 +5322,31 @@ void GLTFDocument::_convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeInd if (meshes.size() != 2) { return; } - Ref mat; - if (csg->get_material_override().is_valid()) { - mat = csg->get_material_override(); + + Ref mesh; + mesh.instantiate(); + { + Ref csg_mesh = csg->get_meshes()[1]; + + for (int32_t surface_i = 0; surface_i < csg_mesh->get_surface_count(); surface_i++) { + Array array = csg_mesh->surface_get_arrays(surface_i); + Ref mat = csg_mesh->surface_get_material(surface_i); + String mat_name; + if (mat.is_valid()) { + mat_name = mat->get_name(); + } else { + // Assign default material when no material is assigned. + mat = Ref(memnew(StandardMaterial3D)); + } + mesh->add_surface(csg_mesh->surface_get_primitive_type(surface_i), + array, csg_mesh->surface_get_blend_shape_arrays(surface_i), csg_mesh->surface_get_lods(surface_i), mat, + mat_name, csg_mesh->surface_get_format(surface_i)); + } } + Ref gltf_mesh; gltf_mesh.instantiate(); - Ref array_mesh = csg->get_meshes()[1]; - gltf_mesh->set_mesh(array_mesh); + gltf_mesh->set_mesh(mesh); GLTFMeshIndex mesh_i = state->meshes.size(); state->meshes.push_back(gltf_mesh); gltf_node->mesh = mesh_i;