Skip to content

Commit

Permalink
Merge pull request #55859 from akien-mga/3.x-gltf-module-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Dec 13, 2021
2 parents 8098d16 + e4c40d8 commit db020ea
Show file tree
Hide file tree
Showing 23 changed files with 301 additions and 254 deletions.
1 change: 0 additions & 1 deletion modules/gltf/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Import("env")
Import("env_modules")

env_gltf = env_modules.Clone()
env_gltf.Prepend(CPPPATH=["."])

# Godot's own source files
env_gltf.add_source_files(env.modules_sources, "*.cpp")
16 changes: 8 additions & 8 deletions modules/gltf/editor_scene_exporter_gltf_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifdef TOOLS_ENABLED

#include "editor_scene_exporter_gltf_plugin.h"
#include "core/object.h"
#include "core/project_settings.h"
#include "core/vector.h"
#include "editor/editor_file_system.h"
#include "scene/3d/mesh_instance.h"
#include "scene/gui/check_box.h"
#include "scene/main/node.h"

#include "editor/editor_file_dialog.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
#include "scene/main/node.h"

String SceneExporterGLTFPlugin::get_name() const {
return "ConvertGLTF2";
Expand Down Expand Up @@ -90,6 +88,8 @@ void SceneExporterGLTFPlugin::convert_scene_to_gltf2(Variant p_null) {
if (filename.empty()) {
filename = root->get_name();
}
file_export_lib->set_current_file(filename + String(".gltf"));
file_export_lib->set_current_file(filename + ".gltf");
file_export_lib->popup_centered_ratio();
}

#endif // TOOLS_ENABLED
4 changes: 3 additions & 1 deletion modules/gltf/editor_scene_exporter_gltf_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#ifndef EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H
#define EDITOR_SCENE_EXPORTER_GLTF_PLUGIN_H

#include "editor/editor_file_dialog.h"
#include "editor/editor_plugin.h"
#include "editor_scene_importer_gltf.h"

#include "packed_scene_gltf.h"

class SceneExporterGLTFPlugin : public EditorPlugin {
GDCLASS(SceneExporterGLTFPlugin, EditorPlugin);
Expand Down
139 changes: 7 additions & 132 deletions modules/gltf/editor_scene_importer_gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,13 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "core/crypto/crypto_core.h"
#include "core/io/json.h"
#include "core/math/disjoint_set.h"
#include "core/math/math_defs.h"
#include "core/os/file_access.h"
#include "core/os/os.h"
#include "editor/import/resource_importer_scene.h"
#include "modules/gltf/gltf_state.h"
#include "modules/regex/regex.h"
#include "scene/3d/bone_attachment.h"
#include "scene/3d/camera.h"
#include "scene/3d/mesh_instance.h"
#include "scene/animation/animation_player.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/surface_tool.h"
#ifdef TOOLS_ENABLED

#include "modules/gltf/editor_scene_importer_gltf.h"
#include "editor_scene_importer_gltf.h"
#include "scene/resources/animation.h"

#include "gltf_state.h"
#include "packed_scene_gltf.h"

uint32_t EditorSceneImporterGLTF::get_import_flags() const {
return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION;
Expand All @@ -70,119 +60,4 @@ Ref<Animation> EditorSceneImporterGLTF::import_animation(const String &p_path,
return Ref<Animation>();
}

void PackedSceneGLTF::_bind_methods() {
ClassDB::bind_method(
D_METHOD("export_gltf", "node", "path", "flags", "bake_fps"),
&PackedSceneGLTF::export_gltf, DEFVAL(0), DEFVAL(1000.0f));
ClassDB::bind_method(D_METHOD("pack_gltf", "path", "flags", "bake_fps", "compress_flags", "state"),
&PackedSceneGLTF::pack_gltf, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT), DEFVAL(Ref<GLTFState>()));
ClassDB::bind_method(D_METHOD("import_gltf_scene", "path", "flags", "bake_fps", "compress_flags", "state"),
&PackedSceneGLTF::import_gltf_scene, DEFVAL(0), DEFVAL(1000.0f), DEFVAL(Mesh::ARRAY_COMPRESS_DEFAULT), DEFVAL(Ref<GLTFState>()));
}
Node *PackedSceneGLTF::import_gltf_scene(const String &p_path, uint32_t p_flags, float p_bake_fps, uint32_t p_compress_flags, Ref<GLTFState> r_state) {
Error err = FAILED;
List<String> deps;
return import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, &deps, &err, r_state);
}

Node *PackedSceneGLTF::import_scene(const String &p_path, uint32_t p_flags,
int p_bake_fps, uint32_t p_compress_flags,
List<String> *r_missing_deps,
Error *r_err,
Ref<GLTFState> r_state) {
if (r_state == Ref<GLTFState>()) {
r_state.instance();
}
r_state->use_named_skin_binds =
p_flags & EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS;
r_state->use_legacy_names =
p_flags & EditorSceneImporter::IMPORT_USE_LEGACY_NAMES;
r_state->compress_flags = p_compress_flags;

Ref<GLTFDocument> gltf_document;
gltf_document.instance();
Error err = gltf_document->parse(r_state, p_path);
*r_err = err;
ERR_FAIL_COND_V(err != Error::OK, nullptr);

Spatial *root = memnew(Spatial);
if (r_state->use_legacy_names) {
root->set_name(gltf_document->_legacy_validate_node_name(r_state->scene_name));
} else {
root->set_name(r_state->scene_name);
}
for (int32_t root_i = 0; root_i < r_state->root_nodes.size(); root_i++) {
gltf_document->_generate_scene_node(r_state, root, root, r_state->root_nodes[root_i]);
}
gltf_document->_process_mesh_instances(r_state, root);
if (r_state->animations.size()) {
AnimationPlayer *ap = memnew(AnimationPlayer);
root->add_child(ap);
ap->set_owner(root);
for (int i = 0; i < r_state->animations.size(); i++) {
gltf_document->_import_animation(r_state, ap, i, p_bake_fps);
}
}

return cast_to<Spatial>(root);
}

void PackedSceneGLTF::pack_gltf(String p_path, int32_t p_flags,
real_t p_bake_fps, uint32_t p_compress_flags, Ref<GLTFState> r_state) {
Error err = FAILED;
List<String> deps;
Node *root = import_scene(p_path, p_flags, p_bake_fps, p_compress_flags, &deps, &err, r_state);
ERR_FAIL_COND(err != OK);
pack(root);
}

void PackedSceneGLTF::save_scene(Node *p_node, const String &p_path,
const String &p_src_path, uint32_t p_flags,
int p_bake_fps, List<String> *r_missing_deps,
Error *r_err) {
Error err = FAILED;
if (r_err) {
*r_err = err;
}
Ref<GLTFDocument> gltf_document;
gltf_document.instance();
Ref<GLTFState> state;
state.instance();
err = gltf_document->serialize(state, p_node, p_path);
if (r_err) {
*r_err = err;
}
}

void PackedSceneGLTF::_build_parent_hierachy(Ref<GLTFState> state) {
// build the hierarchy
for (GLTFNodeIndex node_i = 0; node_i < state->nodes.size(); node_i++) {
for (int j = 0; j < state->nodes[node_i]->children.size(); j++) {
GLTFNodeIndex child_i = state->nodes[node_i]->children[j];
ERR_FAIL_INDEX(child_i, state->nodes.size());
if (state->nodes.write[child_i]->parent != -1) {
continue;
}
state->nodes.write[child_i]->parent = node_i;
}
}
}

Error PackedSceneGLTF::export_gltf(Node *p_root, String p_path,
int32_t p_flags,
real_t p_bake_fps) {
ERR_FAIL_COND_V(!p_root, FAILED);
List<String> deps;
Error err;
String path = p_path;
int32_t flags = p_flags;
real_t baked_fps = p_bake_fps;
Ref<PackedSceneGLTF> exporter;
exporter.instance();
exporter->save_scene(p_root, path, "", flags, baked_fps, &deps, &err);
int32_t error_code = err;
if (error_code != 0) {
return Error(error_code);
}
return OK;
}
#endif // TOOLS_ENABLED
51 changes: 6 additions & 45 deletions modules/gltf/editor_scene_importer_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,16 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifdef TOOLS_ENABLED

#ifndef EDITOR_SCENE_IMPORTER_GLTF_H
#define EDITOR_SCENE_IMPORTER_GLTF_H

#include "core/io/json.h"
#include "core/object.h"
#include "core/project_settings.h"
#include "core/vector.h"
#include "editor/import/resource_importer_scene.h"
#include "modules/csg/csg_shape.h"
#include "modules/gridmap/grid_map.h"
#include "scene/3d/mesh_instance.h"
#include "scene/3d/multimesh_instance.h"
#include "scene/3d/skeleton.h"
#include "scene/3d/spatial.h"
#include "scene/animation/animation_player.h"
#include "scene/gui/check_box.h"
#include "scene/main/node.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/surface_tool.h"

#include "gltf_document.h"
#include "gltf_state.h"

class AnimationPlayer;
class BoneAttachment;
class EditorSceneImporterMeshNode3D;

#ifdef TOOLS_ENABLED
class EditorSceneImporterGLTF : public EditorSceneImporter {
GDCLASS(EditorSceneImporterGLTF, EditorSceneImporter);

Expand All @@ -64,33 +46,12 @@ class EditorSceneImporterGLTF : public EditorSceneImporter {
virtual void get_extensions(List<String> *r_extensions) const;
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
int p_bake_fps, uint32_t p_compress_flags,
List<String> *r_missing_deps = NULL,
Error *r_err = NULL);
List<String> *r_missing_deps = nullptr,
Error *r_err = nullptr);
virtual Ref<Animation> import_animation(const String &p_path,
uint32_t p_flags, int p_bake_fps);
};
#endif

