Skip to content

Commit

Permalink
GLTF: Fix bad pointer to ImporterMeshInstance3D root node at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Dec 16, 2024
1 parent b9437c3 commit de87ca5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ void GLTFDocumentExtensionConvertImporterMesh::_copy_meta(Object *p_src_object,
}
}

MeshInstance3D *GLTFDocumentExtensionConvertImporterMesh::convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d) {
// Convert the node itself first.
MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
ERR_FAIL_NULL_V(p_importer_mesh_instance_3d, mesh_instance_node_3d);
mesh_instance_node_3d->set_name(p_importer_mesh_instance_3d->get_name());
mesh_instance_node_3d->set_transform(p_importer_mesh_instance_3d->get_transform());
mesh_instance_node_3d->set_skin(p_importer_mesh_instance_3d->get_skin());
mesh_instance_node_3d->set_skeleton_path(p_importer_mesh_instance_3d->get_skeleton_path());
mesh_instance_node_3d->set_visible(p_importer_mesh_instance_3d->is_visible());
p_importer_mesh_instance_3d->replace_by(mesh_instance_node_3d);
_copy_meta(p_importer_mesh_instance_3d, mesh_instance_node_3d);
// Convert the mesh data in the mesh resource.
Ref<ImporterMesh> importer_mesh = p_importer_mesh_instance_3d->get_mesh();
if (importer_mesh.is_valid()) {
Ref<ArrayMesh> array_mesh = importer_mesh->get_mesh();
mesh_instance_node_3d->set_mesh(array_mesh);
_copy_meta(importer_mesh.ptr(), array_mesh.ptr());
} else {
WARN_PRINT("glTF: ImporterMeshInstance3D does not have a valid mesh. This should not happen. Continuing anyway.");
}
return mesh_instance_node_3d;
}

Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFState> p_state, Node *p_root) {
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Expand All @@ -54,24 +77,8 @@ Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFState> p_sta
Node *node = E->get();
ImporterMeshInstance3D *importer_mesh_3d = Object::cast_to<ImporterMeshInstance3D>(node);
if (importer_mesh_3d) {
Ref<ImporterMesh> mesh = importer_mesh_3d->get_mesh();
if (mesh.is_valid()) {
MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
Ref<ArrayMesh> array_mesh = mesh->get_mesh();
mesh_instance_node_3d->set_name(node->get_name());
mesh_instance_node_3d->set_transform(importer_mesh_3d->get_transform());
mesh_instance_node_3d->set_mesh(array_mesh);
mesh_instance_node_3d->set_skin(importer_mesh_3d->get_skin());
mesh_instance_node_3d->set_skeleton_path(importer_mesh_3d->get_skeleton_path());
mesh_instance_node_3d->set_visible(importer_mesh_3d->is_visible());
node->replace_by(mesh_instance_node_3d);
_copy_meta(importer_mesh_3d, mesh_instance_node_3d);
_copy_meta(mesh.ptr(), array_mesh.ptr());
delete_queue.push_back(node);
node = mesh_instance_node_3d;
} else {
WARN_PRINT("glTF: ImporterMeshInstance3D does not have a valid mesh. This should not happen. Continuing anyway.");
}
delete_queue.push_back(importer_mesh_3d);
node = convert_importer_mesh_instance_3d(importer_mesh_3d);
}
int child_count = node->get_child_count();
for (int i = 0; i < child_count; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@

#include "gltf_document_extension.h"

class MeshInstance3D;

class GLTFDocumentExtensionConvertImporterMesh : public GLTFDocumentExtension {
GDCLASS(GLTFDocumentExtensionConvertImporterMesh, GLTFDocumentExtension);

protected:
static void _copy_meta(Object *p_src_object, Object *p_dst_object);

public:
static MeshInstance3D *convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d);
Error import_post(Ref<GLTFState> p_state, Node *p_root) override;
};

Expand Down
6 changes: 6 additions & 0 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "gltf_document.h"

#include "extensions/gltf_document_extension_convert_importer_mesh.h"
#include "extensions/gltf_spec_gloss.h"
#include "gltf_state.h"
#include "skin_tool.h"
Expand Down Expand Up @@ -8468,6 +8469,11 @@ Node *GLTFDocument::generate_scene(Ref<GLTFState> p_state, float p_bake_fps, boo
ERR_CONTINUE(err != OK);
}
}
ImporterMeshInstance3D *root_importer_mesh = Object::cast_to<ImporterMeshInstance3D>(root);
if (unlikely(root_importer_mesh)) {
root = GLTFDocumentExtensionConvertImporterMesh::convert_importer_mesh_instance_3d(root_importer_mesh);
memdelete(root_importer_mesh);
}
for (Ref<GLTFDocumentExtension> ext : document_extensions) {
ERR_CONTINUE(ext.is_null());
err = ext->import_post(p_state, root);
Expand Down

0 comments on commit de87ca5

Please sign in to comment.