Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
404 changes: 404 additions & 0 deletions modules/gltf/extensions/gltf_document_extension_multi_mesh.cpp

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions modules/gltf/extensions/gltf_document_extension_multi_mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**************************************************************************/
/* gltf_document_extension_multi_mesh.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "gltf_document_extension.h"

#include "scene/3d/multimesh_instance_3d.h"

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

enum MultiMeshHandling {
MULTI_MESH_HANDLING_OPTIONAL_EXT_MESH_GPU_INSTANCING,
MULTI_MESH_HANDLING_REQUIRED_EXT_MESH_GPU_INSTANCING,
MULTI_MESH_HANDLING_MULTIPLE_NODES_ONLY,
MULTI_MESH_HANDLING_MULTIPLE_NODES_FALLBACK,
};
MultiMeshHandling _handling = MULTI_MESH_HANDLING_OPTIONAL_EXT_MESH_GPU_INSTANCING;

static bool _any_multi_mesh_instance_exists_recursive(Node *p_node);
static GLTFMeshIndex _convert_mesh_resource_into_state(Ref<GLTFState> p_gltf_state, const Ref<MultiMesh> &p_multi_mesh);
static void _convert_multi_mesh_to_extension(Ref<GLTFState> p_gltf_state, Ref<GLTFNode> p_gltf_node, const Ref<MultiMesh> &p_multi_mesh);
static void _convert_multi_mesh_to_gltf_nodes(Ref<GLTFState> p_gltf_state, Ref<GLTFNode> p_gltf_node, MultiMeshInstance3D *p_multi_mesh_node, GLTFMeshIndex p_mesh_index, bool p_for_fallback);
static Transform3D get_multi_mesh_instance_transform(const Ref<MultiMesh> &p_multi_mesh, int p_instance);
static Ref<Mesh> _generate_mesh(Ref<GLTFState> p_gltf_state, GLTFMeshIndex p_mesh_index);

protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;

public:
// Import process.
virtual Error import_preflight(Ref<GLTFState> p_gltf_state, const Vector<String> &p_extensions) override;
virtual Vector<String> get_supported_extensions() override;
virtual Error parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, const Dictionary &p_extensions) override;
virtual Node3D *generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) override;
// Export process.
virtual List<PropertyInfo> export_get_property_list(Node *p_root_node) override;
virtual void convert_scene_node(Ref<GLTFState> p_gltf_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) override;
virtual Error export_node(Ref<GLTFState> p_gltf_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_scene_node) override;
};

VARIANT_ENUM_CAST(GLTFDocumentExtensionMultiMesh::MultiMeshHandling);
56 changes: 3 additions & 53 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include "scene/3d/importer_mesh_instance_3d.h"
#include "scene/3d/light_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/multimesh_instance_3d.h"
#include "scene/animation/animation_player.h"
#include "scene/resources/3d/skin.h"
#include "scene/resources/image_texture.h"
Expand Down Expand Up @@ -4240,9 +4239,6 @@ void GLTFDocument::_convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
_convert_skeleton_to_gltf(skel, p_state, p_gltf_parent, p_gltf_root, gltf_node);
// We ignore the Godot Engine node that is the skeleton.
return;
} else if (Object::cast_to<MultiMeshInstance3D>(p_current)) {
MultiMeshInstance3D *multi = Object::cast_to<MultiMeshInstance3D>(p_current);
_convert_multi_mesh_instance_to_gltf(multi, p_gltf_parent, p_gltf_root, gltf_node, p_state);
#ifdef MODULE_CSG_ENABLED
} else if (Object::cast_to<CSGShape3D>(p_current)) {
CSGShape3D *shape = Object::cast_to<CSGShape3D>(p_current);
Expand Down Expand Up @@ -4401,55 +4397,6 @@ void GLTFDocument::_convert_grid_map_to_gltf(GridMap *p_grid_map, GLTFNodeIndex
#endif // MODULE_GRIDMAP_ENABLED
}

void GLTFDocument::_convert_multi_mesh_instance_to_gltf(
MultiMeshInstance3D *p_multi_mesh_instance,
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state) {
ERR_FAIL_NULL(p_multi_mesh_instance);
Ref<MultiMesh> multi_mesh = p_multi_mesh_instance->get_multimesh();
if (multi_mesh.is_null()) {
return;
}
Ref<GLTFMesh> gltf_mesh;
gltf_mesh.instantiate();
Ref<Mesh> mesh = multi_mesh->get_mesh();
if (mesh.is_null()) {
return;
}
gltf_mesh->set_original_name(multi_mesh->get_name());
gltf_mesh->set_name(multi_mesh->get_name());
gltf_mesh->set_mesh(ImporterMesh::from_mesh(mesh));
GLTFMeshIndex mesh_index = p_state->meshes.size();
p_state->meshes.push_back(gltf_mesh);
for (int32_t instance_i = 0; instance_i < multi_mesh->get_instance_count();
instance_i++) {
Transform3D transform;
if (multi_mesh->get_transform_format() == MultiMesh::TRANSFORM_2D) {
Transform2D xform_2d = multi_mesh->get_instance_transform_2d(instance_i);
transform.origin =
Vector3(xform_2d.get_origin().x, 0, xform_2d.get_origin().y);
real_t rotation = xform_2d.get_rotation();
Quaternion quaternion(Vector3(0, 1, 0), rotation);
Size2 scale = xform_2d.get_scale();
transform.basis.set_quaternion_scale(quaternion,
Vector3(scale.x, 0, scale.y));
transform = p_multi_mesh_instance->get_transform() * transform;
} else if (multi_mesh->get_transform_format() == MultiMesh::TRANSFORM_3D) {
transform = p_multi_mesh_instance->get_transform() *
multi_mesh->get_instance_transform(instance_i);
}
Ref<GLTFNode> new_gltf_node;
new_gltf_node.instantiate();
new_gltf_node->mesh = mesh_index;
new_gltf_node->transform = transform;
new_gltf_node->set_original_name(p_multi_mesh_instance->get_name());
new_gltf_node->set_name(_gen_unique_name(p_state, p_multi_mesh_instance->get_name()));
p_gltf_node->children.push_back(p_state->nodes.size());
p_state->nodes.push_back(new_gltf_node);
}
}

void GLTFDocument::_convert_skeleton_to_gltf(Skeleton3D *p_skeleton3d, Ref<GLTFState> p_state, GLTFNodeIndex p_parent_node_index, GLTFNodeIndex p_root_node_index, Ref<GLTFNode> p_gltf_node) {
Skeleton3D *skeleton = p_skeleton3d;
Ref<GLTFSkeleton> gltf_skeleton;
Expand Down Expand Up @@ -4590,6 +4537,9 @@ bool GLTFDocument::_does_skinned_mesh_require_placeholder_node(Ref<GLTFState> p_

void GLTFDocument::_generate_scene_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root) {
Ref<GLTFNode> gltf_node = p_state->nodes[p_node_index];
if (gltf_node->has_additional_data(StringName("SkipNodeGeneration"))) {
return;
}
Node3D *current_node = nullptr;
// Check if any GLTFDocumentExtension classes want to generate a node for us.
for (Ref<GLTFDocumentExtension> ext : document_extensions) {
Expand Down
6 changes: 0 additions & 6 deletions modules/gltf/gltf_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "gltf_state.h"

#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/multimesh_instance_3d.h"

class CSGShape3D;
class GridMap;
Expand Down Expand Up @@ -270,11 +269,6 @@ class GLTFDocument : public Resource {
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
void _convert_multi_mesh_instance_to_gltf(
MultiMeshInstance3D *p_multi_mesh_instance,
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
void _convert_skeleton_to_gltf(
Skeleton3D *p_scene_parent, Ref<GLTFState> p_state,
GLTFNodeIndex p_parent_node_index,
Expand Down
17 changes: 17 additions & 0 deletions modules/gltf/gltf_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,20 @@ GLTFNodeIndex GLTFState::append_gltf_node(Ref<GLTFNode> p_gltf_node, Node *p_god
}
return new_index;
}

String GLTFState::reserve_unique_name(const String &p_requested_name, bool p_show_warning) {
const String start_name = p_requested_name.validate_node_name();
String unique_name = start_name;
if (unique_names.has(unique_name)) {
uint64_t discriminator = 2;
while (unique_names.has(unique_name)) {
unique_name = p_requested_name + String::num_uint64(discriminator);
discriminator++;
}
if (p_show_warning) {
WARN_PRINT("GLTFState: The requested name " + p_requested_name + " is already in use. The name " + unique_name + " will be used instead.");
}
}
unique_names.insert(unique_name);
return unique_name;
}
1 change: 1 addition & 0 deletions modules/gltf/gltf_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class GLTFState : public Resource {
void add_used_extension(const String &p_extension, bool p_required = false);
GLTFBufferViewIndex append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication);
GLTFNodeIndex append_gltf_node(Ref<GLTFNode> p_gltf_node, Node *p_godot_scene_node, GLTFNodeIndex p_parent_node_index);
String reserve_unique_name(const String &p_requested_name, bool p_show_warning = true);

// Deprecated, use HandleBinaryImageMode instead.
enum GLTFHandleBinary {
Expand Down
2 changes: 2 additions & 0 deletions modules/gltf/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "register_types.h"

#include "extensions/gltf_document_extension_convert_importer_mesh.h"
#include "extensions/gltf_document_extension_multi_mesh.h"
#include "extensions/gltf_document_extension_texture_ktx.h"
#include "extensions/gltf_document_extension_texture_webp.h"
#include "extensions/gltf_spec_gloss.h"
Expand Down Expand Up @@ -139,6 +140,7 @@ void initialize_gltf_module(ModuleInitializationLevel p_level) {
#endif // PHYSICS_3D_DISABLED
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionTextureKTX);
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionTextureWebP);
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionMultiMesh);
bool is_editor = Engine::get_singleton()->is_editor_hint();
if (!is_editor) {
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionConvertImporterMesh);
Expand Down
Loading