class PackedSceneGLTF : public PackedScene {
GDCLASS(PackedSceneGLTF, PackedScene);

protected:
static void _bind_methods();

public:
virtual void save_scene(Node *p_node, const String &p_path, const String &p_src_path,
uint32_t p_flags, int p_bake_fps,
List<String> *r_missing_deps, Error *r_err = NULL);
virtual void _build_parent_hierachy(Ref<GLTFState> state);
virtual Error export_gltf(Node *p_root, String p_path, int32_t p_flags = 0,
real_t p_bake_fps = 1000.0f);
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
int p_bake_fps, uint32_t p_compress_flags,
List<String> *r_missing_deps,
Error *r_err,
Ref<GLTFState> r_state);
virtual Node *import_gltf_scene(const String &p_path, uint32_t p_flags, float p_bake_fps, uint32_t p_compress_flags, Ref<GLTFState> r_state = Ref<GLTFState>());
virtual void pack_gltf(String p_path, int32_t p_flags = 0,
real_t p_bake_fps = 1000.0f, uint32_t p_compress_flags = Mesh::ARRAY_COMPRESS_DEFAULT, Ref<GLTFState> r_state = Ref<GLTFState>());
};
#endif // EDITOR_SCENE_IMPORTER_GLTF_H

#endif // TOOLS_ENABLED
5 changes: 3 additions & 2 deletions modules/gltf/gltf_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define GLTF_ACCESSOR_H

#include "core/resource.h"

#include "gltf_document.h"

struct GLTFAccessor : public Resource {
Expand All @@ -44,8 +45,7 @@ struct GLTFAccessor : public Resource {
int component_type = 0;
bool normalized = false;
int count = 0;
GLTFDocument::GLTFType
type = GLTFDocument::TYPE_SCALAR;
GLTFDocument::GLTFType type = GLTFDocument::TYPE_SCALAR;
PoolVector<float> min;
PoolVector<float> max;
int sparse_count = 0;
Expand Down Expand Up @@ -101,4 +101,5 @@ struct GLTFAccessor : public Resource {
int get_sparse_values_byte_offset();
void set_sparse_values_byte_offset(int p_sparse_values_byte_offset);
};

#endif // GLTF_ACCESSOR_H
1 change: 1 addition & 0 deletions modules/gltf/gltf_animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ class GLTFAnimation : public Resource {
bool loop = false;
Map<int, Track> tracks;
};

#endif // GLTF_ANIMATION_H
2 changes: 2 additions & 0 deletions modules/gltf/gltf_buffer_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define GLTF_BUFFER_VIEW_H

#include "core/resource.h"

#include "gltf_document.h"

class GLTFBufferView : public Resource {
Expand Down Expand Up @@ -65,4 +66,5 @@ class GLTFBufferView : public Resource {
void set_indices(bool p_indices);
// matrices need to be transformed to this
};

#endif // GLTF_BUFFER_VIEW_H
1 change: 1 addition & 0 deletions modules/gltf/gltf_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ class GLTFCamera : public Resource {
float get_znear() const { return znear; }
void set_znear(float p_val) { znear = p_val; }
};

#endif // GLTF_CAMERA_H
11 changes: 1 addition & 10 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,19 @@
#include "gltf_state.h"
#include "gltf_texture.h"

#include "core/bind/core_bind.h"
#include "core/bind/core_bind.h" // FIXME: Shouldn't use _Directory but DirAccess.
#include "core/crypto/crypto_core.h"
#include "core/error_list.h"
#include "core/error_macros.h"
#include "core/io/json.h"
#include "core/math/disjoint_set.h"
#include "core/os/file_access.h"
#include "core/variant.h"
#include "core/version.h"
#include "core/version_hash.gen.h"
#include "drivers/png/png_driver_common.h"
#include "editor/import/resource_importer_scene.h"
#include "scene/2d/node_2d.h"
#include "scene/3d/bone_attachment.h"
#include "scene/3d/camera.h"
#include "scene/3d/mesh_instance.h"
#include "scene/3d/multimesh_instance.h"
#include "scene/3d/skeleton.h"
#include "scene/3d/spatial.h"
#include "scene/animation/animation_player.h"
#include "scene/main/node.h"
Expand All @@ -77,10 +72,6 @@
#include "modules/regex/regex.h"
#endif // MODULE_REGEX_ENABLED

#include <stdio.h>
#include <stdlib.h>
#include <limits>

Error GLTFDocument::serialize(Ref<GLTFState> state, Node *p_root, const String &p_path) {
uint64_t begin_time = OS::get_singleton()->get_ticks_usec();

Expand Down
Loading

0 comments on commit db020ea

Please sign in to comment.